Skip to content

Commit

Permalink
Factory: Implement Get ProtocolFee in USD
Browse files Browse the repository at this point in the history
  • Loading branch information
rsbkmr committed Jun 27, 2024
1 parent 695f6c7 commit daad428
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 22 deletions.
4 changes: 4 additions & 0 deletions src/chains/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export interface GetProtocolFeeInUSD {
protocolFeeInUSD: () => Promise<bigint>;
}

export interface Decimals {
decimals: () => number;
}

/**
* Represents an interface for getting the approved token amount for a particular user.
*/
Expand Down
48 changes: 28 additions & 20 deletions src/chains/ton.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import {
import type {
ChainID,
ChainName,
Decimals,
FetchTxInfo,
GetBalance,
GetBridgeAddress,
GetEmmetHashFromTx,
GetEstimatedTime,
GetProtocolFeeInUSD,
GetProvider,
GetTokenBalance,
GetTxFee,
Expand Down Expand Up @@ -49,7 +49,7 @@ export type TonHelper = GetBalance &
TokenInfo &
GetEstimatedTime &
GetBridgeAddress &
GetProtocolFeeInUSD;
Decimals;

export interface TonParams {
client: TonClient;
Expand Down Expand Up @@ -89,7 +89,7 @@ export function tonHandler({
return (await bridge.send(
sender,
{
value: amount + gasArgs.value
value: amount + gasArgs.value,
},
{
$$type: 'FreezeTon',
Expand Down Expand Up @@ -129,15 +129,15 @@ export function tonHandler({

return (await jtw.send(
signer,
{...gasArgs },
{ ...gasArgs },
{
$$type: 'JettonTransfer',
amount: amt,
custom_payload: null,
query_id: 0n,
destination: burner,
forward_payload: beginCell()
.storeAddress(bridge)
.storeAddress(bridge)
.storeUint(cid, 64) // Target Chain
.storeRef(
beginCell()
Expand Down Expand Up @@ -240,18 +240,26 @@ export function tonHandler({
);
};

async function isWrappedToken(targetChain: bigint, fromTokenId: bigint, targetTokenId: bigint) {
async function isWrappedToken(
targetChain: bigint,
fromTokenId: bigint,
targetTokenId: bigint
) {
const steps = await bridgeReader.getCrossChainStrategy();
const strategy = steps.get(targetChain)?.i.get(fromTokenId)?.i.get(targetTokenId);
if (!strategy) return false
const strategy = steps
.get(targetChain)
?.i.get(fromTokenId)
?.i.get(targetTokenId);
if (!strategy) return false;
for (let i = 0; i < strategy.local_steps.size; i++) {
const strat = strategy.local_steps.steps.get(BigInt(i));
if (strat === 3n) return true
if (strat === 3n) return true;
}
return false
return false;
}

return {
decimals: () => 9,
estimateTime: () => Promise.resolve(undefined),
async emmetHashFromtx(hash) {
const txs = await client.getTransactions(bridge, {
Expand All @@ -277,8 +285,11 @@ export function tonHandler({
nativeCoin: () => 'TON',
chainName: () => chainName,
async txFee(tc) {
const fee = await bridgeReader.getProtocolFee() + ((await bridgeReader.getChainFees()).get(tc) ?? raise("Chain fees not configured for this chain"))
return fee
const fee =
(await bridgeReader.getProtocolFee()) +
((await bridgeReader.getChainFees()).get(tc) ??
raise('Chain fees not configured for this chain'));
return fee;
},
async token(symbol) {
const tokens = await bridgeReader.getTokens();
Expand Down Expand Up @@ -306,9 +317,6 @@ export function tonHandler({
protocolFee() {
return bridgeReader.getProtocolFee();
},
async protocolFeeInUSD() {
return 0n;
},
async txInfo(hash) {
try {
const tx = await client.getTransactions(bridge, {
Expand Down Expand Up @@ -343,8 +351,8 @@ export function tonHandler({
) => {
const lastBridgeTxHash = await getLastBridgeTxHashInBase64();
const bc = client.open(Bridge.fromAddress(bridge));
const fsid = BigInt(`0x${sha256_sync(fromSymbol).toString("hex")}`);
const tid = BigInt(`0x${sha256_sync(targetSymbol).toString("hex")}`);
const fsid = BigInt(`0x${sha256_sync(fromSymbol).toString('hex')}`);
const tid = BigInt(`0x${sha256_sync(targetSymbol).toString('hex')}`);
const isWrapped = await isWrappedToken(cid, fsid, tid);
const gs =
fee !== undefined
Expand All @@ -355,7 +363,7 @@ export function tonHandler({
value:
(await bridgeReader.getProtocolFee()) +
((await bridgeReader.getChainFees()).get(cid) ??
raise("Chain fees not configured for this chain")),
raise('Chain fees not configured for this chain')),
};
if (tid === nativeTokenId) {
await transferTon(bc, signer, destAddress, targetSymbol, cid, amt, gs);
Expand All @@ -368,7 +376,7 @@ export function tonHandler({
cid,
amt,
destAddress,
gs,
gs
);
} else {
await transferJetton(
Expand All @@ -379,7 +387,7 @@ export function tonHandler({
cid,
amt,
destAddress,
gs,
gs
);
}

Expand Down
5 changes: 4 additions & 1 deletion src/chains/web3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import type {
AddressBook,
ChainID,
ChainName,
Decimals,
FetchTxInfo,
GetApprovedTokenAmount,
GetBalance,
Expand Down Expand Up @@ -51,7 +52,8 @@ export type Web3Helper = GetBalance &
GetEmmetHashFromTx &
GetEstimatedTime &
GetBridgeAddress &
GetProtocolFeeInUSD;
GetProtocolFeeInUSD &
Decimals;

export interface Web3Params {
provider: Provider;
Expand Down Expand Up @@ -142,6 +144,7 @@ export async function web3Helper({
symbol: token.symbol,
};
},
decimals: () => 18,
nativeCoin: () => nativeCoin,
chainName: () => chainName,
preTransfer: async (signer, tid, spender, amt, gasArgs) => {
Expand Down
7 changes: 7 additions & 0 deletions src/factory/factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,5 +195,12 @@ export async function ChainFactoryBuilder(
getPriceDecimals(symbol) {
return emmetData.getPriceDecimals(symbol);
},
async getProtocolFeeInUSD(chain) {
const tp = Number(await emmetData.getTokenPrice(chain.nativeCoin()));
const td = Number(await emmetData.getPriceDecimals(chain.nativeCoin()));
const pf = Number(await chain.protocolFee());
const cd = chain.decimals();
return Number(((pf * tp) / 10 ** (cd + td)).toFixed(2));
},
};
}
12 changes: 11 additions & 1 deletion src/factory/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import type { Web3Helper, Web3Params } from '../../chains/web3';
import type { TonHelper, TonParams } from '../../chains/ton';
import type { GetTxFee, PreTransfer, SendInstallment } from '../../chains';
import type {
Decimals,
GetTxFee,
NativeCoinName,
PreTransfer,
ProtocolFee,
SendInstallment,
} from '../../chains';
import type { JsonRpcProvider } from 'ethers';

export type EvmMeta = [Web3Helper, Web3Params];
Expand Down Expand Up @@ -100,6 +107,9 @@ export interface ChainFactory {
getTxCount: () => Promise<bigint>;
getTokenPrice: (symbol: string) => Promise<bigint>;
getPriceDecimals: (symbol: string) => Promise<bigint>;
getProtocolFeeInUSD: (
chain: ProtocolFee & NativeCoinName & Decimals
) => Promise<number>;
}

export interface Transaction {
Expand Down

0 comments on commit daad428

Please sign in to comment.