barterjs-sdk
Version:
Barter Network SDK
80 lines (79 loc) • 3.46 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.TokenRegister = void 0;
const ethers_1 = require("ethers");
const TokenRegister_json_1 = __importDefault(require("../abis/TokenRegister.json"));
const abstract_provider_1 = require("@ethersproject/abstract-provider");
const responseUtil_1 = require("../utils/responseUtil");
const utils_1 = require("../utils");
class TokenRegister {
constructor(contractAddress, signerOrProvider) {
if (signerOrProvider instanceof ethers_1.Signer ||
signerOrProvider instanceof abstract_provider_1.Provider) {
this.contract = new ethers_1.ethers.Contract(contractAddress, TokenRegister_json_1.default.abi, signerOrProvider);
}
else {
this.contract = new signerOrProvider.Contract(TokenRegister_json_1.default.abi, contractAddress);
}
this.provider = signerOrProvider;
}
async registerToken(sourceChain, sourceMapToken, mapToken) {
let regTokenTx;
if (this.contract instanceof ethers_1.ethers.Contract) {
regTokenTx = await this.contract.regToken(sourceChain, (0, utils_1.getHexAddress)(sourceMapToken, sourceChain, true), mapToken);
regTokenTx = await regTokenTx.wait();
}
else {
regTokenTx = await this.contract.methods
.regToken(sourceChain, sourceMapToken, mapToken)
.send({ from: this.provider.defaultAccount });
}
return (0, responseUtil_1.adaptEthReceipt)(regTokenTx);
}
async getToChainToken(tokenAddress, targetChain) {
if (this.contract instanceof ethers_1.ethers.Contract) {
return await this.contract.getToChainToken(tokenAddress, targetChain);
}
else
return '';
}
async getToChainAmount(token, amount, toChainId) {
if (this.contract instanceof ethers_1.ethers.Contract) {
return await this.contract.getToChainAmount((0, utils_1.getHexAddress)(token.address, token.chainId, true), amount, toChainId);
}
else
return '';
}
async getRelayChainToken(fromChain, fromToken) {
if (this.contract instanceof ethers_1.ethers.Contract) {
return await this.contract.getRelayChainToken(fromChain, (0, utils_1.getHexAddress)(fromToken.address, fromToken.chainId, false));
}
else
return '';
}
async getRelayChainAmount(token, fromChain, amount) {
if (this.contract instanceof ethers_1.ethers.Contract) {
return await this.contract.getRelayChainAmount((0, utils_1.getHexAddress)(token.address, token.chainId, true), fromChain, amount);
}
else
return '';
}
async getVaultToken(tokenAddress) {
if (this.contract instanceof ethers_1.ethers.Contract) {
return await this.contract.getVaultToken(tokenAddress);
}
else
return '';
}
async getTokenFee(tokenAddress, amount, toChain) {
if (this.contract instanceof ethers_1.ethers.Contract) {
return await this.contract.getTokenFee(tokenAddress, ethers_1.BigNumber.from(amount), ethers_1.BigNumber.from(toChain));
}
else
return '';
}
}
exports.TokenRegister = TokenRegister;