butterjs-sdk
Version:
Butter Network SDK
242 lines (241 loc) • 9.86 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.EVMOmnichainService = 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 EVMOmnichainService {
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,
};
}
}
/**
* 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,
};
}
}
/**
* 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 swapData
* @param options
*/
async doSwapOutToken(fromAddress, tokenAddress, amount, toAddress, toChainId, swapData, options) {
let txHash;
if (this.contract instanceof ethers_1.Contract) {
const SwapOutTx = await this.contract.swapOutToken(fromAddress, tokenAddress, toAddress, amount, toChainId, swapData, { gasLimit: options.gas });
txHash = SwapOutTx.hash;
return (0, responseUtil_1.assembleEVMTransactionResponse)(txHash, this.provider);
// receipt = await SwapOutTx.wait();
}
else {
const promiReceipt = this.contract.methods
.swapOutToken(fromAddress, tokenAddress, toAddress, amount, toChainId, swapData)
.send({
from: fromAddress,
gas: Number.parseInt(options.gas.toString()),
});
return {
promiReceipt: promiReceipt,
};
}
}
/**
* 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 swapData
* @param options
*/
async doSwapOutNative(fromAddress, toAddress, toChainId, amount, swapData, options) {
let txHash;
if (this.contract instanceof ethers_1.Contract) {
const SwapOutTx = await this.contract.swapOutNative(fromAddress, toAddress, toChainId, swapData, {
// gasLimit: options.gas,
value: amount,
});
txHash = SwapOutTx.hash;
return (0, responseUtil_1.assembleEVMTransactionResponse)(txHash, this.provider);
}
else {
const promiReceipt = this.contract.methods
.swapOutNative(fromAddress, toAddress, toChainId, swapData)
.send({
value: amount,
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;
}
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 gasEstimateSwapOutToken(fromAddress, tokenAddress, amount, toAddress, toChainId, swapData) {
// gas estimation
let estimatedGas = '';
if (this.contract instanceof ethers_1.Contract) {
const gas = await this.contract.estimateGas.swapOutToken(fromAddress, tokenAddress, toAddress, amount, toChainId, swapData);
estimatedGas = gas.toString();
}
else {
const gas = await this.contract.methods
.swapOutToken(fromAddress, tokenAddress, toAddress, amount, toChainId, swapData)
.estimateGas({ from: fromAddress });
estimatedGas = gas.toString();
}
return estimatedGas;
}
async gasEstimateSwapOutNative(fromAddress, toAddress, toChainId, amount, swapData) {
// gas estimation
let estimatedGas;
if (this.contract instanceof ethers_1.Contract) {
const gas = await this.contract.estimateGas.swapOutNative(fromAddress, toAddress, toChainId, swapData, {
value: amount,
});
estimatedGas = gas.toString();
}
else {
const gas = await this.contract.methods
.swapOutNative(fromAddress, toAddress, toChainId, swapData)
.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');
}
}
async isMintable(tokenAddress) {
if (this.contract instanceof ethers_1.Contract) {
return await this.contract.isMintable(tokenAddress);
}
else {
throw new Error('provided not supported');
}
}
}
exports.EVMOmnichainService = EVMOmnichainService;