PythTest.spec.ts 100 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008
  1. import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
  2. import { Cell, CommonMessageInfoInternal, toNano } from "@ton/core";
  3. import "@ton/test-utils";
  4. import { compile } from "@ton/blueprint";
  5. import { HexString, Price } from "@pythnetwork/price-service-sdk";
  6. import { PythTest, PythTestConfig } from "../wrappers/PythTest";
  7. import {
  8. BTC_PRICE_FEED_ID,
  9. HERMES_BTC_ETH_UPDATE,
  10. PYTH_SET_DATA_SOURCES,
  11. PYTH_SET_FEE,
  12. TEST_GUARDIAN_ADDRESS1,
  13. PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER,
  14. PYTH_REQUEST_GOVERNANCE_DATA_SOURCE_TRANSFER,
  15. TEST_GUARDIAN_ADDRESS2,
  16. ETH_PRICE_FEED_ID,
  17. HERMES_BTC_PRICE,
  18. HERMES_ETH_PRICE,
  19. HERMES_ETH_PUBLISH_TIME,
  20. HERMES_BTC_PUBLISH_TIME,
  21. HERMES_BTC_CONF,
  22. HERMES_BTC_EXPO,
  23. HERMES_BTC_EMA_CONF,
  24. HERMES_BTC_EMA_EXPO,
  25. HERMES_BTC_EMA_PRICE,
  26. HERMES_ETH_CONF,
  27. HERMES_ETH_EMA_CONF,
  28. HERMES_ETH_EMA_EXPO,
  29. HERMES_ETH_EMA_PRICE,
  30. HERMES_ETH_EXPO,
  31. HERMES_BTC_ETH_UNIQUE_UPDATE,
  32. HERMES_ETH_UNIQUE_EMA_PRICE,
  33. HERMES_BTC_UNIQUE_CONF,
  34. HERMES_BTC_UNIQUE_EMA_CONF,
  35. HERMES_BTC_UNIQUE_EMA_EXPO,
  36. HERMES_BTC_UNIQUE_EMA_PRICE,
  37. HERMES_BTC_UNIQUE_EMA_PUBLISH_TIME,
  38. HERMES_BTC_UNIQUE_EXPO,
  39. HERMES_BTC_UNIQUE_PRICE,
  40. HERMES_BTC_UNIQUE_PUBLISH_TIME,
  41. HERMES_ETH_UNIQUE_CONF,
  42. HERMES_ETH_UNIQUE_EMA_CONF,
  43. HERMES_ETH_UNIQUE_EMA_EXPO,
  44. HERMES_ETH_UNIQUE_EMA_PUBLISH_TIME,
  45. HERMES_ETH_UNIQUE_EXPO,
  46. HERMES_ETH_UNIQUE_PRICE,
  47. HERMES_ETH_UNIQUE_PUBLISH_TIME,
  48. HERMES_SOL_TON_PYTH_USDT_UPDATE,
  49. PYTH_PRICE_FEED_ID,
  50. SOL_PRICE_FEED_ID,
  51. TON_PRICE_FEED_ID,
  52. USDT_PRICE_FEED_ID,
  53. HERMES_SOL_UNIQUE_PUBLISH_TIME,
  54. HERMES_SOL_UNIQUE_PRICE,
  55. HERMES_SOL_UNIQUE_CONF,
  56. HERMES_SOL_UNIQUE_EXPO,
  57. HERMES_USDT_UNIQUE_PRICE,
  58. HERMES_USDT_UNIQUE_EXPO,
  59. HERMES_USDT_UNIQUE_CONF,
  60. HERMES_USDT_UNIQUE_PUBLISH_TIME,
  61. HERMES_UPDATE_10_PRICE_FEEDS,
  62. DOGE_PRICE_FEED_ID,
  63. SOL_USD_PRICE_FEED_ID,
  64. PYTH_USD_PRICE_FEED_ID,
  65. ARBITRUM_USD_PRICE_FEED_ID,
  66. TON_USD_PRICE_FEED_ID,
  67. AAPL_PRICE_FEED_ID,
  68. ABNB_PRICE_FEED_ID,
  69. ADBE_PRICE_FEED_ID,
  70. AMZN_PRICE_FEED_ID,
  71. HERMES_10_BTC_PRICE,
  72. HERMES_10_BTC_CONF,
  73. HERMES_10_BTC_EXPO,
  74. HERMES_10_BTC_PUBLISH_TIME,
  75. HERMES_10_BTC_EMA_PRICE,
  76. HERMES_10_BTC_EMA_CONF,
  77. HERMES_10_BTC_EMA_EXPO,
  78. HERMES_10_BTC_EMA_PUBLISH_TIME,
  79. HERMES_10_ETH_PRICE,
  80. HERMES_10_ETH_CONF,
  81. HERMES_10_ETH_EXPO,
  82. HERMES_10_ETH_PUBLISH_TIME,
  83. HERMES_10_ETH_EMA_PRICE,
  84. HERMES_10_ETH_EMA_CONF,
  85. HERMES_10_ETH_EMA_EXPO,
  86. HERMES_10_ETH_EMA_PUBLISH_TIME,
  87. HERMES_10_DOGE_PRICE,
  88. HERMES_10_DOGE_CONF,
  89. HERMES_10_DOGE_EXPO,
  90. HERMES_10_DOGE_PUBLISH_TIME,
  91. HERMES_10_DOGE_EMA_PRICE,
  92. HERMES_10_DOGE_EMA_CONF,
  93. HERMES_10_DOGE_EMA_EXPO,
  94. HERMES_10_DOGE_EMA_PUBLISH_TIME,
  95. HERMES_10_SOL_PRICE,
  96. HERMES_10_SOL_CONF,
  97. HERMES_10_SOL_EXPO,
  98. HERMES_10_SOL_PUBLISH_TIME,
  99. HERMES_10_SOL_EMA_PRICE,
  100. HERMES_10_SOL_EMA_CONF,
  101. HERMES_10_SOL_EMA_EXPO,
  102. HERMES_10_SOL_EMA_PUBLISH_TIME,
  103. HERMES_10_PYTH_PRICE,
  104. HERMES_10_PYTH_CONF,
  105. HERMES_10_PYTH_EXPO,
  106. HERMES_10_PYTH_PUBLISH_TIME,
  107. HERMES_10_PYTH_EMA_PRICE,
  108. HERMES_10_PYTH_EMA_CONF,
  109. HERMES_10_PYTH_EMA_EXPO,
  110. HERMES_10_PYTH_EMA_PUBLISH_TIME,
  111. HERMES_10_ARB_PRICE,
  112. HERMES_10_ARB_CONF,
  113. HERMES_10_ARB_EXPO,
  114. HERMES_10_ARB_PUBLISH_TIME,
  115. HERMES_10_ARB_EMA_PRICE,
  116. HERMES_10_ARB_EMA_CONF,
  117. HERMES_10_ARB_EMA_EXPO,
  118. HERMES_10_ARB_EMA_PUBLISH_TIME,
  119. HERMES_10_TON_PRICE,
  120. HERMES_10_TON_CONF,
  121. HERMES_10_TON_EXPO,
  122. HERMES_10_TON_PUBLISH_TIME,
  123. HERMES_10_TON_EMA_PRICE,
  124. HERMES_10_TON_EMA_CONF,
  125. HERMES_10_TON_EMA_EXPO,
  126. HERMES_10_TON_EMA_PUBLISH_TIME,
  127. HERMES_10_AAPL_PRICE,
  128. HERMES_10_AAPL_CONF,
  129. HERMES_10_AAPL_EXPO,
  130. HERMES_10_AAPL_PUBLISH_TIME,
  131. HERMES_10_AAPL_EMA_PRICE,
  132. HERMES_10_AAPL_EMA_CONF,
  133. HERMES_10_AAPL_EMA_EXPO,
  134. HERMES_10_AAPL_EMA_PUBLISH_TIME,
  135. HERMES_10_ABNB_PRICE,
  136. HERMES_10_ABNB_CONF,
  137. HERMES_10_ABNB_EXPO,
  138. HERMES_10_ABNB_PUBLISH_TIME,
  139. HERMES_10_ABNB_EMA_PRICE,
  140. HERMES_10_ABNB_EMA_CONF,
  141. HERMES_10_ABNB_EMA_EXPO,
  142. HERMES_10_ABNB_EMA_PUBLISH_TIME,
  143. HERMES_10_ADBE_PRICE,
  144. HERMES_10_ADBE_CONF,
  145. HERMES_10_ADBE_EXPO,
  146. HERMES_10_ADBE_PUBLISH_TIME,
  147. HERMES_10_ADBE_EMA_PRICE,
  148. HERMES_10_ADBE_EMA_CONF,
  149. HERMES_10_ADBE_EMA_EXPO,
  150. HERMES_10_ADBE_EMA_PUBLISH_TIME,
  151. HERMES_10_AMZN_PRICE,
  152. HERMES_10_AMZN_CONF,
  153. HERMES_10_AMZN_EXPO,
  154. HERMES_10_AMZN_PUBLISH_TIME,
  155. HERMES_10_AMZN_EMA_PRICE,
  156. HERMES_10_AMZN_EMA_CONF,
  157. HERMES_10_AMZN_EMA_EXPO,
  158. HERMES_10_AMZN_EMA_PUBLISH_TIME,
  159. HERMES_9_BTC_PRICE,
  160. HERMES_9_BTC_CONF,
  161. HERMES_9_BTC_EXPO,
  162. HERMES_9_BTC_PUBLISH_TIME,
  163. HERMES_9_BTC_EMA_PRICE,
  164. HERMES_9_BTC_EMA_CONF,
  165. HERMES_9_BTC_EMA_EXPO,
  166. HERMES_9_BTC_EMA_PUBLISH_TIME,
  167. HERMES_9_ETH_PRICE,
  168. HERMES_9_ETH_CONF,
  169. HERMES_9_ETH_EXPO,
  170. HERMES_9_ETH_PUBLISH_TIME,
  171. HERMES_9_ETH_EMA_PRICE,
  172. HERMES_9_ETH_EMA_CONF,
  173. HERMES_9_ETH_EMA_EXPO,
  174. HERMES_9_ETH_EMA_PUBLISH_TIME,
  175. HERMES_9_DOGE_PRICE,
  176. HERMES_9_DOGE_CONF,
  177. HERMES_9_DOGE_EXPO,
  178. HERMES_9_DOGE_PUBLISH_TIME,
  179. HERMES_9_DOGE_EMA_PRICE,
  180. HERMES_9_DOGE_EMA_CONF,
  181. HERMES_9_DOGE_EMA_EXPO,
  182. HERMES_9_DOGE_EMA_PUBLISH_TIME,
  183. HERMES_9_SOL_PRICE,
  184. HERMES_9_SOL_CONF,
  185. HERMES_9_SOL_EXPO,
  186. HERMES_9_SOL_PUBLISH_TIME,
  187. HERMES_9_SOL_EMA_PRICE,
  188. HERMES_9_SOL_EMA_CONF,
  189. HERMES_9_SOL_EMA_EXPO,
  190. HERMES_9_SOL_EMA_PUBLISH_TIME,
  191. HERMES_9_PYTH_PRICE,
  192. HERMES_9_PYTH_CONF,
  193. HERMES_9_PYTH_EXPO,
  194. HERMES_9_PYTH_PUBLISH_TIME,
  195. HERMES_9_PYTH_EMA_PRICE,
  196. HERMES_9_PYTH_EMA_CONF,
  197. HERMES_9_PYTH_EMA_EXPO,
  198. HERMES_9_PYTH_EMA_PUBLISH_TIME,
  199. HERMES_9_ARB_PRICE,
  200. HERMES_9_ARB_CONF,
  201. HERMES_9_ARB_EXPO,
  202. HERMES_9_ARB_PUBLISH_TIME,
  203. HERMES_9_ARB_EMA_PRICE,
  204. HERMES_9_ARB_EMA_CONF,
  205. HERMES_9_ARB_EMA_EXPO,
  206. HERMES_9_ARB_EMA_PUBLISH_TIME,
  207. HERMES_9_TON_PRICE,
  208. HERMES_9_TON_CONF,
  209. HERMES_9_TON_EXPO,
  210. HERMES_9_TON_PUBLISH_TIME,
  211. HERMES_9_TON_EMA_PRICE,
  212. HERMES_9_TON_EMA_CONF,
  213. HERMES_9_TON_EMA_EXPO,
  214. HERMES_9_TON_EMA_PUBLISH_TIME,
  215. HERMES_9_AAPL_PRICE,
  216. HERMES_9_AAPL_CONF,
  217. HERMES_9_AAPL_EXPO,
  218. HERMES_9_AAPL_PUBLISH_TIME,
  219. HERMES_9_AAPL_EMA_PRICE,
  220. HERMES_9_AAPL_EMA_CONF,
  221. HERMES_9_AAPL_EMA_EXPO,
  222. HERMES_9_AAPL_EMA_PUBLISH_TIME,
  223. HERMES_9_ABNB_PRICE,
  224. HERMES_9_ABNB_CONF,
  225. HERMES_9_ABNB_EXPO,
  226. HERMES_9_ABNB_PUBLISH_TIME,
  227. HERMES_9_ABNB_EMA_PRICE,
  228. HERMES_9_ABNB_EMA_CONF,
  229. HERMES_9_ABNB_EMA_EXPO,
  230. HERMES_9_ABNB_EMA_PUBLISH_TIME,
  231. HERMES_8_BTC_PRICE,
  232. HERMES_8_BTC_CONF,
  233. HERMES_8_BTC_EXPO,
  234. HERMES_8_BTC_PUBLISH_TIME,
  235. HERMES_8_BTC_EMA_PRICE,
  236. HERMES_8_BTC_EMA_CONF,
  237. HERMES_8_BTC_EMA_EXPO,
  238. HERMES_8_BTC_EMA_PUBLISH_TIME,
  239. HERMES_8_ETH_PRICE,
  240. HERMES_8_ETH_CONF,
  241. HERMES_8_ETH_EXPO,
  242. HERMES_8_ETH_PUBLISH_TIME,
  243. HERMES_8_ETH_EMA_PRICE,
  244. HERMES_8_ETH_EMA_CONF,
  245. HERMES_8_ETH_EMA_EXPO,
  246. HERMES_8_ETH_EMA_PUBLISH_TIME,
  247. HERMES_8_DOGE_PRICE,
  248. HERMES_8_DOGE_CONF,
  249. HERMES_8_DOGE_EXPO,
  250. HERMES_8_DOGE_PUBLISH_TIME,
  251. HERMES_8_DOGE_EMA_PRICE,
  252. HERMES_8_DOGE_EMA_CONF,
  253. HERMES_8_DOGE_EMA_EXPO,
  254. HERMES_8_DOGE_EMA_PUBLISH_TIME,
  255. HERMES_8_SOL_PRICE,
  256. HERMES_8_SOL_CONF,
  257. HERMES_8_SOL_EXPO,
  258. HERMES_8_SOL_PUBLISH_TIME,
  259. HERMES_8_SOL_EMA_PRICE,
  260. HERMES_8_SOL_EMA_CONF,
  261. HERMES_8_SOL_EMA_EXPO,
  262. HERMES_8_SOL_EMA_PUBLISH_TIME,
  263. HERMES_8_PYTH_PRICE,
  264. HERMES_8_PYTH_CONF,
  265. HERMES_8_PYTH_EXPO,
  266. HERMES_8_PYTH_PUBLISH_TIME,
  267. HERMES_8_PYTH_EMA_PRICE,
  268. HERMES_8_PYTH_EMA_CONF,
  269. HERMES_8_PYTH_EMA_EXPO,
  270. HERMES_8_PYTH_EMA_PUBLISH_TIME,
  271. HERMES_8_ARB_PRICE,
  272. HERMES_8_ARB_CONF,
  273. HERMES_8_ARB_EXPO,
  274. HERMES_8_ARB_PUBLISH_TIME,
  275. HERMES_8_ARB_EMA_PRICE,
  276. HERMES_8_ARB_EMA_CONF,
  277. HERMES_8_ARB_EMA_EXPO,
  278. HERMES_8_ARB_EMA_PUBLISH_TIME,
  279. HERMES_8_TON_PRICE,
  280. HERMES_8_TON_CONF,
  281. HERMES_8_TON_EXPO,
  282. HERMES_8_TON_PUBLISH_TIME,
  283. HERMES_8_TON_EMA_PRICE,
  284. HERMES_8_TON_EMA_CONF,
  285. HERMES_8_TON_EMA_EXPO,
  286. HERMES_8_TON_EMA_PUBLISH_TIME,
  287. HERMES_8_AAPL_PRICE,
  288. HERMES_8_AAPL_CONF,
  289. HERMES_8_AAPL_EXPO,
  290. HERMES_8_AAPL_PUBLISH_TIME,
  291. HERMES_8_AAPL_EMA_PRICE,
  292. HERMES_8_AAPL_EMA_CONF,
  293. HERMES_8_AAPL_EMA_EXPO,
  294. HERMES_8_AAPL_EMA_PUBLISH_TIME,
  295. HERMES_7_BTC_PRICE,
  296. HERMES_7_BTC_CONF,
  297. HERMES_7_BTC_EXPO,
  298. HERMES_7_BTC_PUBLISH_TIME,
  299. HERMES_7_BTC_EMA_PRICE,
  300. HERMES_7_BTC_EMA_CONF,
  301. HERMES_7_BTC_EMA_EXPO,
  302. HERMES_7_BTC_EMA_PUBLISH_TIME,
  303. HERMES_7_ETH_PRICE,
  304. HERMES_7_ETH_CONF,
  305. HERMES_7_ETH_EXPO,
  306. HERMES_7_ETH_PUBLISH_TIME,
  307. HERMES_7_ETH_EMA_PRICE,
  308. HERMES_7_ETH_EMA_CONF,
  309. HERMES_7_ETH_EMA_EXPO,
  310. HERMES_7_ETH_EMA_PUBLISH_TIME,
  311. HERMES_7_DOGE_PRICE,
  312. HERMES_7_DOGE_CONF,
  313. HERMES_7_DOGE_EXPO,
  314. HERMES_7_DOGE_PUBLISH_TIME,
  315. HERMES_7_DOGE_EMA_PRICE,
  316. HERMES_7_DOGE_EMA_CONF,
  317. HERMES_7_DOGE_EMA_EXPO,
  318. HERMES_7_DOGE_EMA_PUBLISH_TIME,
  319. HERMES_7_SOL_PRICE,
  320. HERMES_7_SOL_CONF,
  321. HERMES_7_SOL_EXPO,
  322. HERMES_7_SOL_PUBLISH_TIME,
  323. HERMES_7_SOL_EMA_PRICE,
  324. HERMES_7_SOL_EMA_CONF,
  325. HERMES_7_SOL_EMA_EXPO,
  326. HERMES_7_SOL_EMA_PUBLISH_TIME,
  327. HERMES_7_PYTH_PRICE,
  328. HERMES_7_PYTH_CONF,
  329. HERMES_7_PYTH_EXPO,
  330. HERMES_7_PYTH_PUBLISH_TIME,
  331. HERMES_7_PYTH_EMA_PRICE,
  332. HERMES_7_PYTH_EMA_CONF,
  333. HERMES_7_PYTH_EMA_EXPO,
  334. HERMES_7_PYTH_EMA_PUBLISH_TIME,
  335. HERMES_7_ARB_PRICE,
  336. HERMES_7_ARB_CONF,
  337. HERMES_7_ARB_EXPO,
  338. HERMES_7_ARB_PUBLISH_TIME,
  339. HERMES_7_ARB_EMA_PRICE,
  340. HERMES_7_ARB_EMA_CONF,
  341. HERMES_7_ARB_EMA_EXPO,
  342. HERMES_7_ARB_EMA_PUBLISH_TIME,
  343. HERMES_7_TON_PRICE,
  344. HERMES_7_TON_CONF,
  345. HERMES_7_TON_EXPO,
  346. HERMES_7_TON_PUBLISH_TIME,
  347. HERMES_7_TON_EMA_PRICE,
  348. HERMES_7_TON_EMA_CONF,
  349. HERMES_7_TON_EMA_EXPO,
  350. HERMES_7_TON_EMA_PUBLISH_TIME,
  351. HERMES_UPDATE_7_PRICE_FEEDS,
  352. HERMES_UPDATE_1_PRICE_FEED,
  353. HERMES_1_BTC_PUBLISH_TIME,
  354. HERMES_1_BTC_PRICE,
  355. HERMES_1_BTC_CONF,
  356. HERMES_1_BTC_EXPO,
  357. HERMES_1_BTC_EMA_PRICE,
  358. HERMES_1_BTC_EMA_CONF,
  359. HERMES_1_BTC_EMA_EXPO,
  360. HERMES_1_BTC_EMA_PUBLISH_TIME,
  361. HERMES_UPDATE_8_PRICE_FEEDS,
  362. } from "./utils/pyth";
  363. import { GUARDIAN_SET_0, MAINNET_UPGRADE_VAAS } from "./utils/wormhole";
  364. import { DataSource } from "@pythnetwork/xc-admin-common";
  365. import { createAuthorizeUpgradePayload } from "./utils";
  366. import {
  367. UniversalAddress,
  368. composeLiteral,
  369. createVAA,
  370. serialize,
  371. } from "@wormhole-foundation/sdk-definitions";
  372. import { mocks } from "@wormhole-foundation/sdk-definitions/testing";
  373. import { calculateUpdatePriceFeedsFee } from "@pythnetwork/pyth-ton-js";
  374. const TIME_PERIOD = 60;
  375. const PRICE = new Price({
  376. price: "1",
  377. conf: "2",
  378. expo: 3,
  379. publishTime: 4,
  380. });
  381. const EMA_PRICE = new Price({
  382. price: "5",
  383. conf: "6",
  384. expo: 7,
  385. publishTime: 8,
  386. });
  387. const SINGLE_UPDATE_FEE = 1;
  388. const DATA_SOURCES: DataSource[] = [
  389. {
  390. emitterChain: 26,
  391. emitterAddress:
  392. "e101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71",
  393. },
  394. ];
  395. const TEST_GOVERNANCE_DATA_SOURCES: DataSource[] = [
  396. {
  397. emitterChain: 1,
  398. emitterAddress:
  399. "0000000000000000000000000000000000000000000000000000000000000029",
  400. },
  401. {
  402. emitterChain: 2,
  403. emitterAddress:
  404. "000000000000000000000000000000000000000000000000000000000000002b",
  405. },
  406. {
  407. emitterChain: 1,
  408. emitterAddress:
  409. "0000000000000000000000000000000000000000000000000000000000000000",
  410. },
  411. ];
  412. const CUSTOM_PAYLOAD = Buffer.from("1234567890abcdef", "hex");
  413. describe("PythTest", () => {
  414. let code: Cell;
  415. beforeAll(async () => {
  416. code = await compile("PythTest");
  417. });
  418. let blockchain: Blockchain;
  419. let deployer: SandboxContract<TreasuryContract>;
  420. let mockDeployer: SandboxContract<TreasuryContract>;
  421. let pythTest: SandboxContract<PythTest>;
  422. beforeEach(async () => {
  423. blockchain = await Blockchain.create();
  424. deployer = await blockchain.treasury("deployer");
  425. mockDeployer = await blockchain.treasury("mockDeployer");
  426. });
  427. async function deployContract(
  428. priceFeedId: HexString = BTC_PRICE_FEED_ID,
  429. price: Price = PRICE,
  430. emaPrice: Price = EMA_PRICE,
  431. singleUpdateFee: number = SINGLE_UPDATE_FEE,
  432. dataSources: DataSource[] = DATA_SOURCES,
  433. guardianSetIndex: number = 0,
  434. guardianSet: string[] = GUARDIAN_SET_0,
  435. chainId: number = 1,
  436. governanceChainId: number = 1,
  437. governanceContract: string = "0000000000000000000000000000000000000000000000000000000000000004",
  438. governanceDataSource?: DataSource,
  439. ) {
  440. const config: PythTestConfig = {
  441. priceFeedId,
  442. price,
  443. emaPrice,
  444. singleUpdateFee,
  445. dataSources,
  446. guardianSetIndex,
  447. guardianSet,
  448. chainId,
  449. governanceChainId,
  450. governanceContract,
  451. governanceDataSource,
  452. };
  453. pythTest = blockchain.openContract(PythTest.createFromConfig(config, code));
  454. const deployResult = await pythTest.sendDeploy(
  455. deployer.getSender(),
  456. toNano("0.05"),
  457. );
  458. expect(deployResult.transactions).toHaveTransaction({
  459. from: deployer.address,
  460. to: pythTest.address,
  461. deploy: true,
  462. success: true,
  463. });
  464. }
  465. async function updateGuardianSets(
  466. pythTest: SandboxContract<PythTest>,
  467. deployer: SandboxContract<TreasuryContract>,
  468. ) {
  469. for (const vaa of MAINNET_UPGRADE_VAAS) {
  470. const result = await pythTest.sendUpdateGuardianSet(
  471. deployer.getSender(),
  472. Buffer.from(vaa, "hex"),
  473. );
  474. expect(result.transactions).toHaveTransaction({
  475. from: deployer.address,
  476. to: pythTest.address,
  477. success: true,
  478. });
  479. }
  480. }
  481. it("should correctly get price unsafe", async () => {
  482. await deployContract();
  483. const result = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  484. expect(result.price).toBe(1);
  485. expect(result.conf).toBe(2);
  486. expect(result.expo).toBe(3);
  487. expect(result.publishTime).toBe(4);
  488. });
  489. it("should correctly get price no older than", async () => {
  490. const timeNow = Math.floor(Date.now() / 1000) - TIME_PERIOD + 5; // 5 seconds buffer
  491. const price = new Price({
  492. price: "1",
  493. conf: "2",
  494. expo: 3,
  495. publishTime: timeNow,
  496. });
  497. await deployContract(BTC_PRICE_FEED_ID, price, EMA_PRICE);
  498. const result = await pythTest.getPriceNoOlderThan(
  499. TIME_PERIOD,
  500. BTC_PRICE_FEED_ID,
  501. );
  502. expect(result.price).toBe(1);
  503. expect(result.conf).toBe(2);
  504. expect(result.expo).toBe(3);
  505. expect(result.publishTime).toBe(timeNow);
  506. });
  507. it("should fail to get price no older than", async () => {
  508. await deployContract();
  509. await expect(
  510. pythTest.getPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID),
  511. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  512. });
  513. it("should correctly get ema price no older than", async () => {
  514. const timeNow = Math.floor(Date.now() / 1000) - TIME_PERIOD + 5; // 5 seconds buffer
  515. const emaPrice = new Price({
  516. price: "5",
  517. conf: "6",
  518. expo: 7,
  519. publishTime: timeNow,
  520. });
  521. await deployContract(BTC_PRICE_FEED_ID, PRICE, emaPrice);
  522. const result = await pythTest.getEmaPriceNoOlderThan(
  523. TIME_PERIOD,
  524. BTC_PRICE_FEED_ID,
  525. );
  526. expect(result.price).toBe(5);
  527. expect(result.conf).toBe(6);
  528. expect(result.expo).toBe(7);
  529. expect(result.publishTime).toBe(timeNow);
  530. });
  531. it("should fail to get ema price no older than", async () => {
  532. await deployContract();
  533. await expect(
  534. pythTest.getEmaPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID),
  535. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  536. });
  537. it("should correctly get ema price unsafe", async () => {
  538. await deployContract();
  539. const result = await pythTest.getEmaPriceUnsafe(BTC_PRICE_FEED_ID);
  540. expect(result.price).toBe(5);
  541. expect(result.conf).toBe(6);
  542. expect(result.expo).toBe(7);
  543. expect(result.publishTime).toBe(8);
  544. });
  545. it("should correctly get update fee", async () => {
  546. await deployContract();
  547. const result = await pythTest.getUpdateFee(
  548. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  549. );
  550. expect(result).toBe(2);
  551. });
  552. it("should correctly update price feeds", async () => {
  553. await deployContract();
  554. let result;
  555. await updateGuardianSets(pythTest, deployer);
  556. // Check initial prices
  557. const initialBtcPrice = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  558. expect(initialBtcPrice.price).not.toBe(HERMES_BTC_PRICE);
  559. // Expect an error for ETH price feed as it doesn't exist initially
  560. await expect(pythTest.getPriceUnsafe(ETH_PRICE_FEED_ID)).rejects.toThrow(
  561. "Unable to execute get method. Got exit_code: 2000",
  562. ); // ERROR_PRICE_FEED_NOT_FOUND = 2000
  563. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  564. const updateFee = await pythTest.getUpdateFee(updateData);
  565. result = await pythTest.sendUpdatePriceFeeds(
  566. deployer.getSender(),
  567. updateData,
  568. toNano(updateFee),
  569. );
  570. expect(result.transactions).toHaveTransaction({
  571. from: deployer.address,
  572. to: pythTest.address,
  573. success: true,
  574. });
  575. // Check if both BTC and ETH prices have been updated
  576. const updatedBtcPrice = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  577. expect(updatedBtcPrice.price).toBe(HERMES_BTC_PRICE);
  578. expect(updatedBtcPrice.publishTime).toBe(HERMES_BTC_PUBLISH_TIME);
  579. const updatedEthPrice = await pythTest.getPriceUnsafe(ETH_PRICE_FEED_ID);
  580. expect(updatedEthPrice.price).toBe(HERMES_ETH_PRICE);
  581. expect(updatedEthPrice.publishTime).toBe(HERMES_ETH_PUBLISH_TIME);
  582. });
  583. it("should fail to get update fee with invalid data", async () => {
  584. await deployContract();
  585. await updateGuardianSets(pythTest, deployer);
  586. const invalidUpdateData = Buffer.from("invalid data");
  587. await expect(pythTest.getUpdateFee(invalidUpdateData)).rejects.toThrow(
  588. "Unable to execute get method. Got exit_code: 2002",
  589. ); // ERROR_INVALID_MAGIC = 2002
  590. });
  591. it("should fail to update price feeds with invalid data", async () => {
  592. await deployContract();
  593. await updateGuardianSets(pythTest, deployer);
  594. const invalidUpdateData = Buffer.from("invalid data");
  595. // Use a fixed value for updateFee since we can't get it from getUpdateFee
  596. const updateFee = toNano("0.1"); // Use a reasonable amount
  597. const result = await pythTest.sendUpdatePriceFeeds(
  598. deployer.getSender(),
  599. invalidUpdateData,
  600. updateFee,
  601. );
  602. expect(result.transactions).toHaveTransaction({
  603. from: deployer.address,
  604. to: pythTest.address,
  605. success: false,
  606. exitCode: 2002, // ERROR_INVALID_MAGIC
  607. });
  608. });
  609. it("should fail to update price feeds with outdated guardian set", async () => {
  610. await deployContract();
  611. // Don't update guardian sets
  612. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  613. const updateFee = await pythTest.getUpdateFee(updateData);
  614. const result = await pythTest.sendUpdatePriceFeeds(
  615. deployer.getSender(),
  616. updateData,
  617. toNano(updateFee),
  618. );
  619. expect(result.transactions).toHaveTransaction({
  620. from: deployer.address,
  621. to: pythTest.address,
  622. success: false,
  623. exitCode: 1002, // ERROR_GUARDIAN_SET_NOT_FOUND
  624. });
  625. });
  626. it("should fail to update price feeds with invalid data source", async () => {
  627. await deployContract(
  628. BTC_PRICE_FEED_ID,
  629. PRICE,
  630. EMA_PRICE,
  631. SINGLE_UPDATE_FEE,
  632. [], // Empty data sources
  633. );
  634. await updateGuardianSets(pythTest, deployer);
  635. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  636. const updateFee = await pythTest.getUpdateFee(updateData);
  637. const result = await pythTest.sendUpdatePriceFeeds(
  638. deployer.getSender(),
  639. updateData,
  640. toNano(updateFee),
  641. );
  642. expect(result.transactions).toHaveTransaction({
  643. from: deployer.address,
  644. to: pythTest.address,
  645. success: false,
  646. exitCode: 2005, // ERROR_UPDATE_DATA_SOURCE_NOT_FOUND
  647. });
  648. });
  649. it("should correctly handle stale prices", async () => {
  650. const staleTime = Math.floor(Date.now() / 1000) - TIME_PERIOD - 10; // 10 seconds past the allowed period
  651. const stalePrice = new Price({
  652. price: "1",
  653. conf: "2",
  654. expo: 3,
  655. publishTime: staleTime,
  656. });
  657. await deployContract(BTC_PRICE_FEED_ID, stalePrice, EMA_PRICE);
  658. await expect(
  659. pythTest.getPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID),
  660. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  661. });
  662. it("should fail to update price feeds with insufficient gas", async () => {
  663. await deployContract();
  664. await updateGuardianSets(pythTest, deployer);
  665. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  666. let result = await pythTest.sendUpdatePriceFeeds(
  667. deployer.getSender(),
  668. updateData,
  669. calculateUpdatePriceFeedsFee(1n), // Send enough gas for 1 update instead of 2
  670. );
  671. expect(result.transactions).toHaveTransaction({
  672. from: deployer.address,
  673. to: pythTest.address,
  674. success: false,
  675. exitCode: 3000, // ERROR_INSUFFICIENT_GAS
  676. });
  677. });
  678. it("should fail to update price feeds with insufficient fee", async () => {
  679. await deployContract();
  680. await updateGuardianSets(pythTest, deployer);
  681. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  682. const updateFee = await pythTest.getUpdateFee(updateData);
  683. // Send less than the required fee
  684. const insufficientFee = updateFee - 1;
  685. const result = await pythTest.sendUpdatePriceFeeds(
  686. deployer.getSender(),
  687. updateData,
  688. calculateUpdatePriceFeedsFee(2n) + BigInt(insufficientFee),
  689. );
  690. // Check that the transaction did not succeed
  691. expect(result.transactions).toHaveTransaction({
  692. from: deployer.address,
  693. to: pythTest.address,
  694. success: false,
  695. exitCode: 2011, // ERROR_INSUFFICIENT_FEE = 2011
  696. });
  697. });
  698. it("should fail to get prices for non-existent price feed", async () => {
  699. await deployContract();
  700. const nonExistentPriceFeedId =
  701. "0000000000000000000000000000000000000000000000000000000000000000";
  702. await expect(
  703. pythTest.getPriceUnsafe(nonExistentPriceFeedId),
  704. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND = 2000
  705. await expect(
  706. pythTest.getPriceNoOlderThan(TIME_PERIOD, nonExistentPriceFeedId),
  707. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND
  708. await expect(
  709. pythTest.getEmaPriceUnsafe(nonExistentPriceFeedId),
  710. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND
  711. });
  712. it("should correctly get chain ID", async () => {
  713. await deployContract();
  714. const result = await pythTest.getChainId();
  715. expect(result).toEqual(1);
  716. });
  717. it("should correctly get last executed governance sequence", async () => {
  718. await deployContract(
  719. BTC_PRICE_FEED_ID,
  720. PRICE,
  721. EMA_PRICE,
  722. SINGLE_UPDATE_FEE,
  723. DATA_SOURCES,
  724. 0,
  725. [TEST_GUARDIAN_ADDRESS1],
  726. 60051,
  727. 1,
  728. "0000000000000000000000000000000000000000000000000000000000000004",
  729. TEST_GOVERNANCE_DATA_SOURCES[0],
  730. );
  731. // Check initial value
  732. let result = await pythTest.getLastExecutedGovernanceSequence();
  733. expect(result).toEqual(0);
  734. // Execute a governance action (e.g., set fee)
  735. await pythTest.sendExecuteGovernanceAction(
  736. deployer.getSender(),
  737. Buffer.from(PYTH_SET_FEE, "hex"),
  738. );
  739. // Check that the sequence has increased
  740. result = await pythTest.getLastExecutedGovernanceSequence();
  741. expect(result).toEqual(1);
  742. });
  743. it("should correctly get governance data source index", async () => {
  744. // Deploy contract with initial governance data source
  745. await deployContract(
  746. BTC_PRICE_FEED_ID,
  747. PRICE,
  748. EMA_PRICE,
  749. SINGLE_UPDATE_FEE,
  750. DATA_SOURCES,
  751. 0,
  752. [TEST_GUARDIAN_ADDRESS1],
  753. 60051,
  754. 1,
  755. "0000000000000000000000000000000000000000000000000000000000000004",
  756. TEST_GOVERNANCE_DATA_SOURCES[0],
  757. );
  758. // Check initial value
  759. let result = await pythTest.getGovernanceDataSourceIndex();
  760. expect(result).toEqual(0);
  761. // Execute governance action to change data source
  762. await pythTest.sendExecuteGovernanceAction(
  763. deployer.getSender(),
  764. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  765. );
  766. // Check that the index has increased
  767. result = await pythTest.getGovernanceDataSourceIndex();
  768. expect(result).toEqual(1);
  769. });
  770. it("should correctly get governance data source", async () => {
  771. // Deploy contract without initial governance data source
  772. await deployContract();
  773. // Check initial value (should be empty)
  774. let result = await pythTest.getGovernanceDataSource();
  775. expect(result).toBeFalsy();
  776. // Deploy contract with initial governance data source
  777. await deployContract(
  778. BTC_PRICE_FEED_ID,
  779. PRICE,
  780. EMA_PRICE,
  781. SINGLE_UPDATE_FEE,
  782. DATA_SOURCES,
  783. 0,
  784. [TEST_GUARDIAN_ADDRESS1],
  785. 60051,
  786. 1,
  787. "0000000000000000000000000000000000000000000000000000000000000004",
  788. TEST_GOVERNANCE_DATA_SOURCES[0],
  789. );
  790. // Check that the governance data source is set
  791. result = await pythTest.getGovernanceDataSource();
  792. expect(result).toEqual(TEST_GOVERNANCE_DATA_SOURCES[0]);
  793. // Execute governance action to change data source
  794. await pythTest.sendExecuteGovernanceAction(
  795. deployer.getSender(),
  796. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  797. );
  798. // Check that the data source has changed
  799. result = await pythTest.getGovernanceDataSource();
  800. expect(result).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]);
  801. });
  802. it("should correctly get single update fee", async () => {
  803. await deployContract();
  804. // Get the initial fee
  805. const result = await pythTest.getSingleUpdateFee();
  806. expect(result).toBe(SINGLE_UPDATE_FEE);
  807. });
  808. it("should execute set data sources governance instruction", async () => {
  809. await deployContract(
  810. BTC_PRICE_FEED_ID,
  811. PRICE,
  812. EMA_PRICE,
  813. SINGLE_UPDATE_FEE,
  814. DATA_SOURCES,
  815. 0,
  816. [TEST_GUARDIAN_ADDRESS1],
  817. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  818. 1,
  819. "0000000000000000000000000000000000000000000000000000000000000004",
  820. TEST_GOVERNANCE_DATA_SOURCES[0],
  821. );
  822. // Execute the governance action
  823. const result = await pythTest.sendExecuteGovernanceAction(
  824. deployer.getSender(),
  825. Buffer.from(PYTH_SET_DATA_SOURCES, "hex"),
  826. );
  827. expect(result.transactions).toHaveTransaction({
  828. from: deployer.address,
  829. to: pythTest.address,
  830. success: true,
  831. });
  832. // Verify that the new data sources are set correctly
  833. const newDataSources: DataSource[] = [
  834. {
  835. emitterChain: 1,
  836. emitterAddress:
  837. "6bb14509a612f01fbbc4cffeebd4bbfb492a86df717ebe92eb6df432a3f00a25",
  838. },
  839. {
  840. emitterChain: 3,
  841. emitterAddress:
  842. "000000000000000000000000000000000000000000000000000000000000012d",
  843. },
  844. ];
  845. for (const dataSource of newDataSources) {
  846. const isValid = await pythTest.getIsValidDataSource(dataSource);
  847. expect(isValid).toBe(true);
  848. }
  849. // Verify that the old data source is no longer valid
  850. const oldDataSource = DATA_SOURCES[0];
  851. const oldDataSourceIsValid =
  852. await pythTest.getIsValidDataSource(oldDataSource);
  853. expect(oldDataSourceIsValid).toBe(false);
  854. });
  855. it("should execute set fee governance instruction", async () => {
  856. await deployContract(
  857. BTC_PRICE_FEED_ID,
  858. PRICE,
  859. EMA_PRICE,
  860. SINGLE_UPDATE_FEE,
  861. DATA_SOURCES,
  862. 0,
  863. [TEST_GUARDIAN_ADDRESS1],
  864. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  865. 1,
  866. "0000000000000000000000000000000000000000000000000000000000000004",
  867. TEST_GOVERNANCE_DATA_SOURCES[0],
  868. );
  869. // Get the initial fee
  870. const initialFee = await pythTest.getSingleUpdateFee();
  871. expect(initialFee).toBe(SINGLE_UPDATE_FEE);
  872. // Execute the governance action
  873. const result = await pythTest.sendExecuteGovernanceAction(
  874. deployer.getSender(),
  875. Buffer.from(PYTH_SET_FEE, "hex"),
  876. );
  877. expect(result.transactions).toHaveTransaction({
  878. from: deployer.address,
  879. to: pythTest.address,
  880. success: true,
  881. });
  882. // Get the new fee
  883. const newFee = await pythTest.getSingleUpdateFee();
  884. expect(newFee).toBe(4200); // The new fee value is 4200 in the PYTH_SET_FEE payload
  885. // Verify that the new fee is used for updates
  886. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  887. const updateFee = await pythTest.getUpdateFee(updateData);
  888. expect(updateFee).toBe(8400); // There are two price updates in HERMES_BTC_ETH_UPDATE
  889. });
  890. it("should execute authorize governance data source transfer", async () => {
  891. await deployContract(
  892. BTC_PRICE_FEED_ID,
  893. PRICE,
  894. EMA_PRICE,
  895. SINGLE_UPDATE_FEE,
  896. DATA_SOURCES,
  897. 0,
  898. [TEST_GUARDIAN_ADDRESS1],
  899. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  900. 1,
  901. "0000000000000000000000000000000000000000000000000000000000000004",
  902. TEST_GOVERNANCE_DATA_SOURCES[0],
  903. );
  904. // Get the initial governance data source index
  905. const initialIndex = await pythTest.getGovernanceDataSourceIndex();
  906. expect(initialIndex).toEqual(0); // Initial value should be 0
  907. // Get the initial governance data source
  908. const initialDataSource = await pythTest.getGovernanceDataSource();
  909. expect(initialDataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[0]);
  910. // Get the initial last executed governance sequence
  911. const initialSequence = await pythTest.getLastExecutedGovernanceSequence();
  912. expect(initialSequence).toEqual(0); // Initial value should be 0
  913. // Execute the governance action
  914. const result = await pythTest.sendExecuteGovernanceAction(
  915. deployer.getSender(),
  916. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  917. );
  918. expect(result.transactions).toHaveTransaction({
  919. from: deployer.address,
  920. to: pythTest.address,
  921. success: true,
  922. });
  923. // Get the new governance data source index
  924. const newIndex = await pythTest.getGovernanceDataSourceIndex();
  925. expect(newIndex).toEqual(1); // The new index value should match the one in the test payload
  926. // Get the new governance data source
  927. const newDataSource = await pythTest.getGovernanceDataSource();
  928. expect(newDataSource).not.toEqual(initialDataSource); // The data source should have changed
  929. expect(newDataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]); // The data source should have changed
  930. // Get the new last executed governance sequence
  931. const newSequence = await pythTest.getLastExecutedGovernanceSequence();
  932. expect(newSequence).toBeGreaterThan(initialSequence); // The sequence should have increased
  933. expect(newSequence).toBe(1);
  934. });
  935. it("should fail when executing request governance data source transfer directly", async () => {
  936. await deployContract(
  937. BTC_PRICE_FEED_ID,
  938. PRICE,
  939. EMA_PRICE,
  940. SINGLE_UPDATE_FEE,
  941. DATA_SOURCES,
  942. 0,
  943. [TEST_GUARDIAN_ADDRESS1],
  944. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  945. 1,
  946. "0000000000000000000000000000000000000000000000000000000000000004",
  947. TEST_GOVERNANCE_DATA_SOURCES[1],
  948. );
  949. const result = await pythTest.sendExecuteGovernanceAction(
  950. deployer.getSender(),
  951. Buffer.from(PYTH_REQUEST_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  952. );
  953. // Check that the transaction did not succeed
  954. expect(result.transactions).toHaveTransaction({
  955. from: deployer.address,
  956. to: pythTest.address,
  957. success: false,
  958. exitCode: 1012, // ERROR_INVALID_GOVERNANCE_ACTION
  959. });
  960. // Verify that the governance data source index hasn't changed
  961. const index = await pythTest.getGovernanceDataSourceIndex();
  962. expect(index).toEqual(0); // Should still be the initial value
  963. // Verify that the governance data source hasn't changed
  964. const dataSource = await pythTest.getGovernanceDataSource();
  965. expect(dataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]); // Should still be the initial value
  966. });
  967. it("should fail to execute governance action with invalid governance data source", async () => {
  968. await deployContract(
  969. BTC_PRICE_FEED_ID,
  970. PRICE,
  971. EMA_PRICE,
  972. SINGLE_UPDATE_FEE,
  973. DATA_SOURCES,
  974. 0,
  975. [TEST_GUARDIAN_ADDRESS1],
  976. 60051,
  977. 1,
  978. "0000000000000000000000000000000000000000000000000000000000000004",
  979. TEST_GOVERNANCE_DATA_SOURCES[1],
  980. );
  981. const result = await pythTest.sendExecuteGovernanceAction(
  982. deployer.getSender(),
  983. Buffer.from(PYTH_SET_FEE, "hex"),
  984. );
  985. expect(result.transactions).toHaveTransaction({
  986. from: deployer.address,
  987. to: pythTest.address,
  988. success: false,
  989. exitCode: 2013, // ERROR_INVALID_GOVERNANCE_DATA_SOURCE
  990. });
  991. });
  992. it("should fail to execute governance action with old sequence number", async () => {
  993. await deployContract(
  994. BTC_PRICE_FEED_ID,
  995. PRICE,
  996. EMA_PRICE,
  997. SINGLE_UPDATE_FEE,
  998. DATA_SOURCES,
  999. 0,
  1000. [TEST_GUARDIAN_ADDRESS1],
  1001. 60051,
  1002. 1,
  1003. "0000000000000000000000000000000000000000000000000000000000000004",
  1004. TEST_GOVERNANCE_DATA_SOURCES[0],
  1005. );
  1006. // Execute a governance action to increase the sequence number
  1007. await pythTest.sendExecuteGovernanceAction(
  1008. deployer.getSender(),
  1009. Buffer.from(PYTH_SET_FEE, "hex"),
  1010. );
  1011. // Try to execute the same governance action again
  1012. const result = await pythTest.sendExecuteGovernanceAction(
  1013. deployer.getSender(),
  1014. Buffer.from(PYTH_SET_FEE, "hex"),
  1015. );
  1016. expect(result.transactions).toHaveTransaction({
  1017. from: deployer.address,
  1018. to: pythTest.address,
  1019. success: false,
  1020. exitCode: 2014, // ERROR_OLD_GOVERNANCE_MESSAGE
  1021. });
  1022. });
  1023. it("should fail to execute governance action with invalid chain ID", async () => {
  1024. const invalidChainId = 999;
  1025. await deployContract(
  1026. BTC_PRICE_FEED_ID,
  1027. PRICE,
  1028. EMA_PRICE,
  1029. SINGLE_UPDATE_FEE,
  1030. DATA_SOURCES,
  1031. 0,
  1032. [TEST_GUARDIAN_ADDRESS1],
  1033. invalidChainId,
  1034. 1,
  1035. "0000000000000000000000000000000000000000000000000000000000000004",
  1036. TEST_GOVERNANCE_DATA_SOURCES[0],
  1037. );
  1038. const result = await pythTest.sendExecuteGovernanceAction(
  1039. deployer.getSender(),
  1040. Buffer.from(PYTH_SET_FEE, "hex"),
  1041. );
  1042. expect(result.transactions).toHaveTransaction({
  1043. from: deployer.address,
  1044. to: pythTest.address,
  1045. success: false,
  1046. exitCode: 2015, // ERROR_INVALID_GOVERNANCE_TARGET
  1047. });
  1048. });
  1049. it("should successfully upgrade the contract", async () => {
  1050. // Compile the upgraded contract
  1051. const upgradedCode = await compile("PythTestUpgraded");
  1052. const upgradedCodeHash = upgradedCode.hash();
  1053. // Create the authorize upgrade payload
  1054. const authorizeUpgradePayload =
  1055. createAuthorizeUpgradePayload(upgradedCodeHash);
  1056. const authorizeUpgradeVaa = createVAA("Uint8Array", {
  1057. guardianSet: 0,
  1058. timestamp: 0,
  1059. nonce: 0,
  1060. emitterChain: "Solana",
  1061. emitterAddress: new UniversalAddress(new Uint8Array(32)),
  1062. sequence: 1n,
  1063. consistencyLevel: 0,
  1064. signatures: [],
  1065. payload: authorizeUpgradePayload,
  1066. });
  1067. const guardianSet = mocks.devnetGuardianSet();
  1068. guardianSet.setSignatures(authorizeUpgradeVaa);
  1069. await deployContract(
  1070. BTC_PRICE_FEED_ID,
  1071. PRICE,
  1072. EMA_PRICE,
  1073. SINGLE_UPDATE_FEE,
  1074. DATA_SOURCES,
  1075. 0,
  1076. [TEST_GUARDIAN_ADDRESS2],
  1077. 1,
  1078. 1,
  1079. "0000000000000000000000000000000000000000000000000000000000000000",
  1080. TEST_GOVERNANCE_DATA_SOURCES[2],
  1081. );
  1082. // Execute the upgrade
  1083. const sendExecuteGovernanceActionResult =
  1084. await pythTest.sendExecuteGovernanceAction(
  1085. deployer.getSender(),
  1086. Buffer.from(serialize(authorizeUpgradeVaa)),
  1087. );
  1088. expect(sendExecuteGovernanceActionResult.transactions).toHaveTransaction({
  1089. from: deployer.address,
  1090. to: pythTest.address,
  1091. success: true,
  1092. });
  1093. // Execute the upgrade
  1094. const sendUpgradeContractResult = await pythTest.sendUpgradeContract(
  1095. deployer.getSender(),
  1096. upgradedCode,
  1097. );
  1098. expect(sendUpgradeContractResult.transactions).toHaveTransaction({
  1099. from: deployer.address,
  1100. to: pythTest.address,
  1101. success: true,
  1102. });
  1103. // Verify that the contract has been upgraded by calling a new method
  1104. const newMethodResult = await pythTest.getNewFunction();
  1105. expect(newMethodResult).toBe(1);
  1106. });
  1107. it("should fail to upgrade the contract with modified code", async () => {
  1108. // Compile the upgraded contract
  1109. const upgradedCode = await compile("PythTestUpgraded");
  1110. const upgradedCodeHash = upgradedCode.hash();
  1111. // Create the authorize upgrade payload
  1112. const authorizeUpgradePayload =
  1113. createAuthorizeUpgradePayload(upgradedCodeHash);
  1114. const authorizeUpgradeVaa = createVAA("Uint8Array", {
  1115. guardianSet: 0,
  1116. timestamp: 0,
  1117. nonce: 0,
  1118. emitterChain: "Solana",
  1119. emitterAddress: new UniversalAddress(new Uint8Array(32)),
  1120. sequence: 1n,
  1121. consistencyLevel: 0,
  1122. signatures: [],
  1123. payload: authorizeUpgradePayload,
  1124. });
  1125. const guardianSet = mocks.devnetGuardianSet();
  1126. guardianSet.setSignatures(authorizeUpgradeVaa);
  1127. await deployContract(
  1128. BTC_PRICE_FEED_ID,
  1129. PRICE,
  1130. EMA_PRICE,
  1131. SINGLE_UPDATE_FEE,
  1132. DATA_SOURCES,
  1133. 0,
  1134. [TEST_GUARDIAN_ADDRESS2],
  1135. 1,
  1136. 1,
  1137. "0000000000000000000000000000000000000000000000000000000000000000",
  1138. TEST_GOVERNANCE_DATA_SOURCES[2],
  1139. );
  1140. // Execute the upgrade authorization
  1141. const sendExecuteGovernanceActionResult =
  1142. await pythTest.sendExecuteGovernanceAction(
  1143. deployer.getSender(),
  1144. Buffer.from(serialize(authorizeUpgradeVaa)),
  1145. );
  1146. expect(sendExecuteGovernanceActionResult.transactions).toHaveTransaction({
  1147. from: deployer.address,
  1148. to: pythTest.address,
  1149. success: true,
  1150. });
  1151. // Attempt to execute the upgrade with a different code
  1152. const wormholeTestCode = await compile("WormholeTest");
  1153. const sendUpgradeContractResult = await pythTest.sendUpgradeContract(
  1154. deployer.getSender(),
  1155. wormholeTestCode,
  1156. );
  1157. // Expect the transaction to fail
  1158. expect(sendUpgradeContractResult.transactions).toHaveTransaction({
  1159. from: deployer.address,
  1160. to: pythTest.address,
  1161. success: false,
  1162. exitCode: 2018, // ERROR_INVALID_CODE_HASH
  1163. });
  1164. // Verify that the contract has not been upgraded by attempting to call the new method
  1165. await expect(pythTest.getNewFunction()).rejects.toThrow();
  1166. });
  1167. it("should successfully parse price feed updates", async () => {
  1168. await deployContract();
  1169. await updateGuardianSets(pythTest, deployer);
  1170. const sentValue = toNano("1");
  1171. const result = await pythTest.sendParsePriceFeedUpdates(
  1172. deployer.getSender(),
  1173. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1174. sentValue,
  1175. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1176. HERMES_BTC_PUBLISH_TIME,
  1177. HERMES_BTC_PUBLISH_TIME,
  1178. deployer.address,
  1179. CUSTOM_PAYLOAD,
  1180. );
  1181. // Verify transaction success and message count
  1182. expect(result.transactions).toHaveTransaction({
  1183. from: deployer.address,
  1184. to: pythTest.address,
  1185. success: true,
  1186. outMessagesCount: 1,
  1187. });
  1188. // Get the output message
  1189. const outMessage = result.transactions[1].outMessages.values()[0];
  1190. // Verify excess value is returned
  1191. expect(
  1192. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1193. ).toBeGreaterThan(0);
  1194. const cs = outMessage.body.beginParse();
  1195. // Verify message header
  1196. const op = cs.loadUint(32);
  1197. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  1198. // Verify number of price feeds
  1199. const numPriceFeeds = cs.loadUint(8);
  1200. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1201. // Load and verify price feeds
  1202. const priceFeedsCell = cs.loadRef();
  1203. let currentCell = priceFeedsCell;
  1204. // First price feed (BTC)
  1205. const btcCs = currentCell.beginParse();
  1206. const btcPriceId =
  1207. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  1208. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  1209. const btcPriceFeedCell = btcCs.loadRef();
  1210. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  1211. // Verify BTC current price
  1212. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  1213. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  1214. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_PRICE);
  1215. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_CONF);
  1216. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_EXPO);
  1217. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  1218. // Verify BTC EMA price
  1219. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  1220. const btcEmaPrice = btcEmaPriceCell.beginParse();
  1221. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_EMA_PRICE);
  1222. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_EMA_CONF);
  1223. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_EMA_EXPO);
  1224. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  1225. // Move to ETH price feed
  1226. currentCell = btcCs.loadRef();
  1227. // Second price feed (ETH)
  1228. const ethCs = currentCell.beginParse();
  1229. const ethPriceId =
  1230. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  1231. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  1232. const ethPriceFeedCell = ethCs.loadRef();
  1233. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  1234. // Verify ETH current price
  1235. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  1236. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  1237. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_PRICE);
  1238. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_CONF);
  1239. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_EXPO);
  1240. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  1241. // Verify ETH EMA price
  1242. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  1243. const ethEmaPrice = ethEmaPriceCell.beginParse();
  1244. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_EMA_PRICE);
  1245. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_EMA_CONF);
  1246. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_EMA_EXPO);
  1247. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  1248. // Verify this is the end of the chain
  1249. expect(ethCs.remainingRefs).toBe(0);
  1250. // Verify sender address
  1251. const senderAddress = cs.loadAddress();
  1252. expect(senderAddress?.toString()).toBe(
  1253. deployer.getSender().address.toString(),
  1254. );
  1255. // Verify custom payload
  1256. const customPayloadCell = cs.loadRef();
  1257. const customPayloadSlice = customPayloadCell.beginParse();
  1258. const receivedPayload = Buffer.from(
  1259. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1260. );
  1261. expect(receivedPayload.toString("hex")).toBe(
  1262. CUSTOM_PAYLOAD.toString("hex"),
  1263. );
  1264. });
  1265. it("should successfully parse price feed updates with more than 3 price feed ids", async () => {
  1266. await deployContract();
  1267. await updateGuardianSets(pythTest, deployer);
  1268. const sentValue = toNano("1");
  1269. const result = await pythTest.sendParsePriceFeedUpdates(
  1270. deployer.getSender(),
  1271. Buffer.from(HERMES_SOL_TON_PYTH_USDT_UPDATE, "hex"),
  1272. sentValue,
  1273. [
  1274. SOL_PRICE_FEED_ID,
  1275. TON_PRICE_FEED_ID,
  1276. PYTH_PRICE_FEED_ID,
  1277. USDT_PRICE_FEED_ID,
  1278. ],
  1279. HERMES_SOL_UNIQUE_PUBLISH_TIME,
  1280. HERMES_SOL_UNIQUE_PUBLISH_TIME,
  1281. deployer.address,
  1282. CUSTOM_PAYLOAD,
  1283. );
  1284. // Verify transaction success and message count
  1285. expect(result.transactions).toHaveTransaction({
  1286. from: deployer.address,
  1287. to: pythTest.address,
  1288. success: true,
  1289. outMessagesCount: 1,
  1290. });
  1291. // Get the output message
  1292. const outMessage = result.transactions[1].outMessages.values()[0];
  1293. // Verify excess value is returned
  1294. expect(
  1295. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1296. ).toBeGreaterThan(0);
  1297. const cs = outMessage.body.beginParse();
  1298. // Verify message header
  1299. const op = cs.loadUint(32);
  1300. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  1301. // Verify number of price feeds
  1302. const numPriceFeeds = cs.loadUint(8);
  1303. expect(numPriceFeeds).toBe(4); // We expect SOL, TON, PYTH and USDT price feeds
  1304. // Load and verify price feeds
  1305. const priceFeedsCell = cs.loadRef();
  1306. let currentCell = priceFeedsCell;
  1307. // First price feed (SOL)
  1308. const solCs = currentCell.beginParse();
  1309. const solPriceId =
  1310. "0x" + solCs.loadUintBig(256).toString(16).padStart(64, "0");
  1311. expect(solPriceId).toBe(SOL_PRICE_FEED_ID);
  1312. const solPriceFeedCell = solCs.loadRef();
  1313. const solPriceFeedSlice = solPriceFeedCell.beginParse();
  1314. // Verify SOL current price
  1315. const solCurrentPriceCell = solPriceFeedSlice.loadRef();
  1316. const solCurrentPrice = solCurrentPriceCell.beginParse();
  1317. expect(solCurrentPrice.loadInt(64)).toBe(HERMES_SOL_UNIQUE_PRICE);
  1318. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_CONF);
  1319. expect(solCurrentPrice.loadInt(32)).toBe(HERMES_SOL_UNIQUE_EXPO);
  1320. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_PUBLISH_TIME);
  1321. // Move through TON and PYTH price feeds to reach USDT
  1322. currentCell = solCs.loadRef(); // Move to TON
  1323. const tonCs = currentCell.beginParse();
  1324. tonCs.loadUintBig(256); // Skip TON price ID
  1325. tonCs.loadRef(); // Skip TON price data
  1326. currentCell = tonCs.loadRef(); // Move to PYTH
  1327. const pythCs = currentCell.beginParse();
  1328. pythCs.loadUintBig(256); // Skip PYTH price ID
  1329. pythCs.loadRef(); // Skip PYTH price data
  1330. currentCell = pythCs.loadRef(); // Move to USDT
  1331. const usdtCs = currentCell.beginParse();
  1332. const usdtPriceId =
  1333. "0x" + usdtCs.loadUintBig(256).toString(16).padStart(64, "0");
  1334. expect(usdtPriceId).toBe(USDT_PRICE_FEED_ID);
  1335. const usdtPriceFeedCell = usdtCs.loadRef();
  1336. const usdtPriceFeedSlice = usdtPriceFeedCell.beginParse();
  1337. // Verify USDT current price
  1338. const usdtCurrentPriceCell = usdtPriceFeedSlice.loadRef();
  1339. const usdtCurrentPrice = usdtCurrentPriceCell.beginParse();
  1340. expect(usdtCurrentPrice.loadInt(64)).toBe(HERMES_USDT_UNIQUE_PRICE);
  1341. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_CONF);
  1342. expect(usdtCurrentPrice.loadInt(32)).toBe(HERMES_USDT_UNIQUE_EXPO);
  1343. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_PUBLISH_TIME);
  1344. // Verify this is the end of the chain
  1345. expect(usdtCs.remainingRefs).toBe(0);
  1346. // Verify sender address
  1347. const senderAddress = cs.loadAddress();
  1348. expect(senderAddress?.toString()).toBe(
  1349. deployer.getSender().address.toString(),
  1350. );
  1351. // Verify custom payload
  1352. const customPayloadCell = cs.loadRef();
  1353. const customPayloadSlice = customPayloadCell.beginParse();
  1354. const receivedPayload = Buffer.from(
  1355. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1356. );
  1357. expect(receivedPayload.toString("hex")).toBe(
  1358. CUSTOM_PAYLOAD.toString("hex"),
  1359. );
  1360. });
  1361. it("should successfully parse unique price feed updates", async () => {
  1362. await deployContract();
  1363. await updateGuardianSets(pythTest, deployer);
  1364. const sentValue = toNano("1");
  1365. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1366. deployer.getSender(),
  1367. Buffer.from(HERMES_BTC_ETH_UNIQUE_UPDATE, "hex"),
  1368. sentValue,
  1369. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1370. HERMES_BTC_PUBLISH_TIME,
  1371. 60,
  1372. deployer.address,
  1373. CUSTOM_PAYLOAD,
  1374. );
  1375. // Verify transaction success and message count
  1376. expect(result.transactions).toHaveTransaction({
  1377. from: deployer.address,
  1378. to: pythTest.address,
  1379. success: true,
  1380. outMessagesCount: 1,
  1381. });
  1382. // Get the output message
  1383. const outMessage = result.transactions[1].outMessages.values()[0];
  1384. // Verify excess value is returned
  1385. expect(
  1386. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1387. ).toBeGreaterThan(0);
  1388. const cs = outMessage.body.beginParse();
  1389. // Verify message header
  1390. const op = cs.loadUint(32);
  1391. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1392. // Verify number of price feeds
  1393. const numPriceFeeds = cs.loadUint(8);
  1394. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1395. // Load and verify price feeds
  1396. const priceFeedsCell = cs.loadRef();
  1397. let currentCell = priceFeedsCell;
  1398. // First price feed (BTC)
  1399. const btcCs = currentCell.beginParse();
  1400. const btcPriceId =
  1401. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  1402. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  1403. const btcPriceFeedCell = btcCs.loadRef();
  1404. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  1405. // Verify BTC current price
  1406. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  1407. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  1408. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_PRICE);
  1409. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_CONF);
  1410. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EXPO);
  1411. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_PUBLISH_TIME);
  1412. // Verify BTC EMA price
  1413. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  1414. const btcEmaPrice = btcEmaPriceCell.beginParse();
  1415. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_EMA_PRICE);
  1416. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_CONF);
  1417. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EMA_EXPO);
  1418. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_PUBLISH_TIME);
  1419. // Move to ETH price feed
  1420. currentCell = btcCs.loadRef();
  1421. // Second price feed (ETH)
  1422. const ethCs = currentCell.beginParse();
  1423. const ethPriceId =
  1424. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  1425. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  1426. const ethPriceFeedCell = ethCs.loadRef();
  1427. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  1428. // Verify ETH current price
  1429. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  1430. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  1431. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_PRICE);
  1432. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_CONF);
  1433. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EXPO);
  1434. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_PUBLISH_TIME);
  1435. // Verify ETH EMA price
  1436. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  1437. const ethEmaPrice = ethEmaPriceCell.beginParse();
  1438. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_EMA_PRICE);
  1439. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_CONF);
  1440. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EMA_EXPO);
  1441. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_PUBLISH_TIME);
  1442. // Verify this is the end of the chain
  1443. expect(ethCs.remainingRefs).toBe(0);
  1444. // Verify sender address
  1445. const senderAddress = cs.loadAddress();
  1446. expect(senderAddress?.toString()).toBe(
  1447. deployer.getSender().address.toString(),
  1448. );
  1449. // Verify custom payload
  1450. const customPayloadCell = cs.loadRef();
  1451. const customPayloadSlice = customPayloadCell.beginParse();
  1452. const receivedPayload = Buffer.from(
  1453. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1454. );
  1455. expect(receivedPayload.toString("hex")).toBe(
  1456. CUSTOM_PAYLOAD.toString("hex"),
  1457. );
  1458. });
  1459. it("should successfully parse unique price feed updates with more than 3 price feed ids", async () => {
  1460. await deployContract();
  1461. await updateGuardianSets(pythTest, deployer);
  1462. const sentValue = toNano("1");
  1463. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1464. deployer.getSender(),
  1465. Buffer.from(HERMES_SOL_TON_PYTH_USDT_UPDATE, "hex"),
  1466. sentValue,
  1467. [
  1468. SOL_PRICE_FEED_ID,
  1469. TON_PRICE_FEED_ID,
  1470. PYTH_PRICE_FEED_ID,
  1471. USDT_PRICE_FEED_ID,
  1472. ],
  1473. HERMES_SOL_UNIQUE_PUBLISH_TIME,
  1474. 60,
  1475. deployer.address,
  1476. CUSTOM_PAYLOAD,
  1477. );
  1478. // Verify transaction success and message count
  1479. expect(result.transactions).toHaveTransaction({
  1480. from: deployer.address,
  1481. to: pythTest.address,
  1482. success: true,
  1483. outMessagesCount: 1,
  1484. });
  1485. // Get the output message
  1486. const outMessage = result.transactions[1].outMessages.values()[0];
  1487. // Verify excess value is returned
  1488. expect(
  1489. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1490. ).toBeGreaterThan(0);
  1491. const cs = outMessage.body.beginParse();
  1492. // Verify message header
  1493. const op = cs.loadUint(32);
  1494. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1495. // Verify number of price feeds
  1496. const numPriceFeeds = cs.loadUint(8);
  1497. expect(numPriceFeeds).toBe(4); // We expect SOL, TON, PYTH and USDT price feeds
  1498. // Load and verify price feeds
  1499. const priceFeedsCell = cs.loadRef();
  1500. let currentCell = priceFeedsCell;
  1501. // First price feed (SOL)
  1502. const solCs = currentCell.beginParse();
  1503. const solPriceId =
  1504. "0x" + solCs.loadUintBig(256).toString(16).padStart(64, "0");
  1505. expect(solPriceId).toBe(SOL_PRICE_FEED_ID);
  1506. const solPriceFeedCell = solCs.loadRef();
  1507. const solPriceFeedSlice = solPriceFeedCell.beginParse();
  1508. // Verify SOL current price
  1509. const solCurrentPriceCell = solPriceFeedSlice.loadRef();
  1510. const solCurrentPrice = solCurrentPriceCell.beginParse();
  1511. expect(solCurrentPrice.loadInt(64)).toBe(HERMES_SOL_UNIQUE_PRICE);
  1512. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_CONF);
  1513. expect(solCurrentPrice.loadInt(32)).toBe(HERMES_SOL_UNIQUE_EXPO);
  1514. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_PUBLISH_TIME);
  1515. // Move through TON and PYTH price feeds to reach USDT
  1516. currentCell = solCs.loadRef(); // Move to TON
  1517. const tonCs = currentCell.beginParse();
  1518. tonCs.loadUintBig(256); // Skip TON price ID
  1519. tonCs.loadRef(); // Skip TON price data
  1520. currentCell = tonCs.loadRef(); // Move to PYTH
  1521. const pythCs = currentCell.beginParse();
  1522. pythCs.loadUintBig(256); // Skip PYTH price ID
  1523. pythCs.loadRef(); // Skip PYTH price data
  1524. currentCell = pythCs.loadRef(); // Move to USDT
  1525. const usdtCs = currentCell.beginParse();
  1526. const usdtPriceId =
  1527. "0x" + usdtCs.loadUintBig(256).toString(16).padStart(64, "0");
  1528. expect(usdtPriceId).toBe(USDT_PRICE_FEED_ID);
  1529. const usdtPriceFeedCell = usdtCs.loadRef();
  1530. const usdtPriceFeedSlice = usdtPriceFeedCell.beginParse();
  1531. // Verify USDT current price
  1532. const usdtCurrentPriceCell = usdtPriceFeedSlice.loadRef();
  1533. const usdtCurrentPrice = usdtCurrentPriceCell.beginParse();
  1534. expect(usdtCurrentPrice.loadInt(64)).toBe(HERMES_USDT_UNIQUE_PRICE);
  1535. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_CONF);
  1536. expect(usdtCurrentPrice.loadInt(32)).toBe(HERMES_USDT_UNIQUE_EXPO);
  1537. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_PUBLISH_TIME);
  1538. // Verify this is the end of the chain
  1539. expect(usdtCs.remainingRefs).toBe(0);
  1540. // Verify sender address
  1541. const senderAddress = cs.loadAddress();
  1542. expect(senderAddress?.toString()).toBe(
  1543. deployer.getSender().address.toString(),
  1544. );
  1545. // Verify custom payload
  1546. const customPayloadCell = cs.loadRef();
  1547. const customPayloadSlice = customPayloadCell.beginParse();
  1548. const receivedPayload = Buffer.from(
  1549. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1550. );
  1551. expect(receivedPayload.toString("hex")).toBe(
  1552. CUSTOM_PAYLOAD.toString("hex"),
  1553. );
  1554. });
  1555. it("should fail to parse invalid price feed updates", async () => {
  1556. await deployContract();
  1557. await updateGuardianSets(pythTest, deployer);
  1558. const invalidUpdateData = Buffer.from("invalid data");
  1559. const result = await pythTest.sendParsePriceFeedUpdates(
  1560. deployer.getSender(),
  1561. invalidUpdateData,
  1562. toNano("1"),
  1563. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1564. HERMES_BTC_PUBLISH_TIME,
  1565. HERMES_BTC_PUBLISH_TIME,
  1566. deployer.address,
  1567. CUSTOM_PAYLOAD,
  1568. );
  1569. // Verify transaction success but error response sent
  1570. expect(result.transactions).toHaveTransaction({
  1571. from: deployer.address,
  1572. to: pythTest.address,
  1573. success: true,
  1574. });
  1575. // Find the error response message - it's in the second transaction's outMessages
  1576. const errorTx = result.transactions[1]; // The PythTest contract transaction
  1577. expect(errorTx.outMessages.values().length).toBeGreaterThan(0);
  1578. const errorMessage = errorTx.outMessages.values()[0];
  1579. expect(errorMessage).toBeDefined();
  1580. const cs = errorMessage.body.beginParse();
  1581. // Verify error response format
  1582. const op = cs.loadUint(32);
  1583. expect(op).toBe(0x10002); // OP_RESPONSE_ERROR
  1584. const errorCode = cs.loadUint(32);
  1585. expect(errorCode).toBe(2002); // ERROR_INVALID_MAGIC
  1586. const originalOp = cs.loadUint(32);
  1587. expect(originalOp).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  1588. // Verify custom payload is preserved
  1589. const customPayloadCell = cs.loadRef();
  1590. const customPayloadSlice = customPayloadCell.beginParse();
  1591. expect(
  1592. Buffer.from(
  1593. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1594. ).toString("hex"),
  1595. ).toBe(CUSTOM_PAYLOAD.toString("hex"));
  1596. });
  1597. it("should fail to parse price feed updates within range", async () => {
  1598. await deployContract();
  1599. await updateGuardianSets(pythTest, deployer);
  1600. const sentValue = toNano("1");
  1601. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1602. deployer.getSender(),
  1603. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1604. sentValue,
  1605. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1606. HERMES_BTC_PUBLISH_TIME + 1,
  1607. HERMES_BTC_PUBLISH_TIME + 1,
  1608. deployer.address,
  1609. CUSTOM_PAYLOAD,
  1610. );
  1611. // Verify transaction success but error response sent
  1612. expect(result.transactions).toHaveTransaction({
  1613. from: deployer.address,
  1614. to: pythTest.address,
  1615. success: true,
  1616. });
  1617. // Find the error response message - it's in the second transaction's outMessages
  1618. const errorTx = result.transactions[1]; // The PythTest contract transaction
  1619. expect(errorTx.outMessages.values().length).toBeGreaterThan(0);
  1620. const errorMessage = errorTx.outMessages.values()[0];
  1621. expect(errorMessage).toBeDefined();
  1622. const cs = errorMessage.body.beginParse();
  1623. // Verify error response format
  1624. const op = cs.loadUint(32);
  1625. expect(op).toBe(0x10002); // OP_RESPONSE_ERROR
  1626. const errorCode = cs.loadUint(32);
  1627. expect(errorCode).toBe(2020); // ERROR_PRICE_FEED_NOT_FOUND_WITHIN_RANGE
  1628. const originalOp = cs.loadUint(32);
  1629. expect(originalOp).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1630. // Verify custom payload is preserved
  1631. const customPayloadCell = cs.loadRef();
  1632. const customPayloadSlice = customPayloadCell.beginParse();
  1633. expect(
  1634. Buffer.from(
  1635. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1636. ).toString("hex"),
  1637. ).toBe(CUSTOM_PAYLOAD.toString("hex"));
  1638. });
  1639. it("should fail to parse unique price feed updates", async () => {
  1640. await deployContract();
  1641. await updateGuardianSets(pythTest, deployer);
  1642. const sentValue = toNano("1");
  1643. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1644. deployer.getSender(),
  1645. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1646. sentValue,
  1647. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1648. HERMES_BTC_PUBLISH_TIME,
  1649. 60,
  1650. deployer.address,
  1651. CUSTOM_PAYLOAD,
  1652. );
  1653. // Verify transaction success but error response sent
  1654. expect(result.transactions).toHaveTransaction({
  1655. from: deployer.address,
  1656. to: pythTest.address,
  1657. success: true,
  1658. });
  1659. // Find the error response message - it's in the second transaction's outMessages
  1660. const errorTx = result.transactions[1]; // The PythTest contract transaction
  1661. expect(errorTx.outMessages.values().length).toBeGreaterThan(0);
  1662. const errorMessage = errorTx.outMessages.values()[0];
  1663. expect(errorMessage).toBeDefined();
  1664. const cs = errorMessage.body.beginParse();
  1665. // Verify error response format
  1666. const op = cs.loadUint(32);
  1667. expect(op).toBe(0x10002); // OP_RESPONSE_ERROR
  1668. const errorCode = cs.loadUint(32);
  1669. expect(errorCode).toBe(2020); // ERROR_PRICE_FEED_NOT_FOUND_WITHIN_RANGE
  1670. const originalOp = cs.loadUint(32);
  1671. expect(originalOp).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1672. // Verify custom payload is preserved
  1673. const customPayloadCell = cs.loadRef();
  1674. const customPayloadSlice = customPayloadCell.beginParse();
  1675. expect(
  1676. Buffer.from(
  1677. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1678. ).toString("hex"),
  1679. ).toBe(CUSTOM_PAYLOAD.toString("hex"));
  1680. });
  1681. it("should successfully parse price feed updates in price ids order", async () => {
  1682. await deployContract();
  1683. await updateGuardianSets(pythTest, deployer);
  1684. const sentValue = toNano("1");
  1685. const result = await pythTest.sendParsePriceFeedUpdates(
  1686. deployer.getSender(),
  1687. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1688. sentValue,
  1689. [ETH_PRICE_FEED_ID, BTC_PRICE_FEED_ID],
  1690. HERMES_BTC_PUBLISH_TIME,
  1691. HERMES_BTC_PUBLISH_TIME,
  1692. deployer.address,
  1693. CUSTOM_PAYLOAD,
  1694. );
  1695. // Verify transaction success and message count
  1696. expect(result.transactions).toHaveTransaction({
  1697. from: deployer.address,
  1698. to: pythTest.address,
  1699. success: true,
  1700. outMessagesCount: 1,
  1701. });
  1702. // Get the output message
  1703. const outMessage = result.transactions[1].outMessages.values()[0];
  1704. // Verify excess value is returned
  1705. expect(
  1706. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1707. ).toBeGreaterThan(0);
  1708. expect((outMessage.info as CommonMessageInfoInternal).dest.toString()).toBe(
  1709. deployer.address.toString(),
  1710. );
  1711. const cs = outMessage.body.beginParse();
  1712. // Verify message header
  1713. const op = cs.loadUint(32);
  1714. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  1715. // Verify number of price feeds
  1716. const numPriceFeeds = cs.loadUint(8);
  1717. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1718. // Load and verify price feeds
  1719. const priceFeedsCell = cs.loadRef();
  1720. let currentCell = priceFeedsCell;
  1721. // First price feed (ETH)
  1722. const ethCs = currentCell.beginParse();
  1723. const ethPriceId =
  1724. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  1725. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  1726. const ethPriceFeedCell = ethCs.loadRef();
  1727. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  1728. // Verify ETH current price
  1729. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  1730. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  1731. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_PRICE);
  1732. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_CONF);
  1733. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_EXPO);
  1734. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  1735. // Verify ETH EMA price
  1736. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  1737. const ethEmaPrice = ethEmaPriceCell.beginParse();
  1738. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_EMA_PRICE);
  1739. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_EMA_CONF);
  1740. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_EMA_EXPO);
  1741. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  1742. // Move to ETH price feed
  1743. currentCell = ethCs.loadRef();
  1744. // Second price feed (BTC)
  1745. const btcCs = currentCell.beginParse();
  1746. const btcPriceId =
  1747. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  1748. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  1749. const btcPriceFeedCell = btcCs.loadRef();
  1750. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  1751. // Verify BTC current price
  1752. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  1753. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  1754. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_PRICE);
  1755. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_CONF);
  1756. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_EXPO);
  1757. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  1758. // Verify BTC EMA price
  1759. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  1760. const btcEmaPrice = btcEmaPriceCell.beginParse();
  1761. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_EMA_PRICE);
  1762. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_EMA_CONF);
  1763. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_EMA_EXPO);
  1764. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  1765. // Verify this is the end of the chain
  1766. expect(ethCs.remainingRefs).toBe(0);
  1767. });
  1768. it("should successfully parse unique price feed updates in price ids order", async () => {
  1769. await deployContract();
  1770. await updateGuardianSets(pythTest, deployer);
  1771. const sentValue = toNano("1");
  1772. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1773. deployer.getSender(),
  1774. Buffer.from(HERMES_BTC_ETH_UNIQUE_UPDATE, "hex"),
  1775. sentValue,
  1776. [ETH_PRICE_FEED_ID, BTC_PRICE_FEED_ID],
  1777. HERMES_BTC_PUBLISH_TIME,
  1778. 60,
  1779. deployer.address,
  1780. CUSTOM_PAYLOAD,
  1781. );
  1782. // Verify transaction success and message count
  1783. expect(result.transactions).toHaveTransaction({
  1784. from: deployer.address,
  1785. to: pythTest.address,
  1786. success: true,
  1787. outMessagesCount: 1,
  1788. });
  1789. // Get the output message
  1790. const outMessage = result.transactions[1].outMessages.values()[0];
  1791. // Verify excess value is returned
  1792. expect(
  1793. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1794. ).toBeGreaterThan(0);
  1795. const cs = outMessage.body.beginParse();
  1796. // Verify message header
  1797. const op = cs.loadUint(32);
  1798. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1799. // Verify number of price feeds
  1800. const numPriceFeeds = cs.loadUint(8);
  1801. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1802. // Load and verify price feeds
  1803. const priceFeedsCell = cs.loadRef();
  1804. let currentCell = priceFeedsCell;
  1805. // First price feed (ETH)
  1806. const ethCs = currentCell.beginParse();
  1807. const ethPriceId =
  1808. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  1809. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  1810. const ethPriceFeedCell = ethCs.loadRef();
  1811. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  1812. // Verify ETH current price
  1813. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  1814. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  1815. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_PRICE);
  1816. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_CONF);
  1817. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EXPO);
  1818. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_PUBLISH_TIME);
  1819. // Verify ETH EMA price
  1820. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  1821. const ethEmaPrice = ethEmaPriceCell.beginParse();
  1822. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_EMA_PRICE);
  1823. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_CONF);
  1824. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EMA_EXPO);
  1825. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_PUBLISH_TIME);
  1826. currentCell = ethCs.loadRef();
  1827. // Second price feed (BTC)
  1828. const btcCs = currentCell.beginParse();
  1829. const btcPriceId =
  1830. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  1831. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  1832. const btcPriceFeedCell = btcCs.loadRef();
  1833. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  1834. // Verify BTC current price
  1835. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  1836. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  1837. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_PRICE);
  1838. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_CONF);
  1839. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EXPO);
  1840. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_PUBLISH_TIME);
  1841. // Verify BTC EMA price
  1842. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  1843. const btcEmaPrice = btcEmaPriceCell.beginParse();
  1844. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_EMA_PRICE);
  1845. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_CONF);
  1846. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EMA_EXPO);
  1847. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_PUBLISH_TIME);
  1848. // Verify this is the end of the chain
  1849. expect(btcCs.remainingRefs).toBe(0);
  1850. });
  1851. it("should successfully parse price feed updates with 1 price feed", async () => {
  1852. await deployContract();
  1853. await updateGuardianSets(pythTest, deployer);
  1854. const sentValue = toNano("15");
  1855. const result = await pythTest.sendParsePriceFeedUpdates(
  1856. deployer.getSender(),
  1857. Buffer.from(HERMES_UPDATE_1_PRICE_FEED, "hex"),
  1858. sentValue,
  1859. [BTC_PRICE_FEED_ID],
  1860. HERMES_1_BTC_PUBLISH_TIME,
  1861. HERMES_1_BTC_PUBLISH_TIME,
  1862. deployer.address,
  1863. CUSTOM_PAYLOAD,
  1864. );
  1865. expect(result.transactions).toHaveTransaction({
  1866. from: deployer.address,
  1867. to: pythTest.address,
  1868. success: true,
  1869. outMessagesCount: 1,
  1870. });
  1871. expect(result.transactions).toHaveTransaction({
  1872. from: pythTest.address,
  1873. to: deployer.address,
  1874. success: true,
  1875. });
  1876. const outMessage = result.transactions[1].outMessages.values()[0];
  1877. expect(
  1878. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1879. ).toBeGreaterThan(0);
  1880. const cs = outMessage.body.beginParse();
  1881. console.log(outMessage.body);
  1882. const op = cs.loadUint(32);
  1883. expect(op).toBe(5);
  1884. const numPriceFeeds = cs.loadUint(8);
  1885. expect(numPriceFeeds).toBe(1);
  1886. const priceFeedsCell = cs.loadRef();
  1887. let currentCell = priceFeedsCell;
  1888. const expectedFeeds = [
  1889. {
  1890. id: BTC_PRICE_FEED_ID,
  1891. price: HERMES_1_BTC_PRICE,
  1892. conf: HERMES_1_BTC_CONF,
  1893. expo: HERMES_1_BTC_EXPO,
  1894. publishTime: HERMES_1_BTC_PUBLISH_TIME,
  1895. emaPrice: HERMES_1_BTC_EMA_PRICE,
  1896. emaConf: HERMES_1_BTC_EMA_CONF,
  1897. emaExpo: HERMES_1_BTC_EMA_EXPO,
  1898. emaPublishTime: HERMES_1_BTC_EMA_PUBLISH_TIME,
  1899. },
  1900. ];
  1901. for (let i = 0; i < expectedFeeds.length; i++) {
  1902. const feedCs = currentCell.beginParse();
  1903. const priceId =
  1904. "0x" + feedCs.loadUintBig(256).toString(16).padStart(64, "0");
  1905. expect(priceId).toBe(expectedFeeds[i].id);
  1906. const priceFeedCell = feedCs.loadRef();
  1907. const priceFeedSlice = priceFeedCell.beginParse();
  1908. const currentPriceCell = priceFeedSlice.loadRef();
  1909. const currentPrice = currentPriceCell.beginParse();
  1910. expect(currentPrice.loadInt(64)).toBe(expectedFeeds[i].price);
  1911. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].conf);
  1912. expect(currentPrice.loadInt(32)).toBe(expectedFeeds[i].expo);
  1913. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].publishTime);
  1914. const emaPriceCell = priceFeedSlice.loadRef();
  1915. const emaPrice = emaPriceCell.beginParse();
  1916. expect(emaPrice.loadInt(64)).toBe(expectedFeeds[i].emaPrice);
  1917. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaConf);
  1918. expect(emaPrice.loadInt(32)).toBe(expectedFeeds[i].emaExpo);
  1919. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaPublishTime);
  1920. if (i < expectedFeeds.length - 1) {
  1921. currentCell = feedCs.loadRef();
  1922. } else {
  1923. expect(feedCs.remainingRefs).toBe(0);
  1924. }
  1925. }
  1926. const senderAddress = cs.loadAddress();
  1927. expect(senderAddress?.toString()).toBe(
  1928. deployer.getSender().address.toString(),
  1929. );
  1930. const customPayloadCell = cs.loadRef();
  1931. const customPayloadSlice = customPayloadCell.beginParse();
  1932. const receivedPayload = Buffer.from(
  1933. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1934. );
  1935. expect(receivedPayload.toString("hex")).toBe(
  1936. CUSTOM_PAYLOAD.toString("hex"),
  1937. );
  1938. });
  1939. it("should successfully parse price feed updates with 7 price feeds", async () => {
  1940. await deployContract();
  1941. await updateGuardianSets(pythTest, deployer);
  1942. const sentValue = toNano("15");
  1943. const result = await pythTest.sendParsePriceFeedUpdates(
  1944. deployer.getSender(),
  1945. Buffer.from(HERMES_UPDATE_7_PRICE_FEEDS, "hex"),
  1946. sentValue,
  1947. [
  1948. BTC_PRICE_FEED_ID,
  1949. ETH_PRICE_FEED_ID,
  1950. DOGE_PRICE_FEED_ID,
  1951. SOL_USD_PRICE_FEED_ID,
  1952. PYTH_USD_PRICE_FEED_ID,
  1953. ARBITRUM_USD_PRICE_FEED_ID,
  1954. TON_USD_PRICE_FEED_ID,
  1955. ],
  1956. HERMES_7_ETH_PUBLISH_TIME,
  1957. HERMES_7_ETH_PUBLISH_TIME,
  1958. deployer.address,
  1959. CUSTOM_PAYLOAD,
  1960. );
  1961. expect(result.transactions).toHaveTransaction({
  1962. from: deployer.address,
  1963. to: pythTest.address,
  1964. success: true,
  1965. outMessagesCount: 1,
  1966. });
  1967. expect(result.transactions).toHaveTransaction({
  1968. from: pythTest.address,
  1969. to: deployer.address,
  1970. success: true,
  1971. });
  1972. const outMessage = result.transactions[1].outMessages.values()[0];
  1973. expect(
  1974. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1975. ).toBeGreaterThan(0);
  1976. const cs = outMessage.body.beginParse();
  1977. console.log(outMessage.body);
  1978. const op = cs.loadUint(32);
  1979. expect(op).toBe(5);
  1980. const numPriceFeeds = cs.loadUint(8);
  1981. expect(numPriceFeeds).toBe(7);
  1982. const priceFeedsCell = cs.loadRef();
  1983. let currentCell = priceFeedsCell;
  1984. const expectedFeeds = [
  1985. {
  1986. id: BTC_PRICE_FEED_ID,
  1987. price: HERMES_7_BTC_PRICE,
  1988. conf: HERMES_7_BTC_CONF,
  1989. expo: HERMES_7_BTC_EXPO,
  1990. publishTime: HERMES_7_BTC_PUBLISH_TIME,
  1991. emaPrice: HERMES_7_BTC_EMA_PRICE,
  1992. emaConf: HERMES_7_BTC_EMA_CONF,
  1993. emaExpo: HERMES_7_BTC_EMA_EXPO,
  1994. emaPublishTime: HERMES_7_BTC_EMA_PUBLISH_TIME,
  1995. },
  1996. {
  1997. id: ETH_PRICE_FEED_ID,
  1998. price: HERMES_7_ETH_PRICE,
  1999. conf: HERMES_7_ETH_CONF,
  2000. expo: HERMES_7_ETH_EXPO,
  2001. publishTime: HERMES_7_ETH_PUBLISH_TIME,
  2002. emaPrice: HERMES_7_ETH_EMA_PRICE,
  2003. emaConf: HERMES_7_ETH_EMA_CONF,
  2004. emaExpo: HERMES_7_ETH_EMA_EXPO,
  2005. emaPublishTime: HERMES_7_ETH_EMA_PUBLISH_TIME,
  2006. },
  2007. {
  2008. id: DOGE_PRICE_FEED_ID,
  2009. price: HERMES_7_DOGE_PRICE,
  2010. conf: HERMES_7_DOGE_CONF,
  2011. expo: HERMES_7_DOGE_EXPO,
  2012. publishTime: HERMES_7_DOGE_PUBLISH_TIME,
  2013. emaPrice: HERMES_7_DOGE_EMA_PRICE,
  2014. emaConf: HERMES_7_DOGE_EMA_CONF,
  2015. emaExpo: HERMES_7_DOGE_EMA_EXPO,
  2016. emaPublishTime: HERMES_7_DOGE_EMA_PUBLISH_TIME,
  2017. },
  2018. {
  2019. id: SOL_USD_PRICE_FEED_ID,
  2020. price: HERMES_7_SOL_PRICE,
  2021. conf: HERMES_7_SOL_CONF,
  2022. expo: HERMES_7_SOL_EXPO,
  2023. publishTime: HERMES_7_SOL_PUBLISH_TIME,
  2024. emaPrice: HERMES_7_SOL_EMA_PRICE,
  2025. emaConf: HERMES_7_SOL_EMA_CONF,
  2026. emaExpo: HERMES_7_SOL_EMA_EXPO,
  2027. emaPublishTime: HERMES_7_SOL_EMA_PUBLISH_TIME,
  2028. },
  2029. {
  2030. id: PYTH_USD_PRICE_FEED_ID,
  2031. price: HERMES_7_PYTH_PRICE,
  2032. conf: HERMES_7_PYTH_CONF,
  2033. expo: HERMES_7_PYTH_EXPO,
  2034. publishTime: HERMES_7_PYTH_PUBLISH_TIME,
  2035. emaPrice: HERMES_7_PYTH_EMA_PRICE,
  2036. emaConf: HERMES_7_PYTH_EMA_CONF,
  2037. emaExpo: HERMES_7_PYTH_EMA_EXPO,
  2038. emaPublishTime: HERMES_7_PYTH_EMA_PUBLISH_TIME,
  2039. },
  2040. {
  2041. id: ARBITRUM_USD_PRICE_FEED_ID,
  2042. price: HERMES_7_ARB_PRICE,
  2043. conf: HERMES_7_ARB_CONF,
  2044. expo: HERMES_7_ARB_EXPO,
  2045. publishTime: HERMES_7_ARB_PUBLISH_TIME,
  2046. emaPrice: HERMES_7_ARB_EMA_PRICE,
  2047. emaConf: HERMES_7_ARB_EMA_CONF,
  2048. emaExpo: HERMES_7_ARB_EMA_EXPO,
  2049. emaPublishTime: HERMES_7_ARB_EMA_PUBLISH_TIME,
  2050. },
  2051. {
  2052. id: TON_USD_PRICE_FEED_ID,
  2053. price: HERMES_7_TON_PRICE,
  2054. conf: HERMES_7_TON_CONF,
  2055. expo: HERMES_7_TON_EXPO,
  2056. publishTime: HERMES_7_TON_PUBLISH_TIME,
  2057. emaPrice: HERMES_7_TON_EMA_PRICE,
  2058. emaConf: HERMES_7_TON_EMA_CONF,
  2059. emaExpo: HERMES_7_TON_EMA_EXPO,
  2060. emaPublishTime: HERMES_7_TON_EMA_PUBLISH_TIME,
  2061. },
  2062. ];
  2063. for (let i = 0; i < expectedFeeds.length; i++) {
  2064. const feedCs = currentCell.beginParse();
  2065. const priceId =
  2066. "0x" + feedCs.loadUintBig(256).toString(16).padStart(64, "0");
  2067. expect(priceId).toBe(expectedFeeds[i].id);
  2068. const priceFeedCell = feedCs.loadRef();
  2069. const priceFeedSlice = priceFeedCell.beginParse();
  2070. const currentPriceCell = priceFeedSlice.loadRef();
  2071. const currentPrice = currentPriceCell.beginParse();
  2072. expect(currentPrice.loadInt(64)).toBe(expectedFeeds[i].price);
  2073. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].conf);
  2074. expect(currentPrice.loadInt(32)).toBe(expectedFeeds[i].expo);
  2075. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].publishTime);
  2076. const emaPriceCell = priceFeedSlice.loadRef();
  2077. const emaPrice = emaPriceCell.beginParse();
  2078. expect(emaPrice.loadInt(64)).toBe(expectedFeeds[i].emaPrice);
  2079. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaConf);
  2080. expect(emaPrice.loadInt(32)).toBe(expectedFeeds[i].emaExpo);
  2081. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaPublishTime);
  2082. if (i < expectedFeeds.length - 1) {
  2083. currentCell = feedCs.loadRef();
  2084. } else {
  2085. expect(feedCs.remainingRefs).toBe(0);
  2086. }
  2087. }
  2088. const senderAddress = cs.loadAddress();
  2089. expect(senderAddress?.toString()).toBe(
  2090. deployer.getSender().address.toString(),
  2091. );
  2092. const customPayloadCell = cs.loadRef();
  2093. const customPayloadSlice = customPayloadCell.beginParse();
  2094. const receivedPayload = Buffer.from(
  2095. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  2096. );
  2097. expect(receivedPayload.toString("hex")).toBe(
  2098. CUSTOM_PAYLOAD.toString("hex"),
  2099. );
  2100. });
  2101. it("should successfully parse price feed updates with 8 price feeds", async () => {
  2102. await deployContract();
  2103. await updateGuardianSets(pythTest, deployer);
  2104. const sentValue = toNano("15");
  2105. const result = await pythTest.sendParsePriceFeedUpdates(
  2106. deployer.getSender(),
  2107. Buffer.from(HERMES_UPDATE_8_PRICE_FEEDS, "hex"),
  2108. sentValue,
  2109. [
  2110. BTC_PRICE_FEED_ID,
  2111. ETH_PRICE_FEED_ID,
  2112. DOGE_PRICE_FEED_ID,
  2113. SOL_USD_PRICE_FEED_ID,
  2114. PYTH_USD_PRICE_FEED_ID,
  2115. ARBITRUM_USD_PRICE_FEED_ID,
  2116. TON_USD_PRICE_FEED_ID,
  2117. AAPL_PRICE_FEED_ID,
  2118. ],
  2119. HERMES_8_ETH_PUBLISH_TIME,
  2120. HERMES_8_ETH_PUBLISH_TIME,
  2121. deployer.address,
  2122. CUSTOM_PAYLOAD,
  2123. );
  2124. expect(result.transactions).toHaveTransaction({
  2125. from: deployer.address,
  2126. to: pythTest.address,
  2127. success: true,
  2128. outMessagesCount: 1,
  2129. });
  2130. expect(result.transactions).toHaveTransaction({
  2131. from: pythTest.address,
  2132. to: deployer.address,
  2133. success: true,
  2134. });
  2135. const outMessage = result.transactions[1].outMessages.values()[0];
  2136. expect(
  2137. (outMessage.info as CommonMessageInfoInternal).value.coins,
  2138. ).toBeGreaterThan(0);
  2139. const cs = outMessage.body.beginParse();
  2140. const op = cs.loadUint(32);
  2141. expect(op).toBe(5);
  2142. const numPriceFeeds = cs.loadUint(8);
  2143. expect(numPriceFeeds).toBe(8);
  2144. const priceFeedsCell = cs.loadRef();
  2145. let currentCell = priceFeedsCell;
  2146. const expectedFeeds = [
  2147. {
  2148. id: BTC_PRICE_FEED_ID,
  2149. price: HERMES_8_BTC_PRICE,
  2150. conf: HERMES_8_BTC_CONF,
  2151. expo: HERMES_8_BTC_EXPO,
  2152. publishTime: HERMES_8_BTC_PUBLISH_TIME,
  2153. emaPrice: HERMES_8_BTC_EMA_PRICE,
  2154. emaConf: HERMES_8_BTC_EMA_CONF,
  2155. emaExpo: HERMES_8_BTC_EMA_EXPO,
  2156. emaPublishTime: HERMES_8_BTC_EMA_PUBLISH_TIME,
  2157. },
  2158. {
  2159. id: ETH_PRICE_FEED_ID,
  2160. price: HERMES_8_ETH_PRICE,
  2161. conf: HERMES_8_ETH_CONF,
  2162. expo: HERMES_8_ETH_EXPO,
  2163. publishTime: HERMES_8_ETH_PUBLISH_TIME,
  2164. emaPrice: HERMES_8_ETH_EMA_PRICE,
  2165. emaConf: HERMES_8_ETH_EMA_CONF,
  2166. emaExpo: HERMES_8_ETH_EMA_EXPO,
  2167. emaPublishTime: HERMES_8_ETH_EMA_PUBLISH_TIME,
  2168. },
  2169. {
  2170. id: DOGE_PRICE_FEED_ID,
  2171. price: HERMES_8_DOGE_PRICE,
  2172. conf: HERMES_8_DOGE_CONF,
  2173. expo: HERMES_8_DOGE_EXPO,
  2174. publishTime: HERMES_8_DOGE_PUBLISH_TIME,
  2175. emaPrice: HERMES_8_DOGE_EMA_PRICE,
  2176. emaConf: HERMES_8_DOGE_EMA_CONF,
  2177. emaExpo: HERMES_8_DOGE_EMA_EXPO,
  2178. emaPublishTime: HERMES_8_DOGE_EMA_PUBLISH_TIME,
  2179. },
  2180. {
  2181. id: SOL_USD_PRICE_FEED_ID,
  2182. price: HERMES_8_SOL_PRICE,
  2183. conf: HERMES_8_SOL_CONF,
  2184. expo: HERMES_8_SOL_EXPO,
  2185. publishTime: HERMES_8_SOL_PUBLISH_TIME,
  2186. emaPrice: HERMES_8_SOL_EMA_PRICE,
  2187. emaConf: HERMES_8_SOL_EMA_CONF,
  2188. emaExpo: HERMES_8_SOL_EMA_EXPO,
  2189. emaPublishTime: HERMES_8_SOL_EMA_PUBLISH_TIME,
  2190. },
  2191. {
  2192. id: PYTH_USD_PRICE_FEED_ID,
  2193. price: HERMES_8_PYTH_PRICE,
  2194. conf: HERMES_8_PYTH_CONF,
  2195. expo: HERMES_8_PYTH_EXPO,
  2196. publishTime: HERMES_8_PYTH_PUBLISH_TIME,
  2197. emaPrice: HERMES_8_PYTH_EMA_PRICE,
  2198. emaConf: HERMES_8_PYTH_EMA_CONF,
  2199. emaExpo: HERMES_8_PYTH_EMA_EXPO,
  2200. emaPublishTime: HERMES_8_PYTH_EMA_PUBLISH_TIME,
  2201. },
  2202. {
  2203. id: ARBITRUM_USD_PRICE_FEED_ID,
  2204. price: HERMES_8_ARB_PRICE,
  2205. conf: HERMES_8_ARB_CONF,
  2206. expo: HERMES_8_ARB_EXPO,
  2207. publishTime: HERMES_8_ARB_PUBLISH_TIME,
  2208. emaPrice: HERMES_8_ARB_EMA_PRICE,
  2209. emaConf: HERMES_8_ARB_EMA_CONF,
  2210. emaExpo: HERMES_8_ARB_EMA_EXPO,
  2211. emaPublishTime: HERMES_8_ARB_EMA_PUBLISH_TIME,
  2212. },
  2213. {
  2214. id: TON_USD_PRICE_FEED_ID,
  2215. price: HERMES_8_TON_PRICE,
  2216. conf: HERMES_8_TON_CONF,
  2217. expo: HERMES_8_TON_EXPO,
  2218. publishTime: HERMES_8_TON_PUBLISH_TIME,
  2219. emaPrice: HERMES_8_TON_EMA_PRICE,
  2220. emaConf: HERMES_8_TON_EMA_CONF,
  2221. emaExpo: HERMES_8_TON_EMA_EXPO,
  2222. emaPublishTime: HERMES_8_TON_EMA_PUBLISH_TIME,
  2223. },
  2224. {
  2225. id: AAPL_PRICE_FEED_ID,
  2226. price: HERMES_8_AAPL_PRICE,
  2227. conf: HERMES_8_AAPL_CONF,
  2228. expo: HERMES_8_AAPL_EXPO,
  2229. publishTime: HERMES_8_AAPL_PUBLISH_TIME,
  2230. emaPrice: HERMES_8_AAPL_EMA_PRICE,
  2231. emaConf: HERMES_8_AAPL_EMA_CONF,
  2232. emaExpo: HERMES_8_AAPL_EMA_EXPO,
  2233. emaPublishTime: HERMES_8_AAPL_EMA_PUBLISH_TIME,
  2234. },
  2235. ];
  2236. for (let i = 0; i < expectedFeeds.length; i++) {
  2237. const feedCs = currentCell.beginParse();
  2238. const priceId =
  2239. "0x" + feedCs.loadUintBig(256).toString(16).padStart(64, "0");
  2240. expect(priceId).toBe(expectedFeeds[i].id);
  2241. const priceFeedCell = feedCs.loadRef();
  2242. const priceFeedSlice = priceFeedCell.beginParse();
  2243. const currentPriceCell = priceFeedSlice.loadRef();
  2244. const currentPrice = currentPriceCell.beginParse();
  2245. expect(currentPrice.loadInt(64)).toBe(expectedFeeds[i].price);
  2246. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].conf);
  2247. expect(currentPrice.loadInt(32)).toBe(expectedFeeds[i].expo);
  2248. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].publishTime);
  2249. const emaPriceCell = priceFeedSlice.loadRef();
  2250. const emaPrice = emaPriceCell.beginParse();
  2251. expect(emaPrice.loadInt(64)).toBe(expectedFeeds[i].emaPrice);
  2252. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaConf);
  2253. expect(emaPrice.loadInt(32)).toBe(expectedFeeds[i].emaExpo);
  2254. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaPublishTime);
  2255. if (i < expectedFeeds.length - 1) {
  2256. currentCell = feedCs.loadRef();
  2257. } else {
  2258. expect(feedCs.remainingRefs).toBe(0);
  2259. }
  2260. }
  2261. const senderAddress = cs.loadAddress();
  2262. expect(senderAddress?.toString()).toBe(
  2263. deployer.getSender().address.toString(),
  2264. );
  2265. const customPayloadCell = cs.loadRef();
  2266. const customPayloadSlice = customPayloadCell.beginParse();
  2267. const receivedPayload = Buffer.from(
  2268. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  2269. );
  2270. expect(receivedPayload.toString("hex")).toBe(
  2271. CUSTOM_PAYLOAD.toString("hex"),
  2272. );
  2273. });
  2274. // The following two test cases are commented out, since TON cannot currently handle more than 8 price feeds in a single update.
  2275. /*
  2276. it("should successfully parse price feed updates with 9 price feeds", async () => {
  2277. await deployContract();
  2278. await updateGuardianSets(pythTest, deployer);
  2279. const sentValue = toNano("15");
  2280. const result = await pythTest.sendParsePriceFeedUpdates(
  2281. deployer.getSender(),
  2282. Buffer.from(HERMES_UPDATE_10_PRICE_FEEDS, "hex"),
  2283. sentValue,
  2284. [
  2285. BTC_PRICE_FEED_ID,
  2286. ETH_PRICE_FEED_ID,
  2287. DOGE_PRICE_FEED_ID,
  2288. SOL_USD_PRICE_FEED_ID,
  2289. PYTH_USD_PRICE_FEED_ID,
  2290. ARBITRUM_USD_PRICE_FEED_ID,
  2291. TON_USD_PRICE_FEED_ID,
  2292. AAPL_PRICE_FEED_ID,
  2293. ABNB_PRICE_FEED_ID,
  2294. ],
  2295. HERMES_9_ETH_PUBLISH_TIME,
  2296. HERMES_9_ETH_PUBLISH_TIME,
  2297. deployer.address,
  2298. CUSTOM_PAYLOAD,
  2299. );
  2300. expect(result.transactions).toHaveTransaction({
  2301. from: deployer.address,
  2302. to: pythTest.address,
  2303. success: true,
  2304. outMessagesCount: 1,
  2305. });
  2306. expect(result.transactions).toHaveTransaction({
  2307. from: pythTest.address,
  2308. to: deployer.address,
  2309. success: true,
  2310. });
  2311. const outMessage = result.transactions[1].outMessages.values()[0];
  2312. expect(
  2313. (outMessage.info as CommonMessageInfoInternal).value.coins,
  2314. ).toBeGreaterThan(0);
  2315. const cs = outMessage.body.beginParse();
  2316. const op = cs.loadUint(32);
  2317. expect(op).toBe(5);
  2318. const numPriceFeeds = cs.loadUint(8);
  2319. expect(numPriceFeeds).toBe(9);
  2320. const priceFeedsCell = cs.loadRef();
  2321. let currentCell = priceFeedsCell;
  2322. const expectedFeeds = [
  2323. { id: BTC_PRICE_FEED_ID, price: HERMES_9_BTC_PRICE, conf: HERMES_9_BTC_CONF, expo: HERMES_9_BTC_EXPO, publishTime: HERMES_9_BTC_PUBLISH_TIME, emaPrice: HERMES_9_BTC_EMA_PRICE, emaConf: HERMES_9_BTC_EMA_CONF, emaExpo: HERMES_9_BTC_EMA_EXPO, emaPublishTime: HERMES_9_BTC_EMA_PUBLISH_TIME },
  2324. { id: ETH_PRICE_FEED_ID, price: HERMES_9_ETH_PRICE, conf: HERMES_9_ETH_CONF, expo: HERMES_9_ETH_EXPO, publishTime: HERMES_9_ETH_PUBLISH_TIME, emaPrice: HERMES_9_ETH_EMA_PRICE, emaConf: HERMES_9_ETH_EMA_CONF, emaExpo: HERMES_9_ETH_EMA_EXPO, emaPublishTime: HERMES_9_ETH_EMA_PUBLISH_TIME },
  2325. { id: DOGE_PRICE_FEED_ID, price: HERMES_9_DOGE_PRICE, conf: HERMES_9_DOGE_CONF, expo: HERMES_9_DOGE_EXPO, publishTime: HERMES_9_DOGE_PUBLISH_TIME, emaPrice: HERMES_9_DOGE_EMA_PRICE, emaConf: HERMES_9_DOGE_EMA_CONF, emaExpo: HERMES_9_DOGE_EMA_EXPO, emaPublishTime: HERMES_9_DOGE_EMA_PUBLISH_TIME },
  2326. { id: SOL_USD_PRICE_FEED_ID, price: HERMES_9_SOL_PRICE, conf: HERMES_9_SOL_CONF, expo: HERMES_9_SOL_EXPO, publishTime: HERMES_9_SOL_PUBLISH_TIME, emaPrice: HERMES_9_SOL_EMA_PRICE, emaConf: HERMES_9_SOL_EMA_CONF, emaExpo: HERMES_9_SOL_EMA_EXPO, emaPublishTime: HERMES_9_SOL_EMA_PUBLISH_TIME },
  2327. { id: PYTH_USD_PRICE_FEED_ID, price: HERMES_9_PYTH_PRICE, conf: HERMES_9_PYTH_CONF, expo: HERMES_9_PYTH_EXPO, publishTime: HERMES_9_PYTH_PUBLISH_TIME, emaPrice: HERMES_9_PYTH_EMA_PRICE, emaConf: HERMES_9_PYTH_EMA_CONF, emaExpo: HERMES_9_PYTH_EMA_EXPO, emaPublishTime: HERMES_9_PYTH_EMA_PUBLISH_TIME },
  2328. { id: ARBITRUM_USD_PRICE_FEED_ID, price: HERMES_9_ARB_PRICE, conf: HERMES_9_ARB_CONF, expo: HERMES_9_ARB_EXPO, publishTime: HERMES_9_ARB_PUBLISH_TIME, emaPrice: HERMES_9_ARB_EMA_PRICE, emaConf: HERMES_9_ARB_EMA_CONF, emaExpo: HERMES_9_ARB_EMA_EXPO, emaPublishTime: HERMES_9_ARB_EMA_PUBLISH_TIME },
  2329. { id: TON_USD_PRICE_FEED_ID, price: HERMES_9_TON_PRICE, conf: HERMES_9_TON_CONF, expo: HERMES_9_TON_EXPO, publishTime: HERMES_9_TON_PUBLISH_TIME, emaPrice: HERMES_9_TON_EMA_PRICE, emaConf: HERMES_9_TON_EMA_CONF, emaExpo: HERMES_9_TON_EMA_EXPO, emaPublishTime: HERMES_9_TON_EMA_PUBLISH_TIME },
  2330. { id: AAPL_PRICE_FEED_ID, price: HERMES_9_AAPL_PRICE, conf: HERMES_9_AAPL_CONF, expo: HERMES_9_AAPL_EXPO, publishTime: HERMES_9_AAPL_PUBLISH_TIME, emaPrice: HERMES_9_AAPL_EMA_PRICE, emaConf: HERMES_9_AAPL_EMA_CONF, emaExpo: HERMES_9_AAPL_EMA_EXPO, emaPublishTime: HERMES_9_AAPL_EMA_PUBLISH_TIME },
  2331. { id: ABNB_PRICE_FEED_ID, price: HERMES_9_ABNB_PRICE, conf: HERMES_9_ABNB_CONF, expo: HERMES_9_ABNB_EXPO, publishTime: HERMES_9_ABNB_PUBLISH_TIME, emaPrice: HERMES_9_ABNB_EMA_PRICE, emaConf: HERMES_9_ABNB_EMA_CONF, emaExpo: HERMES_9_ABNB_EMA_EXPO, emaPublishTime: HERMES_9_ABNB_EMA_PUBLISH_TIME },
  2332. ];
  2333. for (let i = 0; i < expectedFeeds.length; i++) {
  2334. const feedCs = currentCell.beginParse();
  2335. const priceId = "0x" + feedCs.loadUintBig(256).toString(16).padStart(64, "0");
  2336. expect(priceId).toBe(expectedFeeds[i].id);
  2337. const priceFeedCell = feedCs.loadRef();
  2338. const priceFeedSlice = priceFeedCell.beginParse();
  2339. const currentPriceCell = priceFeedSlice.loadRef();
  2340. const currentPrice = currentPriceCell.beginParse();
  2341. expect(currentPrice.loadInt(64)).toBe(expectedFeeds[i].price);
  2342. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].conf);
  2343. expect(currentPrice.loadInt(32)).toBe(expectedFeeds[i].expo);
  2344. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].publishTime);
  2345. const emaPriceCell = priceFeedSlice.loadRef();
  2346. const emaPrice = emaPriceCell.beginParse();
  2347. expect(emaPrice.loadInt(64)).toBe(expectedFeeds[i].emaPrice);
  2348. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaConf);
  2349. expect(emaPrice.loadInt(32)).toBe(expectedFeeds[i].emaExpo);
  2350. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaPublishTime);
  2351. if (i < expectedFeeds.length - 1) {
  2352. currentCell = feedCs.loadRef();
  2353. } else {
  2354. expect(feedCs.remainingRefs).toBe(0);
  2355. }
  2356. }
  2357. const senderAddress = cs.loadAddress();
  2358. expect(senderAddress?.toString()).toBe(
  2359. deployer.getSender().address.toString(),
  2360. );
  2361. const customPayloadCell = cs.loadRef();
  2362. const customPayloadSlice = customPayloadCell.beginParse();
  2363. const receivedPayload = Buffer.from(
  2364. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  2365. );
  2366. expect(receivedPayload.toString("hex")).toBe(
  2367. CUSTOM_PAYLOAD.toString("hex"),
  2368. );
  2369. });
  2370. it("should successfully parse price feed updates with 11 price feeds", async () => {
  2371. await deployContract();
  2372. await updateGuardianSets(pythTest, deployer);
  2373. const sentValue = toNano("15");
  2374. const result = await pythTest.sendParsePriceFeedUpdates(
  2375. deployer.getSender(),
  2376. Buffer.from(HERMES_UPDATE_10_PRICE_FEEDS, "hex"),
  2377. sentValue,
  2378. [
  2379. BTC_PRICE_FEED_ID,
  2380. ETH_PRICE_FEED_ID,
  2381. DOGE_PRICE_FEED_ID,
  2382. SOL_USD_PRICE_FEED_ID,
  2383. PYTH_USD_PRICE_FEED_ID,
  2384. ARBITRUM_USD_PRICE_FEED_ID,
  2385. TON_USD_PRICE_FEED_ID,
  2386. AAPL_PRICE_FEED_ID,
  2387. ABNB_PRICE_FEED_ID,
  2388. ADBE_PRICE_FEED_ID,
  2389. AMZN_PRICE_FEED_ID,
  2390. ],
  2391. HERMES_10_ETH_PUBLISH_TIME,
  2392. HERMES_10_ETH_PUBLISH_TIME,
  2393. deployer.address,
  2394. CUSTOM_PAYLOAD,
  2395. );
  2396. console.log(result.transactions.length);
  2397. console.log("deployer address: " + deployer.address.toString());
  2398. console.log("pythTest address: " + pythTest.address.toString());
  2399. let i = 0;
  2400. result.transactions.forEach((tx) => {
  2401. console.log("Transaction " + i.toString() + ":");
  2402. console.log(tx.inMessage?.info.src?.toString() + " -> " + tx.inMessage?.info.dest?.toString());
  2403. console.log("out message count: " + tx.outMessages.values().length);
  2404. if (tx.outMessages.values().length > 0) {
  2405. console.log("Out Messages: ");
  2406. console.log(tx.outMessages.values()[0]);
  2407. }
  2408. i++;
  2409. });
  2410. expect(result.transactions).toHaveTransaction({
  2411. from: deployer.address,
  2412. to: pythTest.address,
  2413. success: true,
  2414. outMessagesCount: 1,
  2415. });
  2416. expect(result.transactions).toHaveTransaction({
  2417. from: pythTest.address,
  2418. to: deployer.address,
  2419. success: true,
  2420. });
  2421. const outMessage = result.transactions[1].outMessages.values()[0];
  2422. expect(
  2423. (outMessage.info as CommonMessageInfoInternal).value.coins,
  2424. ).toBeGreaterThan(0);
  2425. const cs = outMessage.body.beginParse();
  2426. const op = cs.loadUint(32);
  2427. expect(op).toBe(5);
  2428. const numPriceFeeds = cs.loadUint(8);
  2429. expect(numPriceFeeds).toBe(10);
  2430. const priceFeedsCell = cs.loadRef();
  2431. let currentCell = priceFeedsCell;
  2432. const expectedFeeds = [
  2433. { id: BTC_PRICE_FEED_ID, price: HERMES_10_BTC_PRICE, conf: HERMES_10_BTC_CONF, expo: HERMES_10_BTC_EXPO, publishTime: HERMES_10_BTC_PUBLISH_TIME, emaPrice: HERMES_10_BTC_EMA_PRICE, emaConf: HERMES_10_BTC_EMA_CONF, emaExpo: HERMES_10_BTC_EMA_EXPO, emaPublishTime: HERMES_10_BTC_EMA_PUBLISH_TIME },
  2434. { id: ETH_PRICE_FEED_ID, price: HERMES_10_ETH_PRICE, conf: HERMES_10_ETH_CONF, expo: HERMES_10_ETH_EXPO, publishTime: HERMES_10_ETH_PUBLISH_TIME, emaPrice: HERMES_10_ETH_EMA_PRICE, emaConf: HERMES_10_ETH_EMA_CONF, emaExpo: HERMES_10_ETH_EMA_EXPO, emaPublishTime: HERMES_10_ETH_EMA_PUBLISH_TIME },
  2435. { id: DOGE_PRICE_FEED_ID, price: HERMES_10_DOGE_PRICE, conf: HERMES_10_DOGE_CONF, expo: HERMES_10_DOGE_EXPO, publishTime: HERMES_10_DOGE_PUBLISH_TIME, emaPrice: HERMES_10_DOGE_EMA_PRICE, emaConf: HERMES_10_DOGE_EMA_CONF, emaExpo: HERMES_10_DOGE_EMA_EXPO, emaPublishTime: HERMES_10_DOGE_EMA_PUBLISH_TIME },
  2436. { id: SOL_USD_PRICE_FEED_ID, price: HERMES_10_SOL_PRICE, conf: HERMES_10_SOL_CONF, expo: HERMES_10_SOL_EXPO, publishTime: HERMES_10_SOL_PUBLISH_TIME, emaPrice: HERMES_10_SOL_EMA_PRICE, emaConf: HERMES_10_SOL_EMA_CONF, emaExpo: HERMES_10_SOL_EMA_EXPO, emaPublishTime: HERMES_10_SOL_EMA_PUBLISH_TIME },
  2437. { id: PYTH_USD_PRICE_FEED_ID, price: HERMES_10_PYTH_PRICE, conf: HERMES_10_PYTH_CONF, expo: HERMES_10_PYTH_EXPO, publishTime: HERMES_10_PYTH_PUBLISH_TIME, emaPrice: HERMES_10_PYTH_EMA_PRICE, emaConf: HERMES_10_PYTH_EMA_CONF, emaExpo: HERMES_10_PYTH_EMA_EXPO, emaPublishTime: HERMES_10_PYTH_EMA_PUBLISH_TIME },
  2438. { id: ARBITRUM_USD_PRICE_FEED_ID, price: HERMES_10_ARB_PRICE, conf: HERMES_10_ARB_CONF, expo: HERMES_10_ARB_EXPO, publishTime: HERMES_10_ARB_PUBLISH_TIME, emaPrice: HERMES_10_ARB_EMA_PRICE, emaConf: HERMES_10_ARB_EMA_CONF, emaExpo: HERMES_10_ARB_EMA_EXPO, emaPublishTime: HERMES_10_ARB_EMA_PUBLISH_TIME },
  2439. { id: TON_USD_PRICE_FEED_ID, price: HERMES_10_TON_PRICE, conf: HERMES_10_TON_CONF, expo: HERMES_10_TON_EXPO, publishTime: HERMES_10_TON_PUBLISH_TIME, emaPrice: HERMES_10_TON_EMA_PRICE, emaConf: HERMES_10_TON_EMA_CONF, emaExpo: HERMES_10_TON_EMA_EXPO, emaPublishTime: HERMES_10_TON_EMA_PUBLISH_TIME },
  2440. { id: AAPL_PRICE_FEED_ID, price: HERMES_10_AAPL_PRICE, conf: HERMES_10_AAPL_CONF, expo: HERMES_10_AAPL_EXPO, publishTime: HERMES_10_AAPL_PUBLISH_TIME, emaPrice: HERMES_10_AAPL_EMA_PRICE, emaConf: HERMES_10_AAPL_EMA_CONF, emaExpo: HERMES_10_AAPL_EMA_EXPO, emaPublishTime: HERMES_10_AAPL_EMA_PUBLISH_TIME },
  2441. { id: ABNB_PRICE_FEED_ID, price: HERMES_10_ABNB_PRICE, conf: HERMES_10_ABNB_CONF, expo: HERMES_10_ABNB_EXPO, publishTime: HERMES_10_ABNB_PUBLISH_TIME, emaPrice: HERMES_10_ABNB_EMA_PRICE, emaConf: HERMES_10_ABNB_EMA_CONF, emaExpo: HERMES_10_ABNB_EMA_EXPO, emaPublishTime: HERMES_10_ABNB_EMA_PUBLISH_TIME },
  2442. { id: ADBE_PRICE_FEED_ID, price: HERMES_10_ADBE_PRICE, conf: HERMES_10_ADBE_CONF, expo: HERMES_10_ADBE_EXPO, publishTime: HERMES_10_ADBE_PUBLISH_TIME, emaPrice: HERMES_10_ADBE_EMA_PRICE, emaConf: HERMES_10_ADBE_EMA_CONF, emaExpo: HERMES_10_ADBE_EMA_EXPO, emaPublishTime: HERMES_10_ADBE_EMA_PUBLISH_TIME },
  2443. { id: AMZN_PRICE_FEED_ID, price: HERMES_10_AMZN_PRICE, conf: HERMES_10_AMZN_CONF, expo: HERMES_10_AMZN_EXPO, publishTime: HERMES_10_AMZN_PUBLISH_TIME, emaPrice: HERMES_10_AMZN_EMA_PRICE, emaConf: HERMES_10_AMZN_EMA_CONF, emaExpo: HERMES_10_AMZN_EMA_EXPO, emaPublishTime: HERMES_10_AMZN_EMA_PUBLISH_TIME },
  2444. ];
  2445. for (let i = 0; i < expectedFeeds.length; i++) {
  2446. const feedCs = currentCell.beginParse();
  2447. const priceId = "0x" + feedCs.loadUintBig(256).toString(16).padStart(64, "0");
  2448. expect(priceId).toBe(expectedFeeds[i].id);
  2449. const priceFeedCell = feedCs.loadRef();
  2450. const priceFeedSlice = priceFeedCell.beginParse();
  2451. const currentPriceCell = priceFeedSlice.loadRef();
  2452. const currentPrice = currentPriceCell.beginParse();
  2453. expect(currentPrice.loadInt(64)).toBe(expectedFeeds[i].price);
  2454. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].conf);
  2455. expect(currentPrice.loadInt(32)).toBe(expectedFeeds[i].expo);
  2456. expect(currentPrice.loadUint(64)).toBe(expectedFeeds[i].publishTime);
  2457. const emaPriceCell = priceFeedSlice.loadRef();
  2458. const emaPrice = emaPriceCell.beginParse();
  2459. expect(emaPrice.loadInt(64)).toBe(expectedFeeds[i].emaPrice);
  2460. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaConf);
  2461. expect(emaPrice.loadInt(32)).toBe(expectedFeeds[i].emaExpo);
  2462. expect(emaPrice.loadUint(64)).toBe(expectedFeeds[i].emaPublishTime);
  2463. if (i < expectedFeeds.length - 1) {
  2464. currentCell = feedCs.loadRef();
  2465. } else {
  2466. expect(feedCs.remainingRefs).toBe(0);
  2467. }
  2468. }
  2469. const senderAddress = cs.loadAddress();
  2470. expect(senderAddress?.toString()).toBe(
  2471. deployer.getSender().address.toString(),
  2472. );
  2473. const customPayloadCell = cs.loadRef();
  2474. const customPayloadSlice = customPayloadCell.beginParse();
  2475. const receivedPayload = Buffer.from(
  2476. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  2477. );
  2478. expect(receivedPayload.toString("hex")).toBe(
  2479. CUSTOM_PAYLOAD.toString("hex"),
  2480. );
  2481. });
  2482. */
  2483. it("should successfully parse price feed updates with a different target address", async () => {
  2484. await deployContract();
  2485. await updateGuardianSets(pythTest, deployer);
  2486. const sentValue = toNano("1");
  2487. const result = await pythTest.sendParsePriceFeedUpdates(
  2488. deployer.getSender(),
  2489. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  2490. sentValue,
  2491. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  2492. HERMES_BTC_PUBLISH_TIME,
  2493. HERMES_BTC_PUBLISH_TIME,
  2494. mockDeployer.address,
  2495. CUSTOM_PAYLOAD,
  2496. );
  2497. // Verify transaction success and message count
  2498. expect(result.transactions).toHaveTransaction({
  2499. from: deployer.address,
  2500. to: pythTest.address,
  2501. success: true,
  2502. outMessagesCount: 1,
  2503. });
  2504. // Verify message success to target address
  2505. expect(result.transactions).toHaveTransaction({
  2506. from: pythTest.address,
  2507. to: mockDeployer.address,
  2508. success: true,
  2509. });
  2510. // Get the output message
  2511. const outMessage = result.transactions[1].outMessages.values()[0];
  2512. // Verify excess value is returned
  2513. expect(
  2514. (outMessage.info as CommonMessageInfoInternal).value.coins,
  2515. ).toBeGreaterThan(0);
  2516. const cs = outMessage.body.beginParse();
  2517. // Verify message header
  2518. const op = cs.loadUint(32);
  2519. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  2520. // Verify number of price feeds
  2521. const numPriceFeeds = cs.loadUint(8);
  2522. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  2523. cs.loadRef(); // Skip price feeds
  2524. // Verify sender address
  2525. const senderAddress = cs.loadAddress();
  2526. expect(senderAddress?.toString()).toBe(
  2527. deployer.getSender().address.toString(),
  2528. );
  2529. // Verify custom payload
  2530. const customPayloadCell = cs.loadRef();
  2531. const customPayloadSlice = customPayloadCell.beginParse();
  2532. const receivedPayload = Buffer.from(
  2533. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  2534. );
  2535. expect(receivedPayload.toString("hex")).toBe(
  2536. CUSTOM_PAYLOAD.toString("hex"),
  2537. );
  2538. });
  2539. it("should successfully parse unique price feed updates with a different target address", async () => {
  2540. await deployContract();
  2541. await updateGuardianSets(pythTest, deployer);
  2542. const sentValue = toNano("1");
  2543. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  2544. deployer.getSender(),
  2545. Buffer.from(HERMES_BTC_ETH_UNIQUE_UPDATE, "hex"),
  2546. sentValue,
  2547. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  2548. HERMES_BTC_PUBLISH_TIME,
  2549. 60,
  2550. mockDeployer.address,
  2551. CUSTOM_PAYLOAD,
  2552. );
  2553. // Verify transaction success and message count
  2554. expect(result.transactions).toHaveTransaction({
  2555. from: deployer.address,
  2556. to: pythTest.address,
  2557. success: true,
  2558. outMessagesCount: 1,
  2559. });
  2560. // Verify message success to target address
  2561. expect(result.transactions).toHaveTransaction({
  2562. from: pythTest.address,
  2563. to: mockDeployer.address,
  2564. success: true,
  2565. });
  2566. // Get the output message
  2567. const outMessage = result.transactions[1].outMessages.values()[0];
  2568. // Verify excess value is returned
  2569. expect(
  2570. (outMessage.info as CommonMessageInfoInternal).value.coins,
  2571. ).toBeGreaterThan(0);
  2572. const cs = outMessage.body.beginParse();
  2573. // Verify message header
  2574. const op = cs.loadUint(32);
  2575. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  2576. // Verify number of price feeds
  2577. const numPriceFeeds = cs.loadUint(8);
  2578. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  2579. cs.loadRef(); // Skip price feeds
  2580. // Verify sender address
  2581. const senderAddress = cs.loadAddress();
  2582. expect(senderAddress?.toString()).toBe(
  2583. deployer.getSender().address.toString(),
  2584. );
  2585. // Verify custom payload
  2586. const customPayloadCell = cs.loadRef();
  2587. const customPayloadSlice = customPayloadCell.beginParse();
  2588. const receivedPayload = Buffer.from(
  2589. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  2590. );
  2591. expect(receivedPayload.toString("hex")).toBe(
  2592. CUSTOM_PAYLOAD.toString("hex"),
  2593. );
  2594. });
  2595. });