barterjs-sdk
Version:
Barter Network SDK
201 lines (200 loc) • 7.63 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.RelayCrossChainService = void 0;
const ethers_1 = require("ethers");
const responseUtil_1 = require("../../utils/responseUtil");
const abstract_provider_1 = require("@ethersproject/abstract-provider");
class RelayCrossChainService {
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);
txHash = transferOutTx.hash;
return (0, responseUtil_1.assembleEVMTransactionResponse)(txHash, this.provider);
}
else {
const promiReceipt = this.contract.methods
.transferOutToken(tokenAddress, toAddress, amount, toChainId)
.send({
from: fromAddress,
gas: options.gas,
});
return {
promiReceipt: promiReceipt,
};
}
}
async gasEstimateTransferOutToken(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();
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, {
value: amount,
});
txHash = transferOutTx.hash;
return (0, responseUtil_1.assembleEVMTransactionResponse)(txHash, this.provider);
}
else {
const promiReceipt = this.contract.methods.transferOutToken(toAddress, toChainId).send({
value: amount,
from: fromAddress,
gas: options.gas,
});
return {
promiReceipt: promiReceipt,
};
}
}
async gasEstimateTransferOutNative(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();
estimatedGas = gas.toString();
}
return estimatedGas;
}
async doDepositOutToken(tokenAddress, from, to, amount) {
if (this.contract instanceof ethers_1.Contract) {
const depositOutTx = await this.contract.depositOutToken(tokenAddress, from, to, amount);
const receipt = await depositOutTx.wait();
return receipt.transactionHash;
}
else {
throw new Error('provider not supported');
}
}
/**
* set id table
* @param chainId
* @param id
*/
// async doSetIdTable(chainId: string, id: string): Promise<string> {
// const setIdTableTx: ContractTransaction = await this.contract.setIdTable(
// chainId,
// id
// );
//
// const receipt = await setIdTableTx.wait();
// return receipt.transactionHash;
// }
//
// async doSetNearHash(hash: string): Promise<string> {
// const setNearHashTx: ContractTransaction = await this.contract.setNearHash(
// hash
// );
//
// const receipt = await setNearHashTx.wait();
// return receipt.transactionHash;
// }
/**
* specify token decimal for the convertion of different token on different chain
* @param selfTokenAddress
* @param chainId
* @param decimals
*/
async doSetTokenOtherChainDecimals(selfTokenAddress, chainId, decimals) {
if (this.contract instanceof ethers_1.Contract) {
const tx = await this.contract.setTokenOtherChainDecimals(selfTokenAddress, chainId, decimals);
const receipt = await tx.wait();
return receipt.transactionHash;
}
else {
throw new Error('need ethers provider');
}
}
async doAddAuthToken(tokens) {
if (this.contract instanceof ethers_1.Contract) {
const addAuthTokenTx = await this.contract.addAuthToken(tokens);
const receipt = await addAuthTokenTx.wait();
return receipt.transactionHash;
}
else {
throw new Error('need ethers provider');
}
}
/**
* set accepted bridge address
* @param chainId chain id of the bridge address is residing on
* @param bridgeAddress bridge address
*/
async doSetBridgeAddress(chainId, bridgeAddress) {
if (this.contract instanceof ethers_1.Contract) {
const tx = await this.contract.setBridgeAddress(chainId, bridgeAddress);
const receipt = await tx.wait();
return receipt.transactionHash;
}
else {
throw new Error('need ethers provider');
}
}
async setVaultBalance(toChain, address, amount) {
if (this.contract instanceof ethers_1.Contract) {
const tx = await this.contract.setVaultBalance(toChain, address, amount);
const receipt = await tx.wait();
return receipt.transactionHash;
}
else {
throw new Error('need ethers provider');
}
}
async getVaultBalance(toChainId, tokenAddress) {
if (this.contract instanceof ethers_1.Contract) {
const balance = await this.contract.vaultBalance(toChainId, tokenAddress);
return Promise.resolve(balance.toString());
}
else {
throw new Error('need ethers provider');
}
}
}
exports.RelayCrossChainService = RelayCrossChainService;