@kaiachain/kss-bridges-celer
Version:
TypeScript client and use-cases for cBridge
55 lines • 3.27 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.requestRefund = void 0;
const GatewayServiceClientPb_1 = require("../ts-proto/gateway/GatewayServiceClientPb");
const gateway_pb_1 = require("../ts-proto/gateway/gateway_pb");
const tx_pb_1 = require("../ts-proto/sgn/cbridge/v1/tx_pb");
const withdraw_1 = require("./withdraw");
const helper_1 = require("../helper");
const StatusTracker_1 = require("./StatusTracker");
const requestRefund = async (type, contractInstance, CBRIDGE_GATEWAY_URL, TRANSFER_ID, estimated, SRC_CHAIN_RPC, PRIVATE_KEY, CONFIRMATIONS) => {
const client = new GatewayServiceClientPb_1.WebClient(CBRIDGE_GATEWAY_URL, null, null);
const timestamp = Math.floor(Date.now() / 1000);
const withdrawReqProto = new tx_pb_1.WithdrawReq();
withdrawReqProto.setXferId(TRANSFER_ID);
withdrawReqProto.setReqId(timestamp);
withdrawReqProto.setWithdrawType(tx_pb_1.WithdrawType.WITHDRAW_TYPE_REFUND_TRANSFER);
const req = new gateway_pb_1.WithdrawLiquidityRequest();
req.setWithdrawReq(withdrawReqProto.serializeBinary());
if (estimated) {
req.setEstimatedReceivedAmt(estimated);
}
req.setMethodType(gateway_pb_1.WithdrawMethodType.WD_METHOD_TYPE_ONE_RM);
console.log("2. Submitting withdrawal request to cBRIDGE network...");
const wres = await client.withdrawLiquidity(req, null);
let refundTx;
let resolver;
const refund = new Promise((r) => { resolver = r; });
if (!wres.getErr() || wres.getErr()?.getCode() == 500) {
(0, StatusTracker_1.statusTracker)(CBRIDGE_GATEWAY_URL, TRANSFER_ID, async (res) => {
if (res.status !== 8)
return console.error("invalid transfer status: " + res.status);
const { wdmsg, sigs, signers, powers } = (0, withdraw_1.parseRefundTxResponse)(res.wdOnchain, res.signersList, res.sortedSigsList, res.powersList);
console.log("3. Confirming Refund Request on-chain...");
refundTx = await (0, helper_1.transactor)(type === "BURN" ?
contractInstance.mint(wdmsg, sigs, signers, powers, { gasLimit: 200000 }) :
contractInstance.withdraw(wdmsg, sigs, signers, powers, { gasLimit: 200000 }), SRC_CHAIN_RPC, PRIVATE_KEY);
if (!refundTx)
throw new Error("Error while refunding on-chain");
console.log("refundTx hash: " + refundTx.hash);
console.log("Waiting for the confirmations of refundTx");
const confirmationReceipt = await refundTx.wait(CONFIRMATIONS ?? 6);
// = await getConfirmations(refundTx.hash, CONFIRMATIONS, type === "BURN" ? DST_CHAIN_RPC: SRC_CHAIN_RPC); // instead of waiting for fixed time, wait for some confirmations
console.log(`refundTx confirmed upto ${confirmationReceipt.confirmations} confirmations`);
(0, StatusTracker_1.statusTracker)(CBRIDGE_GATEWAY_URL, TRANSFER_ID, null, 8);
resolver(confirmationReceipt);
}, 7);
return await refund;
}
else {
console.log(`Refund error`, wres.getErr()?.toObject());
throw new Error(wres.getErr()?.toObject()?.msg);
}
};
exports.requestRefund = requestRefund;
//# sourceMappingURL=RequestRefund.js.map