ethers-override
Version:
> ⚡🚀 SDK for `eth_call` override.
73 lines (72 loc) • 2.05 kB
JavaScript
export class AddressAsIf {
constructor(address) {
this.address = address;
this._field = undefined;
this._slot = undefined;
this._value = undefined;
this._registeredPromises = [];
}
balance() {
this._field = "balance";
return this;
}
nonce() {
this._field = "nonce";
return this;
}
code() {
this._field = "code";
return this;
}
stateDiff(slot) {
this._field = "stateDiff";
this._slot = slot;
return this;
}
state(slot) {
this._field = "state";
this._slot = slot;
return this;
}
is(value) {
// if (this._field === "balance" || this._field === "nonce") {
// if (typeof value !== 'bigint') {
// throw new Error(`Invalid value type for field ${this._field}.`)
// }
// this._value = defaultAbiCoder.encode([ "uint" ], [ value ]);
// return this;
// }
this._value = value.toString();
return this;
}
async valueAsync() {
await Promise.all(this._registeredPromises);
if (this._field === undefined) {
throw new Error(`Field of AsIf at ${this.address} is not set!`);
}
if (this._value === undefined) {
throw new Error(`Value of AsIf at ${this.address} is not set!`);
}
if (this._field === "stateDiff" || this._field === "state") {
if (this._slot === undefined) {
throw new Error(`StateDiff slot of AsIf at ${this.address} is not set!`);
}
return {
[this.address]: {
[this._field]: {
[this._slot]: this._value,
}
}
};
}
return {
[this.address]: {
[this._field]: this._value,
}
};
}
}
;
export const addressAt = (address) => {
return new AddressAsIf(address);
};