Skip to content

Commit

Permalink
Helpers: Chains: Implement getting coin price and chain names
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Apr 15, 2024
1 parent 52ac506 commit bcf8173
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
8 changes: 8 additions & 0 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,4 +110,12 @@ export interface CalculateCoinFees {

export interface CalculateDestinationTransactionFees {
calculateTransactionFees: (chain_name: string) => Promise<bigint>;
}

export interface GetCoinPrice {
getCoinPrice: (coin_name: string) => Promise<bigint>
}

export interface ChainName {
chainName: () => string;
}
18 changes: 17 additions & 1 deletion src/chains/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ import {
import type {
CalculateCoinFees,
CalculateDestinationTransactionFees,
ChainName,
GetBalance,
GetCoinPrice,
GetProvider,
GetTokenBalance,
SendInstallment,
Expand All @@ -31,14 +33,17 @@ export type TonHelper = GetBalance &
ValidateAddress &
GetTokenBalance &
CalculateCoinFees &
CalculateDestinationTransactionFees;
CalculateDestinationTransactionFees &
GetCoinPrice &
ChainName;

export interface TonParams {
client: TonClient;
bridge: Address;
nativeTokenId: bigint;
oracle: Address;
burner: Address;
chainName: string;
}

export function tonHandler({
Expand All @@ -47,6 +52,7 @@ export function tonHandler({
nativeTokenId,
oracle,
burner,
chainName,
}: TonParams): TonHelper {
const oracleContract = client.open(Oracle.fromAddress(oracle));
const bridgeReader = client.open(Bridge.fromAddress(bridge));
Expand Down Expand Up @@ -177,6 +183,16 @@ export function tonHandler({
}

return {
chainName: () => chainName,
getCoinPrice:async (coin) => {
const pf = await oracleContract.getPriceFeed();
const cid = BigInt(`0x${sha256_sync(coin).toString('hex')}`)
const data = pf.get(cid)
if (!data) {
throw new Error(`No price info found for symbol ${coin}, id ${cid}`)
}
return data.price
} ,
calculateTransactionFees: async (chain_name) =>
oracleContract.getCalculateTransactionFees(chain_name),
calculateCoinFees: async (coin_name, amt) =>
Expand Down
9 changes: 8 additions & 1 deletion src/chains/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import {
import type {
CalculateCoinFees,
CalculateDestinationTransactionFees,
ChainName,
GetApprovedTokenAmount,
GetBalance,
GetCoinPrice,
GetProvider,
GetTokenBalance,
PreTransfer,
Expand All @@ -30,22 +32,27 @@ export type Web3Helper = GetBalance &
GetApprovedTokenAmount &
PreTransfer<Signer, PayableOverrides> &
CalculateCoinFees &
CalculateDestinationTransactionFees;
CalculateDestinationTransactionFees & GetCoinPrice &
ChainName;

export interface Web3Params {
provider: Provider;
contract: string;
oracle: string;
chainName: string;
}

export function web3Helper({
provider,
contract,
oracle,
chainName
}: Web3Params): Web3Helper {
const bridge = FTBridge__factory.connect(contract, provider);
const orac = IEmmetFeeOracle__factory.connect(oracle, provider);
return {
chainName: () => chainName,
getCoinPrice: (c) => orac.getCoinPrice(c),
calculateCoinFees: (coinName, amt) => orac.calculateCoinFees(coinName, amt),
calculateTransactionFees: async (destChain) =>
orac.calculateTransactionFee(destChain),
Expand Down
2 changes: 2 additions & 0 deletions src/factory/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ export namespace ChainFactoryConfigs {
burner: Address.parse(
"EQBtE7sxSqbDZwuWhxxQRzSZZ3UAm8j4mhR25iWS2xfEmZ6D",
),
chainName: "tonTestnet"
},
polygonParams: {
contract: ethers.getAddress(
"0x379388Ae42f2EeE0CD30B89541CFaf90843F8762",
),
provider: new JsonRpcProvider(TestNetRpcUri.POLYGON),
oracle: ethers.getAddress("0x95DB799744A5b36D6E7BE9AD3b451dBC5b8De673"),
chainName: "polygon"
},
} satisfies Partial<ChainParams>;
}
Expand Down

0 comments on commit bcf8173

Please sign in to comment.