Skip to content

Commit

Permalink
Fees: Update fee calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
imsk17 committed Jun 6, 2024
1 parent ac520ac commit bbb888c
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 28 deletions.
11 changes: 2 additions & 9 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,16 +146,9 @@ export interface GetApprovedTokenAmount {
*/
getApprovedAmount: (token: string, owner: string) => Promise<bigint>;
}
export interface CalculateCoinFees {
calculateCoinFees: (coin_name: string, amt: bigint) => Promise<bigint>;
}

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

export interface GetCoinPrice {
getCoinPrice: (coin_name: string) => Promise<bigint>;
export interface GetTxFee {
txFee: (targetChain: bigint, fromToken: string, targetToken: string) => Promise<bigint>;
}

export interface ChainName {
Expand Down
37 changes: 19 additions & 18 deletions src/chains/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@ import {
type TonClient,
} from "@ton/ton";
import type {
CalculateCoinFees,
CalculateDestinationTransactionFees,
ChainID,
ChainName,
GetBalance,
GetCoinPrice,
GetProvider,
GetTokenBalance,
GetTxFee,
NativeCoinName,
SendInstallment,
ValidateAddress,
Expand All @@ -34,9 +32,7 @@ export type TonHelper = GetBalance &
SendInstallment<Sender, string, TonGasArgs> &
ValidateAddress &
GetTokenBalance &
CalculateCoinFees &
CalculateDestinationTransactionFees &
GetCoinPrice &
GetTxFee &
ChainName &
NativeCoinName &
ChainID;
Expand All @@ -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(
Expand Down Expand Up @@ -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) => {
Expand Down
8 changes: 7 additions & 1 deletion src/chains/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import type {
GetBalance,
GetProvider,
GetTokenBalance,
GetTxFee,
NativeCoinName,
PreTransfer,
SendInstallment,
Expand All @@ -38,7 +39,7 @@ export type Web3Helper = GetBalance &
NativeCoinName &
AddressBook &
TokenInfo &
ChainID;
ChainID & GetTxFee;

export interface Web3Params {
provider: Provider;
Expand All @@ -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 {
Expand Down

0 comments on commit bbb888c

Please sign in to comment.