PythTest.spec.ts 64 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978
  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. } from "./utils/pyth";
  62. import { GUARDIAN_SET_0, MAINNET_UPGRADE_VAAS } from "./utils/wormhole";
  63. import { DataSource } from "@pythnetwork/xc-admin-common";
  64. import { createAuthorizeUpgradePayload } from "./utils";
  65. import {
  66. UniversalAddress,
  67. createVAA,
  68. serialize,
  69. } from "@wormhole-foundation/sdk-definitions";
  70. import { mocks } from "@wormhole-foundation/sdk-definitions/testing";
  71. import { calculateUpdatePriceFeedsFee } from "@pythnetwork/pyth-ton-js";
  72. const TIME_PERIOD = 60;
  73. const PRICE = new Price({
  74. price: "1",
  75. conf: "2",
  76. expo: 3,
  77. publishTime: 4,
  78. });
  79. const EMA_PRICE = new Price({
  80. price: "5",
  81. conf: "6",
  82. expo: 7,
  83. publishTime: 8,
  84. });
  85. const SINGLE_UPDATE_FEE = 1;
  86. const DATA_SOURCES: DataSource[] = [
  87. {
  88. emitterChain: 26,
  89. emitterAddress:
  90. "e101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71",
  91. },
  92. ];
  93. const TEST_GOVERNANCE_DATA_SOURCES: DataSource[] = [
  94. {
  95. emitterChain: 1,
  96. emitterAddress:
  97. "0000000000000000000000000000000000000000000000000000000000000029",
  98. },
  99. {
  100. emitterChain: 2,
  101. emitterAddress:
  102. "000000000000000000000000000000000000000000000000000000000000002b",
  103. },
  104. {
  105. emitterChain: 1,
  106. emitterAddress:
  107. "0000000000000000000000000000000000000000000000000000000000000000",
  108. },
  109. ];
  110. const CUSTOM_PAYLOAD = Buffer.from("1234567890abcdef", "hex");
  111. describe("PythTest", () => {
  112. let code: Cell;
  113. beforeAll(async () => {
  114. code = await compile("PythTest");
  115. });
  116. let blockchain: Blockchain;
  117. let deployer: SandboxContract<TreasuryContract>;
  118. let mockDeployer: SandboxContract<TreasuryContract>;
  119. let pythTest: SandboxContract<PythTest>;
  120. beforeEach(async () => {
  121. blockchain = await Blockchain.create();
  122. deployer = await blockchain.treasury("deployer");
  123. mockDeployer = await blockchain.treasury("mockDeployer");
  124. });
  125. async function deployContract(
  126. priceFeedId: HexString = BTC_PRICE_FEED_ID,
  127. price: Price = PRICE,
  128. emaPrice: Price = EMA_PRICE,
  129. singleUpdateFee: number = SINGLE_UPDATE_FEE,
  130. dataSources: DataSource[] = DATA_SOURCES,
  131. guardianSetIndex: number = 0,
  132. guardianSet: string[] = GUARDIAN_SET_0,
  133. chainId: number = 1,
  134. governanceChainId: number = 1,
  135. governanceContract: string = "0000000000000000000000000000000000000000000000000000000000000004",
  136. governanceDataSource?: DataSource,
  137. ) {
  138. const config: PythTestConfig = {
  139. priceFeedId,
  140. price,
  141. emaPrice,
  142. singleUpdateFee,
  143. dataSources,
  144. guardianSetIndex,
  145. guardianSet,
  146. chainId,
  147. governanceChainId,
  148. governanceContract,
  149. governanceDataSource,
  150. };
  151. pythTest = blockchain.openContract(PythTest.createFromConfig(config, code));
  152. const deployResult = await pythTest.sendDeploy(
  153. deployer.getSender(),
  154. toNano("0.05"),
  155. );
  156. expect(deployResult.transactions).toHaveTransaction({
  157. from: deployer.address,
  158. to: pythTest.address,
  159. deploy: true,
  160. success: true,
  161. });
  162. }
  163. async function updateGuardianSets(
  164. pythTest: SandboxContract<PythTest>,
  165. deployer: SandboxContract<TreasuryContract>,
  166. ) {
  167. for (const vaa of MAINNET_UPGRADE_VAAS) {
  168. const result = await pythTest.sendUpdateGuardianSet(
  169. deployer.getSender(),
  170. Buffer.from(vaa, "hex"),
  171. );
  172. expect(result.transactions).toHaveTransaction({
  173. from: deployer.address,
  174. to: pythTest.address,
  175. success: true,
  176. });
  177. }
  178. }
  179. it("should correctly get price unsafe", async () => {
  180. await deployContract();
  181. const result = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  182. expect(result.price).toBe(1);
  183. expect(result.conf).toBe(2);
  184. expect(result.expo).toBe(3);
  185. expect(result.publishTime).toBe(4);
  186. });
  187. it("should correctly get price no older than", async () => {
  188. const timeNow = Math.floor(Date.now() / 1000) - TIME_PERIOD + 5; // 5 seconds buffer
  189. const price = new Price({
  190. price: "1",
  191. conf: "2",
  192. expo: 3,
  193. publishTime: timeNow,
  194. });
  195. await deployContract(BTC_PRICE_FEED_ID, price, EMA_PRICE);
  196. const result = await pythTest.getPriceNoOlderThan(
  197. TIME_PERIOD,
  198. BTC_PRICE_FEED_ID,
  199. );
  200. expect(result.price).toBe(1);
  201. expect(result.conf).toBe(2);
  202. expect(result.expo).toBe(3);
  203. expect(result.publishTime).toBe(timeNow);
  204. });
  205. it("should fail to get price no older than", async () => {
  206. await deployContract();
  207. await expect(
  208. pythTest.getPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID),
  209. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  210. });
  211. it("should correctly get ema price no older than", async () => {
  212. const timeNow = Math.floor(Date.now() / 1000) - TIME_PERIOD + 5; // 5 seconds buffer
  213. const emaPrice = new Price({
  214. price: "5",
  215. conf: "6",
  216. expo: 7,
  217. publishTime: timeNow,
  218. });
  219. await deployContract(BTC_PRICE_FEED_ID, PRICE, emaPrice);
  220. const result = await pythTest.getEmaPriceNoOlderThan(
  221. TIME_PERIOD,
  222. BTC_PRICE_FEED_ID,
  223. );
  224. expect(result.price).toBe(5);
  225. expect(result.conf).toBe(6);
  226. expect(result.expo).toBe(7);
  227. expect(result.publishTime).toBe(timeNow);
  228. });
  229. it("should fail to get ema price no older than", async () => {
  230. await deployContract();
  231. await expect(
  232. pythTest.getEmaPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID),
  233. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  234. });
  235. it("should correctly get ema price unsafe", async () => {
  236. await deployContract();
  237. const result = await pythTest.getEmaPriceUnsafe(BTC_PRICE_FEED_ID);
  238. expect(result.price).toBe(5);
  239. expect(result.conf).toBe(6);
  240. expect(result.expo).toBe(7);
  241. expect(result.publishTime).toBe(8);
  242. });
  243. it("should correctly get update fee", async () => {
  244. await deployContract();
  245. const result = await pythTest.getUpdateFee(
  246. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  247. );
  248. expect(result).toBe(2);
  249. });
  250. it("should correctly update price feeds", async () => {
  251. await deployContract();
  252. let result;
  253. await updateGuardianSets(pythTest, deployer);
  254. // Check initial prices
  255. const initialBtcPrice = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  256. expect(initialBtcPrice.price).not.toBe(HERMES_BTC_PRICE);
  257. // Expect an error for ETH price feed as it doesn't exist initially
  258. await expect(pythTest.getPriceUnsafe(ETH_PRICE_FEED_ID)).rejects.toThrow(
  259. "Unable to execute get method. Got exit_code: 2000",
  260. ); // ERROR_PRICE_FEED_NOT_FOUND = 2000
  261. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  262. const updateFee = await pythTest.getUpdateFee(updateData);
  263. result = await pythTest.sendUpdatePriceFeeds(
  264. deployer.getSender(),
  265. updateData,
  266. toNano(updateFee),
  267. );
  268. expect(result.transactions).toHaveTransaction({
  269. from: deployer.address,
  270. to: pythTest.address,
  271. success: true,
  272. });
  273. // Check if both BTC and ETH prices have been updated
  274. const updatedBtcPrice = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  275. expect(updatedBtcPrice.price).toBe(HERMES_BTC_PRICE);
  276. expect(updatedBtcPrice.publishTime).toBe(HERMES_BTC_PUBLISH_TIME);
  277. const updatedEthPrice = await pythTest.getPriceUnsafe(ETH_PRICE_FEED_ID);
  278. expect(updatedEthPrice.price).toBe(HERMES_ETH_PRICE);
  279. expect(updatedEthPrice.publishTime).toBe(HERMES_ETH_PUBLISH_TIME);
  280. });
  281. it("should fail to get update fee with invalid data", async () => {
  282. await deployContract();
  283. await updateGuardianSets(pythTest, deployer);
  284. const invalidUpdateData = Buffer.from("invalid data");
  285. await expect(pythTest.getUpdateFee(invalidUpdateData)).rejects.toThrow(
  286. "Unable to execute get method. Got exit_code: 2002",
  287. ); // ERROR_INVALID_MAGIC = 2002
  288. });
  289. it("should fail to update price feeds with invalid data", async () => {
  290. await deployContract();
  291. await updateGuardianSets(pythTest, deployer);
  292. const invalidUpdateData = Buffer.from("invalid data");
  293. // Use a fixed value for updateFee since we can't get it from getUpdateFee
  294. const updateFee = toNano("0.1"); // Use a reasonable amount
  295. const result = await pythTest.sendUpdatePriceFeeds(
  296. deployer.getSender(),
  297. invalidUpdateData,
  298. updateFee,
  299. );
  300. expect(result.transactions).toHaveTransaction({
  301. from: deployer.address,
  302. to: pythTest.address,
  303. success: false,
  304. exitCode: 2002, // ERROR_INVALID_MAGIC
  305. });
  306. });
  307. it("should fail to update price feeds with outdated guardian set", async () => {
  308. await deployContract();
  309. // Don't update guardian sets
  310. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  311. const updateFee = await pythTest.getUpdateFee(updateData);
  312. const result = await pythTest.sendUpdatePriceFeeds(
  313. deployer.getSender(),
  314. updateData,
  315. toNano(updateFee),
  316. );
  317. expect(result.transactions).toHaveTransaction({
  318. from: deployer.address,
  319. to: pythTest.address,
  320. success: false,
  321. exitCode: 1002, // ERROR_GUARDIAN_SET_NOT_FOUND
  322. });
  323. });
  324. it("should fail to update price feeds with invalid data source", async () => {
  325. await deployContract(
  326. BTC_PRICE_FEED_ID,
  327. PRICE,
  328. EMA_PRICE,
  329. SINGLE_UPDATE_FEE,
  330. [], // Empty data sources
  331. );
  332. await updateGuardianSets(pythTest, deployer);
  333. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  334. const updateFee = await pythTest.getUpdateFee(updateData);
  335. const result = await pythTest.sendUpdatePriceFeeds(
  336. deployer.getSender(),
  337. updateData,
  338. toNano(updateFee),
  339. );
  340. expect(result.transactions).toHaveTransaction({
  341. from: deployer.address,
  342. to: pythTest.address,
  343. success: false,
  344. exitCode: 2005, // ERROR_UPDATE_DATA_SOURCE_NOT_FOUND
  345. });
  346. });
  347. it("should correctly handle stale prices", async () => {
  348. const staleTime = Math.floor(Date.now() / 1000) - TIME_PERIOD - 10; // 10 seconds past the allowed period
  349. const stalePrice = new Price({
  350. price: "1",
  351. conf: "2",
  352. expo: 3,
  353. publishTime: staleTime,
  354. });
  355. await deployContract(BTC_PRICE_FEED_ID, stalePrice, EMA_PRICE);
  356. await expect(
  357. pythTest.getPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID),
  358. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  359. });
  360. it("should fail to update price feeds with insufficient gas", async () => {
  361. await deployContract();
  362. await updateGuardianSets(pythTest, deployer);
  363. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  364. let result = await pythTest.sendUpdatePriceFeeds(
  365. deployer.getSender(),
  366. updateData,
  367. calculateUpdatePriceFeedsFee(1n), // Send enough gas for 1 update instead of 2
  368. );
  369. expect(result.transactions).toHaveTransaction({
  370. from: deployer.address,
  371. to: pythTest.address,
  372. success: false,
  373. exitCode: 3000, // ERROR_INSUFFICIENT_GAS
  374. });
  375. });
  376. it("should fail to update price feeds with insufficient fee", async () => {
  377. await deployContract();
  378. await updateGuardianSets(pythTest, deployer);
  379. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  380. const updateFee = await pythTest.getUpdateFee(updateData);
  381. // Send less than the required fee
  382. const insufficientFee = updateFee - 1;
  383. const result = await pythTest.sendUpdatePriceFeeds(
  384. deployer.getSender(),
  385. updateData,
  386. calculateUpdatePriceFeedsFee(2n) + BigInt(insufficientFee),
  387. );
  388. // Check that the transaction did not succeed
  389. expect(result.transactions).toHaveTransaction({
  390. from: deployer.address,
  391. to: pythTest.address,
  392. success: false,
  393. exitCode: 2011, // ERROR_INSUFFICIENT_FEE = 2011
  394. });
  395. });
  396. it("should fail to get prices for non-existent price feed", async () => {
  397. await deployContract();
  398. const nonExistentPriceFeedId =
  399. "0000000000000000000000000000000000000000000000000000000000000000";
  400. await expect(
  401. pythTest.getPriceUnsafe(nonExistentPriceFeedId),
  402. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND = 2000
  403. await expect(
  404. pythTest.getPriceNoOlderThan(TIME_PERIOD, nonExistentPriceFeedId),
  405. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND
  406. await expect(
  407. pythTest.getEmaPriceUnsafe(nonExistentPriceFeedId),
  408. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND
  409. });
  410. it("should correctly get chain ID", async () => {
  411. await deployContract();
  412. const result = await pythTest.getChainId();
  413. expect(result).toEqual(1);
  414. });
  415. it("should correctly get last executed governance sequence", async () => {
  416. await deployContract(
  417. BTC_PRICE_FEED_ID,
  418. PRICE,
  419. EMA_PRICE,
  420. SINGLE_UPDATE_FEE,
  421. DATA_SOURCES,
  422. 0,
  423. [TEST_GUARDIAN_ADDRESS1],
  424. 60051,
  425. 1,
  426. "0000000000000000000000000000000000000000000000000000000000000004",
  427. TEST_GOVERNANCE_DATA_SOURCES[0],
  428. );
  429. // Check initial value
  430. let result = await pythTest.getLastExecutedGovernanceSequence();
  431. expect(result).toEqual(0);
  432. // Execute a governance action (e.g., set fee)
  433. await pythTest.sendExecuteGovernanceAction(
  434. deployer.getSender(),
  435. Buffer.from(PYTH_SET_FEE, "hex"),
  436. );
  437. // Check that the sequence has increased
  438. result = await pythTest.getLastExecutedGovernanceSequence();
  439. expect(result).toEqual(1);
  440. });
  441. it("should correctly get governance data source index", async () => {
  442. // Deploy contract with initial governance data source
  443. await deployContract(
  444. BTC_PRICE_FEED_ID,
  445. PRICE,
  446. EMA_PRICE,
  447. SINGLE_UPDATE_FEE,
  448. DATA_SOURCES,
  449. 0,
  450. [TEST_GUARDIAN_ADDRESS1],
  451. 60051,
  452. 1,
  453. "0000000000000000000000000000000000000000000000000000000000000004",
  454. TEST_GOVERNANCE_DATA_SOURCES[0],
  455. );
  456. // Check initial value
  457. let result = await pythTest.getGovernanceDataSourceIndex();
  458. expect(result).toEqual(0);
  459. // Execute governance action to change data source
  460. await pythTest.sendExecuteGovernanceAction(
  461. deployer.getSender(),
  462. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  463. );
  464. // Check that the index has increased
  465. result = await pythTest.getGovernanceDataSourceIndex();
  466. expect(result).toEqual(1);
  467. });
  468. it("should correctly get governance data source", async () => {
  469. // Deploy contract without initial governance data source
  470. await deployContract();
  471. // Check initial value (should be empty)
  472. let result = await pythTest.getGovernanceDataSource();
  473. expect(result).toEqual(null);
  474. // Deploy contract with initial governance data source
  475. await deployContract(
  476. BTC_PRICE_FEED_ID,
  477. PRICE,
  478. EMA_PRICE,
  479. SINGLE_UPDATE_FEE,
  480. DATA_SOURCES,
  481. 0,
  482. [TEST_GUARDIAN_ADDRESS1],
  483. 60051,
  484. 1,
  485. "0000000000000000000000000000000000000000000000000000000000000004",
  486. TEST_GOVERNANCE_DATA_SOURCES[0],
  487. );
  488. // Check that the governance data source is set
  489. result = await pythTest.getGovernanceDataSource();
  490. expect(result).toEqual(TEST_GOVERNANCE_DATA_SOURCES[0]);
  491. // Execute governance action to change data source
  492. await pythTest.sendExecuteGovernanceAction(
  493. deployer.getSender(),
  494. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  495. );
  496. // Check that the data source has changed
  497. result = await pythTest.getGovernanceDataSource();
  498. expect(result).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]);
  499. });
  500. it("should correctly get single update fee", async () => {
  501. await deployContract();
  502. // Get the initial fee
  503. const result = await pythTest.getSingleUpdateFee();
  504. expect(result).toBe(SINGLE_UPDATE_FEE);
  505. });
  506. it("should execute set data sources governance instruction", async () => {
  507. await deployContract(
  508. BTC_PRICE_FEED_ID,
  509. PRICE,
  510. EMA_PRICE,
  511. SINGLE_UPDATE_FEE,
  512. DATA_SOURCES,
  513. 0,
  514. [TEST_GUARDIAN_ADDRESS1],
  515. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  516. 1,
  517. "0000000000000000000000000000000000000000000000000000000000000004",
  518. TEST_GOVERNANCE_DATA_SOURCES[0],
  519. );
  520. // Execute the governance action
  521. const result = await pythTest.sendExecuteGovernanceAction(
  522. deployer.getSender(),
  523. Buffer.from(PYTH_SET_DATA_SOURCES, "hex"),
  524. );
  525. expect(result.transactions).toHaveTransaction({
  526. from: deployer.address,
  527. to: pythTest.address,
  528. success: true,
  529. });
  530. // Verify that the new data sources are set correctly
  531. const newDataSources: DataSource[] = [
  532. {
  533. emitterChain: 1,
  534. emitterAddress:
  535. "6bb14509a612f01fbbc4cffeebd4bbfb492a86df717ebe92eb6df432a3f00a25",
  536. },
  537. {
  538. emitterChain: 3,
  539. emitterAddress:
  540. "000000000000000000000000000000000000000000000000000000000000012d",
  541. },
  542. ];
  543. for (const dataSource of newDataSources) {
  544. const isValid = await pythTest.getIsValidDataSource(dataSource);
  545. expect(isValid).toBe(true);
  546. }
  547. // Verify that the old data source is no longer valid
  548. const oldDataSource = DATA_SOURCES[0];
  549. const oldDataSourceIsValid =
  550. await pythTest.getIsValidDataSource(oldDataSource);
  551. expect(oldDataSourceIsValid).toBe(false);
  552. });
  553. it("should execute set fee governance instruction", async () => {
  554. await deployContract(
  555. BTC_PRICE_FEED_ID,
  556. PRICE,
  557. EMA_PRICE,
  558. SINGLE_UPDATE_FEE,
  559. DATA_SOURCES,
  560. 0,
  561. [TEST_GUARDIAN_ADDRESS1],
  562. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  563. 1,
  564. "0000000000000000000000000000000000000000000000000000000000000004",
  565. TEST_GOVERNANCE_DATA_SOURCES[0],
  566. );
  567. // Get the initial fee
  568. const initialFee = await pythTest.getSingleUpdateFee();
  569. expect(initialFee).toBe(SINGLE_UPDATE_FEE);
  570. // Execute the governance action
  571. const result = await pythTest.sendExecuteGovernanceAction(
  572. deployer.getSender(),
  573. Buffer.from(PYTH_SET_FEE, "hex"),
  574. );
  575. expect(result.transactions).toHaveTransaction({
  576. from: deployer.address,
  577. to: pythTest.address,
  578. success: true,
  579. });
  580. // Get the new fee
  581. const newFee = await pythTest.getSingleUpdateFee();
  582. expect(newFee).toBe(4200); // The new fee value is 4200 in the PYTH_SET_FEE payload
  583. // Verify that the new fee is used for updates
  584. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  585. const updateFee = await pythTest.getUpdateFee(updateData);
  586. expect(updateFee).toBe(8400); // There are two price updates in HERMES_BTC_ETH_UPDATE
  587. });
  588. it("should execute authorize governance data source transfer", async () => {
  589. await deployContract(
  590. BTC_PRICE_FEED_ID,
  591. PRICE,
  592. EMA_PRICE,
  593. SINGLE_UPDATE_FEE,
  594. DATA_SOURCES,
  595. 0,
  596. [TEST_GUARDIAN_ADDRESS1],
  597. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  598. 1,
  599. "0000000000000000000000000000000000000000000000000000000000000004",
  600. TEST_GOVERNANCE_DATA_SOURCES[0],
  601. );
  602. // Get the initial governance data source index
  603. const initialIndex = await pythTest.getGovernanceDataSourceIndex();
  604. expect(initialIndex).toEqual(0); // Initial value should be 0
  605. // Get the initial governance data source
  606. const initialDataSource = await pythTest.getGovernanceDataSource();
  607. expect(initialDataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[0]);
  608. // Get the initial last executed governance sequence
  609. const initialSequence = await pythTest.getLastExecutedGovernanceSequence();
  610. expect(initialSequence).toEqual(0); // Initial value should be 0
  611. // Execute the governance action
  612. const result = await pythTest.sendExecuteGovernanceAction(
  613. deployer.getSender(),
  614. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  615. );
  616. expect(result.transactions).toHaveTransaction({
  617. from: deployer.address,
  618. to: pythTest.address,
  619. success: true,
  620. });
  621. // Get the new governance data source index
  622. const newIndex = await pythTest.getGovernanceDataSourceIndex();
  623. expect(newIndex).toEqual(1); // The new index value should match the one in the test payload
  624. // Get the new governance data source
  625. const newDataSource = await pythTest.getGovernanceDataSource();
  626. expect(newDataSource).not.toEqual(initialDataSource); // The data source should have changed
  627. expect(newDataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]); // The data source should have changed
  628. // Get the new last executed governance sequence
  629. const newSequence = await pythTest.getLastExecutedGovernanceSequence();
  630. expect(newSequence).toBeGreaterThan(initialSequence); // The sequence should have increased
  631. expect(newSequence).toBe(1);
  632. });
  633. it("should fail when executing request governance data source transfer directly", async () => {
  634. await deployContract(
  635. BTC_PRICE_FEED_ID,
  636. PRICE,
  637. EMA_PRICE,
  638. SINGLE_UPDATE_FEE,
  639. DATA_SOURCES,
  640. 0,
  641. [TEST_GUARDIAN_ADDRESS1],
  642. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  643. 1,
  644. "0000000000000000000000000000000000000000000000000000000000000004",
  645. TEST_GOVERNANCE_DATA_SOURCES[1],
  646. );
  647. const result = await pythTest.sendExecuteGovernanceAction(
  648. deployer.getSender(),
  649. Buffer.from(PYTH_REQUEST_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex"),
  650. );
  651. // Check that the transaction did not succeed
  652. expect(result.transactions).toHaveTransaction({
  653. from: deployer.address,
  654. to: pythTest.address,
  655. success: false,
  656. exitCode: 1012, // ERROR_INVALID_GOVERNANCE_ACTION
  657. });
  658. // Verify that the governance data source index hasn't changed
  659. const index = await pythTest.getGovernanceDataSourceIndex();
  660. expect(index).toEqual(0); // Should still be the initial value
  661. // Verify that the governance data source hasn't changed
  662. const dataSource = await pythTest.getGovernanceDataSource();
  663. expect(dataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]); // Should still be the initial value
  664. });
  665. it("should fail to execute governance action with invalid governance data source", async () => {
  666. await deployContract(
  667. BTC_PRICE_FEED_ID,
  668. PRICE,
  669. EMA_PRICE,
  670. SINGLE_UPDATE_FEE,
  671. DATA_SOURCES,
  672. 0,
  673. [TEST_GUARDIAN_ADDRESS1],
  674. 60051,
  675. 1,
  676. "0000000000000000000000000000000000000000000000000000000000000004",
  677. TEST_GOVERNANCE_DATA_SOURCES[1],
  678. );
  679. const result = await pythTest.sendExecuteGovernanceAction(
  680. deployer.getSender(),
  681. Buffer.from(PYTH_SET_FEE, "hex"),
  682. );
  683. expect(result.transactions).toHaveTransaction({
  684. from: deployer.address,
  685. to: pythTest.address,
  686. success: false,
  687. exitCode: 2013, // ERROR_INVALID_GOVERNANCE_DATA_SOURCE
  688. });
  689. });
  690. it("should fail to execute governance action with old sequence number", async () => {
  691. await deployContract(
  692. BTC_PRICE_FEED_ID,
  693. PRICE,
  694. EMA_PRICE,
  695. SINGLE_UPDATE_FEE,
  696. DATA_SOURCES,
  697. 0,
  698. [TEST_GUARDIAN_ADDRESS1],
  699. 60051,
  700. 1,
  701. "0000000000000000000000000000000000000000000000000000000000000004",
  702. TEST_GOVERNANCE_DATA_SOURCES[0],
  703. );
  704. // Execute a governance action to increase the sequence number
  705. await pythTest.sendExecuteGovernanceAction(
  706. deployer.getSender(),
  707. Buffer.from(PYTH_SET_FEE, "hex"),
  708. );
  709. // Try to execute the same governance action again
  710. const result = await pythTest.sendExecuteGovernanceAction(
  711. deployer.getSender(),
  712. Buffer.from(PYTH_SET_FEE, "hex"),
  713. );
  714. expect(result.transactions).toHaveTransaction({
  715. from: deployer.address,
  716. to: pythTest.address,
  717. success: false,
  718. exitCode: 2014, // ERROR_OLD_GOVERNANCE_MESSAGE
  719. });
  720. });
  721. it("should fail to execute governance action with invalid chain ID", async () => {
  722. const invalidChainId = 999;
  723. await deployContract(
  724. BTC_PRICE_FEED_ID,
  725. PRICE,
  726. EMA_PRICE,
  727. SINGLE_UPDATE_FEE,
  728. DATA_SOURCES,
  729. 0,
  730. [TEST_GUARDIAN_ADDRESS1],
  731. invalidChainId,
  732. 1,
  733. "0000000000000000000000000000000000000000000000000000000000000004",
  734. TEST_GOVERNANCE_DATA_SOURCES[0],
  735. );
  736. const result = await pythTest.sendExecuteGovernanceAction(
  737. deployer.getSender(),
  738. Buffer.from(PYTH_SET_FEE, "hex"),
  739. );
  740. expect(result.transactions).toHaveTransaction({
  741. from: deployer.address,
  742. to: pythTest.address,
  743. success: false,
  744. exitCode: 2015, // ERROR_INVALID_GOVERNANCE_TARGET
  745. });
  746. });
  747. it("should successfully upgrade the contract", async () => {
  748. // Compile the upgraded contract
  749. const upgradedCode = await compile("PythTestUpgraded");
  750. const upgradedCodeHash = upgradedCode.hash();
  751. // Create the authorize upgrade payload
  752. const authorizeUpgradePayload =
  753. createAuthorizeUpgradePayload(upgradedCodeHash);
  754. const authorizeUpgradeVaa = createVAA("Uint8Array", {
  755. guardianSet: 0,
  756. timestamp: 0,
  757. nonce: 0,
  758. emitterChain: "Solana",
  759. emitterAddress: new UniversalAddress(new Uint8Array(32)),
  760. sequence: 1n,
  761. consistencyLevel: 0,
  762. signatures: [],
  763. payload: authorizeUpgradePayload,
  764. });
  765. const guardianSet = mocks.devnetGuardianSet();
  766. guardianSet.setSignatures(authorizeUpgradeVaa);
  767. await deployContract(
  768. BTC_PRICE_FEED_ID,
  769. PRICE,
  770. EMA_PRICE,
  771. SINGLE_UPDATE_FEE,
  772. DATA_SOURCES,
  773. 0,
  774. [TEST_GUARDIAN_ADDRESS2],
  775. 1,
  776. 1,
  777. "0000000000000000000000000000000000000000000000000000000000000000",
  778. TEST_GOVERNANCE_DATA_SOURCES[2],
  779. );
  780. // Execute the upgrade
  781. const sendExecuteGovernanceActionResult =
  782. await pythTest.sendExecuteGovernanceAction(
  783. deployer.getSender(),
  784. Buffer.from(serialize(authorizeUpgradeVaa)),
  785. );
  786. expect(sendExecuteGovernanceActionResult.transactions).toHaveTransaction({
  787. from: deployer.address,
  788. to: pythTest.address,
  789. success: true,
  790. });
  791. // Execute the upgrade
  792. const sendUpgradeContractResult = await pythTest.sendUpgradeContract(
  793. deployer.getSender(),
  794. upgradedCode,
  795. );
  796. expect(sendUpgradeContractResult.transactions).toHaveTransaction({
  797. from: deployer.address,
  798. to: pythTest.address,
  799. success: true,
  800. });
  801. // Verify that the contract has been upgraded by calling a new method
  802. const newMethodResult = await pythTest.getNewFunction();
  803. expect(newMethodResult).toBe(1);
  804. });
  805. it("should fail to upgrade the contract with modified code", async () => {
  806. // Compile the upgraded contract
  807. const upgradedCode = await compile("PythTestUpgraded");
  808. const upgradedCodeHash = upgradedCode.hash();
  809. // Create the authorize upgrade payload
  810. const authorizeUpgradePayload =
  811. createAuthorizeUpgradePayload(upgradedCodeHash);
  812. const authorizeUpgradeVaa = createVAA("Uint8Array", {
  813. guardianSet: 0,
  814. timestamp: 0,
  815. nonce: 0,
  816. emitterChain: "Solana",
  817. emitterAddress: new UniversalAddress(new Uint8Array(32)),
  818. sequence: 1n,
  819. consistencyLevel: 0,
  820. signatures: [],
  821. payload: authorizeUpgradePayload,
  822. });
  823. const guardianSet = mocks.devnetGuardianSet();
  824. guardianSet.setSignatures(authorizeUpgradeVaa);
  825. await deployContract(
  826. BTC_PRICE_FEED_ID,
  827. PRICE,
  828. EMA_PRICE,
  829. SINGLE_UPDATE_FEE,
  830. DATA_SOURCES,
  831. 0,
  832. [TEST_GUARDIAN_ADDRESS2],
  833. 1,
  834. 1,
  835. "0000000000000000000000000000000000000000000000000000000000000000",
  836. TEST_GOVERNANCE_DATA_SOURCES[2],
  837. );
  838. // Execute the upgrade authorization
  839. const sendExecuteGovernanceActionResult =
  840. await pythTest.sendExecuteGovernanceAction(
  841. deployer.getSender(),
  842. Buffer.from(serialize(authorizeUpgradeVaa)),
  843. );
  844. expect(sendExecuteGovernanceActionResult.transactions).toHaveTransaction({
  845. from: deployer.address,
  846. to: pythTest.address,
  847. success: true,
  848. });
  849. // Attempt to execute the upgrade with a different code
  850. const wormholeTestCode = await compile("WormholeTest");
  851. const sendUpgradeContractResult = await pythTest.sendUpgradeContract(
  852. deployer.getSender(),
  853. wormholeTestCode,
  854. );
  855. // Expect the transaction to fail
  856. expect(sendUpgradeContractResult.transactions).toHaveTransaction({
  857. from: deployer.address,
  858. to: pythTest.address,
  859. success: false,
  860. exitCode: 2018, // ERROR_INVALID_CODE_HASH
  861. });
  862. // Verify that the contract has not been upgraded by attempting to call the new method
  863. await expect(pythTest.getNewFunction()).rejects.toThrow();
  864. });
  865. it("should successfully parse price feed updates", async () => {
  866. await deployContract();
  867. await updateGuardianSets(pythTest, deployer);
  868. const sentValue = toNano("1");
  869. const result = await pythTest.sendParsePriceFeedUpdates(
  870. deployer.getSender(),
  871. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  872. sentValue,
  873. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  874. HERMES_BTC_PUBLISH_TIME,
  875. HERMES_BTC_PUBLISH_TIME,
  876. deployer.address,
  877. CUSTOM_PAYLOAD,
  878. );
  879. // Verify transaction success and message count
  880. expect(result.transactions).toHaveTransaction({
  881. from: deployer.address,
  882. to: pythTest.address,
  883. success: true,
  884. outMessagesCount: 1,
  885. });
  886. // Get the output message
  887. const outMessage = result.transactions[1].outMessages.values()[0];
  888. // Verify excess value is returned
  889. expect(
  890. (outMessage.info as CommonMessageInfoInternal).value.coins,
  891. ).toBeGreaterThan(0);
  892. const cs = outMessage.body.beginParse();
  893. // Verify message header
  894. const op = cs.loadUint(32);
  895. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  896. // Verify number of price feeds
  897. const numPriceFeeds = cs.loadUint(8);
  898. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  899. // Load and verify price feeds
  900. const priceFeedsCell = cs.loadRef();
  901. let currentCell = priceFeedsCell;
  902. // First price feed (BTC)
  903. const btcCs = currentCell.beginParse();
  904. const btcPriceId =
  905. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  906. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  907. const btcPriceFeedCell = btcCs.loadRef();
  908. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  909. // Verify BTC current price
  910. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  911. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  912. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_PRICE);
  913. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_CONF);
  914. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_EXPO);
  915. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  916. // Verify BTC EMA price
  917. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  918. const btcEmaPrice = btcEmaPriceCell.beginParse();
  919. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_EMA_PRICE);
  920. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_EMA_CONF);
  921. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_EMA_EXPO);
  922. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  923. // Move to ETH price feed
  924. currentCell = btcCs.loadRef();
  925. // Second price feed (ETH)
  926. const ethCs = currentCell.beginParse();
  927. const ethPriceId =
  928. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  929. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  930. const ethPriceFeedCell = ethCs.loadRef();
  931. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  932. // Verify ETH current price
  933. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  934. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  935. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_PRICE);
  936. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_CONF);
  937. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_EXPO);
  938. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  939. // Verify ETH EMA price
  940. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  941. const ethEmaPrice = ethEmaPriceCell.beginParse();
  942. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_EMA_PRICE);
  943. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_EMA_CONF);
  944. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_EMA_EXPO);
  945. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  946. // Verify this is the end of the chain
  947. expect(ethCs.remainingRefs).toBe(0);
  948. // Verify sender address
  949. const senderAddress = cs.loadAddress();
  950. expect(senderAddress?.toString()).toBe(
  951. deployer.getSender().address.toString(),
  952. );
  953. // Verify custom payload
  954. const customPayloadCell = cs.loadRef();
  955. const customPayloadSlice = customPayloadCell.beginParse();
  956. const receivedPayload = Buffer.from(
  957. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  958. );
  959. expect(receivedPayload.toString("hex")).toBe(
  960. CUSTOM_PAYLOAD.toString("hex"),
  961. );
  962. });
  963. it("should successfully parse price feed updates with more than 3 price feed ids", async () => {
  964. await deployContract();
  965. await updateGuardianSets(pythTest, deployer);
  966. const sentValue = toNano("1");
  967. const result = await pythTest.sendParsePriceFeedUpdates(
  968. deployer.getSender(),
  969. Buffer.from(HERMES_SOL_TON_PYTH_USDT_UPDATE, "hex"),
  970. sentValue,
  971. [
  972. SOL_PRICE_FEED_ID,
  973. TON_PRICE_FEED_ID,
  974. PYTH_PRICE_FEED_ID,
  975. USDT_PRICE_FEED_ID,
  976. ],
  977. HERMES_SOL_UNIQUE_PUBLISH_TIME,
  978. HERMES_SOL_UNIQUE_PUBLISH_TIME,
  979. deployer.address,
  980. CUSTOM_PAYLOAD,
  981. );
  982. // Verify transaction success and message count
  983. expect(result.transactions).toHaveTransaction({
  984. from: deployer.address,
  985. to: pythTest.address,
  986. success: true,
  987. outMessagesCount: 1,
  988. });
  989. // Get the output message
  990. const outMessage = result.transactions[1].outMessages.values()[0];
  991. // Verify excess value is returned
  992. expect(
  993. (outMessage.info as CommonMessageInfoInternal).value.coins,
  994. ).toBeGreaterThan(0);
  995. const cs = outMessage.body.beginParse();
  996. // Verify message header
  997. const op = cs.loadUint(32);
  998. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  999. // Verify number of price feeds
  1000. const numPriceFeeds = cs.loadUint(8);
  1001. expect(numPriceFeeds).toBe(4); // We expect SOL, TON, PYTH and USDT price feeds
  1002. // Load and verify price feeds
  1003. const priceFeedsCell = cs.loadRef();
  1004. let currentCell = priceFeedsCell;
  1005. // First price feed (SOL)
  1006. const solCs = currentCell.beginParse();
  1007. const solPriceId =
  1008. "0x" + solCs.loadUintBig(256).toString(16).padStart(64, "0");
  1009. expect(solPriceId).toBe(SOL_PRICE_FEED_ID);
  1010. const solPriceFeedCell = solCs.loadRef();
  1011. const solPriceFeedSlice = solPriceFeedCell.beginParse();
  1012. // Verify SOL current price
  1013. const solCurrentPriceCell = solPriceFeedSlice.loadRef();
  1014. const solCurrentPrice = solCurrentPriceCell.beginParse();
  1015. expect(solCurrentPrice.loadInt(64)).toBe(HERMES_SOL_UNIQUE_PRICE);
  1016. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_CONF);
  1017. expect(solCurrentPrice.loadInt(32)).toBe(HERMES_SOL_UNIQUE_EXPO);
  1018. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_PUBLISH_TIME);
  1019. // Move through TON and PYTH price feeds to reach USDT
  1020. currentCell = solCs.loadRef(); // Move to TON
  1021. const tonCs = currentCell.beginParse();
  1022. tonCs.loadUintBig(256); // Skip TON price ID
  1023. tonCs.loadRef(); // Skip TON price data
  1024. currentCell = tonCs.loadRef(); // Move to PYTH
  1025. const pythCs = currentCell.beginParse();
  1026. pythCs.loadUintBig(256); // Skip PYTH price ID
  1027. pythCs.loadRef(); // Skip PYTH price data
  1028. currentCell = pythCs.loadRef(); // Move to USDT
  1029. const usdtCs = currentCell.beginParse();
  1030. const usdtPriceId =
  1031. "0x" + usdtCs.loadUintBig(256).toString(16).padStart(64, "0");
  1032. expect(usdtPriceId).toBe(USDT_PRICE_FEED_ID);
  1033. const usdtPriceFeedCell = usdtCs.loadRef();
  1034. const usdtPriceFeedSlice = usdtPriceFeedCell.beginParse();
  1035. // Verify USDT current price
  1036. const usdtCurrentPriceCell = usdtPriceFeedSlice.loadRef();
  1037. const usdtCurrentPrice = usdtCurrentPriceCell.beginParse();
  1038. expect(usdtCurrentPrice.loadInt(64)).toBe(HERMES_USDT_UNIQUE_PRICE);
  1039. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_CONF);
  1040. expect(usdtCurrentPrice.loadInt(32)).toBe(HERMES_USDT_UNIQUE_EXPO);
  1041. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_PUBLISH_TIME);
  1042. // Verify this is the end of the chain
  1043. expect(usdtCs.remainingRefs).toBe(0);
  1044. // Verify sender address
  1045. const senderAddress = cs.loadAddress();
  1046. expect(senderAddress?.toString()).toBe(
  1047. deployer.getSender().address.toString(),
  1048. );
  1049. // Verify custom payload
  1050. const customPayloadCell = cs.loadRef();
  1051. const customPayloadSlice = customPayloadCell.beginParse();
  1052. const receivedPayload = Buffer.from(
  1053. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1054. );
  1055. expect(receivedPayload.toString("hex")).toBe(
  1056. CUSTOM_PAYLOAD.toString("hex"),
  1057. );
  1058. });
  1059. it("should successfully parse unique price feed updates", async () => {
  1060. await deployContract();
  1061. await updateGuardianSets(pythTest, deployer);
  1062. const sentValue = toNano("1");
  1063. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1064. deployer.getSender(),
  1065. Buffer.from(HERMES_BTC_ETH_UNIQUE_UPDATE, "hex"),
  1066. sentValue,
  1067. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1068. HERMES_BTC_PUBLISH_TIME,
  1069. 60,
  1070. deployer.address,
  1071. CUSTOM_PAYLOAD,
  1072. );
  1073. // Verify transaction success and message count
  1074. expect(result.transactions).toHaveTransaction({
  1075. from: deployer.address,
  1076. to: pythTest.address,
  1077. success: true,
  1078. outMessagesCount: 1,
  1079. });
  1080. // Get the output message
  1081. const outMessage = result.transactions[1].outMessages.values()[0];
  1082. // Verify excess value is returned
  1083. expect(
  1084. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1085. ).toBeGreaterThan(0);
  1086. const cs = outMessage.body.beginParse();
  1087. // Verify message header
  1088. const op = cs.loadUint(32);
  1089. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1090. // Verify number of price feeds
  1091. const numPriceFeeds = cs.loadUint(8);
  1092. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1093. // Load and verify price feeds
  1094. const priceFeedsCell = cs.loadRef();
  1095. let currentCell = priceFeedsCell;
  1096. // First price feed (BTC)
  1097. const btcCs = currentCell.beginParse();
  1098. const btcPriceId =
  1099. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  1100. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  1101. const btcPriceFeedCell = btcCs.loadRef();
  1102. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  1103. // Verify BTC current price
  1104. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  1105. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  1106. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_PRICE);
  1107. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_CONF);
  1108. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EXPO);
  1109. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_PUBLISH_TIME);
  1110. // Verify BTC EMA price
  1111. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  1112. const btcEmaPrice = btcEmaPriceCell.beginParse();
  1113. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_EMA_PRICE);
  1114. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_CONF);
  1115. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EMA_EXPO);
  1116. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_PUBLISH_TIME);
  1117. // Move to ETH price feed
  1118. currentCell = btcCs.loadRef();
  1119. // Second price feed (ETH)
  1120. const ethCs = currentCell.beginParse();
  1121. const ethPriceId =
  1122. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  1123. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  1124. const ethPriceFeedCell = ethCs.loadRef();
  1125. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  1126. // Verify ETH current price
  1127. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  1128. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  1129. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_PRICE);
  1130. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_CONF);
  1131. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EXPO);
  1132. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_PUBLISH_TIME);
  1133. // Verify ETH EMA price
  1134. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  1135. const ethEmaPrice = ethEmaPriceCell.beginParse();
  1136. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_EMA_PRICE);
  1137. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_CONF);
  1138. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EMA_EXPO);
  1139. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_PUBLISH_TIME);
  1140. // Verify this is the end of the chain
  1141. expect(ethCs.remainingRefs).toBe(0);
  1142. // Verify sender address
  1143. const senderAddress = cs.loadAddress();
  1144. expect(senderAddress?.toString()).toBe(
  1145. deployer.getSender().address.toString(),
  1146. );
  1147. // Verify custom payload
  1148. const customPayloadCell = cs.loadRef();
  1149. const customPayloadSlice = customPayloadCell.beginParse();
  1150. const receivedPayload = Buffer.from(
  1151. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1152. );
  1153. expect(receivedPayload.toString("hex")).toBe(
  1154. CUSTOM_PAYLOAD.toString("hex"),
  1155. );
  1156. });
  1157. it("should successfully parse unique price feed updates with more than 3 price feed ids", async () => {
  1158. await deployContract();
  1159. await updateGuardianSets(pythTest, deployer);
  1160. const sentValue = toNano("1");
  1161. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1162. deployer.getSender(),
  1163. Buffer.from(HERMES_SOL_TON_PYTH_USDT_UPDATE, "hex"),
  1164. sentValue,
  1165. [
  1166. SOL_PRICE_FEED_ID,
  1167. TON_PRICE_FEED_ID,
  1168. PYTH_PRICE_FEED_ID,
  1169. USDT_PRICE_FEED_ID,
  1170. ],
  1171. HERMES_SOL_UNIQUE_PUBLISH_TIME,
  1172. 60,
  1173. deployer.address,
  1174. CUSTOM_PAYLOAD,
  1175. );
  1176. // Verify transaction success and message count
  1177. expect(result.transactions).toHaveTransaction({
  1178. from: deployer.address,
  1179. to: pythTest.address,
  1180. success: true,
  1181. outMessagesCount: 1,
  1182. });
  1183. // Get the output message
  1184. const outMessage = result.transactions[1].outMessages.values()[0];
  1185. // Verify excess value is returned
  1186. expect(
  1187. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1188. ).toBeGreaterThan(0);
  1189. const cs = outMessage.body.beginParse();
  1190. // Verify message header
  1191. const op = cs.loadUint(32);
  1192. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1193. // Verify number of price feeds
  1194. const numPriceFeeds = cs.loadUint(8);
  1195. expect(numPriceFeeds).toBe(4); // We expect SOL, TON, PYTH and USDT price feeds
  1196. // Load and verify price feeds
  1197. const priceFeedsCell = cs.loadRef();
  1198. let currentCell = priceFeedsCell;
  1199. // First price feed (SOL)
  1200. const solCs = currentCell.beginParse();
  1201. const solPriceId =
  1202. "0x" + solCs.loadUintBig(256).toString(16).padStart(64, "0");
  1203. expect(solPriceId).toBe(SOL_PRICE_FEED_ID);
  1204. const solPriceFeedCell = solCs.loadRef();
  1205. const solPriceFeedSlice = solPriceFeedCell.beginParse();
  1206. // Verify SOL current price
  1207. const solCurrentPriceCell = solPriceFeedSlice.loadRef();
  1208. const solCurrentPrice = solCurrentPriceCell.beginParse();
  1209. expect(solCurrentPrice.loadInt(64)).toBe(HERMES_SOL_UNIQUE_PRICE);
  1210. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_CONF);
  1211. expect(solCurrentPrice.loadInt(32)).toBe(HERMES_SOL_UNIQUE_EXPO);
  1212. expect(solCurrentPrice.loadUint(64)).toBe(HERMES_SOL_UNIQUE_PUBLISH_TIME);
  1213. // Move through TON and PYTH price feeds to reach USDT
  1214. currentCell = solCs.loadRef(); // Move to TON
  1215. const tonCs = currentCell.beginParse();
  1216. tonCs.loadUintBig(256); // Skip TON price ID
  1217. tonCs.loadRef(); // Skip TON price data
  1218. currentCell = tonCs.loadRef(); // Move to PYTH
  1219. const pythCs = currentCell.beginParse();
  1220. pythCs.loadUintBig(256); // Skip PYTH price ID
  1221. pythCs.loadRef(); // Skip PYTH price data
  1222. currentCell = pythCs.loadRef(); // Move to USDT
  1223. const usdtCs = currentCell.beginParse();
  1224. const usdtPriceId =
  1225. "0x" + usdtCs.loadUintBig(256).toString(16).padStart(64, "0");
  1226. expect(usdtPriceId).toBe(USDT_PRICE_FEED_ID);
  1227. const usdtPriceFeedCell = usdtCs.loadRef();
  1228. const usdtPriceFeedSlice = usdtPriceFeedCell.beginParse();
  1229. // Verify USDT current price
  1230. const usdtCurrentPriceCell = usdtPriceFeedSlice.loadRef();
  1231. const usdtCurrentPrice = usdtCurrentPriceCell.beginParse();
  1232. expect(usdtCurrentPrice.loadInt(64)).toBe(HERMES_USDT_UNIQUE_PRICE);
  1233. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_CONF);
  1234. expect(usdtCurrentPrice.loadInt(32)).toBe(HERMES_USDT_UNIQUE_EXPO);
  1235. expect(usdtCurrentPrice.loadUint(64)).toBe(HERMES_USDT_UNIQUE_PUBLISH_TIME);
  1236. // Verify this is the end of the chain
  1237. expect(usdtCs.remainingRefs).toBe(0);
  1238. // Verify sender address
  1239. const senderAddress = cs.loadAddress();
  1240. expect(senderAddress?.toString()).toBe(
  1241. deployer.getSender().address.toString(),
  1242. );
  1243. // Verify custom payload
  1244. const customPayloadCell = cs.loadRef();
  1245. const customPayloadSlice = customPayloadCell.beginParse();
  1246. const receivedPayload = Buffer.from(
  1247. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1248. );
  1249. expect(receivedPayload.toString("hex")).toBe(
  1250. CUSTOM_PAYLOAD.toString("hex"),
  1251. );
  1252. });
  1253. it("should fail to parse invalid price feed updates", async () => {
  1254. await deployContract();
  1255. await updateGuardianSets(pythTest, deployer);
  1256. const invalidUpdateData = Buffer.from("invalid data");
  1257. const result = await pythTest.sendParsePriceFeedUpdates(
  1258. deployer.getSender(),
  1259. invalidUpdateData,
  1260. toNano("1"),
  1261. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1262. HERMES_BTC_PUBLISH_TIME,
  1263. HERMES_BTC_PUBLISH_TIME,
  1264. deployer.address,
  1265. CUSTOM_PAYLOAD,
  1266. );
  1267. // Verify transaction success but error response sent
  1268. expect(result.transactions).toHaveTransaction({
  1269. from: deployer.address,
  1270. to: pythTest.address,
  1271. success: true,
  1272. });
  1273. // Find the error response message - it's in the second transaction's outMessages
  1274. const errorTx = result.transactions[1]; // The PythTest contract transaction
  1275. expect(errorTx.outMessages.values().length).toBeGreaterThan(0);
  1276. const errorMessage = errorTx.outMessages.values()[0];
  1277. expect(errorMessage).toBeDefined();
  1278. const cs = errorMessage.body.beginParse();
  1279. // Verify error response format
  1280. const op = cs.loadUint(32);
  1281. expect(op).toBe(0x10002); // OP_RESPONSE_ERROR
  1282. const errorCode = cs.loadUint(32);
  1283. expect(errorCode).toBe(2002); // ERROR_INVALID_MAGIC
  1284. const originalOp = cs.loadUint(32);
  1285. expect(originalOp).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  1286. // Verify custom payload is preserved
  1287. const customPayloadCell = cs.loadRef();
  1288. const customPayloadSlice = customPayloadCell.beginParse();
  1289. expect(
  1290. Buffer.from(
  1291. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1292. ).toString("hex"),
  1293. ).toBe(CUSTOM_PAYLOAD.toString("hex"));
  1294. });
  1295. it("should fail to parse price feed updates within range", async () => {
  1296. await deployContract();
  1297. await updateGuardianSets(pythTest, deployer);
  1298. const sentValue = toNano("1");
  1299. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1300. deployer.getSender(),
  1301. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1302. sentValue,
  1303. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1304. HERMES_BTC_PUBLISH_TIME + 1,
  1305. HERMES_BTC_PUBLISH_TIME + 1,
  1306. deployer.address,
  1307. CUSTOM_PAYLOAD,
  1308. );
  1309. // Verify transaction success but error response sent
  1310. expect(result.transactions).toHaveTransaction({
  1311. from: deployer.address,
  1312. to: pythTest.address,
  1313. success: true,
  1314. });
  1315. // Find the error response message - it's in the second transaction's outMessages
  1316. const errorTx = result.transactions[1]; // The PythTest contract transaction
  1317. expect(errorTx.outMessages.values().length).toBeGreaterThan(0);
  1318. const errorMessage = errorTx.outMessages.values()[0];
  1319. expect(errorMessage).toBeDefined();
  1320. const cs = errorMessage.body.beginParse();
  1321. // Verify error response format
  1322. const op = cs.loadUint(32);
  1323. expect(op).toBe(0x10002); // OP_RESPONSE_ERROR
  1324. const errorCode = cs.loadUint(32);
  1325. expect(errorCode).toBe(2020); // ERROR_PRICE_FEED_NOT_FOUND_WITHIN_RANGE
  1326. const originalOp = cs.loadUint(32);
  1327. expect(originalOp).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1328. // Verify custom payload is preserved
  1329. const customPayloadCell = cs.loadRef();
  1330. const customPayloadSlice = customPayloadCell.beginParse();
  1331. expect(
  1332. Buffer.from(
  1333. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1334. ).toString("hex"),
  1335. ).toBe(CUSTOM_PAYLOAD.toString("hex"));
  1336. });
  1337. it("should fail to parse unique price feed updates", async () => {
  1338. await deployContract();
  1339. await updateGuardianSets(pythTest, deployer);
  1340. const sentValue = toNano("1");
  1341. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1342. deployer.getSender(),
  1343. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1344. sentValue,
  1345. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1346. HERMES_BTC_PUBLISH_TIME,
  1347. 60,
  1348. deployer.address,
  1349. CUSTOM_PAYLOAD,
  1350. );
  1351. // Verify transaction success but error response sent
  1352. expect(result.transactions).toHaveTransaction({
  1353. from: deployer.address,
  1354. to: pythTest.address,
  1355. success: true,
  1356. });
  1357. // Find the error response message - it's in the second transaction's outMessages
  1358. const errorTx = result.transactions[1]; // The PythTest contract transaction
  1359. expect(errorTx.outMessages.values().length).toBeGreaterThan(0);
  1360. const errorMessage = errorTx.outMessages.values()[0];
  1361. expect(errorMessage).toBeDefined();
  1362. const cs = errorMessage.body.beginParse();
  1363. // Verify error response format
  1364. const op = cs.loadUint(32);
  1365. expect(op).toBe(0x10002); // OP_RESPONSE_ERROR
  1366. const errorCode = cs.loadUint(32);
  1367. expect(errorCode).toBe(2020); // ERROR_PRICE_FEED_NOT_FOUND_WITHIN_RANGE
  1368. const originalOp = cs.loadUint(32);
  1369. expect(originalOp).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1370. // Verify custom payload is preserved
  1371. const customPayloadCell = cs.loadRef();
  1372. const customPayloadSlice = customPayloadCell.beginParse();
  1373. expect(
  1374. Buffer.from(
  1375. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1376. ).toString("hex"),
  1377. ).toBe(CUSTOM_PAYLOAD.toString("hex"));
  1378. });
  1379. it("should successfully parse price feed updates in price ids order", async () => {
  1380. await deployContract();
  1381. await updateGuardianSets(pythTest, deployer);
  1382. const sentValue = toNano("1");
  1383. const result = await pythTest.sendParsePriceFeedUpdates(
  1384. deployer.getSender(),
  1385. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1386. sentValue,
  1387. [ETH_PRICE_FEED_ID, BTC_PRICE_FEED_ID],
  1388. HERMES_BTC_PUBLISH_TIME,
  1389. HERMES_BTC_PUBLISH_TIME,
  1390. deployer.address,
  1391. CUSTOM_PAYLOAD,
  1392. );
  1393. // Verify transaction success and message count
  1394. expect(result.transactions).toHaveTransaction({
  1395. from: deployer.address,
  1396. to: pythTest.address,
  1397. success: true,
  1398. outMessagesCount: 1,
  1399. });
  1400. // Get the output message
  1401. const outMessage = result.transactions[1].outMessages.values()[0];
  1402. // Verify excess value is returned
  1403. expect(
  1404. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1405. ).toBeGreaterThan(0);
  1406. expect((outMessage.info as CommonMessageInfoInternal).dest.toString()).toBe(
  1407. deployer.address.toString(),
  1408. );
  1409. const cs = outMessage.body.beginParse();
  1410. // Verify message header
  1411. const op = cs.loadUint(32);
  1412. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  1413. // Verify number of price feeds
  1414. const numPriceFeeds = cs.loadUint(8);
  1415. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1416. // Load and verify price feeds
  1417. const priceFeedsCell = cs.loadRef();
  1418. let currentCell = priceFeedsCell;
  1419. // First price feed (ETH)
  1420. const ethCs = currentCell.beginParse();
  1421. const ethPriceId =
  1422. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  1423. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  1424. const ethPriceFeedCell = ethCs.loadRef();
  1425. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  1426. // Verify ETH current price
  1427. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  1428. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  1429. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_PRICE);
  1430. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_CONF);
  1431. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_EXPO);
  1432. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  1433. // Verify ETH EMA price
  1434. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  1435. const ethEmaPrice = ethEmaPriceCell.beginParse();
  1436. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_EMA_PRICE);
  1437. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_EMA_CONF);
  1438. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_EMA_EXPO);
  1439. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_PUBLISH_TIME);
  1440. // Move to ETH price feed
  1441. currentCell = ethCs.loadRef();
  1442. // Second price feed (BTC)
  1443. const btcCs = currentCell.beginParse();
  1444. const btcPriceId =
  1445. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  1446. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  1447. const btcPriceFeedCell = btcCs.loadRef();
  1448. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  1449. // Verify BTC current price
  1450. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  1451. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  1452. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_PRICE);
  1453. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_CONF);
  1454. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_EXPO);
  1455. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  1456. // Verify BTC EMA price
  1457. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  1458. const btcEmaPrice = btcEmaPriceCell.beginParse();
  1459. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_EMA_PRICE);
  1460. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_EMA_CONF);
  1461. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_EMA_EXPO);
  1462. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_PUBLISH_TIME);
  1463. // Verify this is the end of the chain
  1464. expect(ethCs.remainingRefs).toBe(0);
  1465. });
  1466. it("should successfully parse unique price feed updates in price ids order", async () => {
  1467. await deployContract();
  1468. await updateGuardianSets(pythTest, deployer);
  1469. const sentValue = toNano("1");
  1470. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1471. deployer.getSender(),
  1472. Buffer.from(HERMES_BTC_ETH_UNIQUE_UPDATE, "hex"),
  1473. sentValue,
  1474. [ETH_PRICE_FEED_ID, BTC_PRICE_FEED_ID],
  1475. HERMES_BTC_PUBLISH_TIME,
  1476. 60,
  1477. deployer.address,
  1478. CUSTOM_PAYLOAD,
  1479. );
  1480. // Verify transaction success and message count
  1481. expect(result.transactions).toHaveTransaction({
  1482. from: deployer.address,
  1483. to: pythTest.address,
  1484. success: true,
  1485. outMessagesCount: 1,
  1486. });
  1487. // Get the output message
  1488. const outMessage = result.transactions[1].outMessages.values()[0];
  1489. // Verify excess value is returned
  1490. expect(
  1491. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1492. ).toBeGreaterThan(0);
  1493. const cs = outMessage.body.beginParse();
  1494. // Verify message header
  1495. const op = cs.loadUint(32);
  1496. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1497. // Verify number of price feeds
  1498. const numPriceFeeds = cs.loadUint(8);
  1499. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1500. // Load and verify price feeds
  1501. const priceFeedsCell = cs.loadRef();
  1502. let currentCell = priceFeedsCell;
  1503. // First price feed (ETH)
  1504. const ethCs = currentCell.beginParse();
  1505. const ethPriceId =
  1506. "0x" + ethCs.loadUintBig(256).toString(16).padStart(64, "0");
  1507. expect(ethPriceId).toBe(ETH_PRICE_FEED_ID);
  1508. const ethPriceFeedCell = ethCs.loadRef();
  1509. const ethPriceFeedSlice = ethPriceFeedCell.beginParse();
  1510. // Verify ETH current price
  1511. const ethCurrentPriceCell = ethPriceFeedSlice.loadRef();
  1512. const ethCurrentPrice = ethCurrentPriceCell.beginParse();
  1513. expect(ethCurrentPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_PRICE);
  1514. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_CONF);
  1515. expect(ethCurrentPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EXPO);
  1516. expect(ethCurrentPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_PUBLISH_TIME);
  1517. // Verify ETH EMA price
  1518. const ethEmaPriceCell = ethPriceFeedSlice.loadRef();
  1519. const ethEmaPrice = ethEmaPriceCell.beginParse();
  1520. expect(ethEmaPrice.loadInt(64)).toBe(HERMES_ETH_UNIQUE_EMA_PRICE);
  1521. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_CONF);
  1522. expect(ethEmaPrice.loadInt(32)).toBe(HERMES_ETH_UNIQUE_EMA_EXPO);
  1523. expect(ethEmaPrice.loadUint(64)).toBe(HERMES_ETH_UNIQUE_EMA_PUBLISH_TIME);
  1524. currentCell = ethCs.loadRef();
  1525. // Second price feed (BTC)
  1526. const btcCs = currentCell.beginParse();
  1527. const btcPriceId =
  1528. "0x" + btcCs.loadUintBig(256).toString(16).padStart(64, "0");
  1529. expect(btcPriceId).toBe(BTC_PRICE_FEED_ID);
  1530. const btcPriceFeedCell = btcCs.loadRef();
  1531. const btcPriceFeedSlice = btcPriceFeedCell.beginParse();
  1532. // Verify BTC current price
  1533. const btcCurrentPriceCell = btcPriceFeedSlice.loadRef();
  1534. const btcCurrentPrice = btcCurrentPriceCell.beginParse();
  1535. expect(btcCurrentPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_PRICE);
  1536. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_CONF);
  1537. expect(btcCurrentPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EXPO);
  1538. expect(btcCurrentPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_PUBLISH_TIME);
  1539. // Verify BTC EMA price
  1540. const btcEmaPriceCell = btcPriceFeedSlice.loadRef();
  1541. const btcEmaPrice = btcEmaPriceCell.beginParse();
  1542. expect(btcEmaPrice.loadInt(64)).toBe(HERMES_BTC_UNIQUE_EMA_PRICE);
  1543. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_CONF);
  1544. expect(btcEmaPrice.loadInt(32)).toBe(HERMES_BTC_UNIQUE_EMA_EXPO);
  1545. expect(btcEmaPrice.loadUint(64)).toBe(HERMES_BTC_UNIQUE_EMA_PUBLISH_TIME);
  1546. // Verify this is the end of the chain
  1547. expect(btcCs.remainingRefs).toBe(0);
  1548. });
  1549. it("should successfully parse price feed updates with a different target address", async () => {
  1550. await deployContract();
  1551. await updateGuardianSets(pythTest, deployer);
  1552. const sentValue = toNano("1");
  1553. const result = await pythTest.sendParsePriceFeedUpdates(
  1554. deployer.getSender(),
  1555. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex"),
  1556. sentValue,
  1557. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1558. HERMES_BTC_PUBLISH_TIME,
  1559. HERMES_BTC_PUBLISH_TIME,
  1560. mockDeployer.address,
  1561. CUSTOM_PAYLOAD,
  1562. );
  1563. // Verify transaction success and message count
  1564. expect(result.transactions).toHaveTransaction({
  1565. from: deployer.address,
  1566. to: pythTest.address,
  1567. success: true,
  1568. outMessagesCount: 1,
  1569. });
  1570. // Verify message success to target address
  1571. expect(result.transactions).toHaveTransaction({
  1572. from: pythTest.address,
  1573. to: mockDeployer.address,
  1574. success: true,
  1575. });
  1576. // Get the output message
  1577. const outMessage = result.transactions[1].outMessages.values()[0];
  1578. // Verify excess value is returned
  1579. expect(
  1580. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1581. ).toBeGreaterThan(0);
  1582. const cs = outMessage.body.beginParse();
  1583. // Verify message header
  1584. const op = cs.loadUint(32);
  1585. expect(op).toBe(5); // OP_PARSE_PRICE_FEED_UPDATES
  1586. // Verify number of price feeds
  1587. const numPriceFeeds = cs.loadUint(8);
  1588. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1589. cs.loadRef(); // Skip price feeds
  1590. // Verify sender address
  1591. const senderAddress = cs.loadAddress();
  1592. expect(senderAddress?.toString()).toBe(
  1593. deployer.getSender().address.toString(),
  1594. );
  1595. // Verify custom payload
  1596. const customPayloadCell = cs.loadRef();
  1597. const customPayloadSlice = customPayloadCell.beginParse();
  1598. const receivedPayload = Buffer.from(
  1599. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1600. );
  1601. expect(receivedPayload.toString("hex")).toBe(
  1602. CUSTOM_PAYLOAD.toString("hex"),
  1603. );
  1604. });
  1605. it("should successfully parse unique price feed updates with a different target address", async () => {
  1606. await deployContract();
  1607. await updateGuardianSets(pythTest, deployer);
  1608. const sentValue = toNano("1");
  1609. const result = await pythTest.sendParseUniquePriceFeedUpdates(
  1610. deployer.getSender(),
  1611. Buffer.from(HERMES_BTC_ETH_UNIQUE_UPDATE, "hex"),
  1612. sentValue,
  1613. [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID],
  1614. HERMES_BTC_PUBLISH_TIME,
  1615. 60,
  1616. mockDeployer.address,
  1617. CUSTOM_PAYLOAD,
  1618. );
  1619. // Verify transaction success and message count
  1620. expect(result.transactions).toHaveTransaction({
  1621. from: deployer.address,
  1622. to: pythTest.address,
  1623. success: true,
  1624. outMessagesCount: 1,
  1625. });
  1626. // Verify message success to target address
  1627. expect(result.transactions).toHaveTransaction({
  1628. from: pythTest.address,
  1629. to: mockDeployer.address,
  1630. success: true,
  1631. });
  1632. // Get the output message
  1633. const outMessage = result.transactions[1].outMessages.values()[0];
  1634. // Verify excess value is returned
  1635. expect(
  1636. (outMessage.info as CommonMessageInfoInternal).value.coins,
  1637. ).toBeGreaterThan(0);
  1638. const cs = outMessage.body.beginParse();
  1639. // Verify message header
  1640. const op = cs.loadUint(32);
  1641. expect(op).toBe(6); // OP_PARSE_UNIQUE_PRICE_FEED_UPDATES
  1642. // Verify number of price feeds
  1643. const numPriceFeeds = cs.loadUint(8);
  1644. expect(numPriceFeeds).toBe(2); // We expect BTC and ETH price feeds
  1645. cs.loadRef(); // Skip price feeds
  1646. // Verify sender address
  1647. const senderAddress = cs.loadAddress();
  1648. expect(senderAddress?.toString()).toBe(
  1649. deployer.getSender().address.toString(),
  1650. );
  1651. // Verify custom payload
  1652. const customPayloadCell = cs.loadRef();
  1653. const customPayloadSlice = customPayloadCell.beginParse();
  1654. const receivedPayload = Buffer.from(
  1655. customPayloadSlice.loadBuffer(CUSTOM_PAYLOAD.length),
  1656. );
  1657. expect(receivedPayload.toString("hex")).toBe(
  1658. CUSTOM_PAYLOAD.toString("hex"),
  1659. );
  1660. });
  1661. });