@debridge-finance/desdk
Version:
Send, track and claim arbitrary cross-chain messages over the deBridge protocol programmatically
89 lines • 3.83 kB
JavaScript
import { EventLog } from "ethers";
import { Claim } from "./claim";
import { getDeBridgeGateAddress, getProvider } from "./context";
import { SendAutoParams } from "./structs";
import { DeBridgeGate__factory } from "./typechain";
export var SubmissionStatus;
(function (SubmissionStatus) {
SubmissionStatus[SubmissionStatus["WAITING_CONFIRMATION"] = 0] = "WAITING_CONFIRMATION";
SubmissionStatus[SubmissionStatus["WAITING_CLAIM"] = 1] = "WAITING_CLAIM";
SubmissionStatus[SubmissionStatus["CLAIMED"] = 2] = "CLAIMED";
})(SubmissionStatus || (SubmissionStatus = {}));
export class Submission {
static async findAll(txHash, ctx) {
const events = await getSentEvents(txHash, ctx);
const originChainId = Number((await getProvider(ctx).getNetwork()).chainId);
return events.map((sentEvent) => new Submission({
submissionId: sentEvent.args.submissionId.toString(),
debridgeId: sentEvent.args.debridgeId.toString(),
amount: sentEvent.args.amount.toString(),
receiver: sentEvent.args.receiver.toString(),
nonce: sentEvent.args.nonce.toString(),
chainIdTo: sentEvent.args.chainIdTo.toString(),
referralCode: sentEvent.args.referralCode,
feeParams: sentEvent.args.feeParams,
autoParams: sentEvent.args.autoParams === "0x"
? undefined
: SendAutoParams.decode(sentEvent.args.autoParams),
nativeSender: sentEvent.args.nativeSender.toString(),
originChainId,
sentEvent,
}, ctx));
}
static async find(txHash, submissionId, ctx) {
const submissions = await Submission.findAll(txHash, ctx);
return submissions.find((s) => s.submissionId === submissionId);
}
constructor(args, ctx) {
this.ctx = ctx;
Object.assign(this, args);
}
async hasRequiredBlockConfirmations(overrideBlockConfirmations) {
const requiredConfirmations = overrideBlockConfirmations === undefined
? await this._getRequiredConfirmations()
: overrideBlockConfirmations;
const currentBlockNumber = await getProvider(this.ctx).getBlockNumber();
const submissionBlockNumber = this.sentEvent.blockNumber;
if (submissionBlockNumber + requiredConfirmations <= currentBlockNumber) {
return true;
}
return false;
}
async _getRequiredConfirmations() {
const { chainId } = await getProvider(this.ctx).getNetwork();
if (chainId === 137n)
return 256;
else
return 12;
}
async toEVMClaim(destinationCtx) {
if (!destinationCtx.signatureStorage)
destinationCtx.signatureStorage = this.ctx.signatureStorage;
return new Claim({
submissionId: this.submissionId,
debridgeId: this.debridgeId,
amount: this.amount,
chainIdFrom: this.originChainId,
receiver: this.receiver,
nonce: this.nonce,
autoParams: this.autoParams
? this.autoParams.toClaimAutoParams(this)
: undefined,
}, destinationCtx);
}
}
async function getSentEvents(txHash, opts) {
const provider = getProvider(opts);
const txReceipt = await provider.getTransactionReceipt(txHash);
if (!txReceipt)
return [];
const contract = DeBridgeGate__factory.connect(getDeBridgeGateAddress(opts), provider);
return txReceipt.logs
.map((log) => {
const logDescription = contract.interface.parseLog(log);
if (logDescription?.name === "Sent")
return new EventLog(log, contract.interface, logDescription.fragment);
})
.filter((log) => log !== undefined);
}
//# sourceMappingURL=submission.js.map