@apibara/starknet 2.1.0-beta.4 → 2.1.0-beta.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,181 @@
1
+ /*
2
+
3
+ This file extends "abi-wan-kanabi" to provide a more type-safe way to decode events.
4
+
5
+ https://github.com/keep-starknet-strange/abi-wan-kanabi
6
+
7
+ This is free and unencumbered software released into the public domain.
8
+ Anyone is free to copy, modify, publish, use, compile, sell, or distribute this software, either in source code form or as a compiled binary, for any purpose, commercial or non-commercial, and by any means.
9
+ In jurisdictions that recognize copyright laws, the author or authors of this software dedicate any and all copyright interest in the software to the public domain. We make this dedication for the benefit of the public at large and to the detriment of our heirs and successors. We intend this dedication to be an overt act of relinquishment in perpetuity of all present and future rights to this software under copyright law.
10
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
11
+ For more information, please refer to https://unlicense.org
12
+
13
+ */
14
+
15
+ import type { Abi } from "abi-wan-kanabi";
16
+ import type {
17
+ AbiEventMember,
18
+ ExtractAbiEnum,
19
+ ExtractAbiEvent,
20
+ ExtractAbiEventNames,
21
+ StringToPrimitiveType as OriginalStringToPrimitiveType,
22
+ } from "abi-wan-kanabi/kanabi";
23
+
24
+ export type AbiEventStruct = {
25
+ type: "event";
26
+ name: string;
27
+ kind: "struct";
28
+ members: AbiEventMember[];
29
+ };
30
+
31
+ export type AbiMember = {
32
+ name: string;
33
+ type: string;
34
+ };
35
+
36
+ export type AbiEventEnum = {
37
+ type: "event";
38
+ name: string;
39
+ kind: "enum";
40
+ variants: AbiEventMember[];
41
+ };
42
+
43
+ export type AbiParameter = {
44
+ name: string;
45
+ type: string;
46
+ };
47
+
48
+ export type AbiEnum = {
49
+ type: "enum";
50
+ name: string;
51
+ variants: readonly AbiParameter[];
52
+ };
53
+
54
+ export type AbiEvent = AbiEventStruct | AbiEventEnum;
55
+
56
+ export type AbiItem = Abi[number];
57
+
58
+ // Custom StringToPrimitiveType that overrides abi-wan-kanabi's enum handling.
59
+ // The original StringToPrimitiveType from abi-wan-kanabi produces ObjectToUnion types
60
+ // for enums, which don't include the `_tag` property that our runtime parser generates.
61
+ // This custom version ensures TypeScript types match the actual runtime values.
62
+ export type StringToPrimitiveType<
63
+ TAbi extends Abi,
64
+ T extends string,
65
+ > = ExtractAbiEnum<TAbi, T> extends never
66
+ ? // Not an enum type, forward to original abi-wan-kanabi type
67
+ OriginalStringToPrimitiveType<TAbi, T>
68
+ : ExtractAbiEnum<TAbi, T> extends {
69
+ type: "enum";
70
+ variants: infer TVariants extends readonly AbiParameter[];
71
+ }
72
+ ? // It's an enum type, create tagged union with _tag property
73
+ {
74
+ [Variant in TVariants[number] as Variant["name"]]: Variant["type"] extends "()"
75
+ ? // Unit variant (no data): { _tag: "VariantName"; VariantName: null }
76
+ { _tag: Variant["name"] } & { [K in Variant["name"]]: null }
77
+ : // Variant with data: { _tag: "VariantName"; VariantName: StringToPrimitiveType }
78
+ { _tag: Variant["name"] } & {
79
+ [K in Variant["name"]]: StringToPrimitiveType<
80
+ TAbi,
81
+ Variant["type"]
82
+ >;
83
+ };
84
+ }[TVariants[number]["name"]]
85
+ : never;
86
+
87
+ export type DecodeEventArgs<
88
+ TAbi extends Abi = Abi,
89
+ TEventName extends ExtractAbiEventNames<TAbi> = ExtractAbiEventNames<TAbi>,
90
+ TStrict extends boolean = true,
91
+ > = {
92
+ abi: TAbi;
93
+ eventName: TEventName;
94
+ event: Event;
95
+ strict?: TStrict;
96
+ };
97
+
98
+ export type DecodedEvent<
99
+ TAbi extends Abi = Abi,
100
+ TEventName extends ExtractAbiEventNames<TAbi> = ExtractAbiEventNames<TAbi>,
101
+ > = Event & {
102
+ eventName: TEventName;
103
+ args: EventToPrimitiveType<TAbi, TEventName>;
104
+ };
105
+
106
+ export type DecodeEventReturn<
107
+ TAbi extends Abi = Abi,
108
+ TEventName extends ExtractAbiEventNames<TAbi> = ExtractAbiEventNames<TAbi>,
109
+ TStrict extends boolean = true,
110
+ > = TStrict extends true
111
+ ? DecodedEvent<TAbi, TEventName>
112
+ : DecodedEvent<TAbi, TEventName> | null;
113
+
114
+ // Helper type to resolve the payload type of a nested variant.
115
+ // when the type name corresponds to an event; resolves it using EventToPrimitiveType,
116
+ export type ResolveNestedVariantType<
117
+ TAbi extends Abi,
118
+ TTypeName extends string,
119
+ > = EventToPrimitiveType<TAbi, TTypeName>; // resolve its structure recursively
120
+
121
+ // Helper type to convert a variant member (nested or flat) into its corresponding tagged union part(s).
122
+ export type VariantToTaggedUnion<
123
+ TAbi extends Abi,
124
+ TVariant extends AbiEventMember,
125
+ > = TVariant extends { kind: "nested" }
126
+ ? // Nested: Use the helper to resolve the payload type.
127
+ { _tag: TVariant["name"] } & {
128
+ [K in TVariant["name"]]: ResolveNestedVariantType<TAbi, TVariant["type"]>;
129
+ }
130
+ : TVariant extends { kind: "flat" }
131
+ ? // Flat: Recursively call EventToPrimitiveType on the referenced event type.
132
+ // This will return the union of tagged types for the nested event.
133
+ EventToPrimitiveType<TAbi, TVariant["type"]>
134
+ : never; // Should not happen for valid ABIs
135
+
136
+ // Main type to convert an event definition (struct or enum) to its TS representation.
137
+ export type EventToPrimitiveType<
138
+ TAbi extends Abi,
139
+ TEventName extends ExtractAbiEventNames<TAbi>,
140
+ > = ExtractAbiEvent<TAbi, TEventName> extends infer TEventDef
141
+ ? TEventDef extends {
142
+ type: "event";
143
+ kind: "struct";
144
+ members: infer TMembers extends readonly AbiEventMember[];
145
+ }
146
+ ? // Struct Event: A simple object with member names as keys and their primitive types as values.
147
+ {
148
+ [Member in TMembers[number] as Member["name"]]: StringToPrimitiveType<
149
+ TAbi,
150
+ Member["type"]
151
+ >;
152
+ }
153
+ : TEventDef extends {
154
+ type: "event";
155
+ kind: "enum";
156
+ variants: infer TVariants extends readonly AbiEventMember[];
157
+ }
158
+ ? // Enum Event: Create a union of all possible tagged types derived from its variants.
159
+ {
160
+ // Map each variant to its corresponding tagged union structure(s).
161
+ [Idx in keyof TVariants]: VariantToTaggedUnion<TAbi, TVariants[Idx]>;
162
+ }[number] // Indexing with [number] converts the tuple of union parts into a single union type.
163
+ : // Explicitly handle empty enum events to ensure the `extends never` check works reliably.
164
+ TEventDef extends { type: "event"; kind: "enum"; variants: [] }
165
+ ? never
166
+ : // Not an event definition found for TEventName -> never
167
+ never
168
+ : // If the event name is not found in the ABI, return never.
169
+ never;
170
+
171
+ export function isEventAbi(item: AbiItem): item is AbiEvent {
172
+ return item.type === "event";
173
+ }
174
+
175
+ export function isStructEventAbi(item: AbiItem): item is AbiEventStruct {
176
+ return isEventAbi(item) && item.kind === "struct";
177
+ }
178
+
179
+ export function isEnumEventAbi(item: AbiItem): item is AbiEventEnum {
180
+ return isEventAbi(item) && item.kind === "enum";
181
+ }
package/src/abi.ts CHANGED
@@ -3,6 +3,7 @@ import { keccak } from "@scure/starknet";
3
3
  import type { FieldElement } from "./common";
4
4
  import {
5
5
  parseBool,
6
+ parseBytes31,
6
7
  parseContractAddress,
7
8
  parseFelt252,
8
9
  parseU8,
@@ -43,6 +44,7 @@ export const PrimitiveTypeParsers = {
43
44
  "core::integer::u64": parseU64,
44
45
  "core::integer::u128": parseU128,
45
46
  "core::integer::u256": parseU256,
47
+ "core::bytes_31::bytes31": parseBytes31,
46
48
  "core::starknet::contract_address::ContractAddress": parseContractAddress,
47
49
  };
48
50
 
@@ -77,3 +79,7 @@ export function getOptionType(type: string) {
77
79
  export function isEmptyType(type: string) {
78
80
  return type === "()";
79
81
  }
82
+
83
+ export function isByteArray(type: string) {
84
+ return type === "core::byte_array::ByteArray";
85
+ }