UNPKG

@debridge-finance/desdk

Version:

Send, track and claim arbitrary cross-chain messages over the deBridge protocol programmatically

158 lines 5.2 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClaimAutoParams = exports.SendAutoParams = exports.SubmissionAutoParamsFromParam = exports.SubmissionAutoParamsToParam = exports.Flags = exports.Flag = void 0; const ethers_1 = require("ethers"); var Flag; (function (Flag) { Flag[Flag["UNWRAP_ETH"] = 0] = "UNWRAP_ETH"; Flag[Flag["REVERT_IF_EXTERNAL_FAIL"] = 1] = "REVERT_IF_EXTERNAL_FAIL"; Flag[Flag["PROXY_WITH_SENDER"] = 2] = "PROXY_WITH_SENDER"; Flag[Flag["SEND_HASHED_DATA"] = 3] = "SEND_HASHED_DATA"; Flag[Flag["SEND_EXTERNAL_CALL_GAS_LIMIT"] = 4] = "SEND_EXTERNAL_CALL_GAS_LIMIT"; Flag[Flag["MULTI_SEND"] = 5] = "MULTI_SEND"; })(Flag || (exports.Flag = Flag = {})); class Flags { static decode(rawValue) { const flags = new Flags(); flags._flags = rawValue; return flags; } constructor(...flags) { this._flags = 0; this.setFlags(...flags); } setFlags(...flags) { flags.forEach((f) => this.setFlag(f)); } setFlag(flag) { // tslint:disable-next-line:no-bitwise this._flags = this._flags | (1 << flag); } unsetFlag(flag) { // tslint:disable-next-line:no-bitwise this._flags = this._flags & ~(1 << flag); } isSet(flag) { // tslint:disable-next-line:no-bitwise const v = (this._flags >> flag) & 1; return v === 1; } toString() { return this._flags.toString(); } getFlags() { const ret = []; Object.keys(Flag) .filter((v) => isNaN(Number(v))) .forEach((fl) => { const flag = Flag[fl]; if (this.isSet(flag)) { ret.push(flag); } }); return ret; } toHumanReadableString() { const assertedFlags = []; this.getFlags().forEach((flag) => { assertedFlags.push(Flag[flag]); }); return `Flags { ${assertedFlags.join(", ")} }`; } } exports.Flags = Flags; exports.SubmissionAutoParamsToParam = ethers_1.ParamType.from({ type: "tuple", name: "SubmissionAutoParamsTo", components: [ { name: "executionFee", type: "uint256" }, { name: "flags", type: "uint256" }, { name: "fallbackAddress", type: "bytes" }, { name: "data", type: "bytes" }, ], }); exports.SubmissionAutoParamsFromParam = ethers_1.ParamType.from({ type: "tuple", name: "SubmissionAutoParamsFrom", components: [ { name: "executionFee", type: "uint256" }, { name: "flags", type: "uint256" }, { name: "fallbackAddress", type: "address" }, { name: "data", type: "bytes" }, { name: "nativeSender", type: "bytes" }, ], }); class SendAutoParams { static decode(data) { const [struct] = new ethers_1.AbiCoder().decode([exports.SubmissionAutoParamsToParam], data); // Create a new object with named properties manually using the ParamType definition const components = exports.SubmissionAutoParamsToParam.components; const result = {}; for (let i = 0; i < components.length; i++) { const name = components[i].name; Object.assign(result, { [name]: struct[i] }); } return new SendAutoParams({ ...result, flags: Flags.decode(Number(result.flags)), }); } constructor(args) { Object.assign(this, args); } toString() { return this.encode(); } encode() { return new ethers_1.AbiCoder().encode([exports.SubmissionAutoParamsToParam], [ [ this.executionFee, this.flags.toString(), this.fallbackAddress, this.data, ], ]); } toClaimAutoParams(submission) { return new ClaimAutoParams({ ...this, nativeSender: submission.nativeSender, }); } } exports.SendAutoParams = SendAutoParams; class ClaimAutoParams { static decode(data) { const [struct] = new ethers_1.AbiCoder().decode([exports.SubmissionAutoParamsFromParam], data); // Create a new object with named properties manually using the ParamType definition const components = exports.SubmissionAutoParamsFromParam.components; const result = {}; for (let i = 0; i < components.length; i++) { const name = components[i].name; Object.assign(result, { [name]: struct[i] }); } return new ClaimAutoParams({ ...result, flags: Flags.decode(Number(result.flags)), }); } constructor(args) { Object.assign(this, args); } toString() { return this.encode(); } encode() { return new ethers_1.AbiCoder().encode([exports.SubmissionAutoParamsFromParam], [ [ this.executionFee, this.flags.toString(), this.fallbackAddress, this.data, this.nativeSender, ], ]); } } exports.ClaimAutoParams = ClaimAutoParams; //# sourceMappingURL=structs.js.map