@debridge-finance/desdk
Version:
Send, track and claim arbitrary cross-chain messages over the deBridge protocol programmatically
152 lines • 4.78 kB
JavaScript
import { AbiCoder, ParamType } from "ethers";
export 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 || (Flag = {}));
export 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(", ")} }`;
}
}
export const SubmissionAutoParamsToParam = ParamType.from({
type: "tuple",
name: "SubmissionAutoParamsTo",
components: [
{ name: "executionFee", type: "uint256" },
{ name: "flags", type: "uint256" },
{ name: "fallbackAddress", type: "bytes" },
{ name: "data", type: "bytes" },
],
});
export const SubmissionAutoParamsFromParam = 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" },
],
});
export class SendAutoParams {
static decode(data) {
const [struct] = new AbiCoder().decode([SubmissionAutoParamsToParam], data);
// Create a new object with named properties manually using the ParamType definition
const components = 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 AbiCoder().encode([SubmissionAutoParamsToParam], [
[
this.executionFee,
this.flags.toString(),
this.fallbackAddress,
this.data,
],
]);
}
toClaimAutoParams(submission) {
return new ClaimAutoParams({
...this,
nativeSender: submission.nativeSender,
});
}
}
export class ClaimAutoParams {
static decode(data) {
const [struct] = new AbiCoder().decode([SubmissionAutoParamsFromParam], data);
// Create a new object with named properties manually using the ParamType definition
const components = 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 AbiCoder().encode([SubmissionAutoParamsFromParam], [
[
this.executionFee,
this.flags.toString(),
this.fallbackAddress,
this.data,
this.nativeSender,
],
]);
}
}
//# sourceMappingURL=structs.js.map