barterjs-sdk
Version:
Barter Network SDK
208 lines (207 loc) • 8.78 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.NearCrossChainService = void 0;
const near_api_js_1 = require("near-api-js");
const requestTypes_1 = require("../../types/requestTypes");
const addresses_1 = require("../../constants/addresses");
const chains_1 = require("../../constants/chains");
const near_method_names_1 = require("../../constants/near_method_names");
const bn_js_1 = __importDefault(require("bn.js"));
const utils_1 = require("../../utils");
const responseUtil_1 = require("../../utils/responseUtil");
class NearCrossChainService {
/**
* we treat account as class member cuz to initialize a near account, async is required
* @param provider
*/
constructor(provider) {
this.provider = provider;
}
/**
* transfer out token(not native coin) from source chain to designated token on target chain
* @param fromAddress
* @param tokenAddress input token address
* @param amount amount in minimal unit
* @param toAddress target chain receiving address
* @param toChainId target chain id
* @param options see {@link TransferOutOptions} for more detail
*/
async doTransferOutToken(fromAddress, tokenAddress, amount, toAddress, toChainId, options) {
let mcsAccountId;
let account;
if (this.provider instanceof requestTypes_1.NearNetworkConfig) {
// get mcs contract address
mcsAccountId =
this.provider.networkId === 'testnet'
? addresses_1.MCS_CONTRACT_ADDRESS_SET[chains_1.ChainId.NEAR_TESTNET]
: '';
// prep near connection
const near = await (0, near_api_js_1.connect)(this.provider);
account = await near.account(this.provider.fromAccount);
}
else {
mcsAccountId = this.provider.getAccountId().endsWith('testnet')
? addresses_1.MCS_CONTRACT_ADDRESS_SET[chains_1.ChainId.NEAR_TESTNET]
: '';
account = this.provider.account();
}
try {
// the receiving address on Near need be in the format of number array as input
const decimalArrayAddress = (0, utils_1.hexToDecimalArray)(toAddress, toChainId);
// contract call option
const nearCallOptions = {
contractId: mcsAccountId,
methodName: near_method_names_1.TRANSFER_OUT_TOKEN,
args: {
token: tokenAddress,
to: decimalArrayAddress,
amount: amount,
to_chain: toChainId,
},
attachedDeposit: new bn_js_1.default(amount, 10),
};
// manual input gas if necessary
if (options.gas != undefined) {
nearCallOptions.gas = new bn_js_1.default(options.gas, 10);
}
const executionOutcome = await this._doNearFunctionCall(account, nearCallOptions);
return (0, responseUtil_1.assembleNearTransactionResponse)(executionOutcome);
}
catch (error) {
throw error;
}
}
/**
* transfer out native coin from source chain to designated token on target chain
* @param fromAddress
* @param toAddress target chain receiving address
* @param toChainId target chain id
* @param amount amount to bridge in minimal unit
* @param options see {@link TransferOutOptions} for more detail
*/
async doTransferOutNative(fromAddress, toAddress, toChainId, amount, options) {
let mcsAccountId;
let account;
if (this.provider instanceof requestTypes_1.NearNetworkConfig) {
mcsAccountId =
this.provider.networkId === 'testnet'
? addresses_1.MCS_CONTRACT_ADDRESS_SET[chains_1.ChainId.NEAR_TESTNET]
: '';
const near = await (0, near_api_js_1.connect)(this.provider);
account = await near.account(this.provider.fromAccount);
}
else {
mcsAccountId = this.provider.getAccountId().endsWith('testnet')
? addresses_1.MCS_CONTRACT_ADDRESS_SET[chains_1.ChainId.NEAR_TESTNET]
: '';
account = this.provider.account();
}
try {
const decimalArrayAddress = (0, utils_1.hexToDecimalArray)(toAddress, toChainId);
const nearCallOptions = {
contractId: mcsAccountId,
methodName: near_method_names_1.TRANSFER_OUT_NATIVE,
args: {
to: decimalArrayAddress,
to_chain: Number.parseInt(toChainId),
},
attachedDeposit: new bn_js_1.default(amount, 10),
};
if (options.gas != undefined) {
nearCallOptions.gas = new bn_js_1.default(options.gas, 10);
}
const executionOutcome = await this._doNearFunctionCall(account, nearCallOptions);
return (0, responseUtil_1.assembleNearTransactionResponse)(executionOutcome);
}
catch (error) {
throw error;
}
}
/**
* add tochain to allowed transfer out chains.
* @param toChainId
*/
async addNativeToChain(toChainId) {
if (this.provider instanceof requestTypes_1.NearNetworkConfig) {
const mcsAccountId = this.provider.networkId === 'testnet'
? addresses_1.MCS_CONTRACT_ADDRESS_SET[chains_1.ChainId.NEAR_TESTNET]
: '';
const near = await (0, near_api_js_1.connect)(this.provider);
const account = await near.account(this.provider.fromAccount);
const nearCallOptions = {
contractId: mcsAccountId,
methodName: near_method_names_1.ADD_NATIVE_TO_CHAIN,
args: {
to_chain: toChainId,
},
};
return await this._doNearFunctionCall(account, nearCallOptions);
}
}
async addTokenToChain(tokenAddress, toChainId) {
if (this.provider instanceof requestTypes_1.NearNetworkConfig) {
const mcsAccountId = this.provider.networkId === 'testnet'
? addresses_1.MCS_CONTRACT_ADDRESS_SET[chains_1.ChainId.NEAR_TESTNET]
: '';
const near = await (0, near_api_js_1.connect)(this.provider);
const account = await near.account(this.provider.fromAccount);
const nearCallOptions = {
contractId: mcsAccountId,
methodName: near_method_names_1.ADD_MCS_TOKEN_TO_CHAIN,
args: {
token: tokenAddress,
to_chain: toChainId,
},
};
return await this._doNearFunctionCall(account, nearCallOptions);
}
}
/**
* call near smart contract
* @param account
* @param options
* @private
*/
async _doNearFunctionCall(account, options) {
let outcome;
try {
outcome = await account.functionCall(options);
}
catch (e) {
console.log(e);
}
return outcome;
}
doDepositOutToken(tokenAddress, from, to, amount, options) {
return Promise.resolve('');
}
gasEstimateTransferOutNative(fromAddress, toAddress, toChainId, amount, options) {
return Promise.resolve('');
}
gasEstimateTransferOutToken(fromAddress, tokenAddress, amount, toAddress, toChainId, options) {
return Promise.resolve('');
}
async addFungibleTokenToChain(tokenAddress, toChainId) {
if (this.provider instanceof requestTypes_1.NearNetworkConfig) {
const mcsAccountId = this.provider.networkId === 'testnet'
? addresses_1.MCS_CONTRACT_ADDRESS_SET[chains_1.ChainId.NEAR_TESTNET]
: '';
const near = await (0, near_api_js_1.connect)(this.provider);
const account = await near.account(this.provider.fromAccount);
const nearCallOptions = {
contractId: mcsAccountId,
methodName: 'add_fungible_token_to_chain',
args: {
token: tokenAddress,
to_chain: toChainId,
},
gas: new bn_js_1.default('300000000000000', 10),
};
const executionOutcome = await this._doNearFunctionCall(account, nearCallOptions);
}
}
}
exports.NearCrossChainService = NearCrossChainService;