ethers.js/src.ts/abi/coders/address.ts

37 lines
924 B
TypeScript
Raw Normal View History

2022-09-05 23:14:43 +03:00
import { getAddress } from "../../address/index.js";
import { toHex } from "../../utils/maths.js";
import { Typed } from "../typed.js";
import { Coder } from "./abstract-coder.js";
import type { Reader, Writer } from "./abstract-coder.js";
2022-11-28 05:54:49 +03:00
/**
* @_ignore
*/
2022-09-05 23:14:43 +03:00
export class AddressCoder extends Coder {
constructor(localName: string) {
super("address", "address", localName, false);
}
defaultValue(): string {
return "0x0000000000000000000000000000000000000000";
}
encode(writer: Writer, _value: string | Typed): number {
let value = Typed.dereference(_value, "string");
try {
value = getAddress(value);
} catch (error: any) {
return this._throwError(error.message, _value);
}
return writer.writeValue(value);
}
decode(reader: Reader): any {
return getAddress(toHex(reader.readValue(), 20));
}
}