barterjs-sdk
Version:
Barter Network SDK
139 lines (138 loc) • 5.62 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.EVMCrossChainService = void 0;
const ethers_1 = require("ethers");
const responseUtil_1 = require("../../utils/responseUtil");
const abstract_provider_1 = require("@ethersproject/abstract-provider");
class EVMCrossChainService {
constructor(contractAddress, abi, signerOrProvider) {
if (signerOrProvider instanceof ethers_1.Signer ||
signerOrProvider instanceof abstract_provider_1.Provider) {
this.contract = new ethers_1.ethers.Contract(contractAddress, abi, signerOrProvider);
}
else {
this.contract = new signerOrProvider.Contract(abi, contractAddress);
}
this.provider = signerOrProvider;
}
/**
* 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
*/
async doTransferOutToken(fromAddress, tokenAddress, amount, toAddress, toChainId, options) {
let txHash;
if (this.contract instanceof ethers_1.Contract) {
const transferOutTx = await this.contract.transferOutToken(tokenAddress, toAddress, amount, toChainId
// { gasLimit: options.gas }
);
txHash = transferOutTx.hash;
return (0, responseUtil_1.assembleEVMTransactionResponse)(txHash, this.provider);
// receipt = await transferOutTx.wait();
}
else {
const promiReceipt = this.contract.methods
.transferOutToken(tokenAddress, toAddress, amount, toChainId)
.send({
from: fromAddress,
gas: Number.parseInt(options.gas.toString()),
});
return {
promiReceipt: promiReceipt,
};
}
}
async gasEstimateTransferOutToken(fromAddress, tokenAddress, amount, toAddress, toChainId) {
// gas estimation
let estimatedGas = '';
if (this.contract instanceof ethers_1.Contract) {
const gas = await this.contract.estimateGas.transferOutToken(tokenAddress, toAddress, amount, toChainId);
estimatedGas = gas.toString();
}
else {
const gas = await this.contract.methods
.transferOutToken(tokenAddress, toAddress, amount, toChainId)
.estimateGas({ from: fromAddress });
estimatedGas = gas.toString();
}
return estimatedGas;
}
/**
* 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
*/
async doTransferOutNative(fromAddress, toAddress, toChainId, amount, options) {
let txHash;
if (this.contract instanceof ethers_1.Contract) {
const transferOutTx = await this.contract.transferOutNative(toAddress, toChainId, {
// gasLimit: options.gas,
value: amount,
});
txHash = transferOutTx.hash;
return (0, responseUtil_1.assembleEVMTransactionResponse)(txHash, this.provider);
}
else {
const promiReceipt = this.contract.methods.transferOutNative(toAddress, toChainId).send({
value: amount,
from: fromAddress,
gas: Number.parseInt(options.gas.toString()),
});
return {
promiReceipt: promiReceipt,
};
}
}
async gasEstimateTransferOutNative(fromAddress, toAddress, toChainId, amount) {
// gas estimation
let estimatedGas;
if (this.contract instanceof ethers_1.Contract) {
const gas = await this.contract.estimateGas.transferOutNative(toAddress, toChainId, {
value: amount,
});
estimatedGas = gas.toString();
}
else {
const gas = await this.contract.methods
.transferOutNative(toAddress, toChainId)
.estimateGas({ value: amount });
estimatedGas = gas.toString();
}
return estimatedGas;
}
async doDepositOutToken(tokenAddress, from, to, amount) {
let txHash;
if (this.contract instanceof ethers_1.Contract) {
const depositOutTx = await this.contract.depositOutToken(tokenAddress, from, to, amount);
const receipt = await depositOutTx.wait();
txHash = receipt.transactionHash;
}
else {
const eth = this.provider;
const receipt = await this.contract.methods
.depositOutToken(tokenAddress, from, to, amount)
.send({
from: eth.defaultAccount,
});
txHash = receipt.transactionHash;
}
return txHash;
}
async doSetCanBridgeToken(tokenAddress, toChainId, canBridge) {
if (this.contract instanceof ethers_1.Contract) {
const tx = await this.contract.setCanBridgeToken(tokenAddress, toChainId, canBridge);
const receipt = await tx.wait();
}
else {
throw new Error('provided not supported');
}
}
}
exports.EVMCrossChainService = EVMCrossChainService;