butterjs-sdk
Version:
Butter Network SDK
92 lines (91 loc) • 3.69 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ButterRouter = void 0;
const ethers_1 = require("ethers");
const responseUtil_1 = require("../../utils/responseUtil");
const abstract_provider_1 = require("@ethersproject/abstract-provider");
/**
* EVM Omnichain Chain Service smart contracts abstraction
*/
class ButterRouter {
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 coreParam
* @param targetSwapData
* @param amount amount in minimal unit
* @param toChain
* @param targetToAddress
* @param isNative
* @param options
*/
async entrance(fromAddress, coreParam, targetSwapData, amount, toChain, targetToAddress, isNative, options) {
let txHash;
if (this.contract instanceof ethers_1.Contract) {
let ethersOptions = {
gasLimit: options.gas,
};
if (isNative) {
ethersOptions.value = amount;
}
console.log('targetSwapData', targetSwapData);
console.log('amount', amount);
const entranceTx = await this.contract.entrance(coreParam, targetSwapData, amount, toChain, targetToAddress, ethersOptions);
txHash = entranceTx.hash;
return (0, responseUtil_1.assembleEVMTransactionResponse)(txHash, this.provider);
// receipt = await entranceTx.wait();
}
else {
let web3JSOptions = {
from: fromAddress,
gas: Number.parseInt(options.gas.toString()),
};
if (isNative) {
web3JSOptions.value = amount;
}
const promiReceipt = this.contract.methods
.entrance(coreParam, targetSwapData, amount, toChain, targetToAddress)
.send(web3JSOptions);
return {
promiReceipt: promiReceipt,
};
}
}
async gasEstimateEntrance(fromAddress, coreParam, targetSwapData, amount, toChain, targetToAddress, isNative) {
// gas estimation
let estimatedGas = '';
if (this.contract instanceof ethers_1.Contract) {
// console.log('access param', coreParam);
// console.log('swapData', targetSwapData);
// console.log('amount', amount);
// console.log('toChain', toChain);
// console.log('tagetToAddress', targetToAddress);
// console.log('isNative', isNative);
const gas = await this.contract.estimateGas.entrance(coreParam, targetSwapData, amount, toChain, targetToAddress, {
value: isNative ? amount : undefined,
});
estimatedGas = gas.toString();
}
else {
const gas = await this.contract.methods
.entrance(coreParam, targetSwapData, amount, toChain, targetToAddress)
.estimateGas({
from: fromAddress,
value: isNative ? amount : undefined,
});
estimatedGas = gas.toString();
}
return estimatedGas;
}
}
exports.ButterRouter = ButterRouter;
;