diff --git a/src/chains/index.ts b/src/chains/index.ts index 40b42df..ebfb04f 100644 --- a/src/chains/index.ts +++ b/src/chains/index.ts @@ -146,16 +146,9 @@ export interface GetApprovedTokenAmount { */ getApprovedAmount: (token: string, owner: string) => Promise; } -export interface CalculateCoinFees { - calculateCoinFees: (coin_name: string, amt: bigint) => Promise; -} - -export interface CalculateDestinationTransactionFees { - calculateTransactionFees: (chain_name: string) => Promise; -} -export interface GetCoinPrice { - getCoinPrice: (coin_name: string) => Promise; +export interface GetTxFee { + txFee: (targetChain: bigint, fromToken: string, targetToken: string) => Promise; } export interface ChainName { diff --git a/src/chains/ton.ts b/src/chains/ton.ts index 6acda4e..b814bbc 100644 --- a/src/chains/ton.ts +++ b/src/chains/ton.ts @@ -9,14 +9,12 @@ import { type TonClient, } from "@ton/ton"; import type { - CalculateCoinFees, - CalculateDestinationTransactionFees, ChainID, ChainName, GetBalance, - GetCoinPrice, GetProvider, GetTokenBalance, + GetTxFee, NativeCoinName, SendInstallment, ValidateAddress, @@ -34,9 +32,7 @@ export type TonHelper = GetBalance & SendInstallment & ValidateAddress & GetTokenBalance & - CalculateCoinFees & - CalculateDestinationTransactionFees & - GetCoinPrice & + GetTxFee & ChainName & NativeCoinName & ChainID; @@ -60,6 +56,7 @@ export function tonHandler({ chainName, chainId, }: TonParams): TonHelper { + //@ts-ignore TODO: Use it. const oracleContract = client.open(Oracle.fromAddress(oracle)); const bridgeReader = client.open(Bridge.fromAddress(bridge)); async function transferTon( @@ -205,19 +202,23 @@ export function tonHandler({ id: () => Promise.resolve(chainId), nativeCoin: () => "TON", 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; + txFee(coin_name) { + throw new Error(`Unimplemented ${coin_name}`) }, - calculateTransactionFees: async (chain_name) => - oracleContract.getCalculateTransactionFees(chain_name), - calculateCoinFees: async (coin_name, amt) => - oracleContract.getCalculateCoinFees(coin_name, amt), + // 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) => + // oracleContract.getCalculateCoinFees(coin_name, amt), balance: (addr) => client.getBalance(Address.parse(addr)), provider: () => client, validateAddress: (addr) => { diff --git a/src/chains/web3.ts b/src/chains/web3.ts index 1bc0cd2..2e1de2a 100644 --- a/src/chains/web3.ts +++ b/src/chains/web3.ts @@ -12,6 +12,7 @@ import type { GetBalance, GetProvider, GetTokenBalance, + GetTxFee, NativeCoinName, PreTransfer, SendInstallment, @@ -38,7 +39,7 @@ export type Web3Helper = GetBalance & NativeCoinName & AddressBook & TokenInfo & - ChainID; + ChainID & GetTxFee; export interface Web3Params { provider: Provider; @@ -63,6 +64,11 @@ export async function web3Helper({ async address(contr) { return await addrBook.get(contr); }, + async txFee(targetChainId, fromToken, targetToken) { + const protocolFee = await data.protocolFee() + const ffc = await data.getForeignFeeCompensation(targetChainId, fromToken, targetToken) + return protocolFee.usdEquivalent + ffc + }, async token(symbol) { const token = await data.getToken(symbol); return {