Skip to content

Commit

Permalink
add more functions
Browse files Browse the repository at this point in the history
  • Loading branch information
cctdaniel committed Oct 30, 2024
1 parent 8ff543a commit 9e32362
Show file tree
Hide file tree
Showing 6 changed files with 617 additions and 312 deletions.
2 changes: 2 additions & 0 deletions contract_manager/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@
"@pythnetwork/pyth-sdk-solidity": "workspace:^",
"@pythnetwork/pyth-starknet-js": "^0.2.1",
"@pythnetwork/pyth-sui-js": "workspace:*",
"@pythnetwork/pyth-ton-js": "workspace:*",
"@pythnetwork/solana-utils": "workspace:^",
"@pythnetwork/xc-admin-common": "workspace:*",
"@solana/web3.js": "^1.73.0",
"@sqds/mesh": "^1.0.6",
"@ton/crypto": "^3.3.0",
"@ton/ton": "^15.1.0",
"@types/yargs": "^17.0.32",
"aptos": "^1.5.0",
Expand Down
28 changes: 22 additions & 6 deletions contract_manager/src/chains.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import { TokenId } from "./token";
import { BN, Provider, Wallet, WalletUnlocked } from "fuels";
import { FUEL_ETH_ASSET_ID } from "@pythnetwork/pyth-fuel-js";
import { Contract, RpcProvider, Signer, ec, shortString } from "starknet";
import {
TonClient,
WalletContractV4,
ContractProvider,
Address,
} from "@ton/ton";
import { keyPairFromSecretKey } from "@ton/crypto";

export type ChainConfig = Record<string, string> & {
mainnet: boolean;
Expand Down Expand Up @@ -752,13 +759,21 @@ export class TonChain extends Chain {
super(id, mainnet, wormholeChainName, nativeToken);
}

async getProvider(): Promise<Provider> {
return await Provider.create(this.rpcUrl);
async getProvider(address: string): Promise<ContractProvider> {
const client = new TonClient({
endpoint: this.rpcUrl,
});
return client.provider(Address.parse(address));
}

async getWallet(privateKey: PrivateKey): Promise<WalletUnlocked> {
const provider = await this.getProvider();
return Wallet.fromPrivateKey(privateKey, provider);
async getWallet(privateKey: PrivateKey): Promise<WalletContractV4> {
const keyPair = keyPairFromSecretKey(Buffer.from(privateKey, "hex"));
const wallet = WalletContractV4.create({
publicKey: keyPair.publicKey,
workchain: 0,
});

return wallet;
}

/**
Expand Down Expand Up @@ -802,7 +817,8 @@ export class TonChain extends Chain {

async getAccountBalance(privateKey: PrivateKey): Promise<number> {
const wallet = await this.getWallet(privateKey);
const balance: BN = await wallet.getBalance(FUEL_ETH_ASSET_ID);
const provider = await this.getProvider(wallet.address.toString());
const balance = await wallet.getBalance(provider);
return Number(balance) / 10 ** 9;
}
}
74 changes: 47 additions & 27 deletions contract_manager/src/contracts/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,17 @@ import { WormholeContract } from "./wormhole";
import { PriceFeed, PriceFeedContract, PrivateKey, TxResult } from "../base";
import { TokenQty } from "../token";
import { DataSource } from "@pythnetwork/xc-admin-common";
import { Address, Contract, TonClient } from "@ton/ton";
import {
Address,
Contract,
OpenedContract,
TonClient,
WalletContractV4,
} from "@ton/ton";
import {
PythContract,
PYTH_CONTRACT_ADDRESS_TESTNET,
} from "@pythnetwork/pyth-ton-js";

export class TonWormholeContract extends WormholeContract {
static type = "TonWormholeContract";
Expand Down Expand Up @@ -112,6 +122,15 @@ export class TonPriceFeedContract extends PriceFeedContract {
return TonPriceFeedContract.type;
}

async getContract(): Promise<OpenedContract<PythContract>> {
const provider = await this.chain.getProvider(this.address);
const contract = provider.open(
PythContract.createFromAddress(Address.parse(this.address))
);

return contract;
}

async getTotalFee(): Promise<TokenQty> {
// const contract = await this.getContract();
// const balance = await contract.getBalance();
Expand All @@ -123,33 +142,35 @@ export class TonPriceFeedContract extends PriceFeedContract {

async getLastExecutedGovernanceSequence(): Promise<number> {
// const contract = await this.getContract();
// const result = await contract.get("get_last_executed_governance_sequence");
// const result = await contract.get
// return Number(result);
return 1;
}

async getPriceFeed(feedId: string): Promise<PriceFeed | undefined> {
// const contract = await this.getContract();
// try {
// const result = await contract.get("get_price_unsafe", [feedId]);
// return {
// price: {
// price: result.price.toString(),
// conf: result.conf.toString(),
// expo: result.expo.toString(),
// publishTime: result.publishTime.toString(),
// },
// emaPrice: {
// price: result.emaPrice.price.toString(),
// conf: result.emaPrice.conf.toString(),
// expo: result.emaPrice.expo.toString(),
// publishTime: result.emaPrice.publishTime.toString(),
// },
// };
// } catch (e) {
// return undefined;
// }
return undefined;
const contract = await this.getContract();
const feedIdWithPrefix = `0x${feedId}`;
try {
const price = await contract.getPriceUnsafe(feedIdWithPrefix);
const emaPrice = await contract.getEmaPriceUnsafe(feedIdWithPrefix);
return {
price: {
price: price.price.toString(),
conf: price.conf.toString(),
expo: price.expo.toString(),
publishTime: price.publishTime.toString(),
},
emaPrice: {
price: emaPrice.price.toString(),
conf: emaPrice.conf.toString(),
expo: emaPrice.expo.toString(),
publishTime: emaPrice.publishTime.toString(),
},
};
} catch (e) {
console.error(e);
return undefined;
}
}

async getValidTimePeriod(): Promise<number> {
Expand All @@ -164,11 +185,10 @@ export class TonPriceFeedContract extends PriceFeedContract {
}

async getBaseUpdateFee() {
// const pythContract = await this.getContract();
// const amount = (await pythContract.functions.single_update_fee().get())
// .value;
const contract = await this.getContract();
const amount = await contract.getSingleUpdateFee();
return {
amount: "0",
amount: amount.toString(),
denom: this.chain.getNativeToken(),
};
}
Expand Down
2 changes: 2 additions & 0 deletions contract_manager/src/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
FuelChain,
GlobalChain,
SuiChain,
TonChain,
} from "./chains";
import {
AptosPriceFeedContract,
Expand Down Expand Up @@ -83,6 +84,7 @@ export class Store {
[AptosChain.type]: AptosChain,
[FuelChain.type]: FuelChain,
[StarknetChain.type]: StarknetChain,
[TonChain.type]: TonChain,
};

this.getYamlFiles(`${this.path}/chains/`).forEach((yamlFile) => {
Expand Down
2 changes: 1 addition & 1 deletion contract_manager/store/chains/TonChains.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
- id: ton_testnet
wormholeChainName: ton_testnet
mainnet: false
rpcUrl: https://testnet.tonapi.io
rpcUrl: https://testnet.toncenter.com/api/v2/jsonRPC
type: TonChain
Loading

0 comments on commit 9e32362

Please sign in to comment.