@kaiachain/kss-bridges-celer
Version:
TypeScript client and use-cases for cBridge
154 lines • 6.81 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.approve = exports.checkApprove = exports.getAllowance = exports.getPegConfig = exports.getTransferObject = exports.transactor = exports.getTransferId = exports.getBridgeContractAddress = void 0;
const ethers_1 = require("ethers");
const units_1 = require("@ethersproject/units");
const format_1 = require("celer-web-utils/lib/format");
const constant_1 = require("./constant");
const SingleBridgeTokenPermit_json_1 = __importDefault(require("../contract/abi/pegged/tokens/ERC20Permit/SingleBridgeTokenPermit.sol/SingleBridgeTokenPermit.json"));
const tokenInterface = new ethers_1.utils.Interface(SingleBridgeTokenPermit_json_1.default.abi);
const getBridgeContractAddress = (transferConfigs, chainId) => {
return transferConfigs.chains.find(chain => chain.id === chainId)?.contract_addr;
};
exports.getBridgeContractAddress = getBridgeContractAddress;
const getTransferId = (address, tokenAddress, value, toChainId, nonce, fromChainId) => {
return ethers_1.utils.solidityKeccak256(["address", "address", "address", "uint256", "uint64", "uint64", "uint64"], [
address,
address,
tokenAddress,
value?.toString(),
toChainId?.toString(),
nonce?.toString(),
fromChainId?.toString(),
]);
};
exports.getTransferId = getTransferId;
const transactor = async (tx, rpcURL, privateKey) => {
let result;
if (tx instanceof Promise) {
result = await tx;
}
else {
if (!tx.gasPrice) {
tx.gasPrice = (0, units_1.parseUnits)("4.1", "gwei");
}
if (!tx.gasLimit) {
tx.gasLimit = ethers_1.BigNumber.from(120000);
}
result = await (0, constant_1.getSigner)(rpcURL, privateKey).sendTransaction(tx);
}
return result;
};
exports.transactor = transactor;
const getTransferObject = (transferConfigs, srcChainId, dstChainId, tokenSymbol, transferValue) => {
const transferObject = {};
const transferToken = transferConfigs.chain_token[`${srcChainId}`]?.token.find(({ token }) => token.symbol === tokenSymbol);
const fromChain = transferConfigs.chains.find(({ id }) => id === srcChainId);
const toChain = transferConfigs.chains.find(({ id }) => id === dstChainId);
const value = (0, format_1.safeParseUnits)(transferValue, transferToken?.token?.decimal ?? 18);
const nonce = new Date().getTime();
Object.assign(transferObject, { transferToken, fromChain, toChain, value, nonce });
return transferObject;
};
exports.getTransferObject = getTransferObject;
const getPegConfig = (transferConfigs, srcChainId, dstChainId, tokenSymbol) => {
const depositConfigs = transferConfigs.pegged_pair_configs.filter((e) => e.org_chain_id === srcChainId &&
e.pegged_chain_id === dstChainId &&
e.org_token.token.symbol === tokenSymbol);
if (depositConfigs.length) {
return depositConfigs[0];
}
const burnConfigs = transferConfigs.pegged_pair_configs.filter((e) => e.org_chain_id === dstChainId &&
e.pegged_chain_id === srcChainId &&
e.org_token.token.symbol === tokenSymbol);
if (burnConfigs.length) {
return burnConfigs[0];
}
return;
};
exports.getPegConfig = getPegConfig;
const isNonEVMChain = (chainId) => {
if (chainId === 12340001 ||
chainId === 12340002 ||
chainId === 999999998 ||
chainId === 999999999) {
return true;
}
return false;
};
const getTokenBalanceAddress = (originalAddress, fromChainId = undefined, tokenSymbol = undefined, peggedPairs = undefined) => {
if (!fromChainId || !tokenSymbol || !peggedPairs) {
return originalAddress;
}
const peggedTokens = peggedPairs?.filter((item) => {
return (item.pegged_chain_id === fromChainId && tokenSymbol === item.pegged_token.token.symbol);
});
if (peggedTokens &&
peggedTokens.length > 0 &&
peggedTokens[0].canonical_token_contract_addr.length > 0) {
return peggedTokens[0].canonical_token_contract_addr;
}
if (isNonEVMChain(fromChainId)) {
const nonEVMDeposit = peggedPairs?.find((peggedPairConfig) => {
return (peggedPairConfig.org_chain_id === fromChainId &&
peggedPairConfig.org_token.token.symbol === tokenSymbol);
});
if (nonEVMDeposit) {
return nonEVMDeposit.pegged_token.token.address;
}
const nonEVMBurn = peggedPairs?.find((peggedPairConfig) => {
return (peggedPairConfig.pegged_chain_id === fromChainId &&
peggedPairConfig.pegged_token.token.symbol === tokenSymbol);
});
if (nonEVMBurn) {
return nonEVMBurn.org_token.token.address;
}
}
return originalAddress;
};
const getAllowance = async (walletAddress, spenderAddress, originalAddress, fromChainId = undefined, tokenSymbol = undefined, rpcURL, peggedPairs = undefined) => {
const tokenAddress = getTokenBalanceAddress(originalAddress, fromChainId, tokenSymbol, peggedPairs);
const tokenContract = new ethers_1.Contract(tokenAddress, tokenInterface, (0, constant_1.getSigner)(rpcURL));
const allowance = await tokenContract?.allowance(walletAddress, spenderAddress);
return allowance;
};
exports.getAllowance = getAllowance;
const checkApprove = (allowance, amount, token, isNative) => {
/**Native token case */
if (isNative) {
return false;
}
if (!allowance || allowance.isZero()) {
return true;
}
console.log("Allowance: " + allowance.toString());
console.log("Amount : " + (0, format_1.safeParseUnits)(amount || "0", token?.decimal ?? 18).toString());
try {
const isGreatThanAllowance = (0, format_1.safeParseUnits)(amount || "0", token?.decimal ?? 18).gt(allowance);
return isGreatThanAllowance;
}
catch {
return true;
}
};
exports.checkApprove = checkApprove;
const approve = async (spenderAddress, rpcURL, privateKey, token, amount) => {
if (!token) {
return;
}
try {
const tokenContract = new ethers_1.Contract(token.address, tokenInterface, (0, constant_1.getSigner)(rpcURL, privateKey));
const approveTx = await (0, exports.transactor)(tokenContract.approve(spenderAddress, (0, format_1.safeParseUnits)(amount || "0", token?.decimal ?? 18), { gasLimit: 100000 }), rpcURL, privateKey);
await approveTx.wait();
return approveTx;
}
catch (e) {
console.error(`-Failed to approve token. Error:`, e);
return;
}
};
exports.approve = approve;
//# sourceMappingURL=transactionHelper.js.map