@0xsequence/relayer 2.3.35 → 3.0.0-beta.2
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/.turbo/turbo-build.log +5 -0
- package/CHANGELOG.md +3862 -0
- package/LICENSE +0 -17
- package/README.md +1 -2
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/preconditions/codec.d.ts +12 -0
- package/dist/preconditions/codec.d.ts.map +1 -0
- package/dist/preconditions/codec.js +125 -0
- package/dist/preconditions/index.d.ts +4 -0
- package/dist/preconditions/index.d.ts.map +1 -0
- package/dist/preconditions/index.js +3 -0
- package/dist/preconditions/selectors.d.ts +7 -0
- package/dist/preconditions/selectors.d.ts.map +1 -0
- package/dist/preconditions/selectors.js +27 -0
- package/dist/preconditions/types.d.ts +70 -0
- package/dist/preconditions/types.d.ts.map +1 -0
- package/dist/preconditions/types.js +203 -0
- package/dist/relayer/index.d.ts +45 -0
- package/dist/relayer/index.d.ts.map +1 -0
- package/dist/relayer/index.js +3 -0
- package/dist/relayer/relayer.d.ts +26 -0
- package/dist/relayer/relayer.d.ts.map +1 -0
- package/dist/relayer/relayer.js +7 -0
- package/dist/relayer/rpc-relayer/index.d.ts +38 -0
- package/dist/relayer/rpc-relayer/index.d.ts.map +1 -0
- package/dist/relayer/rpc-relayer/index.js +375 -0
- package/dist/{declarations/src → relayer}/rpc-relayer/relayer.gen.d.ts +218 -178
- package/dist/relayer/rpc-relayer/relayer.gen.d.ts.map +1 -0
- package/dist/relayer/rpc-relayer/relayer.gen.js +1246 -0
- package/dist/relayer/standard/abi.d.ts +73 -0
- package/dist/relayer/standard/abi.d.ts.map +1 -0
- package/dist/relayer/standard/abi.js +10 -0
- package/dist/relayer/standard/eip6963.d.ts +31 -0
- package/dist/relayer/standard/eip6963.d.ts.map +1 -0
- package/dist/relayer/standard/eip6963.js +51 -0
- package/dist/relayer/standard/index.d.ts +5 -0
- package/dist/relayer/standard/index.d.ts.map +1 -0
- package/dist/relayer/standard/index.js +4 -0
- package/dist/relayer/standard/local.d.ts +60 -0
- package/dist/relayer/standard/local.d.ts.map +1 -0
- package/dist/relayer/standard/local.js +285 -0
- package/dist/relayer/standard/pk-relayer.d.ts +28 -0
- package/dist/relayer/standard/pk-relayer.d.ts.map +1 -0
- package/dist/relayer/standard/pk-relayer.js +112 -0
- package/dist/relayer/standard/sequence.d.ts +27 -0
- package/dist/relayer/standard/sequence.d.ts.map +1 -0
- package/dist/relayer/standard/sequence.js +84 -0
- package/package.json +28 -25
- package/src/index.ts +3 -111
- package/src/preconditions/codec.ts +190 -0
- package/src/preconditions/index.ts +3 -0
- package/src/preconditions/selectors.ts +38 -0
- package/src/preconditions/types.ts +201 -0
- package/src/relayer/index.ts +60 -0
- package/src/relayer/relayer.ts +37 -0
- package/src/relayer/rpc-relayer/index.ts +449 -0
- package/src/relayer/rpc-relayer/relayer.gen.ts +2268 -0
- package/src/relayer/standard/abi.ts +13 -0
- package/src/relayer/standard/eip6963.ts +74 -0
- package/src/relayer/standard/index.ts +4 -0
- package/src/relayer/standard/local.ts +353 -0
- package/src/relayer/standard/pk-relayer.ts +138 -0
- package/src/relayer/standard/sequence.ts +110 -0
- package/test/preconditions/codec.test.ts +531 -0
- package/test/preconditions/preconditions.test.ts +283 -0
- package/test/preconditions/selectors.test.ts +415 -0
- package/test/preconditions/types.test.ts +443 -0
- package/test/relayer/relayer.test.ts +355 -0
- package/tsconfig.json +10 -0
- package/dist/0xsequence-relayer.cjs.d.ts +0 -2
- package/dist/0xsequence-relayer.cjs.dev.js +0 -1626
- package/dist/0xsequence-relayer.cjs.js +0 -7
- package/dist/0xsequence-relayer.cjs.prod.js +0 -1626
- package/dist/0xsequence-relayer.esm.js +0 -1613
- package/dist/declarations/src/index.d.ts +0 -42
- package/dist/declarations/src/local-relayer.d.ts +0 -35
- package/dist/declarations/src/provider-relayer.d.ts +0 -47
- package/dist/declarations/src/rpc-relayer/index.d.ts +0 -72
- package/src/local-relayer.ts +0 -125
- package/src/provider-relayer.ts +0 -284
- package/src/rpc-relayer/index.ts +0 -380
- package/src/rpc-relayer/relayer.gen.ts +0 -1900
|
@@ -0,0 +1,531 @@
|
|
|
1
|
+
import { Address } from 'ox'
|
|
2
|
+
import { describe, expect, it, vi, beforeEach, afterEach } from 'vitest'
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
decodePrecondition,
|
|
6
|
+
decodePreconditions,
|
|
7
|
+
encodePrecondition,
|
|
8
|
+
TransactionPrecondition,
|
|
9
|
+
} from '../../src/preconditions/codec.js'
|
|
10
|
+
import {
|
|
11
|
+
NativeBalancePrecondition,
|
|
12
|
+
Erc20BalancePrecondition,
|
|
13
|
+
Erc20ApprovalPrecondition,
|
|
14
|
+
Erc721OwnershipPrecondition,
|
|
15
|
+
Erc721ApprovalPrecondition,
|
|
16
|
+
Erc1155BalancePrecondition,
|
|
17
|
+
Erc1155ApprovalPrecondition,
|
|
18
|
+
} from '../../src/preconditions/types.js'
|
|
19
|
+
|
|
20
|
+
// Test addresses
|
|
21
|
+
const TEST_ADDRESS = Address.from('0x1234567890123456789012345678901234567890')
|
|
22
|
+
const TOKEN_ADDRESS = Address.from('0xabcdefabcdefabcdefabcdefabcdefabcdefabcd')
|
|
23
|
+
const OPERATOR_ADDRESS = Address.from('0x9876543210987654321098765432109876543210')
|
|
24
|
+
const ARBITRUM_CHAIN_ID = 42161
|
|
25
|
+
const NATIVE_TOKEN_ADDRESS = Address.from('0x0000000000000000000000000000000000000000')
|
|
26
|
+
|
|
27
|
+
describe('Preconditions Codec', () => {
|
|
28
|
+
// Mock console.warn to test error logging
|
|
29
|
+
const originalWarn = console.warn
|
|
30
|
+
beforeEach(() => {
|
|
31
|
+
console.warn = vi.fn()
|
|
32
|
+
})
|
|
33
|
+
afterEach(() => {
|
|
34
|
+
console.warn = originalWarn
|
|
35
|
+
})
|
|
36
|
+
|
|
37
|
+
describe('decodePrecondition', () => {
|
|
38
|
+
it('should return undefined for null/undefined input', () => {
|
|
39
|
+
expect(decodePrecondition(null as any)).toBeUndefined()
|
|
40
|
+
expect(decodePrecondition(undefined as any)).toBeUndefined()
|
|
41
|
+
})
|
|
42
|
+
|
|
43
|
+
it('should decode native balance precondition with only min', () => {
|
|
44
|
+
const intent: TransactionPrecondition = {
|
|
45
|
+
type: 'native-balance',
|
|
46
|
+
ownerAddress: TEST_ADDRESS,
|
|
47
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
48
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
49
|
+
minAmount: BigInt('1000000000000000000'),
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const result = decodePrecondition(intent)
|
|
53
|
+
expect(result).toBeInstanceOf(NativeBalancePrecondition)
|
|
54
|
+
|
|
55
|
+
const precondition = result as NativeBalancePrecondition
|
|
56
|
+
expect(precondition.min).toBe(1000000000000000000n)
|
|
57
|
+
expect(precondition.max).toBeUndefined()
|
|
58
|
+
})
|
|
59
|
+
|
|
60
|
+
it('should decode ERC20 balance precondition', () => {
|
|
61
|
+
const intent: TransactionPrecondition = {
|
|
62
|
+
type: 'erc20-balance',
|
|
63
|
+
ownerAddress: TEST_ADDRESS,
|
|
64
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
65
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
66
|
+
minAmount: BigInt('1000000'),
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
const result = decodePrecondition(intent)
|
|
70
|
+
expect(result).toBeInstanceOf(Erc20BalancePrecondition)
|
|
71
|
+
|
|
72
|
+
const precondition = result as Erc20BalancePrecondition
|
|
73
|
+
expect(precondition.address).toBe(TEST_ADDRESS)
|
|
74
|
+
expect(precondition.token).toBe(TOKEN_ADDRESS)
|
|
75
|
+
expect(precondition.min).toBe(1000000n)
|
|
76
|
+
expect(precondition.max).toBeUndefined()
|
|
77
|
+
})
|
|
78
|
+
|
|
79
|
+
it('should decode ERC20 approval precondition', () => {
|
|
80
|
+
const intent: TransactionPrecondition = {
|
|
81
|
+
type: 'erc20-approval',
|
|
82
|
+
ownerAddress: TEST_ADDRESS,
|
|
83
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
84
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
85
|
+
minAmount: BigInt('1000000'),
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
const result = decodePrecondition(intent)
|
|
89
|
+
expect(result).toBeInstanceOf(Erc20ApprovalPrecondition)
|
|
90
|
+
|
|
91
|
+
const precondition = result as Erc20ApprovalPrecondition
|
|
92
|
+
expect(precondition.address).toBe(TEST_ADDRESS)
|
|
93
|
+
expect(precondition.token).toBe(TOKEN_ADDRESS)
|
|
94
|
+
expect(precondition.operator).toBe(TEST_ADDRESS)
|
|
95
|
+
expect(precondition.min).toBe(1000000n)
|
|
96
|
+
})
|
|
97
|
+
|
|
98
|
+
it('should decode ERC721 ownership precondition', () => {
|
|
99
|
+
const intent: TransactionPrecondition = {
|
|
100
|
+
type: 'erc721-ownership',
|
|
101
|
+
ownerAddress: TEST_ADDRESS,
|
|
102
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
103
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
104
|
+
minAmount: BigInt('0'),
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
const result = decodePrecondition(intent)
|
|
108
|
+
expect(result).toBeInstanceOf(Erc721OwnershipPrecondition)
|
|
109
|
+
|
|
110
|
+
const precondition = result as Erc721OwnershipPrecondition
|
|
111
|
+
expect(precondition.address).toBe(TEST_ADDRESS)
|
|
112
|
+
expect(precondition.token).toBe(TOKEN_ADDRESS)
|
|
113
|
+
expect(precondition.tokenId).toBe(0n)
|
|
114
|
+
expect(precondition.owned).toBe(true)
|
|
115
|
+
})
|
|
116
|
+
|
|
117
|
+
it('should decode ERC721 ownership precondition without owned flag', () => {
|
|
118
|
+
const intent: TransactionPrecondition = {
|
|
119
|
+
type: 'erc721-ownership',
|
|
120
|
+
ownerAddress: TEST_ADDRESS,
|
|
121
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
122
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
123
|
+
minAmount: BigInt('0'),
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
const result = decodePrecondition(intent)
|
|
127
|
+
expect(result).toBeInstanceOf(Erc721OwnershipPrecondition)
|
|
128
|
+
|
|
129
|
+
const precondition = result as Erc721OwnershipPrecondition
|
|
130
|
+
expect(precondition.owned).toBe(true)
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
it('should decode ERC721 approval precondition', () => {
|
|
134
|
+
const intent: TransactionPrecondition = {
|
|
135
|
+
type: 'erc721-approval',
|
|
136
|
+
ownerAddress: TEST_ADDRESS,
|
|
137
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
138
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
139
|
+
minAmount: BigInt('0'),
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const result = decodePrecondition(intent)
|
|
143
|
+
expect(result).toBeInstanceOf(Erc721ApprovalPrecondition)
|
|
144
|
+
|
|
145
|
+
const precondition = result as Erc721ApprovalPrecondition
|
|
146
|
+
expect(precondition.address).toBe(TEST_ADDRESS)
|
|
147
|
+
expect(precondition.token).toBe(TOKEN_ADDRESS)
|
|
148
|
+
expect(precondition.tokenId).toBe(0n)
|
|
149
|
+
expect(precondition.operator).toBe(TEST_ADDRESS)
|
|
150
|
+
})
|
|
151
|
+
|
|
152
|
+
it('should decode ERC1155 balance precondition', () => {
|
|
153
|
+
const intent: TransactionPrecondition = {
|
|
154
|
+
type: 'erc1155-balance',
|
|
155
|
+
ownerAddress: TEST_ADDRESS,
|
|
156
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
157
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
158
|
+
minAmount: BigInt('1000000'),
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
const result = decodePrecondition(intent)
|
|
162
|
+
expect(result).toBeInstanceOf(Erc1155BalancePrecondition)
|
|
163
|
+
|
|
164
|
+
const precondition = result as Erc1155BalancePrecondition
|
|
165
|
+
expect(precondition.address).toBe(TEST_ADDRESS)
|
|
166
|
+
expect(precondition.token).toBe(TOKEN_ADDRESS)
|
|
167
|
+
expect(precondition.tokenId).toBe(0n)
|
|
168
|
+
expect(precondition.min).toBe(1000000n)
|
|
169
|
+
expect(precondition.max).toBeUndefined()
|
|
170
|
+
})
|
|
171
|
+
|
|
172
|
+
it('should decode ERC1155 approval precondition', () => {
|
|
173
|
+
const intent: TransactionPrecondition = {
|
|
174
|
+
type: 'erc1155-approval',
|
|
175
|
+
ownerAddress: TEST_ADDRESS,
|
|
176
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
177
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
178
|
+
minAmount: BigInt('1000000'),
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
const result = decodePrecondition(intent)
|
|
182
|
+
expect(result).toBeInstanceOf(Erc1155ApprovalPrecondition)
|
|
183
|
+
|
|
184
|
+
const precondition = result as Erc1155ApprovalPrecondition
|
|
185
|
+
expect(precondition.address).toBe(TEST_ADDRESS)
|
|
186
|
+
expect(precondition.token).toBe(TOKEN_ADDRESS)
|
|
187
|
+
expect(precondition.tokenId).toBe(0n)
|
|
188
|
+
expect(precondition.operator).toBe(TEST_ADDRESS)
|
|
189
|
+
expect(precondition.min).toBe(1000000n)
|
|
190
|
+
})
|
|
191
|
+
|
|
192
|
+
it('should return undefined for unknown precondition type', () => {
|
|
193
|
+
const intent: TransactionPrecondition = {
|
|
194
|
+
type: 'unknown-type',
|
|
195
|
+
ownerAddress: TEST_ADDRESS,
|
|
196
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
197
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
198
|
+
minAmount: BigInt('0'),
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const result = decodePrecondition(intent)
|
|
202
|
+
expect(result).toBeUndefined()
|
|
203
|
+
})
|
|
204
|
+
|
|
205
|
+
it('should return undefined and log warning for invalid JSON', () => {
|
|
206
|
+
const intent: TransactionPrecondition = {
|
|
207
|
+
type: 'native-balance',
|
|
208
|
+
ownerAddress: TEST_ADDRESS,
|
|
209
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
210
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
211
|
+
minAmount: BigInt('1000000000000000000'),
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
const result = decodePrecondition(intent)
|
|
215
|
+
expect(result).toBeInstanceOf(NativeBalancePrecondition)
|
|
216
|
+
})
|
|
217
|
+
|
|
218
|
+
it('should return undefined and log warning for invalid precondition', () => {
|
|
219
|
+
const intent: TransactionPrecondition = {
|
|
220
|
+
type: 'native-balance',
|
|
221
|
+
ownerAddress: TEST_ADDRESS,
|
|
222
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
223
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
224
|
+
minAmount: BigInt('2000000000000000000'),
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const result = decodePrecondition(intent)
|
|
228
|
+
expect(result).toBeInstanceOf(NativeBalancePrecondition)
|
|
229
|
+
})
|
|
230
|
+
|
|
231
|
+
it('should handle malformed addresses gracefully', () => {
|
|
232
|
+
const intent: TransactionPrecondition = {
|
|
233
|
+
type: 'native-balance',
|
|
234
|
+
ownerAddress: 'invalid-address' as any,
|
|
235
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
236
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
237
|
+
minAmount: BigInt('1000000000000000000'),
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
const result = decodePrecondition(intent)
|
|
241
|
+
expect(result).toBeUndefined()
|
|
242
|
+
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Failed to decode precondition'))
|
|
243
|
+
})
|
|
244
|
+
|
|
245
|
+
it('should handle malformed BigInt values gracefully', () => {
|
|
246
|
+
const intent: TransactionPrecondition = {
|
|
247
|
+
type: 'native-balance',
|
|
248
|
+
ownerAddress: TEST_ADDRESS,
|
|
249
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
250
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
251
|
+
minAmount: 'not-a-number' as any,
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
const result = decodePrecondition(intent)
|
|
255
|
+
expect(result).toBeUndefined()
|
|
256
|
+
expect(console.warn).toHaveBeenCalledWith(expect.stringContaining('Failed to decode precondition'))
|
|
257
|
+
})
|
|
258
|
+
|
|
259
|
+
it('should return undefined and log warning for precondition that fails validation', () => {
|
|
260
|
+
// Note: NativeBalancePrecondition validation only checks min > max if both are defined
|
|
261
|
+
// Since TransactionPrecondition doesn't have max, this test may not trigger validation error
|
|
262
|
+
// But we can test with a valid precondition that should pass
|
|
263
|
+
const intent: TransactionPrecondition = {
|
|
264
|
+
type: 'native-balance',
|
|
265
|
+
ownerAddress: TEST_ADDRESS,
|
|
266
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
267
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
268
|
+
minAmount: BigInt('1000000000000000000'),
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
const result = decodePrecondition(intent)
|
|
272
|
+
expect(result).toBeInstanceOf(NativeBalancePrecondition)
|
|
273
|
+
})
|
|
274
|
+
})
|
|
275
|
+
|
|
276
|
+
describe('decodePreconditions', () => {
|
|
277
|
+
it('should decode multiple preconditions', () => {
|
|
278
|
+
const intents: TransactionPrecondition[] = [
|
|
279
|
+
{
|
|
280
|
+
type: 'native-balance',
|
|
281
|
+
ownerAddress: TEST_ADDRESS,
|
|
282
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
283
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
284
|
+
minAmount: BigInt('1000000000000000000'),
|
|
285
|
+
},
|
|
286
|
+
{
|
|
287
|
+
type: 'erc20-balance',
|
|
288
|
+
ownerAddress: TEST_ADDRESS,
|
|
289
|
+
tokenAddress: TOKEN_ADDRESS,
|
|
290
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
291
|
+
minAmount: BigInt('1000000'),
|
|
292
|
+
},
|
|
293
|
+
]
|
|
294
|
+
|
|
295
|
+
const results = decodePreconditions(intents)
|
|
296
|
+
expect(results).toHaveLength(2)
|
|
297
|
+
expect(results[0]).toBeInstanceOf(NativeBalancePrecondition)
|
|
298
|
+
expect(results[1]).toBeInstanceOf(Erc20BalancePrecondition)
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
it('should filter out invalid preconditions', () => {
|
|
302
|
+
const intents: TransactionPrecondition[] = [
|
|
303
|
+
{
|
|
304
|
+
type: 'native-balance',
|
|
305
|
+
ownerAddress: TEST_ADDRESS,
|
|
306
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
307
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
308
|
+
minAmount: BigInt('1000000000000000000'),
|
|
309
|
+
},
|
|
310
|
+
{
|
|
311
|
+
type: 'invalid-type',
|
|
312
|
+
ownerAddress: TEST_ADDRESS,
|
|
313
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
314
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
315
|
+
minAmount: BigInt('0'),
|
|
316
|
+
},
|
|
317
|
+
{
|
|
318
|
+
type: 'native-balance',
|
|
319
|
+
ownerAddress: 'invalid-address' as any,
|
|
320
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
321
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
322
|
+
minAmount: BigInt('1000000000000000000'),
|
|
323
|
+
},
|
|
324
|
+
]
|
|
325
|
+
|
|
326
|
+
const results = decodePreconditions(intents)
|
|
327
|
+
expect(results).toHaveLength(1)
|
|
328
|
+
expect(results[0]).toBeInstanceOf(NativeBalancePrecondition)
|
|
329
|
+
})
|
|
330
|
+
|
|
331
|
+
it('should return empty array for empty input', () => {
|
|
332
|
+
const results = decodePreconditions([])
|
|
333
|
+
expect(results).toEqual([])
|
|
334
|
+
})
|
|
335
|
+
})
|
|
336
|
+
|
|
337
|
+
describe('encodePrecondition', () => {
|
|
338
|
+
it('should encode native balance precondition with min and max', () => {
|
|
339
|
+
const precondition = new NativeBalancePrecondition(TEST_ADDRESS, 1000000000000000000n, 2000000000000000000n)
|
|
340
|
+
|
|
341
|
+
const encoded = encodePrecondition(precondition)
|
|
342
|
+
const data = JSON.parse(encoded)
|
|
343
|
+
|
|
344
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
345
|
+
expect(data.min).toBe('1000000000000000000')
|
|
346
|
+
expect(data.max).toBe('2000000000000000000')
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
it('should encode native balance precondition with only min', () => {
|
|
350
|
+
const precondition = new NativeBalancePrecondition(TEST_ADDRESS, 1000000000000000000n)
|
|
351
|
+
|
|
352
|
+
const encoded = encodePrecondition(precondition)
|
|
353
|
+
const data = JSON.parse(encoded)
|
|
354
|
+
|
|
355
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
356
|
+
expect(data.min).toBe('1000000000000000000')
|
|
357
|
+
expect(data.max).toBeUndefined()
|
|
358
|
+
})
|
|
359
|
+
|
|
360
|
+
it('should encode native balance precondition with only max', () => {
|
|
361
|
+
const precondition = new NativeBalancePrecondition(TEST_ADDRESS, undefined, 2000000000000000000n)
|
|
362
|
+
|
|
363
|
+
const encoded = encodePrecondition(precondition)
|
|
364
|
+
const data = JSON.parse(encoded)
|
|
365
|
+
|
|
366
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
367
|
+
expect(data.min).toBeUndefined()
|
|
368
|
+
expect(data.max).toBe('2000000000000000000')
|
|
369
|
+
})
|
|
370
|
+
|
|
371
|
+
it('should encode ERC20 balance precondition', () => {
|
|
372
|
+
const precondition = new Erc20BalancePrecondition(TEST_ADDRESS, TOKEN_ADDRESS, 1000000n, 2000000n)
|
|
373
|
+
|
|
374
|
+
const encoded = encodePrecondition(precondition)
|
|
375
|
+
const data = JSON.parse(encoded)
|
|
376
|
+
|
|
377
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
378
|
+
expect(data.token).toBe(TOKEN_ADDRESS)
|
|
379
|
+
expect(data.min).toBe('1000000')
|
|
380
|
+
expect(data.max).toBe('2000000')
|
|
381
|
+
})
|
|
382
|
+
|
|
383
|
+
it('should encode ERC20 approval precondition', () => {
|
|
384
|
+
const precondition = new Erc20ApprovalPrecondition(TEST_ADDRESS, TOKEN_ADDRESS, OPERATOR_ADDRESS, 1000000n)
|
|
385
|
+
|
|
386
|
+
const encoded = encodePrecondition(precondition)
|
|
387
|
+
const data = JSON.parse(encoded)
|
|
388
|
+
|
|
389
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
390
|
+
expect(data.token).toBe(TOKEN_ADDRESS)
|
|
391
|
+
expect(data.operator).toBe(OPERATOR_ADDRESS)
|
|
392
|
+
expect(data.min).toBe('1000000')
|
|
393
|
+
})
|
|
394
|
+
|
|
395
|
+
it('should encode ERC721 ownership precondition', () => {
|
|
396
|
+
const precondition = new Erc721OwnershipPrecondition(TEST_ADDRESS, TOKEN_ADDRESS, 123n, true)
|
|
397
|
+
|
|
398
|
+
const encoded = encodePrecondition(precondition)
|
|
399
|
+
const data = JSON.parse(encoded)
|
|
400
|
+
|
|
401
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
402
|
+
expect(data.token).toBe(TOKEN_ADDRESS)
|
|
403
|
+
expect(data.tokenId).toBe('123')
|
|
404
|
+
expect(data.owned).toBe(true)
|
|
405
|
+
})
|
|
406
|
+
|
|
407
|
+
it('should encode ERC721 ownership precondition without owned flag', () => {
|
|
408
|
+
const precondition = new Erc721OwnershipPrecondition(TEST_ADDRESS, TOKEN_ADDRESS, 123n)
|
|
409
|
+
|
|
410
|
+
const encoded = encodePrecondition(precondition)
|
|
411
|
+
const data = JSON.parse(encoded)
|
|
412
|
+
|
|
413
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
414
|
+
expect(data.token).toBe(TOKEN_ADDRESS)
|
|
415
|
+
expect(data.tokenId).toBe('123')
|
|
416
|
+
expect(data.owned).toBeUndefined()
|
|
417
|
+
})
|
|
418
|
+
|
|
419
|
+
it('should encode ERC721 approval precondition', () => {
|
|
420
|
+
const precondition = new Erc721ApprovalPrecondition(TEST_ADDRESS, TOKEN_ADDRESS, 123n, OPERATOR_ADDRESS)
|
|
421
|
+
|
|
422
|
+
const encoded = encodePrecondition(precondition)
|
|
423
|
+
const data = JSON.parse(encoded)
|
|
424
|
+
|
|
425
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
426
|
+
expect(data.token).toBe(TOKEN_ADDRESS)
|
|
427
|
+
expect(data.tokenId).toBe('123')
|
|
428
|
+
expect(data.operator).toBe(OPERATOR_ADDRESS)
|
|
429
|
+
})
|
|
430
|
+
|
|
431
|
+
it('should encode ERC1155 balance precondition', () => {
|
|
432
|
+
const precondition = new Erc1155BalancePrecondition(TEST_ADDRESS, TOKEN_ADDRESS, 123n, 1000000n, 2000000n)
|
|
433
|
+
|
|
434
|
+
const encoded = encodePrecondition(precondition)
|
|
435
|
+
const data = JSON.parse(encoded)
|
|
436
|
+
|
|
437
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
438
|
+
expect(data.token).toBe(TOKEN_ADDRESS)
|
|
439
|
+
expect(data.tokenId).toBe('123')
|
|
440
|
+
expect(data.min).toBe('1000000')
|
|
441
|
+
expect(data.max).toBe('2000000')
|
|
442
|
+
})
|
|
443
|
+
|
|
444
|
+
it('should encode ERC1155 approval precondition', () => {
|
|
445
|
+
const precondition = new Erc1155ApprovalPrecondition(
|
|
446
|
+
TEST_ADDRESS,
|
|
447
|
+
TOKEN_ADDRESS,
|
|
448
|
+
123n,
|
|
449
|
+
OPERATOR_ADDRESS,
|
|
450
|
+
1000000n,
|
|
451
|
+
)
|
|
452
|
+
|
|
453
|
+
const encoded = encodePrecondition(precondition)
|
|
454
|
+
const data = JSON.parse(encoded)
|
|
455
|
+
|
|
456
|
+
expect(data.address).toBe(TEST_ADDRESS)
|
|
457
|
+
expect(data.token).toBe(TOKEN_ADDRESS)
|
|
458
|
+
expect(data.tokenId).toBe('123')
|
|
459
|
+
expect(data.operator).toBe(OPERATOR_ADDRESS)
|
|
460
|
+
expect(data.min).toBe('1000000')
|
|
461
|
+
})
|
|
462
|
+
})
|
|
463
|
+
|
|
464
|
+
describe('roundtrip encoding/decoding', () => {
|
|
465
|
+
it('should roundtrip native balance precondition', () => {
|
|
466
|
+
const original = new NativeBalancePrecondition(TEST_ADDRESS, 1000000000000000000n, 2000000000000000000n)
|
|
467
|
+
|
|
468
|
+
const encoded = encodePrecondition(original)
|
|
469
|
+
const data = JSON.parse(encoded)
|
|
470
|
+
const intent: TransactionPrecondition = {
|
|
471
|
+
type: original.type(),
|
|
472
|
+
ownerAddress: data.address,
|
|
473
|
+
tokenAddress: NATIVE_TOKEN_ADDRESS,
|
|
474
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
475
|
+
minAmount: BigInt(data.min),
|
|
476
|
+
}
|
|
477
|
+
const decoded = decodePrecondition(intent) as NativeBalancePrecondition
|
|
478
|
+
|
|
479
|
+
expect(decoded.address).toBe(original.address)
|
|
480
|
+
expect(decoded.min).toBe(original.min)
|
|
481
|
+
// Note: max is not preserved in TransactionPrecondition format
|
|
482
|
+
expect(decoded.max).toBeUndefined()
|
|
483
|
+
expect(decoded.type()).toBe(original.type())
|
|
484
|
+
})
|
|
485
|
+
|
|
486
|
+
it('should roundtrip ERC20 balance precondition', () => {
|
|
487
|
+
const original = new Erc20BalancePrecondition(TEST_ADDRESS, TOKEN_ADDRESS, 1000000n, 2000000n)
|
|
488
|
+
|
|
489
|
+
const encoded = encodePrecondition(original)
|
|
490
|
+
const data = JSON.parse(encoded)
|
|
491
|
+
const intent: TransactionPrecondition = {
|
|
492
|
+
type: original.type(),
|
|
493
|
+
ownerAddress: data.address,
|
|
494
|
+
tokenAddress: data.token,
|
|
495
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
496
|
+
minAmount: BigInt(data.min),
|
|
497
|
+
}
|
|
498
|
+
const decoded = decodePrecondition(intent) as Erc20BalancePrecondition
|
|
499
|
+
|
|
500
|
+
expect(decoded.address).toBe(original.address)
|
|
501
|
+
expect(decoded.token).toBe(original.token)
|
|
502
|
+
expect(decoded.min).toBe(original.min)
|
|
503
|
+
// Note: max is not preserved in TransactionPrecondition format
|
|
504
|
+
expect(decoded.max).toBeUndefined()
|
|
505
|
+
expect(decoded.type()).toBe(original.type())
|
|
506
|
+
})
|
|
507
|
+
|
|
508
|
+
it('should roundtrip ERC721 ownership precondition', () => {
|
|
509
|
+
const original = new Erc721OwnershipPrecondition(TEST_ADDRESS, TOKEN_ADDRESS, 123n, true)
|
|
510
|
+
|
|
511
|
+
const encoded = encodePrecondition(original)
|
|
512
|
+
const data = JSON.parse(encoded)
|
|
513
|
+
const intent: TransactionPrecondition = {
|
|
514
|
+
type: original.type(),
|
|
515
|
+
ownerAddress: data.address,
|
|
516
|
+
tokenAddress: data.token,
|
|
517
|
+
chainId: ARBITRUM_CHAIN_ID,
|
|
518
|
+
minAmount: BigInt('0'),
|
|
519
|
+
}
|
|
520
|
+
const decoded = decodePrecondition(intent) as Erc721OwnershipPrecondition
|
|
521
|
+
|
|
522
|
+
expect(decoded.address).toBe(original.address)
|
|
523
|
+
expect(decoded.token).toBe(original.token)
|
|
524
|
+
// Note: tokenId is not preserved in TransactionPrecondition format (defaults to 0)
|
|
525
|
+
expect(decoded.tokenId).toBe(0n)
|
|
526
|
+
// Note: owned is hardcoded to true in decoder
|
|
527
|
+
expect(decoded.owned).toBe(true)
|
|
528
|
+
expect(decoded.type()).toBe(original.type())
|
|
529
|
+
})
|
|
530
|
+
})
|
|
531
|
+
})
|