UNPKG

barterjs-sdk

Version:
108 lines (107 loc) 6.42 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.addTokenPair = void 0; const constants_1 = require("../../constants"); const EVMCrossChainService_1 = require("../../libs/mcs/EVMCrossChainService"); const MAPCrossChainService_json_1 = __importDefault(require("../../abis/MAPCrossChainService.json")); const NearCrossChainService_1 = require("../../libs/mcs/NearCrossChainService"); const RelayCrossChainService_1 = require("../../libs/mcs/RelayCrossChainService"); const MAPCrossChainServiceRelay_json_1 = __importDefault(require("../../abis/MAPCrossChainServiceRelay.json")); const TokenRegister_1 = require("../../libs/TokenRegister"); const utils_1 = require("../../utils"); /** * TODO: need improvement! * Approve the bridge of the token pair provided. * @param srcToken source token * @param targetToken target token * @param feeBP bridge fee in BP(tenth of one percent) * @param mapNetwork map network 'testnet' or 'mainnet' * @param mapSigner map signer to sign transaction * @param srcSigner src chain signer if src chain is a evm blockchain * @param nearConfig near network configuration see {@link NearNetworkConfig} * @param mapToken intermediary map token, if the token pair provided both from other blockchain than map, * provide a map intermediary token */ async function addTokenPair({ srcToken, targetToken, feeRate, mapNetwork, mapSigner, srcSigner, nearConfig, mapToken, }) { /** * argument check */ if (targetToken.chainId != (0, constants_1.NETWORK_NAME_TO_ID)(mapNetwork) && srcToken.chainId != (0, constants_1.NETWORK_NAME_TO_ID)(mapNetwork) && mapToken == undefined) { throw new Error('intermediary map token is not specified'); } if ((0, constants_1.IS_EVM)(srcToken.chainId) && !(0, constants_1.IS_MAP)(srcToken.chainId) && srcSigner == undefined) { throw new Error('src chain signer is not provided'); } else if ((0, constants_1.IS_NEAR)(srcToken.chainId) && nearConfig == undefined) { throw new Error('near config is not provided'); } /** * set allowed transfer token for source chain. */ /** case 1: source chain is non-MAP evm chain*/ if ((0, constants_1.IS_EVM)(srcToken.chainId) && !(0, constants_1.IS_MAP)(srcToken.chainId)) { const mcsContractAddress = constants_1.MCS_CONTRACT_ADDRESS_SET[(0, constants_1.ID_TO_CHAIN_ID)(srcToken.chainId)]; const mcsService = new EVMCrossChainService_1.EVMCrossChainService(mcsContractAddress, MAPCrossChainService_json_1.default.abi, srcSigner); await mcsService.doSetCanBridgeToken(srcToken.address, targetToken.chainId, true); } else if ((0, constants_1.IS_NEAR)(srcToken.chainId)) { /** case 2: source chain is Near */ // initialize near contract, nearConfig cannot be undefined cuz we already check previously. const nearMCS = new NearCrossChainService_1.NearCrossChainService(nearConfig); if (srcToken.isNative) { await nearMCS.addNativeToChain(targetToken.chainId); } else { console.log('not near native,', targetToken.chainId); await nearMCS.addFungibleTokenToChain(srcToken.address, targetToken.chainId); } console.log(`add token ${srcToken.name} to ${targetToken.chainId}`); } else if (!(0, constants_1.IS_MAP)(srcToken.chainId)) { throw new Error(`source chainId: ${srcToken.chainId} is not supported yet`); } /** * from this point, we need to set up staffs on Map Relay Chain... * 1. set chain token gas fee: aka bridge fee, used to compensate messenger gas cost * 2. register token: so later on map will know src token will mapped to target token * 3. set decimal: for out amount calculation */ // create contract instance const mcsContractAddress = constants_1.MCS_CONTRACT_ADDRESS_SET[(0, constants_1.NETWORK_NAME_TO_ID)(mapNetwork)]; const mapMCS = new RelayCrossChainService_1.RelayCrossChainService(mcsContractAddress, MAPCrossChainServiceRelay_json_1.default.abi, mapSigner); const tokenRegister = new TokenRegister_1.TokenRegister(constants_1.TOKEN_REGISTER_ADDRESS_SET[(0, constants_1.NETWORK_NAME_TO_ID)(mapNetwork)], mapSigner); /** case 1: target chain is map */ if (targetToken.chainId == (0, constants_1.NETWORK_NAME_TO_ID)(mapNetwork)) { await tokenRegister.registerToken(srcToken.chainId, srcToken.address, targetToken.address); // set token decimals for conversion. await mapMCS.doSetTokenOtherChainDecimals((0, utils_1.getHexAddress)(srcToken.address, srcToken.chainId, false), srcToken.chainId, srcToken.decimals); await mapMCS.doSetTokenOtherChainDecimals((0, utils_1.getHexAddress)(srcToken.address, srcToken.chainId, false), targetToken.chainId, targetToken.decimals); } else if (srcToken.chainId == (0, constants_1.NETWORK_NAME_TO_ID)(mapNetwork)) { /** case 2: source chain is map */ await tokenRegister.registerToken(targetToken.chainId, targetToken.address, srcToken.address); // set token decimals for conversion. await mapMCS.doSetTokenOtherChainDecimals(srcToken.address, srcToken.chainId, srcToken.decimals); await mapMCS.doSetTokenOtherChainDecimals(srcToken.address, targetToken.chainId, targetToken.decimals); } else { /** case 3: neither src chain and target chain is map, then map will act as a relay */ await tokenRegister.registerToken(srcToken.chainId, (0, utils_1.getHexAddress)(srcToken.address, srcToken.chainId, false), mapToken.address); console.log(`register ${srcToken.name} done`); await tokenRegister.registerToken(targetToken.chainId, (0, utils_1.getHexAddress)(targetToken.address, targetToken.chainId, false), mapToken.address); console.log(`register ${targetToken.name} done`); // set token decimals for conversion. await mapMCS.doSetTokenOtherChainDecimals(mapToken.address, srcToken.chainId, srcToken.decimals); await mapMCS.doSetTokenOtherChainDecimals(mapToken.address, targetToken.chainId, targetToken.decimals); console.log(`setfee done`); } console.log('token reg done'); } exports.addTokenPair = addTokenPair;