|
1 | 1 | import { toNano } from "@ton/core";
|
| 2 | +import { MainConfig } from "../wrappers/Main"; |
| 3 | +import { compile, NetworkProvider, sleep } from "@ton/blueprint"; |
| 4 | +import { DataSource } from "@pythnetwork/xc-admin-common"; |
| 5 | +import { HermesClient } from "@pythnetwork/hermes-client"; |
2 | 6 | import { Main } from "../wrappers/Main";
|
3 |
| -import { compile, NetworkProvider } from "@ton/blueprint"; |
| 7 | +import { |
| 8 | + GOVERNANCE_DATA_SOURCE, |
| 9 | + GUARDIAN_SET_0, |
| 10 | + MAINNET_UPGRADE_VAAS, |
| 11 | +} from "../tests/utils/wormhole"; |
| 12 | +import { BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID } from "../tests/utils/pyth"; |
4 | 13 |
|
5 | 14 | export async function run(provider: NetworkProvider) {
|
6 |
| - const main = provider.open(Main.createFromConfig({}, await compile("Main"))); |
| 15 | + const SINGLE_UPDATE_FEE = 1; |
| 16 | + const DATA_SOURCES: DataSource[] = [ |
| 17 | + { |
| 18 | + emitterChain: 26, |
| 19 | + emitterAddress: |
| 20 | + "e101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71", |
| 21 | + }, |
| 22 | + ]; |
7 | 23 |
|
8 |
| - await main.sendDeploy(provider.sender(), toNano("0.05")); |
| 24 | + const config: MainConfig = { |
| 25 | + singleUpdateFee: SINGLE_UPDATE_FEE, |
| 26 | + dataSources: DATA_SOURCES, |
| 27 | + guardianSetIndex: 0, |
| 28 | + guardianSet: GUARDIAN_SET_0, |
| 29 | + chainId: 1, |
| 30 | + governanceChainId: 1, |
| 31 | + governanceContract: |
| 32 | + "0000000000000000000000000000000000000000000000000000000000000004", |
| 33 | + governanceDataSource: GOVERNANCE_DATA_SOURCE, |
| 34 | + }; |
| 35 | + |
| 36 | + const main = provider.open( |
| 37 | + Main.createFromConfig(config, await compile("Main")) |
| 38 | + ); |
| 39 | + |
| 40 | + await main.sendDeploy(provider.sender(), toNano("0.005")); |
9 | 41 |
|
10 | 42 | await provider.waitForDeploy(main.address);
|
11 | 43 |
|
12 |
| - // run methods on `main` |
| 44 | + console.log("Main contract deployed at:", main.address.toString()); |
| 45 | + |
| 46 | + // Call sendUpdateGuardianSet for each VAA |
| 47 | + const currentGuardianSetIndex = await main.getCurrentGuardianSetIndex(); |
| 48 | + console.log(`Current guardian set index: ${currentGuardianSetIndex}`); |
| 49 | + |
| 50 | + for (let i = currentGuardianSetIndex; i < MAINNET_UPGRADE_VAAS.length; i++) { |
| 51 | + const vaa = MAINNET_UPGRADE_VAAS[i]; |
| 52 | + const vaaBuffer = Buffer.from(vaa, "hex"); |
| 53 | + await main.sendUpdateGuardianSet(provider.sender(), vaaBuffer); |
| 54 | + console.log( |
| 55 | + `Successfully updated guardian set ${i + 1} with VAA: ${vaa.slice( |
| 56 | + 0, |
| 57 | + 20 |
| 58 | + )}...` |
| 59 | + ); |
| 60 | + |
| 61 | + // Wait for 30 seconds before checking the guardian set index |
| 62 | + console.log("Waiting for 30 seconds before checking guardian set index..."); |
| 63 | + await sleep(30000); |
| 64 | + |
| 65 | + // Verify the update |
| 66 | + const newIndex = await main.getCurrentGuardianSetIndex(); |
| 67 | + if (newIndex !== i + 1) { |
| 68 | + console.error( |
| 69 | + `Failed to update guardian set. Expected index ${ |
| 70 | + i + 1 |
| 71 | + }, got ${newIndex}` |
| 72 | + ); |
| 73 | + break; |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + console.log("Guardian set update process completed."); |
| 78 | + |
| 79 | + // Initialize HermesClient |
| 80 | + const hermesEndpoint = "https://hermes.pyth.network"; |
| 81 | + const hermesClient = new HermesClient(hermesEndpoint); |
| 82 | + |
| 83 | + // Fetch latest price updates for BTC and ETH |
| 84 | + const priceIds = [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID]; |
| 85 | + const latestPriceUpdates = await hermesClient.getLatestPriceUpdates( |
| 86 | + priceIds, |
| 87 | + { encoding: "hex" } |
| 88 | + ); |
| 89 | + console.log("Hermes BTC price:", latestPriceUpdates.parsed?.[0].price); |
| 90 | + console.log("Hermes ETH price:", latestPriceUpdates.parsed?.[1].price); |
| 91 | + // Combine updates into a single buffer |
| 92 | + const updateData = Buffer.from(latestPriceUpdates.binary.data[0], "hex"); |
| 93 | + console.log("Update data:", latestPriceUpdates.binary.data[0]); |
| 94 | + |
| 95 | + const singleUpdateFee = await main.getSingleUpdateFee(); |
| 96 | + console.log("Single update fee:", singleUpdateFee); |
| 97 | + |
| 98 | + // NOTE: As of 2024/10/14 There's a bug with TON Access (https://ton.access.orbs.network) RPC service where if you provide an update data buffer with length of more than ~320 then the rpc returns error 404 and the function fails |
| 99 | + const updateFee = await main.getUpdateFee(updateData); |
| 100 | + console.log("Update fee:", updateFee); |
| 101 | + |
| 102 | + await main.sendUpdatePriceFeeds( |
| 103 | + provider.sender(), |
| 104 | + updateData, |
| 105 | + toNano(updateFee) |
| 106 | + ); |
| 107 | + console.log("Price feeds updated successfully."); |
| 108 | + |
| 109 | + console.log("Waiting for 30 seconds before checking price feeds..."); |
| 110 | + await sleep(30000); |
| 111 | + |
| 112 | + // Query updated price feeds |
| 113 | + const btcPrice = await main.getPriceUnsafe(BTC_PRICE_FEED_ID); |
| 114 | + console.log( |
| 115 | + `Updated BTC price: ${btcPrice.price}, publish time: ${btcPrice.publishTime}` |
| 116 | + ); |
| 117 | + |
| 118 | + const ethPrice = await main.getPriceUnsafe(ETH_PRICE_FEED_ID); |
| 119 | + console.log( |
| 120 | + `Updated ETH price: ${ethPrice.price}, publish time: ${ethPrice.publishTime}` |
| 121 | + ); |
13 | 122 | }
|
0 commit comments