UNPKG

@swapper-finance/sdk

Version:
69 lines (63 loc) 1.78 kB
import { openTxConfigForm } from "@src/components"; import { IS_PRODUCTION_ENV } from "@src/constants"; import { Chain, ModalIntegrationPayload, Web3Environment } from "@src/models"; import { getChains } from "@src/services"; import { isETHAddressValid } from "@src/utils"; export const openTransactionModal = async ({ integratorId, dstChain, dstToken, customContractCalls = [], desc, dstDisplayToken, lightTheme, defaultWalletPicker, styles, }: ModalIntegrationPayload) => { const supportedChains: Chain[] = (await getChains()).filter( ({ web3Environment, swapSupported }) => !IS_PRODUCTION_ENV || (web3Environment === Web3Environment.MAINNET && swapSupported), ); validateSwapTxData(integratorId, supportedChains, dstChain, dstToken); await openTxConfigForm({ integratorId, dstChainId: dstChain, dstTokenAddr: dstToken, customContractCalls, desc, dstDisplayTokenAddr: dstDisplayToken, supportedChains, lightTheme, defaultWalletPicker, styles, }); }; const validateSwapTxData = ( integratorId: string, supportedChains: Chain[], dstChain: string, dstToken: string, ) => { if (!integratorId) { throw new Error( "Provide integratorId or contact the team to generate one.", ); } const supportedDstChain = supportedChains.find( ({ chainId }) => chainId === dstChain, ); if (!supportedDstChain) { throw new Error(`Provided chain '${dstChain}' is not supported`); } if ( !isETHAddressValid(dstToken) || supportedDstChain.tokens.findIndex( ({ address }) => address.toLowerCase() === dstToken.toLowerCase(), ) === -1 ) { throw new Error( `Provided token address ${dstToken} on chain ${dstChain} is invalid`, ); } };