-
Notifications
You must be signed in to change notification settings - Fork 155
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
191 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
import { bytes, bytes32 } from "../codecs/primitives"; | ||
import { Src } from "../src"; | ||
import { IndexedCodec, ParsedNamedCodecList } from "../codec"; | ||
|
||
export interface EventRecord { | ||
topics: string[]; | ||
data: string; | ||
} | ||
|
||
export type IndexedCodecs<T> = T extends readonly [ | ||
{ indexed: true; isDynamic: true; name?: string }, | ||
...infer R | ||
] | ||
? [ | ||
typeof bytes32 & { indexed: true; name: T[0]["name"] }, | ||
...IndexedCodecs<R> | ||
] | ||
: T extends readonly [any, ...infer R] | ||
? [T[0], ...IndexedCodecs<R>] | ||
: T extends readonly [] | ||
? [] | ||
: never; | ||
|
||
export class AbiEvent< | ||
const T extends ReadonlyArray<IndexedCodec<any, string>> | ||
> { | ||
public readonly params: any; | ||
constructor(public readonly topic: string, ...args: T) { | ||
this.params = args.map((arg) => | ||
arg.indexed && arg.isDynamic | ||
? { | ||
...bytes32, | ||
name: arg.name, | ||
isDynamic: true, | ||
indexed: true, | ||
} | ||
: arg | ||
) as IndexedCodecs<T>; | ||
} | ||
|
||
is(rec: EventRecord): boolean { | ||
return rec.topics[0] === this.topic; | ||
} | ||
|
||
decode(rec: EventRecord): ParsedNamedCodecList<IndexedCodecs<T>> { | ||
const src = new Src(Buffer.from(rec.data.slice(2), "hex")); | ||
const result = {} as any; | ||
let topicCounter = 1; | ||
for (let i = 0; i < this.params.length; i++) { | ||
if (this.params[i].indexed) { | ||
const topic = rec.topics[topicCounter++]; | ||
const topicSrc = new Src(Buffer.from(topic.slice(2), "hex")); | ||
result[this.params[i].name ?? i] = this.params[i].decode(topicSrc); | ||
} else { | ||
result[this.params[i].name ?? i] = this.params[i].decode(src); | ||
} | ||
} | ||
return result; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import { describe, expect, it } from "vitest"; | ||
import { encodeAbiParameters, encodeEventTopics, parseAbiItem } from "viem"; | ||
import { | ||
arg, | ||
bool, | ||
bytes, | ||
indexed, | ||
string, | ||
struct, | ||
uint256, | ||
event as _event, | ||
} from "../src"; | ||
|
||
describe("Event", () => { | ||
it("decodes simple args", () => { | ||
const topics = encodeEventTopics({ | ||
abi: [parseAbiItem("event Test(uint256 indexed a, uint256 b)")], | ||
eventName: "Test", | ||
args: { a: 123n }, | ||
}); | ||
const event = _event( | ||
topics[0], | ||
indexed(arg("a", uint256)), | ||
arg("b", uint256) | ||
); | ||
const decoded = event.decode({ | ||
topics, | ||
data: encodeAbiParameters([{ type: "uint256" }], [100n]), | ||
}); | ||
expect(decoded).toEqual({ a: 123n, b: 100n }); | ||
}); | ||
|
||
it("decodes complex args", () => { | ||
const topics = encodeEventTopics({ | ||
abi: [ | ||
parseAbiItem( | ||
"event Test(string indexed a, string b, bytes c, (uint256, string) d, bool indexed e)" | ||
), | ||
], | ||
eventName: "Test", | ||
args: { a: "xdxdxd", e: true }, | ||
}); | ||
const event = _event( | ||
topics[0], | ||
indexed(arg("a", string)), | ||
arg("b", string), | ||
arg("c", bytes), | ||
arg("d", struct(uint256, string)), | ||
indexed(arg("e", bool)) | ||
); | ||
const decoded = event.decode({ | ||
topics, | ||
data: encodeAbiParameters( | ||
[ | ||
{ type: "string" }, | ||
{ type: "bytes" }, | ||
{ | ||
type: "tuple", | ||
components: [{ type: "uint256" }, { type: "string" }], | ||
}, | ||
], | ||
["hello", "0x1234", [100n, "world"]] | ||
), | ||
}); | ||
expect(decoded).toEqual({ | ||
a: Buffer.from(topics[1].slice(2), "hex"), | ||
b: "hello", | ||
c: Buffer.from([0x12, 0x34]), | ||
d: { 0: 100n, 1: "world" }, | ||
e: true, | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters