Skip to content

Commit

Permalink
fun
Browse files Browse the repository at this point in the history
  • Loading branch information
hazae41 committed Nov 10, 2023
1 parent 045b583 commit 8bea9db
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 40 deletions.
16 changes: 8 additions & 8 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@rollup/plugin-json": "^6.0.1",
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-typescript": "^11.1.5",
"@types/node": "^20.8.10",
"@types/node": "^20.9.0",
"ethereumjs-abi": "^0.6.8",
"ethereumjs-util": "^7.1.5",
"ethers": "^6.8.1",
Expand All @@ -56,7 +56,7 @@
"rollup-plugin-node-externals": "^6.1.2",
"tslib": "^2.6.2",
"typescript": "^5.2.2",
"viem": "^1.18.8",
"viem": "^1.18.9",
"web3": "^4.2.2"
},
"exports": {
Expand Down
16 changes: 12 additions & 4 deletions src/mods/abi/signature/signature.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export function createFunctionSignature<T extends readonly Factory[] = Factory[]
) { }

static create(...instances: Factory.Instances<T>) {
const args = FunctionSignature.funcAndArgs.new(...instances)
const args = FunctionSignature.funcAndArgs.create(...instances)

return new FunctionSignature(args)
}
Expand All @@ -184,7 +184,15 @@ export function createFunctionSignature<T extends readonly Factory[] = Factory[]
}

static codegen() {
return `Abi.createFunctionSignature("${$name}",${$funcAndArgs.codegen()})`
return `Abi.createFunctionSignature(${JSON.stringify($name)},${$funcAndArgs.codegen()})`
}

intoOrThrow() {
return this.inner.intoOrThrow()
}

toJSON() {
return this.inner.toJSON()
}

encodeOrThrow() {
Expand All @@ -196,7 +204,7 @@ export function createFunctionSignature<T extends readonly Factory[] = Factory[]
}

static decodeOrThrow(cursor: TextCursor) {
return $funcAndArgs.decodeOrThrow(cursor)
return new FunctionSignature($funcAndArgs.decodeOrThrow(cursor))
}

sizeOrThrow() {
Expand All @@ -208,7 +216,7 @@ export function createFunctionSignature<T extends readonly Factory[] = Factory[]
}

static readOrThrow(cursor: Cursor) {
return $funcAndArgs.readOrThrow(cursor)
return new FunctionSignature($funcAndArgs.readOrThrow(cursor))
}

}
Expand Down
48 changes: 27 additions & 21 deletions src/mods/abi/types/function/function.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,27 +89,39 @@ export const createFunctionSelectorAndArguments = <T extends readonly Factory[]>
static readonly func = $func
static readonly args = $args

readonly func = this.#class.func
readonly args = this.#class.args

constructor(
readonly inner: TupleInstance<T>
readonly func: FunctionSelector,
readonly args: TupleInstance<T>
) { }

static new(...instances: Factory.Instances<T>) {
const args = FunctionSelectorAndArguments.args.new(instances)
static create(...instances: Factory.Instances<T>) {
const args = FunctionSelectorAndArguments.args.create(instances)

return new FunctionSelectorAndArguments(args)
return new FunctionSelectorAndArguments($func, args)
}

static from(...primitives: Factory.Primitives<T>) {
const args = FunctionSelectorAndArguments.args.from(primitives)

return new FunctionSelectorAndArguments(args)
return new FunctionSelectorAndArguments($func, args)
}

intoOrThrow() {
return this.inner.intoOrThrow()
return this.args.intoOrThrow()
}

toJSON() {
return this.args.toJSON()
}

verify() {
return Bytes.equals($func.value, this.func.value)
}

verifyOrThrow() {
if (!this.verify())
throw new Error(`Invalid function selector`)
return this
}

static codegen() {
Expand All @@ -121,40 +133,34 @@ export const createFunctionSelectorAndArguments = <T extends readonly Factory[]>
}

encodeOrThrow() {
return this.func.encodeOrThrow() + this.inner.encodeOrThrow()
return this.func.encodeOrThrow() + this.args.encodeOrThrow()
}

encodePackedOrThrow() {
return this.func.encodePackedOrThrow() + this.inner.encodePackedOrThrow()
return this.func.encodePackedOrThrow() + this.args.encodePackedOrThrow()
}

static decodeOrThrow(cursor: TextCursor) {
const func = FunctionSelector.decodeOrThrow(cursor)
const args = FunctionSelectorAndArguments.args.decodeOrThrow(cursor)

if (!Bytes.equals(func.value, this.func.value))
throw new Error(`Invalid function selector`)

return new FunctionSelectorAndArguments(args)
return new FunctionSelectorAndArguments(func, args)
}

sizeOrThrow() {
return this.func.sizeOrThrow() + this.inner.sizeOrThrow()
return this.func.sizeOrThrow() + this.args.sizeOrThrow()
}

writeOrThrow(cursor: Cursor) {
this.func.writeOrThrow(cursor)
this.inner.writeOrThrow(cursor)
this.args.writeOrThrow(cursor)
}

static readOrThrow(cursor: Cursor) {
const func = FunctionSelector.readOrThrow(cursor)
const args = FunctionSelectorAndArguments.args.readOrThrow(cursor)

if (!Bytes.equals(func.value, this.func.value))
throw new Error(`Invalid function selector`)

return new FunctionSelectorAndArguments(args)
return new FunctionSelectorAndArguments(func, args)
}

}
Expand Down
6 changes: 3 additions & 3 deletions src/mods/abi/types/tuple/tuple.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export const createTuple = <T extends readonly Factory[]>(...$types: T) => {
readonly size: number,
) { }

static new(instances: Factory.Instances<T>) {
static create(instances: Factory.Instances<T>) {
let length = 0
let offset = 0

Expand Down Expand Up @@ -71,7 +71,7 @@ export const createTuple = <T extends readonly Factory[]>(...$types: T) => {
for (let i = 0; i < AbiTuple.types.length; i++)
result[i] = AbiTuple.types[i].from(primitives[i])

return AbiTuple.new(result as Factory.Instances<T>)
return AbiTuple.create(result as Factory.Instances<T>)
}

intoOrThrow() {
Expand Down Expand Up @@ -214,7 +214,7 @@ export namespace AbiTuple {
}

export function isFactory<T extends readonly Factory[]>(x: Skeleton<TupleFactory<T>>): x is TupleFactory<T> {
return x.name === name && x.new != null
return x.name === name && x.create != null
}

}
4 changes: 2 additions & 2 deletions src/mods/abi/types/vector/vector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ export const createVector = <T extends Factory>($type: T) => {
readonly size: number,
) { }

static new(instances: Factory.Instances<T[]>) {
static create(instances: Factory.Instances<T[]>) {
let length = 0
let offset = 0

Expand Down Expand Up @@ -72,7 +72,7 @@ export const createVector = <T extends Factory>($type: T) => {
for (let i = 0; i < primitives.length; i++)
result[i] = AbiVector.type.from(primitives[i])

return AbiVector.new(result as any as Factory.Instances<T[]>)
return AbiVector.create(result as any as Factory.Instances<T[]>)
}

intoOrThrow() {
Expand Down

0 comments on commit 8bea9db

Please sign in to comment.