UNPKG

butterjs-sdk

Version:
87 lines (86 loc) 4.26 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ButterBridge = void 0; const constants_1 = require("../../constants"); const utils_1 = require("../../utils"); const mosUtils_1 = require("../../libs/utils/mosUtils"); class ButterBridge { /** * The BridgeToken method is used to bridge token from one chain to another. * see {@link BridgeRequestParam} for detail * @param token source token, aka token that user provide * @param toChainId target chain id * @param toAddress target chain receiving address * @param amount amount to bridge, in minimal uint. For example wei in Ethereum, yocto in Near * @param options of bridging, check {@link BridgeOptions} for more details * @return ButterTransactionResponse */ async bridgeToken({ fromAddress, fromToken, toChainId, toAddress, amount, options, }) { // check validity of toAddress according to toChainId toAddress = (0, utils_1.validateAndParseAddressByChainId)(toAddress, toChainId); // if src chain is evm chain, signer must be provided if ((0, constants_1.IS_EVM)(fromToken.chainId) && options.signerOrProvider == undefined) { throw new Error(`bridgeToken: Signer must be provided for EVM blockchains`); } // if src chain is near chain, near network provider must be provided if (constants_1.ChainId.NEAR_TESTNET == fromToken.chainId && options.nearProvider == undefined) { throw new Error(`bridgeToken: Network config must be provided for NEAR blockchain`); } // create mos instance base on src token chainId. const mos = (0, mosUtils_1.createMOSInstance)(fromToken.chainId, options); let result; // convert near address to hex if ((0, constants_1.IS_NEAR)(toChainId)) { const accountState = await (0, utils_1.verifyNearAccountId)(toAddress, toChainId); if (!accountState.isValid) { throw new Error(accountState.errMsg); } toAddress = (0, utils_1.getHexAddress)(toAddress, toChainId, false); } if (fromToken.isNative) { // if input token is Native coin, call transferOutNative method result = await mos.doTransferOutNative(fromAddress, toAddress, toChainId.toString(), amount, { gas: options.gas, }); } else { result = await mos.doTransferOutToken(fromAddress, fromToken.address, amount, toAddress, toChainId.toString(), { gas: options.gas, }); } return result; } async gasEstimateBridgeToken({ fromAddress, fromToken, toChainId, toAddress, amount, options, }) { // check validity of toAddress according to toChainId // toAddress = validateAndParseAddressByChainId(toAddress, toChainId); // if src chain is evm chain, signer must be provided if ((0, constants_1.IS_EVM)(fromToken.chainId) && options.signerOrProvider == undefined) { throw new Error(`provider must be provided`); } // near doesn't provide gas estimation yet // create mos instance base on src token chainId. const mos = (0, mosUtils_1.createMOSInstance)(fromToken.chainId, options); if ((0, constants_1.IS_NEAR)(toChainId)) { // no need to check address validity for gas estimation // const accountState: NearAccountState = await verifyNearAccountId( // toAddress, // toChainId // ); // if (!accountState.isValid) { // throw new Error(accountState.errMsg); // } toAddress = (0, utils_1.getHexAddress)(toAddress, toChainId, false); } let gas; // if input token is Native coin, call transferOutNative method if (fromToken.isNative) { gas = await mos.gasEstimateTransferOutNative(fromAddress, toAddress, toChainId.toString(), amount); } else { gas = await mos.gasEstimateTransferOutToken(fromAddress, fromToken.address, amount, toAddress, toChainId.toString()); } return gas; } } exports.ButterBridge = ButterBridge;