Skip to content
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
424 changes: 213 additions & 211 deletions pnpm-lock.yaml

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,5 @@ packages:
- "target_chains/solana/sdk/js/solana_utils"
- "target_chains/solana/sdk/js/pyth_solana_receiver"
- "target_chains/ton/contracts"
- "target_chains/ton/sdk/js"
- "contract_manager"
2 changes: 2 additions & 0 deletions target_chains/ton/contracts/contracts/Main.fc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
;; * The remainder of the message body is specific for each supported value of `op`.
if (op == OP_UPDATE_GUARDIAN_SET) {
update_guardian_set(data_slice);
} elseif (op == OP_UPDATE_PRICE_FEEDS) {
update_price_feeds(msg_value, data_slice);
} elseif (op == OP_EXECUTE_GOVERNANCE_ACTION) {
execute_governance_action(data_slice);
} elseif (op == OP_UPGRADE_CONTRACT) {
Expand Down
8 changes: 5 additions & 3 deletions target_chains/ton/contracts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,16 @@
"test": "jest --verbose"
},
"devDependencies": {
"@pythnetwork/pyth-ton-js": "workspace:*",
"@pythnetwork/price-service-sdk": "workspace:*",
"@pythnetwork/xc-admin-common": "workspace:*",
"@pythnetwork/hermes-client": "workspace:*",
"@ton/blueprint": "^0.22.0",
"@ton/core": "~0",
"@ton/crypto": "^3.2.0",
"@ton/core": "^0.59.0",
"@ton/crypto": "^3.3.0",
"@ton/sandbox": "^0.20.0",
"@ton/test-utils": "^0.4.2",
"@ton/ton": "^13.11.2",
"@ton/ton": "^15.1.0",
"@types/jest": "^29.5.12",
"@types/node": "^20.14.10",
"@wormhole-foundation/sdk-definitions": "^0.10.7",
Expand Down
117 changes: 113 additions & 4 deletions target_chains/ton/contracts/scripts/deployPyth.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,122 @@
import { toNano } from "@ton/core";
import { MainConfig } from "../wrappers/Main";
import { compile, NetworkProvider, sleep } from "@ton/blueprint";
import { DataSource } from "@pythnetwork/xc-admin-common";
import { HermesClient } from "@pythnetwork/hermes-client";
import { Main } from "../wrappers/Main";
import { compile, NetworkProvider } from "@ton/blueprint";
import {
GOVERNANCE_DATA_SOURCE,
GUARDIAN_SET_0,
MAINNET_UPGRADE_VAAS,
} from "../tests/utils/wormhole";
import { BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID } from "../tests/utils/pyth";

export async function run(provider: NetworkProvider) {
const main = provider.open(Main.createFromConfig({}, await compile("Main")));
const SINGLE_UPDATE_FEE = 1;
const DATA_SOURCES: DataSource[] = [
{
emitterChain: 26,
emitterAddress:
"e101faedac5851e32b9b23b5f9411a8c2bac4aae3ed4dd7b811dd1a72ea4aa71",
},
];

await main.sendDeploy(provider.sender(), toNano("0.05"));
const config: MainConfig = {
singleUpdateFee: SINGLE_UPDATE_FEE,
dataSources: DATA_SOURCES,
guardianSetIndex: 0,
guardianSet: GUARDIAN_SET_0,
chainId: 1,
governanceChainId: 1,
governanceContract:
"0000000000000000000000000000000000000000000000000000000000000004",
governanceDataSource: GOVERNANCE_DATA_SOURCE,
};

const main = provider.open(
Main.createFromConfig(config, await compile("Main"))
);

await main.sendDeploy(provider.sender(), toNano("0.005"));

await provider.waitForDeploy(main.address);

// run methods on `main`
console.log("Main contract deployed at:", main.address.toString());

// Call sendUpdateGuardianSet for each VAA
const currentGuardianSetIndex = await main.getCurrentGuardianSetIndex();
console.log(`Current guardian set index: ${currentGuardianSetIndex}`);

for (let i = currentGuardianSetIndex; i < MAINNET_UPGRADE_VAAS.length; i++) {
const vaa = MAINNET_UPGRADE_VAAS[i];
const vaaBuffer = Buffer.from(vaa, "hex");
await main.sendUpdateGuardianSet(provider.sender(), vaaBuffer);
console.log(
`Successfully updated guardian set ${i + 1} with VAA: ${vaa.slice(
0,
20
)}...`
);

// Wait for 30 seconds before checking the guardian set index
console.log("Waiting for 30 seconds before checking guardian set index...");
await sleep(30000);

// Verify the update
const newIndex = await main.getCurrentGuardianSetIndex();
if (newIndex !== i + 1) {
console.error(
`Failed to update guardian set. Expected index ${
i + 1
}, got ${newIndex}`
);
break;
}
}

console.log("Guardian set update process completed.");

// Initialize HermesClient
const hermesEndpoint = "https://hermes.pyth.network";
const hermesClient = new HermesClient(hermesEndpoint);

// Fetch latest price updates for BTC and ETH
const priceIds = [BTC_PRICE_FEED_ID, ETH_PRICE_FEED_ID];
const latestPriceUpdates = await hermesClient.getLatestPriceUpdates(
priceIds,
{ encoding: "hex" }
);
console.log("Hermes BTC price:", latestPriceUpdates.parsed?.[0].price);
console.log("Hermes ETH price:", latestPriceUpdates.parsed?.[1].price);
// Combine updates into a single buffer
const updateData = Buffer.from(latestPriceUpdates.binary.data[0], "hex");
console.log("Update data:", latestPriceUpdates.binary.data[0]);

const singleUpdateFee = await main.getSingleUpdateFee();
console.log("Single update fee:", singleUpdateFee);

// 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
const updateFee = await main.getUpdateFee(updateData);
console.log("Update fee:", updateFee);

await main.sendUpdatePriceFeeds(
provider.sender(),
updateData,
toNano(updateFee)
);
console.log("Price feeds updated successfully.");

console.log("Waiting for 30 seconds before checking price feeds...");
await sleep(30000);

// Query updated price feeds
const btcPrice = await main.getPriceUnsafe(BTC_PRICE_FEED_ID);
console.log(
`Updated BTC price: ${btcPrice.price}, publish time: ${btcPrice.publishTime}`
);

const ethPrice = await main.getPriceUnsafe(ETH_PRICE_FEED_ID);
console.log(
`Updated ETH price: ${ethPrice.price}, publish time: ${ethPrice.publishTime}`
);
}
21 changes: 18 additions & 3 deletions target_chains/ton/contracts/tests/Main.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Blockchain, SandboxContract, TreasuryContract } from "@ton/sandbox";
import { Cell, toNano } from "@ton/core";
import { Main } from "../wrappers/Main";
import { Main, MainConfig } from "../wrappers/Main";
import "@ton/test-utils";
import { compile } from "@ton/blueprint";

Expand All @@ -17,8 +17,23 @@ describe("Main", () => {

beforeEach(async () => {
blockchain = await Blockchain.create();

main = blockchain.openContract(Main.createFromConfig({}, code));
const config: MainConfig = {
singleUpdateFee: 0,
dataSources: [],
guardianSetIndex: 0,
guardianSet: [],
chainId: 0,
governanceChainId: 0,
governanceContract:
"0000000000000000000000000000000000000000000000000000000000000000",
governanceDataSource: {
emitterChain: 0,
emitterAddress:
"0000000000000000000000000000000000000000000000000000000000000000",
},
};

main = blockchain.openContract(Main.createFromConfig(config, code));

deployer = await blockchain.treasury("deployer");

Expand Down
46 changes: 1 addition & 45 deletions target_chains/ton/contracts/tests/utils.ts
Original file line number Diff line number Diff line change
@@ -1,56 +1,12 @@
import { DataSource } from "@pythnetwork/xc-admin-common";
import { Cell, Transaction, beginCell } from "@ton/core";
import { Cell, Transaction } from "@ton/core";
import { Buffer } from "buffer";

const GOVERNANCE_MAGIC = 0x5054474d;
const GOVERNANCE_MODULE = 1;
const AUTHORIZE_UPGRADE_CONTRACT_ACTION = 0;
const TARGET_CHAIN_ID = 1;

export function createCellChain(buffer: Buffer): Cell {
let chunks = bufferToChunks(buffer, 127);
let lastCell: Cell | null = null;
// Iterate through chunks in reverse order
for (let i = chunks.length - 1; i >= 0; i--) {
const chunk = chunks[i];
const cellBuilder = beginCell();
const buffer = Buffer.from(chunk);
cellBuilder.storeBuffer(buffer);

if (lastCell) {
cellBuilder.storeRef(lastCell);
}

lastCell = cellBuilder.endCell();
}

// lastCell will be the root cell of our chain
if (!lastCell) {
throw new Error("Failed to create cell chain");
}
return lastCell;
}

function bufferToChunks(
buff: Buffer,
chunkSizeBytes: number = 127
): Uint8Array[] {
const chunks: Uint8Array[] = [];
const uint8Array = new Uint8Array(
buff.buffer,
buff.byteOffset,
buff.byteLength
);

for (let offset = 0; offset < uint8Array.length; offset += chunkSizeBytes) {
const remainingBytes = Math.min(chunkSizeBytes, uint8Array.length - offset);
const chunk = uint8Array.subarray(offset, offset + remainingBytes);
chunks.push(chunk);
}

return chunks;
}

// Helper function to parse DataSource from a Cell
export function parseDataSource(cell: Cell): DataSource {
const slice = cell.beginParse();
Expand Down
7 changes: 7 additions & 0 deletions target_chains/ton/contracts/tests/utils/wormhole.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { DataSource } from "@pythnetwork/xc-admin-common";
import { beginCell, Cell, Dictionary } from "@ton/core";

export const GUARDIAN_SET_0 = ["0x58CC3AE5C097B213CE3C81979E1B9F9570746AA5"];
Expand Down Expand Up @@ -191,3 +192,9 @@ export const MAINNET_UPGRADE_VAAS = [
"01000000020d00ce45474d9e1b1e7790a2d210871e195db53a70ffd6f237cfe70e2686a32859ac43c84a332267a8ef66f59719cf91cc8df0101fd7c36aa1878d5139241660edc0010375cc906156ae530786661c0cd9aef444747bc3d8d5aa84cac6a6d2933d4e1a031cffa30383d4af8131e929d9f203f460b07309a647d6cd32ab1cc7724089392c000452305156cfc90343128f97e499311b5cae174f488ff22fbc09591991a0a73d8e6af3afb8a5968441d3ab8437836407481739e9850ad5c95e6acfcc871e951bc30105a7956eefc23e7c945a1966d5ddbe9e4be376c2f54e45e3d5da88c2f8692510c7429b1ea860ae94d929bd97e84923a18187e777aa3db419813a80deb84cc8d22b00061b2a4f3d2666608e0aa96737689e3ba5793810ff3a52ff28ad57d8efb20967735dc5537a2e43ef10f583d144c12a1606542c207f5b79af08c38656d3ac40713301086b62c8e130af3411b3c0d91b5b50dcb01ed5f293963f901fc36e7b0e50114dce203373b32eb45971cef8288e5d928d0ed51cd86e2a3006b0af6a65c396c009080009e93ab4d2c8228901a5f4525934000b2c26d1dc679a05e47fdf0ff3231d98fbc207103159ff4116df2832eea69b38275283434e6cd4a4af04d25fa7a82990b707010aa643f4cf615dfff06ffd65830f7f6cf6512dabc3690d5d9e210fdc712842dc2708b8b2c22e224c99280cd25e5e8bfb40e3d1c55b8c41774e287c1e2c352aecfc010b89c1e85faa20a30601964ccc6a79c0ae53cfd26fb10863db37783428cd91390a163346558239db3cd9d420cfe423a0df84c84399790e2e308011b4b63e6b8015010ca31dcb564ac81a053a268d8090e72097f94f366711d0c5d13815af1ec7d47e662e2d1bde22678113d15963da100b668ba26c0c325970d07114b83c5698f46097010dc9fda39c0d592d9ed92cd22b5425cc6b37430e236f02d0d1f8a2ef45a00bde26223c0a6eb363c8b25fd3bf57234a1d9364976cefb8360e755a267cbbb674b39501108db01e444ab1003dd8b6c96f8eb77958b40ba7a85fefecf32ad00b7a47c0ae7524216262495977e09c0989dd50f280c21453d3756843608eacd17f4fdfe47600001261025228ef5af837cb060bcd986fcfa84ccef75b3fa100468cfd24e7fadf99163938f3b841a33496c2706d0208faab088bd155b2e20fd74c625bb1cc8c43677a0163c53c409e0c5dfa000100000000000000000000000000000000000000000000000000000000000000046c5a054d7833d1e42000000000000000000000000000000000000000000000000000000000436f7265020000000000031358cc3ae5c097b213ce3c81979e1b9f9570746aa5ff6cb952589bde862c25ef4392132fb9d4a42157114de8460193bdf3a2fcf81f86a09765f4762fd1107a0086b32d7a0977926a205131d8731d39cbeb8c82b2fd82faed2711d59af0f2499d16e726f6b211b39756c042441be6d8650b69b54ebe715e234354ce5b4d348fb74b958e8966e2ec3dbd4958a7cd15e7caf07c4e3dc8e7c469f92c8cd88fb8005a2074a3bf913953d695260d88bc1aa25a4eee363ef0000ac0076727b35fbea2dac28fee5ccb0fea768eaf45ced136b9d9e24903464ae889f5c8a723fc14f93124b7c738843cbb89e864c862c38cddcccf95d2cc37a4dc036a8d232b48f62cdd4731412f4890da798f6896a3331f64b48c12d1d57fd9cbe7081171aa1be1d36cafe3867910f99c09e347899c19c38192b6e7387ccd768277c17dab1b7a5027c0b3cf178e21ad2e77ae06711549cfbb1f9c7a9d8096e85e1487f35515d02a92753504a8d75471b9f49edb6fbebc898f403e4773e95feb15e80c9a99c8348d",
"01000000030d03d4a37a6ff4361d91714730831e9d49785f61624c8f348a9c6c1d82bc1d98cadc5e936338204445c6250bb4928f3f3e165ad47ca03a5d63111168a2de4576856301049a5df10464ea4e1961589fd30fc18d1970a7a2ffaad617e56a0f7777f25275253af7d10a0f0f2494dc6e99fc80e444ab9ebbbee252ded2d5dcb50cbf7a54bb5a01055f4603b553b9ba9e224f9c55c7bca3da00abb10abd19e0081aecd3b352be061a70f79f5f388ebe5190838ef3cd13a2f22459c9a94206883b739c90b40d5d74640006a8fade3997f650a36e46bceb1f609edff201ab32362266f166c5c7da713f6a19590c20b68ed3f0119cb24813c727560ede086b3d610c2d7a1efa66f655bad90900080f5e495a75ea52241c59d145c616bfac01e57182ad8d784cbcc9862ed3afb60c0983ccbc690553961ffcf115a0c917367daada8e60be2cbb8b8008bac6341a8c010935ab11e0eea28b87a1edc5ccce3f1fac25f75b5f640fe6b0673a7cd74513c9dc01c544216cf364cc9993b09fda612e0cd1ced9c00fb668b872a16a64ebb55d27010ab2bc39617a2396e7defa24cd7c22f42dc31f3c42ffcd9d1472b02df8468a4d0563911e8fb6a4b5b0ce0bd505daa53779b08ff660967b31f246126ed7f6f29a7e000bdb6d3fd7b33bdc9ac3992916eb4aacb97e7e21d19649e7fa28d2dd6e337937e4274516a96c13ac7a8895da9f91948ea3a09c25f44b982c62ce8842b58e20c8a9000d3d1b19c8bb000856b6610b9d28abde6c35cb7705c6ca5db711f7be96d60eed9d72cfa402a6bfe8bf0496dbc7af35796fc768da51a067b95941b3712dce8ae1e7010ec80085033157fd1a5628fc0c56267469a86f0e5a66d7dede1ad4ce74ecc3dff95b60307a39c3bfbeedc915075070da30d0395def9635130584f709b3885e1bdc0010fc480eb9ee715a2d151b23722b48b42581d7f4001fc1696c75425040bfc1ffc5394fe418adb2b64bd3dc692efda4cc408163677dbe233b16bcdabb853a20843301118ee9e115e1a0c981f19d0772b850e666591322da742a9a12cce9f52a5665bd474abdd59c580016bee8aae67fdf39b315be2528d12eec3a652910e03cc4c6fa3801129d0d1e2e429e969918ec163d16a7a5b2c6729aa44af5dccad07d25d19891556a79b574f42d9adbd9e2a9ae5a6b8750331d2fccb328dd94c3bf8791ee1bfe85aa00661e99781981faea00010000000000000000000000000000000000000000000000000000000000000004fd4c6c55ec8dfd342000000000000000000000000000000000000000000000000000000000436f726502000000000004135893b5a76c3f739645648885bdccc06cd70a3cd3ff6cb952589bde862c25ef4392132fb9d4a42157114de8460193bdf3a2fcf81f86a09765f4762fd1107a0086b32d7a0977926a205131d8731d39cbeb8c82b2fd82faed2711d59af0f2499d16e726f6b211b39756c042441be6d8650b69b54ebe715e234354ce5b4d348fb74b958e8966e2ec3dbd4958a7cd15e7caf07c4e3dc8e7c469f92c8cd88fb8005a2074a3bf913953d695260d88bc1aa25a4eee363ef0000ac0076727b35fbea2dac28fee5ccb0fea768eaf45ced136b9d9e24903464ae889f5c8a723fc14f93124b7c738843cbb89e864c862c38cddcccf95d2cc37a4dc036a8d232b48f62cdd4731412f4890da798f6896a3331f64b48c12d1d57fd9cbe7081171aa1be1d36cafe3867910f99c09e347899c19c38192b6e7387ccd768277c17dab1b7a5027c0b3cf178e21ad2e77ae06711549cfbb1f9c7a9d8096e85e1487f35515d02a92753504a8d75471b9f49edb6fbebc898f403e4773e95feb15e80c9a99c8348d",
];

export const GOVERNANCE_DATA_SOURCE: DataSource = {
emitterChain: 1,
emitterAddress:
"0000000000000000000000000000000000000000000000000000000000000029",
};
Loading