PythTest.spec.ts 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985
  1. import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
  2. import { Cell, 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. } from "./utils/pyth";
  22. import { GUARDIAN_SET_0, MAINNET_UPGRADE_VAAS } from "./utils/wormhole";
  23. import { DataSource } from "@pythnetwork/xc-admin-common";
  24. import { parseDataSource, createAuthorizeUpgradePayload } from "./utils";
  25. import {
  26. UniversalAddress,
  27. createVAA,
  28. serialize,
  29. } from "@wormhole-foundation/sdk-definitions";
  30. import { mocks } from "@wormhole-foundation/sdk-definitions/testing";
  31. const TIME_PERIOD = 60;
  32. const PRICE = new Price({
  33. price: "1",
  34. conf: "2",
  35. expo: 3,
  36. publishTime: 4,
  37. });
  38. const EMA_PRICE = new Price({
  39. price: "5",
  40. conf: "6",
  41. expo: 7,
  42. publishTime: 8,
  43. });
  44. const SINGLE_UPDATE_FEE = 1;
  45. const DATA_SOURCES: DataSource[] = [
  46. {
  47. emitterChain: 26,
  48. emitterAddress:
  49. "e101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71",
  50. },
  51. ];
  52. const TEST_GOVERNANCE_DATA_SOURCES: DataSource[] = [
  53. {
  54. emitterChain: 1,
  55. emitterAddress:
  56. "0000000000000000000000000000000000000000000000000000000000000029",
  57. },
  58. {
  59. emitterChain: 2,
  60. emitterAddress:
  61. "000000000000000000000000000000000000000000000000000000000000002b",
  62. },
  63. {
  64. emitterChain: 1,
  65. emitterAddress:
  66. "0000000000000000000000000000000000000000000000000000000000000000",
  67. },
  68. ];
  69. describe("PythTest", () => {
  70. let code: Cell;
  71. beforeAll(async () => {
  72. code = await compile("PythTest");
  73. });
  74. let blockchain: Blockchain;
  75. let deployer: SandboxContract<TreasuryContract>;
  76. let pythTest: SandboxContract<PythTest>;
  77. beforeEach(async () => {
  78. blockchain = await Blockchain.create();
  79. deployer = await blockchain.treasury("deployer");
  80. });
  81. async function deployContract(
  82. priceFeedId: HexString = BTC_PRICE_FEED_ID,
  83. timePeriod: number = TIME_PERIOD,
  84. price: Price = PRICE,
  85. emaPrice: Price = EMA_PRICE,
  86. singleUpdateFee: number = SINGLE_UPDATE_FEE,
  87. dataSources: DataSource[] = DATA_SOURCES,
  88. guardianSetIndex: number = 0,
  89. guardianSet: string[] = GUARDIAN_SET_0,
  90. chainId: number = 1,
  91. governanceChainId: number = 1,
  92. governanceContract: string = "0000000000000000000000000000000000000000000000000000000000000004",
  93. governanceDataSource?: DataSource
  94. ) {
  95. const config: PythTestConfig = {
  96. priceFeedId,
  97. timePeriod,
  98. price,
  99. emaPrice,
  100. singleUpdateFee,
  101. dataSources,
  102. guardianSetIndex,
  103. guardianSet,
  104. chainId,
  105. governanceChainId,
  106. governanceContract,
  107. governanceDataSource,
  108. };
  109. pythTest = blockchain.openContract(PythTest.createFromConfig(config, code));
  110. const deployResult = await pythTest.sendDeploy(
  111. deployer.getSender(),
  112. toNano("0.05")
  113. );
  114. expect(deployResult.transactions).toHaveTransaction({
  115. from: deployer.address,
  116. to: pythTest.address,
  117. deploy: true,
  118. success: true,
  119. });
  120. }
  121. async function updateGuardianSets(
  122. pythTest: SandboxContract<PythTest>,
  123. deployer: SandboxContract<TreasuryContract>
  124. ) {
  125. for (const vaa of MAINNET_UPGRADE_VAAS) {
  126. const result = await pythTest.sendUpdateGuardianSet(
  127. deployer.getSender(),
  128. Buffer.from(vaa, "hex")
  129. );
  130. expect(result.transactions).toHaveTransaction({
  131. from: deployer.address,
  132. to: pythTest.address,
  133. success: true,
  134. });
  135. }
  136. }
  137. it("should correctly get price unsafe", async () => {
  138. await deployContract();
  139. const result = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  140. expect(result.price).toBe(1);
  141. expect(result.conf).toBe(2);
  142. expect(result.expo).toBe(3);
  143. expect(result.publishTime).toBe(4);
  144. });
  145. it("should correctly get price no older than", async () => {
  146. const timeNow = Math.floor(Date.now() / 1000) - TIME_PERIOD + 5; // 5 seconds buffer
  147. const price = new Price({
  148. price: "1",
  149. conf: "2",
  150. expo: 3,
  151. publishTime: timeNow,
  152. });
  153. await deployContract(BTC_PRICE_FEED_ID, TIME_PERIOD, price, EMA_PRICE);
  154. const result = await pythTest.getPriceNoOlderThan(
  155. TIME_PERIOD,
  156. BTC_PRICE_FEED_ID
  157. );
  158. expect(result.price).toBe(1);
  159. expect(result.conf).toBe(2);
  160. expect(result.expo).toBe(3);
  161. expect(result.publishTime).toBe(timeNow);
  162. });
  163. it("should fail to get price no older than", async () => {
  164. await deployContract();
  165. await expect(
  166. pythTest.getPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID)
  167. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  168. });
  169. it("should correctly get ema price no older than", async () => {
  170. const timeNow = Math.floor(Date.now() / 1000) - TIME_PERIOD + 5; // 5 seconds buffer
  171. const emaPrice = new Price({
  172. price: "5",
  173. conf: "6",
  174. expo: 7,
  175. publishTime: timeNow,
  176. });
  177. await deployContract(BTC_PRICE_FEED_ID, TIME_PERIOD, PRICE, emaPrice);
  178. const result = await pythTest.getEmaPriceNoOlderThan(
  179. TIME_PERIOD,
  180. BTC_PRICE_FEED_ID
  181. );
  182. expect(result.price).toBe(5);
  183. expect(result.conf).toBe(6);
  184. expect(result.expo).toBe(7);
  185. expect(result.publishTime).toBe(timeNow);
  186. });
  187. it("should fail to get ema price no older than", async () => {
  188. await deployContract();
  189. await expect(
  190. pythTest.getEmaPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID)
  191. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  192. });
  193. it("should correctly get ema price unsafe", async () => {
  194. await deployContract();
  195. const result = await pythTest.getEmaPriceUnsafe(BTC_PRICE_FEED_ID);
  196. expect(result.price).toBe(5);
  197. expect(result.conf).toBe(6);
  198. expect(result.expo).toBe(7);
  199. expect(result.publishTime).toBe(8);
  200. });
  201. it("should correctly get update fee", async () => {
  202. await deployContract();
  203. const result = await pythTest.getUpdateFee(
  204. Buffer.from(HERMES_BTC_ETH_UPDATE, "hex")
  205. );
  206. expect(result).toBe(2);
  207. });
  208. it("should correctly update price feeds", async () => {
  209. await deployContract();
  210. let result;
  211. await updateGuardianSets(pythTest, deployer);
  212. // Check initial prices
  213. const initialBtcPrice = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  214. expect(initialBtcPrice.price).not.toBe(HERMES_BTC_PRICE);
  215. // Expect an error for ETH price feed as it doesn't exist initially
  216. await expect(pythTest.getPriceUnsafe(ETH_PRICE_FEED_ID)).rejects.toThrow(
  217. "Unable to execute get method. Got exit_code: 2000"
  218. ); // ERROR_PRICE_FEED_NOT_FOUND = 2000
  219. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  220. const updateFee = await pythTest.getUpdateFee(updateData);
  221. result = await pythTest.sendUpdatePriceFeeds(
  222. deployer.getSender(),
  223. updateData,
  224. toNano(updateFee)
  225. );
  226. expect(result.transactions).toHaveTransaction({
  227. from: deployer.address,
  228. to: pythTest.address,
  229. success: true,
  230. });
  231. // Check if both BTC and ETH prices have been updated
  232. const updatedBtcPrice = await pythTest.getPriceUnsafe(BTC_PRICE_FEED_ID);
  233. expect(updatedBtcPrice.price).toBe(HERMES_BTC_PRICE);
  234. expect(updatedBtcPrice.publishTime).toBe(HERMES_BTC_PUBLISH_TIME);
  235. const updatedEthPrice = await pythTest.getPriceUnsafe(ETH_PRICE_FEED_ID);
  236. expect(updatedEthPrice.price).toBe(HERMES_ETH_PRICE);
  237. expect(updatedEthPrice.publishTime).toBe(HERMES_ETH_PUBLISH_TIME);
  238. });
  239. it("should fail to get update fee with invalid data", async () => {
  240. await deployContract();
  241. await updateGuardianSets(pythTest, deployer);
  242. const invalidUpdateData = Buffer.from("invalid data");
  243. await expect(pythTest.getUpdateFee(invalidUpdateData)).rejects.toThrow(
  244. "Unable to execute get method. Got exit_code: 2002"
  245. ); // ERROR_INVALID_MAGIC = 2002
  246. });
  247. it("should fail to update price feeds with invalid data", async () => {
  248. await deployContract();
  249. await updateGuardianSets(pythTest, deployer);
  250. const invalidUpdateData = Buffer.from("invalid data");
  251. // Use a fixed value for updateFee since we can't get it from getUpdateFee
  252. const updateFee = toNano("0.1"); // Use a reasonable amount
  253. const result = await pythTest.sendUpdatePriceFeeds(
  254. deployer.getSender(),
  255. invalidUpdateData,
  256. updateFee
  257. );
  258. expect(result.transactions).toHaveTransaction({
  259. from: deployer.address,
  260. to: pythTest.address,
  261. success: false,
  262. exitCode: 2002, // ERROR_INVALID_MAGIC
  263. });
  264. });
  265. it("should fail to update price feeds with outdated guardian set", async () => {
  266. await deployContract();
  267. // Don't update guardian sets
  268. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  269. const updateFee = await pythTest.getUpdateFee(updateData);
  270. const result = await pythTest.sendUpdatePriceFeeds(
  271. deployer.getSender(),
  272. updateData,
  273. toNano(updateFee)
  274. );
  275. expect(result.transactions).toHaveTransaction({
  276. from: deployer.address,
  277. to: pythTest.address,
  278. success: false,
  279. exitCode: 1002, // ERROR_GUARDIAN_SET_NOT_FOUND
  280. });
  281. });
  282. it("should fail to update price feeds with invalid data source", async () => {
  283. await deployContract(
  284. BTC_PRICE_FEED_ID,
  285. TIME_PERIOD,
  286. PRICE,
  287. EMA_PRICE,
  288. SINGLE_UPDATE_FEE,
  289. [] // Empty data sources
  290. );
  291. await updateGuardianSets(pythTest, deployer);
  292. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  293. const updateFee = await pythTest.getUpdateFee(updateData);
  294. const result = await pythTest.sendUpdatePriceFeeds(
  295. deployer.getSender(),
  296. updateData,
  297. toNano(updateFee)
  298. );
  299. expect(result.transactions).toHaveTransaction({
  300. from: deployer.address,
  301. to: pythTest.address,
  302. success: false,
  303. exitCode: 2005, // ERROR_UPDATE_DATA_SOURCE_NOT_FOUND
  304. });
  305. });
  306. it("should correctly handle stale prices", async () => {
  307. const staleTime = Math.floor(Date.now() / 1000) - TIME_PERIOD - 10; // 10 seconds past the allowed period
  308. const stalePrice = new Price({
  309. price: "1",
  310. conf: "2",
  311. expo: 3,
  312. publishTime: staleTime,
  313. });
  314. await deployContract(BTC_PRICE_FEED_ID, TIME_PERIOD, stalePrice, EMA_PRICE);
  315. await expect(
  316. pythTest.getPriceNoOlderThan(TIME_PERIOD, BTC_PRICE_FEED_ID)
  317. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2001"); // ERROR_OUTDATED_PRICE = 2001
  318. });
  319. it("should fail to update price feeds with insufficient gas", async () => {
  320. await deployContract();
  321. await updateGuardianSets(pythTest, deployer);
  322. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  323. const result = await pythTest.sendUpdatePriceFeeds(
  324. deployer.getSender(),
  325. updateData,
  326. toNano("0.1") // Insufficient gas
  327. );
  328. expect(result.transactions).toHaveTransaction({
  329. from: deployer.address,
  330. to: pythTest.address,
  331. success: false,
  332. exitCode: 3000, // ERROR_INSUFFICIENT_GAS
  333. });
  334. });
  335. it("should fail to update price feeds with insufficient fee", async () => {
  336. await deployContract();
  337. await updateGuardianSets(pythTest, deployer);
  338. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  339. const updateFee = await pythTest.getUpdateFee(updateData);
  340. // Send less than the required fee
  341. const insufficientFee = updateFee - 1;
  342. const result = await pythTest.sendUpdatePriceFeeds(
  343. deployer.getSender(),
  344. updateData,
  345. 156000000n + BigInt(insufficientFee) // 156000000 = 390000 (estimated gas used for the transaction, this is defined in contracts/common/gas.fc as UPDATE_PRICE_FEEDS_GAS) * 400 (current settings in basechain are as follows: 1 unit of gas costs 400 nanotons)
  346. );
  347. // Check that the transaction did not succeed
  348. expect(result.transactions).toHaveTransaction({
  349. from: deployer.address,
  350. to: pythTest.address,
  351. success: false,
  352. exitCode: 2011, // ERROR_INSUFFICIENT_FEE = 2011
  353. });
  354. });
  355. it("should fail to get prices for non-existent price feed", async () => {
  356. await deployContract();
  357. const nonExistentPriceFeedId =
  358. "0000000000000000000000000000000000000000000000000000000000000000";
  359. await expect(
  360. pythTest.getPriceUnsafe(nonExistentPriceFeedId)
  361. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND = 2000
  362. await expect(
  363. pythTest.getPriceNoOlderThan(TIME_PERIOD, nonExistentPriceFeedId)
  364. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND
  365. await expect(
  366. pythTest.getEmaPriceUnsafe(nonExistentPriceFeedId)
  367. ).rejects.toThrow("Unable to execute get method. Got exit_code: 2000"); // ERROR_PRICE_FEED_NOT_FOUND
  368. });
  369. it("should correctly get chain ID", async () => {
  370. await deployContract();
  371. const result = await pythTest.getChainId();
  372. expect(result).toEqual(1);
  373. });
  374. it("should correctly get last executed governance sequence", async () => {
  375. await deployContract(
  376. BTC_PRICE_FEED_ID,
  377. TIME_PERIOD,
  378. PRICE,
  379. EMA_PRICE,
  380. SINGLE_UPDATE_FEE,
  381. DATA_SOURCES,
  382. 0,
  383. [TEST_GUARDIAN_ADDRESS1],
  384. 60051,
  385. 1,
  386. "0000000000000000000000000000000000000000000000000000000000000004",
  387. TEST_GOVERNANCE_DATA_SOURCES[0]
  388. );
  389. // Check initial value
  390. let result = await pythTest.getLastExecutedGovernanceSequence();
  391. expect(result).toEqual(0);
  392. // Execute a governance action (e.g., set fee)
  393. await pythTest.sendExecuteGovernanceAction(
  394. deployer.getSender(),
  395. Buffer.from(PYTH_SET_FEE, "hex")
  396. );
  397. // Check that the sequence has increased
  398. result = await pythTest.getLastExecutedGovernanceSequence();
  399. expect(result).toEqual(1);
  400. });
  401. it("should correctly get governance data source index", async () => {
  402. // Deploy contract with initial governance data source
  403. await deployContract(
  404. BTC_PRICE_FEED_ID,
  405. TIME_PERIOD,
  406. PRICE,
  407. EMA_PRICE,
  408. SINGLE_UPDATE_FEE,
  409. DATA_SOURCES,
  410. 0,
  411. [TEST_GUARDIAN_ADDRESS1],
  412. 60051,
  413. 1,
  414. "0000000000000000000000000000000000000000000000000000000000000004",
  415. TEST_GOVERNANCE_DATA_SOURCES[0]
  416. );
  417. // Check initial value
  418. let result = await pythTest.getGovernanceDataSourceIndex();
  419. expect(result).toEqual(0);
  420. // Execute governance action to change data source
  421. await pythTest.sendExecuteGovernanceAction(
  422. deployer.getSender(),
  423. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex")
  424. );
  425. // Check that the index has increased
  426. result = await pythTest.getGovernanceDataSourceIndex();
  427. expect(result).toEqual(1);
  428. });
  429. it("should correctly get governance data source", async () => {
  430. // Deploy contract without initial governance data source
  431. await deployContract();
  432. // Check initial value (should be empty)
  433. let result = await pythTest.getGovernanceDataSource();
  434. expect(result).toBeDefined();
  435. expect(result.bits.length).toBe(0);
  436. expect(result.refs.length).toBe(0);
  437. // Deploy contract with initial governance data source
  438. await deployContract(
  439. BTC_PRICE_FEED_ID,
  440. TIME_PERIOD,
  441. PRICE,
  442. EMA_PRICE,
  443. SINGLE_UPDATE_FEE,
  444. DATA_SOURCES,
  445. 0,
  446. [TEST_GUARDIAN_ADDRESS1],
  447. 60051,
  448. 1,
  449. "0000000000000000000000000000000000000000000000000000000000000004",
  450. TEST_GOVERNANCE_DATA_SOURCES[0]
  451. );
  452. // Check that the governance data source is set
  453. result = await pythTest.getGovernanceDataSource();
  454. let dataSource = parseDataSource(result);
  455. expect(dataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[0]);
  456. // Execute governance action to change data source
  457. await pythTest.sendExecuteGovernanceAction(
  458. deployer.getSender(),
  459. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex")
  460. );
  461. // Check that the data source has changed
  462. result = await pythTest.getGovernanceDataSource();
  463. dataSource = parseDataSource(result);
  464. expect(dataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]);
  465. });
  466. it("should correctly get single update fee", async () => {
  467. await deployContract();
  468. // Get the initial fee
  469. const result = await pythTest.getSingleUpdateFee();
  470. expect(result).toBe(SINGLE_UPDATE_FEE);
  471. });
  472. it("should execute set data sources governance instruction", async () => {
  473. await deployContract(
  474. BTC_PRICE_FEED_ID,
  475. TIME_PERIOD,
  476. PRICE,
  477. EMA_PRICE,
  478. SINGLE_UPDATE_FEE,
  479. DATA_SOURCES,
  480. 0,
  481. [TEST_GUARDIAN_ADDRESS1],
  482. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  483. 1,
  484. "0000000000000000000000000000000000000000000000000000000000000004",
  485. TEST_GOVERNANCE_DATA_SOURCES[0]
  486. );
  487. // Execute the governance action
  488. const result = await pythTest.sendExecuteGovernanceAction(
  489. deployer.getSender(),
  490. Buffer.from(PYTH_SET_DATA_SOURCES, "hex")
  491. );
  492. expect(result.transactions).toHaveTransaction({
  493. from: deployer.address,
  494. to: pythTest.address,
  495. success: true,
  496. });
  497. // Verify that the new data sources are set correctly
  498. const newDataSources: DataSource[] = [
  499. {
  500. emitterChain: 1,
  501. emitterAddress:
  502. "6bb14509a612f01fbbc4cffeebd4bbfb492a86df717ebe92eb6df432a3f00a25",
  503. },
  504. {
  505. emitterChain: 3,
  506. emitterAddress:
  507. "000000000000000000000000000000000000000000000000000000000000012d",
  508. },
  509. ];
  510. for (const dataSource of newDataSources) {
  511. const isValid = await pythTest.getIsValidDataSource(dataSource);
  512. expect(isValid).toBe(true);
  513. }
  514. // Verify that the old data source is no longer valid
  515. const oldDataSource = DATA_SOURCES[0];
  516. const oldDataSourceIsValid = await pythTest.getIsValidDataSource(
  517. oldDataSource
  518. );
  519. expect(oldDataSourceIsValid).toBe(false);
  520. });
  521. it("should execute set fee governance instruction", async () => {
  522. await deployContract(
  523. BTC_PRICE_FEED_ID,
  524. TIME_PERIOD,
  525. PRICE,
  526. EMA_PRICE,
  527. SINGLE_UPDATE_FEE,
  528. DATA_SOURCES,
  529. 0,
  530. [TEST_GUARDIAN_ADDRESS1],
  531. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  532. 1,
  533. "0000000000000000000000000000000000000000000000000000000000000004",
  534. TEST_GOVERNANCE_DATA_SOURCES[0]
  535. );
  536. // Get the initial fee
  537. const initialFee = await pythTest.getSingleUpdateFee();
  538. expect(initialFee).toBe(SINGLE_UPDATE_FEE);
  539. // Execute the governance action
  540. const result = await pythTest.sendExecuteGovernanceAction(
  541. deployer.getSender(),
  542. Buffer.from(PYTH_SET_FEE, "hex")
  543. );
  544. expect(result.transactions).toHaveTransaction({
  545. from: deployer.address,
  546. to: pythTest.address,
  547. success: true,
  548. });
  549. // Get the new fee
  550. const newFee = await pythTest.getSingleUpdateFee();
  551. expect(newFee).toBe(4200); // The new fee value is 4200 in the PYTH_SET_FEE payload
  552. // Verify that the new fee is used for updates
  553. const updateData = Buffer.from(HERMES_BTC_ETH_UPDATE, "hex");
  554. const updateFee = await pythTest.getUpdateFee(updateData);
  555. expect(updateFee).toBe(8400); // There are two price updates in HERMES_BTC_ETH_UPDATE
  556. });
  557. it("should execute authorize governance data source transfer", async () => {
  558. await deployContract(
  559. BTC_PRICE_FEED_ID,
  560. TIME_PERIOD,
  561. PRICE,
  562. EMA_PRICE,
  563. SINGLE_UPDATE_FEE,
  564. DATA_SOURCES,
  565. 0,
  566. [TEST_GUARDIAN_ADDRESS1],
  567. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  568. 1,
  569. "0000000000000000000000000000000000000000000000000000000000000004",
  570. TEST_GOVERNANCE_DATA_SOURCES[0]
  571. );
  572. // Get the initial governance data source index
  573. const initialIndex = await pythTest.getGovernanceDataSourceIndex();
  574. expect(initialIndex).toEqual(0); // Initial value should be 0
  575. // Get the initial governance data source
  576. const initialDataSourceCell = await pythTest.getGovernanceDataSource();
  577. const initialDataSource = parseDataSource(initialDataSourceCell);
  578. expect(initialDataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[0]);
  579. // Get the initial last executed governance sequence
  580. const initialSequence = await pythTest.getLastExecutedGovernanceSequence();
  581. expect(initialSequence).toEqual(0); // Initial value should be 0
  582. // Execute the governance action
  583. const result = await pythTest.sendExecuteGovernanceAction(
  584. deployer.getSender(),
  585. Buffer.from(PYTH_AUTHORIZE_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex")
  586. );
  587. expect(result.transactions).toHaveTransaction({
  588. from: deployer.address,
  589. to: pythTest.address,
  590. success: true,
  591. });
  592. // Get the new governance data source index
  593. const newIndex = await pythTest.getGovernanceDataSourceIndex();
  594. expect(newIndex).toEqual(1); // The new index value should match the one in the test payload
  595. // Get the new governance data source
  596. const newDataSourceCell = await pythTest.getGovernanceDataSource();
  597. const newDataSource = parseDataSource(newDataSourceCell);
  598. expect(newDataSource).not.toEqual(initialDataSource); // The data source should have changed
  599. expect(newDataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]); // The data source should have changed
  600. // Get the new last executed governance sequence
  601. const newSequence = await pythTest.getLastExecutedGovernanceSequence();
  602. expect(newSequence).toBeGreaterThan(initialSequence); // The sequence should have increased
  603. expect(newSequence).toBe(1);
  604. });
  605. it("should fail when executing request governance data source transfer directly", async () => {
  606. await deployContract(
  607. BTC_PRICE_FEED_ID,
  608. TIME_PERIOD,
  609. PRICE,
  610. EMA_PRICE,
  611. SINGLE_UPDATE_FEE,
  612. DATA_SOURCES,
  613. 0,
  614. [TEST_GUARDIAN_ADDRESS1],
  615. 60051, // CHAIN_ID of starknet since we are using the test payload for starknet
  616. 1,
  617. "0000000000000000000000000000000000000000000000000000000000000004",
  618. TEST_GOVERNANCE_DATA_SOURCES[1]
  619. );
  620. const result = await pythTest.sendExecuteGovernanceAction(
  621. deployer.getSender(),
  622. Buffer.from(PYTH_REQUEST_GOVERNANCE_DATA_SOURCE_TRANSFER, "hex")
  623. );
  624. // Check that the transaction did not succeed
  625. expect(result.transactions).toHaveTransaction({
  626. from: deployer.address,
  627. to: pythTest.address,
  628. success: false,
  629. exitCode: 1012, // ERROR_INVALID_GOVERNANCE_ACTION
  630. });
  631. // Verify that the governance data source index hasn't changed
  632. const index = await pythTest.getGovernanceDataSourceIndex();
  633. expect(index).toEqual(0); // Should still be the initial value
  634. // Verify that the governance data source hasn't changed
  635. const dataSourceCell = await pythTest.getGovernanceDataSource();
  636. const dataSource = parseDataSource(dataSourceCell);
  637. expect(dataSource).toEqual(TEST_GOVERNANCE_DATA_SOURCES[1]); // Should still be the initial value
  638. });
  639. it("should fail to execute governance action with invalid governance data source", async () => {
  640. await deployContract(
  641. BTC_PRICE_FEED_ID,
  642. TIME_PERIOD,
  643. PRICE,
  644. EMA_PRICE,
  645. SINGLE_UPDATE_FEE,
  646. DATA_SOURCES,
  647. 0,
  648. [TEST_GUARDIAN_ADDRESS1],
  649. 60051,
  650. 1,
  651. "0000000000000000000000000000000000000000000000000000000000000004",
  652. TEST_GOVERNANCE_DATA_SOURCES[1]
  653. );
  654. const result = await pythTest.sendExecuteGovernanceAction(
  655. deployer.getSender(),
  656. Buffer.from(PYTH_SET_FEE, "hex")
  657. );
  658. expect(result.transactions).toHaveTransaction({
  659. from: deployer.address,
  660. to: pythTest.address,
  661. success: false,
  662. exitCode: 2013, // ERROR_INVALID_GOVERNANCE_DATA_SOURCE
  663. });
  664. });
  665. it("should fail to execute governance action with old sequence number", async () => {
  666. await deployContract(
  667. BTC_PRICE_FEED_ID,
  668. TIME_PERIOD,
  669. PRICE,
  670. EMA_PRICE,
  671. SINGLE_UPDATE_FEE,
  672. DATA_SOURCES,
  673. 0,
  674. [TEST_GUARDIAN_ADDRESS1],
  675. 60051,
  676. 1,
  677. "0000000000000000000000000000000000000000000000000000000000000004",
  678. TEST_GOVERNANCE_DATA_SOURCES[0]
  679. );
  680. // Execute a governance action to increase the sequence number
  681. await pythTest.sendExecuteGovernanceAction(
  682. deployer.getSender(),
  683. Buffer.from(PYTH_SET_FEE, "hex")
  684. );
  685. // Try to execute the same governance action again
  686. const result = await pythTest.sendExecuteGovernanceAction(
  687. deployer.getSender(),
  688. Buffer.from(PYTH_SET_FEE, "hex")
  689. );
  690. expect(result.transactions).toHaveTransaction({
  691. from: deployer.address,
  692. to: pythTest.address,
  693. success: false,
  694. exitCode: 2014, // ERROR_OLD_GOVERNANCE_MESSAGE
  695. });
  696. });
  697. it("should fail to execute governance action with invalid chain ID", async () => {
  698. const invalidChainId = 999;
  699. await deployContract(
  700. BTC_PRICE_FEED_ID,
  701. TIME_PERIOD,
  702. PRICE,
  703. EMA_PRICE,
  704. SINGLE_UPDATE_FEE,
  705. DATA_SOURCES,
  706. 0,
  707. [TEST_GUARDIAN_ADDRESS1],
  708. invalidChainId,
  709. 1,
  710. "0000000000000000000000000000000000000000000000000000000000000004",
  711. TEST_GOVERNANCE_DATA_SOURCES[0]
  712. );
  713. const result = await pythTest.sendExecuteGovernanceAction(
  714. deployer.getSender(),
  715. Buffer.from(PYTH_SET_FEE, "hex")
  716. );
  717. expect(result.transactions).toHaveTransaction({
  718. from: deployer.address,
  719. to: pythTest.address,
  720. success: false,
  721. exitCode: 2015, // ERROR_INVALID_GOVERNANCE_TARGET
  722. });
  723. });
  724. it("should successfully upgrade the contract", async () => {
  725. // Compile the upgraded contract
  726. const upgradedCode = await compile("PythTestUpgraded");
  727. const upgradedCodeHash = upgradedCode.hash();
  728. // Create the authorize upgrade payload
  729. const authorizeUpgradePayload =
  730. createAuthorizeUpgradePayload(upgradedCodeHash);
  731. const authorizeUpgradeVaa = createVAA("Uint8Array", {
  732. guardianSet: 0,
  733. timestamp: 0,
  734. nonce: 0,
  735. emitterChain: "Solana",
  736. emitterAddress: new UniversalAddress(new Uint8Array(32)),
  737. sequence: 1n,
  738. consistencyLevel: 0,
  739. signatures: [],
  740. payload: authorizeUpgradePayload,
  741. });
  742. const guardianSet = mocks.devnetGuardianSet();
  743. guardianSet.setSignatures(authorizeUpgradeVaa);
  744. await deployContract(
  745. BTC_PRICE_FEED_ID,
  746. TIME_PERIOD,
  747. PRICE,
  748. EMA_PRICE,
  749. SINGLE_UPDATE_FEE,
  750. DATA_SOURCES,
  751. 0,
  752. [TEST_GUARDIAN_ADDRESS2],
  753. 1,
  754. 1,
  755. "0000000000000000000000000000000000000000000000000000000000000000",
  756. TEST_GOVERNANCE_DATA_SOURCES[2]
  757. );
  758. // Execute the upgrade
  759. const sendExecuteGovernanceActionResult =
  760. await pythTest.sendExecuteGovernanceAction(
  761. deployer.getSender(),
  762. Buffer.from(serialize(authorizeUpgradeVaa))
  763. );
  764. expect(sendExecuteGovernanceActionResult.transactions).toHaveTransaction({
  765. from: deployer.address,
  766. to: pythTest.address,
  767. success: true,
  768. });
  769. // Execute the upgrade
  770. const sendUpgradeContractResult = await pythTest.sendUpgradeContract(
  771. deployer.getSender(),
  772. upgradedCode
  773. );
  774. expect(sendUpgradeContractResult.transactions).toHaveTransaction({
  775. from: deployer.address,
  776. to: pythTest.address,
  777. success: true,
  778. });
  779. // Verify that the contract has been upgraded by calling a new method
  780. const newMethodResult = await pythTest.getNewFunction();
  781. expect(newMethodResult).toBe(1);
  782. });
  783. it("should fail to upgrade the contract with modified code", async () => {
  784. // Compile the upgraded contract
  785. const upgradedCode = await compile("PythTestUpgraded");
  786. const upgradedCodeHash = upgradedCode.hash();
  787. // Create the authorize upgrade payload
  788. const authorizeUpgradePayload =
  789. createAuthorizeUpgradePayload(upgradedCodeHash);
  790. const authorizeUpgradeVaa = createVAA("Uint8Array", {
  791. guardianSet: 0,
  792. timestamp: 0,
  793. nonce: 0,
  794. emitterChain: "Solana",
  795. emitterAddress: new UniversalAddress(new Uint8Array(32)),
  796. sequence: 1n,
  797. consistencyLevel: 0,
  798. signatures: [],
  799. payload: authorizeUpgradePayload,
  800. });
  801. const guardianSet = mocks.devnetGuardianSet();
  802. guardianSet.setSignatures(authorizeUpgradeVaa);
  803. await deployContract(
  804. BTC_PRICE_FEED_ID,
  805. TIME_PERIOD,
  806. PRICE,
  807. EMA_PRICE,
  808. SINGLE_UPDATE_FEE,
  809. DATA_SOURCES,
  810. 0,
  811. [TEST_GUARDIAN_ADDRESS2],
  812. 1,
  813. 1,
  814. "0000000000000000000000000000000000000000000000000000000000000000",
  815. TEST_GOVERNANCE_DATA_SOURCES[2]
  816. );
  817. // Execute the upgrade authorization
  818. const sendExecuteGovernanceActionResult =
  819. await pythTest.sendExecuteGovernanceAction(
  820. deployer.getSender(),
  821. Buffer.from(serialize(authorizeUpgradeVaa))
  822. );
  823. expect(sendExecuteGovernanceActionResult.transactions).toHaveTransaction({
  824. from: deployer.address,
  825. to: pythTest.address,
  826. success: true,
  827. });
  828. // Attempt to execute the upgrade with a different code
  829. const wormholeTestCode = await compile("WormholeTest");
  830. const sendUpgradeContractResult = await pythTest.sendUpgradeContract(
  831. deployer.getSender(),
  832. wormholeTestCode
  833. );
  834. // Expect the transaction to fail
  835. expect(sendUpgradeContractResult.transactions).toHaveTransaction({
  836. from: deployer.address,
  837. to: pythTest.address,
  838. success: false,
  839. exitCode: 2018, // ERROR_INVALID_CODE_HASH
  840. });
  841. // Verify that the contract has not been upgraded by attempting to call the new method
  842. await expect(pythTest.getNewFunction()).rejects.toThrow();
  843. });
  844. });