@fatsolutions/cairo-abi-codec 0.0.4 → 0.1.1
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.
- package/README.md +101 -14
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
# @fatsolutions/cairo-abi-codec
|
|
2
2
|
|
|
3
|
-
Type-safe encode/decode for Cairo structs and
|
|
3
|
+
Type-safe encode/decode for Cairo structs, enums, constructors, and events to Starknet calldata.
|
|
4
4
|
|
|
5
5
|
## Installation
|
|
6
6
|
|
|
@@ -100,6 +100,72 @@ decoded.activeVariant(); // => 'Move'
|
|
|
100
100
|
decoded.unwrap(); // => 42n
|
|
101
101
|
```
|
|
102
102
|
|
|
103
|
+
## Constructors
|
|
104
|
+
|
|
105
|
+
Encode/decode constructor calldata for deploy transactions:
|
|
106
|
+
|
|
107
|
+
```typescript
|
|
108
|
+
const abi = [
|
|
109
|
+
{
|
|
110
|
+
type: "constructor",
|
|
111
|
+
name: "constructor",
|
|
112
|
+
inputs: [
|
|
113
|
+
{ name: "owner", type: "core::starknet::contract_address::ContractAddress" },
|
|
114
|
+
{ name: "initial_supply", type: "core::integer::u64" },
|
|
115
|
+
],
|
|
116
|
+
},
|
|
117
|
+
] as const;
|
|
118
|
+
|
|
119
|
+
const codec = createTypedCodec(abi);
|
|
120
|
+
|
|
121
|
+
const encoded = codec.encodeConstructor({
|
|
122
|
+
owner: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
|
|
123
|
+
initial_supply: 1000n,
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const decoded = codec.decodeConstructor(encoded);
|
|
127
|
+
// => { owner: '0x049d...dc7', initial_supply: 1000n }
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
The constructor can reference struct/enum types defined in the same ABI.
|
|
131
|
+
|
|
132
|
+
## Events
|
|
133
|
+
|
|
134
|
+
Decode events from transaction receipts. Event members are split by `kind`: `"key"` members come from the keys array, `"data"` members from the data array.
|
|
135
|
+
|
|
136
|
+
```typescript
|
|
137
|
+
const abi = [
|
|
138
|
+
{
|
|
139
|
+
type: "event",
|
|
140
|
+
name: "Transfer",
|
|
141
|
+
kind: "struct",
|
|
142
|
+
members: [
|
|
143
|
+
{ name: "from", type: "core::starknet::contract_address::ContractAddress", kind: "key" },
|
|
144
|
+
{ name: "to", type: "core::starknet::contract_address::ContractAddress", kind: "key" },
|
|
145
|
+
{ name: "amount", type: "core::integer::u64", kind: "data" },
|
|
146
|
+
],
|
|
147
|
+
},
|
|
148
|
+
] as const;
|
|
149
|
+
|
|
150
|
+
const codec = createTypedCodec(abi);
|
|
151
|
+
|
|
152
|
+
// Decoding from a transaction receipt — strip the selector (keys[0]) first:
|
|
153
|
+
const decoded = codec.decodeEvent("Transfer", {
|
|
154
|
+
keys: receipt.events[0].keys.slice(1), // skip selector
|
|
155
|
+
data: receipt.events[0].data,
|
|
156
|
+
});
|
|
157
|
+
// => { from: '0x049d...dc7', to: '0x000...001', amount: 500n }
|
|
158
|
+
|
|
159
|
+
// Encoding (for testing) — returns { keys, data } without selector
|
|
160
|
+
const encoded = codec.encodeEvent("Transfer", {
|
|
161
|
+
from: "0x049d36570d4e46f48e99674bd3fcc84644ddd6b96f7c741b1562b82f9e004dc7",
|
|
162
|
+
to: "0x0000000000000000000000000000000000000000000000000000000000000001",
|
|
163
|
+
amount: 500n,
|
|
164
|
+
});
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
Only `kind: "struct"` events are supported. Enum events (`kind: "enum"`) are Cairo's internal dispatch pattern and don't need off-chain handling.
|
|
168
|
+
|
|
103
169
|
## ContractAddress
|
|
104
170
|
|
|
105
171
|
Decoded `ContractAddress` fields are automatically formatted as `0x`-prefixed, zero-padded 64-char hex strings. This applies in all positions: top-level struct members, nested structs, Options, Arrays, and custom enum variants.
|
|
@@ -124,19 +190,38 @@ const codec = createTypedCodec(subset);
|
|
|
124
190
|
|
|
125
191
|
### `createTypedCodec(abi)`
|
|
126
192
|
|
|
127
|
-
Creates a reusable codec with
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
193
|
+
Creates a reusable codec with the following methods:
|
|
194
|
+
|
|
195
|
+
| Method | Description |
|
|
196
|
+
|---|---|
|
|
197
|
+
| `encode(typeName, data)` | Encode a struct or enum to calldata |
|
|
198
|
+
| `decode(typeName, calldata)` | Decode calldata to a struct or enum |
|
|
199
|
+
| `encodeConstructor(data)` | Encode constructor arguments to calldata |
|
|
200
|
+
| `decodeConstructor(calldata)` | Decode constructor calldata |
|
|
201
|
+
| `encodeEvent(eventName, data)` | Encode event data to `{ keys, data }` |
|
|
202
|
+
| `decodeEvent(eventName, { keys, data })` | Decode event keys/data to a typed object |
|
|
203
|
+
|
|
204
|
+
### One-off helpers
|
|
205
|
+
|
|
206
|
+
| Function | Description |
|
|
207
|
+
|---|---|
|
|
208
|
+
| `encodeTyped(abi, typeName, data)` | One-off struct/enum encoding |
|
|
209
|
+
| `decodeTyped(abi, typeName, calldata)` | One-off struct/enum decoding |
|
|
210
|
+
| `encodeConstructor(abi, data)` | One-off constructor encoding |
|
|
211
|
+
| `decodeConstructor(abi, calldata)` | One-off constructor decoding |
|
|
212
|
+
| `encodeEvent(abi, eventName, data)` | One-off event encoding |
|
|
213
|
+
| `decodeEvent(abi, eventName, event)` | One-off event decoding |
|
|
214
|
+
|
|
215
|
+
### Type utilities
|
|
216
|
+
|
|
217
|
+
| Type | Description |
|
|
218
|
+
|---|---|
|
|
219
|
+
| `AbiType<TAbi, TName>` | TypeScript type for a struct or enum |
|
|
220
|
+
| `ConstructorArgs<TAbi>` | Typed object for constructor inputs |
|
|
221
|
+
| `AbiEventType<TAbi, TName>` | Typed object for event members |
|
|
222
|
+
| `ExtractAbiTypeNames<TAbi>` | Union of all struct/enum names |
|
|
223
|
+
| `ExtractAbiConstructor<TAbi>` | Constructor entry from the ABI |
|
|
224
|
+
| `ExtractAbiEventNames<TAbi>` | Union of all event names |
|
|
140
225
|
|
|
141
226
|
### `narrowAbi(abi, names)`
|
|
142
227
|
|
|
@@ -148,6 +233,8 @@ Filters an ABI to only the named struct/enum entries, preserving the const tuple
|
|
|
148
233
|
- Integer types (`u64`, `u128`, `u256`, `felt252`) resolve to `bigint`
|
|
149
234
|
- `Option<T>` resolves to `CairoOption<T>`, custom enums to `CairoCustomEnum`
|
|
150
235
|
- `ContractAddress` decodes to `0x`-prefixed hex strings (64 chars, zero-padded)
|
|
236
|
+
- Constructor ABIs use `inputs` (not `members`) — the codec handles this automatically
|
|
237
|
+
- Event `decodeEvent` expects keys **without** the selector — strip `keys[0]` from receipt data
|
|
151
238
|
|
|
152
239
|
## Build & Test
|
|
153
240
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fatsolutions/cairo-abi-codec",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.1.1",
|
|
4
4
|
"description": "Encode/decode arbitrary Cairo structs to calldata",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -39,7 +39,7 @@
|
|
|
39
39
|
},
|
|
40
40
|
"dependencies": {
|
|
41
41
|
"abi-wan-kanabi": "^2.2.4",
|
|
42
|
-
"starknet": "^9.4.2"
|
|
42
|
+
"starknet": "^9.4.2 || ^10.0.0"
|
|
43
43
|
},
|
|
44
44
|
"scripts": {
|
|
45
45
|
"build": "tsc",
|