diff --git a/src/chains/index.ts b/src/chains/index.ts index da723f2..b66a352 100644 --- a/src/chains/index.ts +++ b/src/chains/index.ts @@ -51,7 +51,7 @@ export interface SendInstallment { tokenSymbol: string, destAddress: string, fee?: bigint, - gasArgs?: GasArgs + gasArgs?: GasArgs, ) => Promise<{ hash: string; tx: Ret }>; } @@ -72,21 +72,21 @@ export interface GetTokenBalance { } export type AddressBookKeys = - | 'GasFees' - | 'EmmetTokenVault' - | 'EmmetData' - | 'CCTPHelper' - | 'HashHelper' - | 'SignatureVerifier' - | 'LiquidityPoolHelper' - | 'EmmetBridge' - | 'AddressStorageHelper' - | 'WTON' // Wrapped Token - | 'EMMET' // Token - | 'TON/USD' // price feed - | 'BNB/USD' // price feed - | 'MATIC/USD' // Price Feed - | 'EmmetMultisig'; + | "GasFees" + | "EmmetTokenVault" + | "EmmetData" + | "CCTPHelper" + | "HashHelper" + | "SignatureVerifier" + | "LiquidityPoolHelper" + | "EmmetBridge" + | "AddressStorageHelper" + | "WTON" // Wrapped Token + | "EMMET" // Token + | "TON/USD" // price feed + | "BNB/USD" // price feed + | "MATIC/USD" // Price Feed + | "EmmetMultisig"; export interface AddressBook { address: (contr: AddressBookKeys) => Promise; @@ -132,7 +132,7 @@ export interface PreTransfer { token: string, spender: string, amount: bigint, - gasArgs: GasArgs + gasArgs: GasArgs, ) => Promise; } @@ -157,7 +157,7 @@ export interface GetApprovedTokenAmount { getApprovedAmount: ( token: string, owner: string, - spender: string + spender: string, ) => Promise; } @@ -165,7 +165,7 @@ export interface GetTxFee { txFee: ( targetChain: bigint, fromToken: string, - targetToken: string + targetToken: string, ) => Promise; } @@ -198,7 +198,7 @@ export interface GetEstimatedTime { estimateTime( targetChain: bigint, fromToken: string, - targetToken: string + targetToken: string, ): Promise; } diff --git a/src/chains/ton.ts b/src/chains/ton.ts index 0b9fc3f..7d536d8 100644 --- a/src/chains/ton.ts +++ b/src/chains/ton.ts @@ -7,7 +7,7 @@ import { type Sender, toNano, type TonClient, -} from '@ton/ton'; +} from "@ton/ton"; import type { ChainID, ChainName, @@ -25,12 +25,12 @@ import type { SendInstallment, TokenInfo, ValidateAddress, -} from '.'; -import { Bridge, loadSentInstallment } from '../contracts/ton'; -import { sha256_sync } from '@ton/crypto'; -import { Oracle } from '../contracts/ton/oracle'; -import { WrappedJetton } from '../contracts/ton/jetton-master'; -import { WrappedJettonWallet } from '../contracts/ton/jetton-wallet'; +} from "."; +import { Bridge, loadSentInstallment } from "../contracts/ton"; +import { sha256_sync } from "@ton/crypto"; +import { Oracle } from "../contracts/ton/oracle"; +import { WrappedJetton } from "../contracts/ton/jetton-master"; +import { WrappedJettonWallet } from "../contracts/ton/jetton-wallet"; export type TonGasArgs = { value: bigint; bounce?: boolean | null | undefined }; @@ -75,7 +75,7 @@ export function tonHandler({ const bridgeReader = client.open(Bridge.fromAddress(bridge)); async function getLastBridgeTxHashInBase64() { const txns = await client.getTransactions(bridge, { limit: 1 }); - return txns[0].hash().toString('base64'); + return txns[0].hash().toString("base64"); } async function transferTon( bridge: OpenedContract, @@ -84,7 +84,7 @@ export function tonHandler({ targetTkn: string, chainId: bigint, amount: bigint, - gasArgs: TonGasArgs + gasArgs: TonGasArgs, ): Promise { return (await bridge.send( sender, @@ -92,19 +92,19 @@ export function tonHandler({ value: amount + gasArgs.value, }, { - $$type: 'FreezeTon', + $$type: "FreezeTon", amount: amount, target_chain: BigInt(chainId), to: beginCell().storeStringRefTail(to).endCell(), from_token: beginCell() - .storeInt(toKey('TON'), 256) - .storeStringRefTail('TON') + .storeInt(toKey("TON"), 256) + .storeStringRefTail("TON") .endCell(), to_token: beginCell() .storeInt(toKey(targetTkn), 256) .storeStringRefTail(targetTkn) .endCell(), - } + }, )) as unknown as Promise; } @@ -115,7 +115,7 @@ export function tonHandler({ amt: bigint, destAddress: string, cid: bigint, - gasArgs: TonGasArgs + gasArgs: TonGasArgs, ): Promise => { const tid = toKey(fromToken); const wtd = await bridgeReader.getTokens(); @@ -123,15 +123,15 @@ export function tonHandler({ const jt = client.open(WrappedJetton.fromAddress(wt.address)); const jtw = client.open( WrappedJettonWallet.fromAddress( - await jt.getGetWalletAddress(signer.address!) - ) + await jt.getGetWalletAddress(signer.address!), + ), ); return (await jtw.send( signer, { ...gasArgs }, { - $$type: 'JettonTransfer', + $$type: "JettonTransfer", amount: amt, custom_payload: null, query_id: 0n, @@ -143,19 +143,19 @@ export function tonHandler({ beginCell() .storeUint(toKey(fromToken), 256) .storeStringRefTail(fromToken) - .asCell() + .asCell(), ) .storeRef(beginCell().storeStringRefTail(destAddress).asCell()) .storeRef( beginCell() .storeUint(toKey(targetToken), 256) .storeStringRefTail(targetToken) - .asCell() + .asCell(), ) .endCell(), - forward_ton_amount: toNano('0.40'), + forward_ton_amount: toNano("0.40"), response_destination: bridge, - } + }, )) as unknown as Promise; }; const transferJettonToBridge = async ( @@ -165,7 +165,7 @@ export function tonHandler({ target_chain: bigint, destAddress: string, amt: bigint, - gasArgs: TonGasArgs + gasArgs: TonGasArgs, ) => { const tid = toKey(fromToken); const ntd = await bridgeReader.getTokens(); @@ -173,14 +173,14 @@ export function tonHandler({ const jt = client.open(WrappedJetton.fromAddress(wt.address)); const jtw = client.open( WrappedJettonWallet.fromAddress( - await jt.getGetWalletAddress(signer.address!) - ) + await jt.getGetWalletAddress(signer.address!), + ), ); return (await jtw.send( signer, { ...gasArgs }, { - $$type: 'JettonTransfer', + $$type: "JettonTransfer", amount: amt, custom_payload: null, destination: bridge, @@ -190,20 +190,20 @@ export function tonHandler({ beginCell() .storeUint(toKey(fromToken), 256) .storeStringRefTail(fromToken) - .asCell() + .asCell(), ) .storeRef(beginCell().storeStringRefTail(destAddress).asCell()) .storeRef( beginCell() .storeUint(toKey(targetToken), 256) .storeStringRefTail(targetToken) - .asCell() + .asCell(), ) .endCell(), - forward_ton_amount: toNano('0.40'), + forward_ton_amount: toNano("0.40"), query_id: 0n, response_destination: bridge, - } + }, )) as unknown as Promise; }; @@ -215,7 +215,7 @@ export function tonHandler({ chainId: bigint, amount: bigint, destAddress: string, - gasArgs: TonGasArgs + gasArgs: TonGasArgs, ): Promise => { if (to.toString() === burner.toString()) { return await transferJettonToBurner( @@ -225,7 +225,7 @@ export function tonHandler({ amount, destAddress, chainId, - gasArgs + gasArgs, ); } @@ -236,14 +236,14 @@ export function tonHandler({ chainId, destAddress, amount, - gasArgs + gasArgs, ); }; async function isWrappedToken( targetChain: bigint, fromTokenId: bigint, - targetTokenId: bigint + targetTokenId: bigint, ) { const steps = await bridgeReader.getCrossChainStrategy(); const strategy = steps @@ -276,25 +276,25 @@ export function tonHandler({ } } } - throw new Error('No send installment found'); + throw new Error("No send installment found"); }, id: () => Promise.resolve(chainId), async bridge() { return await bridge.toString(); }, - nativeCoin: () => 'TON', + 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')); + raise("Chain fees not configured for this chain")); return fee; }, async token(symbol) { const tokens = await bridgeReader.getTokens(); const qToken = tokens.get(toKey(symbol)); - if (!qToken) throw new Error('No Such Token Found in Storage'); + if (!qToken) throw new Error("No Such Token Found in Storage"); return { address: qToken.address.toString(), decimals: qToken.decimals, @@ -347,12 +347,12 @@ export function tonHandler({ fromSymbol, targetSymbol, destAddress, - fee + fee, ) => { 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 @@ -363,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); @@ -376,7 +376,7 @@ export function tonHandler({ cid, amt, destAddress, - gs + gs, ); } else { await transferJetton( @@ -387,19 +387,19 @@ export function tonHandler({ cid, amt, destAddress, - gs + gs, ); } let foundTx = false; - let hash = ''; + let hash = ""; let retries = 0; while (!foundTx && retries < 10) { await new Promise((e) => setTimeout(e, 2000)); const latestTx = ( await client.getTransactions(bridge, { limit: 1 }) )[0]; - if (latestTx.hash().toString('base64') === lastBridgeTxHash) { + if (latestTx.hash().toString("base64") === lastBridgeTxHash) { await new Promise((e) => setTimeout(e, 10000)); retries++; continue; @@ -407,8 +407,8 @@ export function tonHandler({ const txs = await client.getTransactions(bridge, { limit: 2 }); for (const tx of txs) { for (let i = 0; i < tx.outMessages.size; i++) { - const msg = tx.outMessages.get(i) ?? raise('Unreachable'); - if (tx.hash().toString('base64') === lastBridgeTxHash) { + const msg = tx.outMessages.get(i) ?? raise("Unreachable"); + if (tx.hash().toString("base64") === lastBridgeTxHash) { await new Promise((e) => setTimeout(e, 10000)); continue; } @@ -419,7 +419,7 @@ export function tonHandler({ const emmethash = log.tx_hash; const txn = (await bridgeReader.getOutgoing()).get(emmethash) ?? - raise('Unreachable'); + raise("Unreachable"); if ( destAddress === txn.to.asSlice().loadStringRefTail() && @@ -427,7 +427,7 @@ export function tonHandler({ txn.from_token.asSlice().loadStringRefTail() === fromSymbol ) { foundTx = true; - hash = tx.hash().toString('hex'); + hash = tx.hash().toString("hex"); } } } @@ -443,7 +443,7 @@ export function tonHandler({ } const toKey = (key: string) => { - return BigInt(`0x${sha256_sync(key).toString('hex')}`); + return BigInt(`0x${sha256_sync(key).toString("hex")}`); }; export function raise(msg: string): never { throw new Error(msg); diff --git a/src/chains/web3.ts b/src/chains/web3.ts index 3f78ec2..19ba70f 100644 --- a/src/chains/web3.ts +++ b/src/chains/web3.ts @@ -3,7 +3,7 @@ import { isAddress, type Provider, type Signer, -} from 'ethers'; +} from "ethers"; import type { AddressBook, ChainID, @@ -25,14 +25,14 @@ import type { SendInstallment, TokenInfo, ValidateAddress, -} from '.'; +} from "."; import { EmmetAddressBook__factory, EmmetBridge__factory, EmmetData__factory, WrappedERC20__factory, -} from '@emmet-contracts/web3'; -import type { PayableOverrides } from '@emmet-contracts/web3/dist/common'; +} from "@emmet-contracts/web3"; +import type { PayableOverrides } from "@emmet-contracts/web3/dist/common"; export type Web3Helper = GetBalance & GetProvider & @@ -69,8 +69,8 @@ export async function web3Helper({ nativeCoin, }: Web3Params): Promise { const addrBook = EmmetAddressBook__factory.connect(addressBook, provider); - const bridgeAddr = await addrBook.get('EmmetBridge'); - const emmetData = await addrBook.get('EmmetData'); + const bridgeAddr = await addrBook.get("EmmetBridge"); + const emmetData = await addrBook.get("EmmetData"); const bridge = EmmetBridge__factory.connect(bridgeAddr, provider); const data = EmmetData__factory.connect(emmetData, provider); return { @@ -86,18 +86,18 @@ export async function web3Helper({ const ffc = await data.getForeignFeeCompensation( targetChainId, fromToken, - targetToken + targetToken, ); return protocolFee + ffc; }, async txInfo(hash) { - if (hash === '') { + if (hash === "") { return { timestamp: 0n, value: 0n, }; } - if (!hash.startsWith('0x')) { + if (!hash.startsWith("0x")) { //biome-ignore lint/style/noParameterAssign: ignore hash = `0x${hash}`; } @@ -122,8 +122,8 @@ export async function web3Helper({ if (!receipt) throw new Error(`No receipt found for tx hash: ${hash}`); const log = receipt.logs.find((e) => e.topics.includes( - bridge.interface.getEvent('SendInstallment').topicHash - ) + bridge.interface.getEvent("SendInstallment").topicHash, + ), ); if (!log) throw new Error(`No send installment log found for tx hash: ${hash}`); @@ -161,7 +161,7 @@ export async function web3Helper({ getApprovedAmount: async (tid, owner, spender) => await WrappedERC20__factory.connect(tid, provider).allowance( owner, - spender + spender, ), balance: (addr) => provider.getBalance(addr), provider: () => provider, @@ -169,7 +169,7 @@ export async function web3Helper({ const ts = await data.getCrossChainTokenStrategy( targetChain, fromToken, - targetToken + targetToken, ); const localSteps = ts[0]; const foreignSteps = ts[1]; diff --git a/src/factory/factory.ts b/src/factory/factory.ts index b81e4ab..d7cd77c 100644 --- a/src/factory/factory.ts +++ b/src/factory/factory.ts @@ -2,10 +2,10 @@ import { EmmetAddressBook__factory, EmmetData__factory, EmmetMultisig__factory, -} from '@emmet-contracts/web3'; -import { tonHandler } from '../chains/ton'; -import { web3Helper } from '../chains/web3'; -import { Chain, type ChainFactory } from './types'; +} from "@emmet-contracts/web3"; +import { tonHandler } from "../chains/ton"; +import { web3Helper } from "../chains/web3"; +import { Chain, type ChainFactory } from "./types"; import type { ChainInfo, @@ -13,8 +13,8 @@ import type { ChainParams, HelperMap, ParamMap, -} from './types'; -import { ChainIDToDomain, type SupportedChainID } from '../explorer-utils'; +} from "./types"; +import { ChainIDToDomain, type SupportedChainID } from "../explorer-utils"; function mapNonceToParams(chainParams: Partial): ParamMap { const cToP: ParamMap = new Map(); @@ -30,50 +30,50 @@ export const CHAIN_INFO: ChainInfo = new Map(); CHAIN_INFO.set(Chain.POLYGON, { constructor: web3Helper, decimals: 18, - name: 'Polygon', + name: "Polygon", nonce: Chain.POLYGON, }); CHAIN_INFO.set(Chain.BSC, { constructor: web3Helper, decimals: 18, - name: 'BSC', + name: "BSC", nonce: Chain.BSC, }); CHAIN_INFO.set(Chain.ETHEREUM, { constructor: web3Helper, decimals: 18, - name: 'Ethereum', + name: "Ethereum", nonce: Chain.ETHEREUM, }); CHAIN_INFO.set(Chain.TON, { decimals: 18, - name: 'Ton', + name: "Ton", nonce: Chain.TON, constructor: async (...args) => tonHandler(...args), }); export async function ChainFactoryBuilder( - chainParams: Partial + chainParams: Partial, ): Promise { const helpers: HelperMap = new Map(); const cToP = mapNonceToParams(chainParams); const ab = EmmetAddressBook__factory.connect( chainParams.multisigParams!.ab, - chainParams.multisigParams?.provider + chainParams.multisigParams?.provider, ); - const msig = await ab.get('EmmetMultisig'); - const mData = await ab.get('EmmetData'); + const msig = await ab.get("EmmetMultisig"); + const mData = await ab.get("EmmetData"); const multisig = EmmetMultisig__factory.connect( msig, - chainParams.multisigParams?.provider + chainParams.multisigParams?.provider, ); const emmetData = EmmetData__factory.connect( mData, - chainParams.multisigParams?.provider + chainParams.multisigParams?.provider, ); const inner = async (chain: T) => { @@ -164,7 +164,7 @@ export async function ChainFactoryBuilder( fromSymbol, tokenSymbol, destAddress, - gasArgs + gasArgs, ) => { const dc = await inner(chainId as ChainNonce); const targetChainId = await dc.id(); @@ -174,7 +174,7 @@ export async function ChainFactoryBuilder( const isValid = await dc.validateAddress(destAddress); if (!isValid) { throw new Error( - `Invalid destination user address for chain id: ${chainId}` + `Invalid destination user address for chain id: ${chainId}`, ); } const fee = await chain.txFee(targetChainId, fromSymbol, tokenSymbol); @@ -186,7 +186,7 @@ export async function ChainFactoryBuilder( tokenSymbol, destAddress, fee, - gasArgs + gasArgs, ); }, getTokenPrice(symbol) { diff --git a/src/factory/types/index.ts b/src/factory/types/index.ts index b0520dc..d0a2d3c 100644 --- a/src/factory/types/index.ts +++ b/src/factory/types/index.ts @@ -1,5 +1,5 @@ -import type { Web3Helper, Web3Params } from '../../chains/web3'; -import type { TonHelper, TonParams } from '../../chains/ton'; +import type { Web3Helper, Web3Params } from "../../chains/web3"; +import type { TonHelper, TonParams } from "../../chains/ton"; import type { Decimals, GetTxFee, @@ -7,8 +7,8 @@ import type { PreTransfer, ProtocolFee, SendInstallment, -} from '../../chains'; -import type { JsonRpcProvider } from 'ethers'; +} from "../../chains"; +import type { JsonRpcProvider } from "ethers"; export type EvmMeta = [Web3Helper, Web3Params]; export type TonMeta = [TonHelper, TonParams]; @@ -88,7 +88,7 @@ export interface ChainFactory { fromSymbol: string, tokenSymbol: string, destAddress: string, - gasArgs?: GasArgs + gasArgs?: GasArgs, ) => Promise<{ hash: string; tx: RetTx }>; preTransfer: ( chain: PreTransfer, @@ -96,11 +96,11 @@ export interface ChainFactory { tid: string, spender: string, amount: bigint, - gasArgs: GasArgs + gasArgs: GasArgs, ) => Promise; getTransactions: ( batch: bigint | number, - offset: bigint | number + offset: bigint | number, ) => Promise; getTransaction: (hash: string) => Promise; getExplorerStats: () => Promise; @@ -108,7 +108,7 @@ export interface ChainFactory { getTokenPrice: (symbol: string) => Promise; getPriceDecimals: (symbol: string) => Promise; getProtocolFeeInUSD: ( - chain: ProtocolFee & NativeCoinName & Decimals + chain: ProtocolFee & NativeCoinName & Decimals, ) => Promise; }