@meshconnect/uwc-injected-connector 0.27.2 → 0.27.3
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/dist/services/solana/solana-transaction-builder.d.ts.map +1 -1
- package/dist/services/solana/solana-transaction-builder.js +2 -13
- package/dist/services/solana/solana-transaction-builder.js.map +1 -1
- package/dist/services/solana/solana-transaction-service.d.ts.map +1 -1
- package/dist/services/solana/solana-transaction-service.js +2 -1
- package/dist/services/solana/solana-transaction-service.js.map +1 -1
- package/package.json +6 -6
- package/src/services/solana/__fixtures__/relay-solana-quotes.json +1133 -0
- package/src/services/solana/__fixtures__/wallets.ts +28 -0
- package/src/services/solana/solana-transaction-builder.lookup-tables.test.ts +379 -0
- package/src/services/solana/solana-transaction-builder.test.ts +31 -0
- package/src/services/solana/solana-transaction-builder.ts +6 -16
- package/src/services/solana/solana-transaction-service.lookup-tables.test.ts +116 -0
- package/src/services/solana/solana-transaction-service.regression.test.ts +216 -0
- package/src/services/solana/solana-transaction-service.relay-quotes.test.ts +232 -0
- package/src/services/solana/solana-transaction-service.ts +2 -1
- package/src/services/solana/solana-transaction-size.test.ts +91 -0
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { vi } from 'vitest'
|
|
2
|
+
import type { ExtendedStandardWalletAdapter } from '../../../wallet-standard-discovery'
|
|
3
|
+
|
|
4
|
+
// A wallet behind the iframe bridge: UWC hands it the serialized bytes.
|
|
5
|
+
export const bridgedWallet = (address: string) => {
|
|
6
|
+
const sent: Uint8Array[] = []
|
|
7
|
+
const adapter = {
|
|
8
|
+
publicKey: { toBase58: vi.fn().mockResolvedValue(address) },
|
|
9
|
+
supportedTransactionVersions: new Set(['legacy', 0]),
|
|
10
|
+
sendSerializedTransaction: vi.fn(async (bytes: Uint8Array) => {
|
|
11
|
+
sent.push(bytes)
|
|
12
|
+
return 'sig-bridge'
|
|
13
|
+
}),
|
|
14
|
+
sendTransaction: vi.fn()
|
|
15
|
+
} as unknown as ExtendedStandardWalletAdapter
|
|
16
|
+
return { adapter, sent }
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
// A wallet UWC talks to directly: it is handed the transaction object.
|
|
20
|
+
export const directWallet = (address: string) => {
|
|
21
|
+
const sendTransaction = vi.fn().mockResolvedValue('sig-direct')
|
|
22
|
+
const adapter = {
|
|
23
|
+
publicKey: { toBase58: vi.fn().mockResolvedValue(address) },
|
|
24
|
+
supportedTransactionVersions: new Set(['legacy', 0]),
|
|
25
|
+
sendTransaction
|
|
26
|
+
} as unknown as ExtendedStandardWalletAdapter
|
|
27
|
+
return { adapter, sendTransaction }
|
|
28
|
+
}
|
|
@@ -0,0 +1,379 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
// Real @solana/web3.js (not mocked): these tests compile and serialize actual
|
|
3
|
+
// V0 messages, which needs Node crypto rather than the repo-default jsdom.
|
|
4
|
+
import { describe, it, expect } from 'vitest'
|
|
5
|
+
import {
|
|
6
|
+
AddressLookupTableAccount,
|
|
7
|
+
PACKET_DATA_SIZE,
|
|
8
|
+
PublicKey,
|
|
9
|
+
TransactionMessage,
|
|
10
|
+
VersionedTransaction
|
|
11
|
+
} from '@solana/web3.js'
|
|
12
|
+
import type {
|
|
13
|
+
SolanaAddressLookupTable,
|
|
14
|
+
SolanaGenericTransferRequest,
|
|
15
|
+
SolanaTransactionInstruction
|
|
16
|
+
} from '@meshconnect/uwc-types'
|
|
17
|
+
import { SolanaTransactionBuilder } from './solana-transaction-builder'
|
|
18
|
+
|
|
19
|
+
const SENDER = '5uvp3sU4xEEwXeuR3x3HSn1oVUK7VNWf5TEKJUCoz8rn'
|
|
20
|
+
const MEMO_PROGRAM = 'MemoSq4gqABAXKb96qnH8TysNcWxMyWCqXgDLGmfcHr'
|
|
21
|
+
const SWAP_PROGRAM = 'JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4'
|
|
22
|
+
// u64::MAX: the slot web3.js reads as "never deactivated".
|
|
23
|
+
const ACTIVE = BigInt('18446744073709551615')
|
|
24
|
+
|
|
25
|
+
// Deterministic, distinct 32-byte keys standing in for a route's accounts.
|
|
26
|
+
const keyFor = (seed: number): string =>
|
|
27
|
+
new PublicKey(
|
|
28
|
+
Uint8Array.from({ length: 32 }, (_, i) => (seed * 7 + i * 13 + 1) % 256)
|
|
29
|
+
).toBase58()
|
|
30
|
+
|
|
31
|
+
const BLOCKHASH = keyFor(999)
|
|
32
|
+
const ROUTE_ACCOUNTS = Array.from({ length: 40 }, (_, i) => keyFor(i + 1))
|
|
33
|
+
const TABLE_KEY = keyFor(200)
|
|
34
|
+
const SECOND_TABLE_KEY = keyFor(201)
|
|
35
|
+
|
|
36
|
+
const builder = new SolanaTransactionBuilder()
|
|
37
|
+
|
|
38
|
+
const table = (
|
|
39
|
+
over: Partial<SolanaAddressLookupTable> = {}
|
|
40
|
+
): SolanaAddressLookupTable => ({
|
|
41
|
+
key: TABLE_KEY,
|
|
42
|
+
addresses: ROUTE_ACCOUNTS,
|
|
43
|
+
deactivationSlot: ACTIVE,
|
|
44
|
+
lastExtendedSlot: 280_000_000,
|
|
45
|
+
lastExtendedStartIndex: 0,
|
|
46
|
+
...over
|
|
47
|
+
})
|
|
48
|
+
|
|
49
|
+
// A multi-hop swap: the sender signs, the route touches every pool account.
|
|
50
|
+
const swapInstruction = (
|
|
51
|
+
accounts: string[] = ROUTE_ACCOUNTS,
|
|
52
|
+
writable = false
|
|
53
|
+
): SolanaTransactionInstruction => ({
|
|
54
|
+
programId: SWAP_PROGRAM,
|
|
55
|
+
accounts: [
|
|
56
|
+
{
|
|
57
|
+
shouldFillPubkey: false,
|
|
58
|
+
pubKey: SENDER,
|
|
59
|
+
isSigner: true,
|
|
60
|
+
isWritable: true
|
|
61
|
+
},
|
|
62
|
+
...accounts.map(pubKey => ({
|
|
63
|
+
shouldFillPubkey: false,
|
|
64
|
+
pubKey,
|
|
65
|
+
isSigner: false,
|
|
66
|
+
isWritable: writable
|
|
67
|
+
}))
|
|
68
|
+
],
|
|
69
|
+
data: Buffer.from('route').toString('base64')
|
|
70
|
+
})
|
|
71
|
+
|
|
72
|
+
const request = (
|
|
73
|
+
over: Partial<SolanaGenericTransferRequest> = {}
|
|
74
|
+
): SolanaGenericTransferRequest => ({
|
|
75
|
+
feePayer: SENDER,
|
|
76
|
+
blockhash: BLOCKHASH,
|
|
77
|
+
instructions: [swapInstruction()],
|
|
78
|
+
...over
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
const compile = (req: SolanaGenericTransferRequest) =>
|
|
82
|
+
builder.getVersionedTransaction(new PublicKey(SENDER), req)
|
|
83
|
+
|
|
84
|
+
// web3.js serializes into fixed PACKET_DATA_SIZE buffers, so an oversized
|
|
85
|
+
// message either throws while encoding (what users saw in production) or comes
|
|
86
|
+
// out longer than the limit. Either way the network would reject it.
|
|
87
|
+
const fitsInOnePacket = (tx: VersionedTransaction): boolean => {
|
|
88
|
+
try {
|
|
89
|
+
return tx.serialize().length <= PACKET_DATA_SIZE
|
|
90
|
+
} catch (error) {
|
|
91
|
+
if (error instanceof Error && /encoding overruns/.test(error.message)) {
|
|
92
|
+
return false
|
|
93
|
+
}
|
|
94
|
+
throw error
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const toLookupAccounts = (
|
|
99
|
+
states: SolanaAddressLookupTable[]
|
|
100
|
+
): AddressLookupTableAccount[] =>
|
|
101
|
+
states.map(
|
|
102
|
+
state =>
|
|
103
|
+
new AddressLookupTableAccount({
|
|
104
|
+
key: new PublicKey(state.key),
|
|
105
|
+
state: {
|
|
106
|
+
deactivationSlot: BigInt(state.deactivationSlot),
|
|
107
|
+
lastExtendedSlot: state.lastExtendedSlot,
|
|
108
|
+
lastExtendedSlotStartIndex: state.lastExtendedStartIndex,
|
|
109
|
+
addresses: state.addresses.map(a => new PublicKey(a))
|
|
110
|
+
}
|
|
111
|
+
})
|
|
112
|
+
)
|
|
113
|
+
|
|
114
|
+
describe('SolanaTransactionBuilder — address lookup tables', () => {
|
|
115
|
+
describe('the size problem the tables solve', () => {
|
|
116
|
+
it('a 40-account route without lookup tables goes over the transaction size limit', async () => {
|
|
117
|
+
expect(fitsInOnePacket(await compile(request()))).toBe(false)
|
|
118
|
+
})
|
|
119
|
+
|
|
120
|
+
it('the same route fits once its accounts are in a lookup table', async () => {
|
|
121
|
+
const tx = await compile(request({ states: [table()] }))
|
|
122
|
+
|
|
123
|
+
expect(fitsInOnePacket(tx)).toBe(true)
|
|
124
|
+
})
|
|
125
|
+
})
|
|
126
|
+
|
|
127
|
+
describe('compiled message', () => {
|
|
128
|
+
it('moves table accounts out of the static keys and into a table lookup', async () => {
|
|
129
|
+
const { message } = await compile(request({ states: [table()] }))
|
|
130
|
+
|
|
131
|
+
expect(message.addressTableLookups).toHaveLength(1)
|
|
132
|
+
expect(message.addressTableLookups[0]!.accountKey.toBase58()).toBe(
|
|
133
|
+
TABLE_KEY
|
|
134
|
+
)
|
|
135
|
+
expect(message.addressTableLookups[0]!.readonlyIndexes).toHaveLength(
|
|
136
|
+
ROUTE_ACCOUNTS.length
|
|
137
|
+
)
|
|
138
|
+
const staticKeys = message.staticAccountKeys.map(k => k.toBase58())
|
|
139
|
+
for (const account of ROUTE_ACCOUNTS) {
|
|
140
|
+
expect(staticKeys).not.toContain(account)
|
|
141
|
+
}
|
|
142
|
+
})
|
|
143
|
+
|
|
144
|
+
it('puts writable table accounts in writableIndexes, not readonlyIndexes', async () => {
|
|
145
|
+
const { message } = await compile(
|
|
146
|
+
request({
|
|
147
|
+
instructions: [swapInstruction(ROUTE_ACCOUNTS.slice(0, 3), true)],
|
|
148
|
+
states: [table()]
|
|
149
|
+
})
|
|
150
|
+
)
|
|
151
|
+
|
|
152
|
+
const lookup = message.addressTableLookups[0]!
|
|
153
|
+
expect(lookup.writableIndexes).toEqual([0, 1, 2])
|
|
154
|
+
expect(lookup.readonlyIndexes).toEqual([])
|
|
155
|
+
})
|
|
156
|
+
|
|
157
|
+
it('keeps the fee payer and signers static even when a table lists them', async () => {
|
|
158
|
+
const { message } = await compile(
|
|
159
|
+
request({ states: [table({ addresses: [SENDER, ...ROUTE_ACCOUNTS] })] })
|
|
160
|
+
)
|
|
161
|
+
|
|
162
|
+
expect(message.staticAccountKeys[0]!.toBase58()).toBe(SENDER)
|
|
163
|
+
expect(message.header.numRequiredSignatures).toBe(1)
|
|
164
|
+
const indexes = [
|
|
165
|
+
...message.addressTableLookups[0]!.writableIndexes,
|
|
166
|
+
...message.addressTableLookups[0]!.readonlyIndexes
|
|
167
|
+
]
|
|
168
|
+
expect(indexes).not.toContain(0)
|
|
169
|
+
})
|
|
170
|
+
|
|
171
|
+
it('keeps invoked program ids static even when a table lists them', async () => {
|
|
172
|
+
const { message } = await compile(
|
|
173
|
+
request({
|
|
174
|
+
states: [table({ addresses: [SWAP_PROGRAM, ...ROUTE_ACCOUNTS] })]
|
|
175
|
+
})
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
const staticKeys = message.staticAccountKeys.map(k => k.toBase58())
|
|
179
|
+
expect(staticKeys).toContain(SWAP_PROGRAM)
|
|
180
|
+
})
|
|
181
|
+
|
|
182
|
+
it('splits accounts across several tables and drops a table nothing uses', async () => {
|
|
183
|
+
const unused = table({ key: keyFor(202), addresses: [keyFor(300)] })
|
|
184
|
+
const { message } = await compile(
|
|
185
|
+
request({
|
|
186
|
+
states: [
|
|
187
|
+
table({ addresses: ROUTE_ACCOUNTS.slice(0, 20) }),
|
|
188
|
+
table({
|
|
189
|
+
key: SECOND_TABLE_KEY,
|
|
190
|
+
addresses: ROUTE_ACCOUNTS.slice(20)
|
|
191
|
+
}),
|
|
192
|
+
unused
|
|
193
|
+
]
|
|
194
|
+
})
|
|
195
|
+
)
|
|
196
|
+
|
|
197
|
+
expect(
|
|
198
|
+
message.addressTableLookups.map(l => l.accountKey.toBase58())
|
|
199
|
+
).toEqual([TABLE_KEY, SECOND_TABLE_KEY])
|
|
200
|
+
expect(message.addressTableLookups[0]!.readonlyIndexes).toHaveLength(20)
|
|
201
|
+
expect(message.addressTableLookups[1]!.readonlyIndexes).toHaveLength(20)
|
|
202
|
+
})
|
|
203
|
+
|
|
204
|
+
it('uses the table only for the accounts it holds, inlining the rest', async () => {
|
|
205
|
+
const { message } = await compile(
|
|
206
|
+
request({ states: [table({ addresses: ROUTE_ACCOUNTS.slice(0, 10) })] })
|
|
207
|
+
)
|
|
208
|
+
|
|
209
|
+
const staticKeys = message.staticAccountKeys.map(k => k.toBase58())
|
|
210
|
+
expect(message.addressTableLookups[0]!.readonlyIndexes).toHaveLength(10)
|
|
211
|
+
for (const account of ROUTE_ACCOUNTS.slice(10)) {
|
|
212
|
+
expect(staticKeys).toContain(account)
|
|
213
|
+
}
|
|
214
|
+
})
|
|
215
|
+
|
|
216
|
+
it('round-trips: decompiling with the same tables restores every instruction', async () => {
|
|
217
|
+
const states = [table()]
|
|
218
|
+
const tx = await compile(request({ states }))
|
|
219
|
+
const decoded = VersionedTransaction.deserialize(tx.serialize())
|
|
220
|
+
|
|
221
|
+
const restored = TransactionMessage.decompile(decoded.message, {
|
|
222
|
+
addressLookupTableAccounts: toLookupAccounts(states)
|
|
223
|
+
})
|
|
224
|
+
|
|
225
|
+
expect(restored.payerKey.toBase58()).toBe(SENDER)
|
|
226
|
+
expect(restored.recentBlockhash).toBe(BLOCKHASH)
|
|
227
|
+
expect(restored.instructions).toHaveLength(1)
|
|
228
|
+
const ix = restored.instructions[0]!
|
|
229
|
+
expect(ix.programId.toBase58()).toBe(SWAP_PROGRAM)
|
|
230
|
+
expect(ix.keys.map(k => k.pubkey.toBase58())).toEqual([
|
|
231
|
+
SENDER,
|
|
232
|
+
...ROUTE_ACCOUNTS
|
|
233
|
+
])
|
|
234
|
+
expect(Buffer.from(ix.data).toString()).toBe('route')
|
|
235
|
+
})
|
|
236
|
+
|
|
237
|
+
it('applies the tables to additionalRequests instructions too', async () => {
|
|
238
|
+
const { message } = await compile(
|
|
239
|
+
request({
|
|
240
|
+
instructions: [
|
|
241
|
+
{
|
|
242
|
+
programId: MEMO_PROGRAM,
|
|
243
|
+
accounts: [],
|
|
244
|
+
data: Buffer.from('m').toString('base64')
|
|
245
|
+
}
|
|
246
|
+
],
|
|
247
|
+
states: [table()],
|
|
248
|
+
additionalRequests: [
|
|
249
|
+
{
|
|
250
|
+
from: SENDER,
|
|
251
|
+
to: ROUTE_ACCOUNTS[0]!,
|
|
252
|
+
amount: 1n,
|
|
253
|
+
blockhash: BLOCKHASH
|
|
254
|
+
}
|
|
255
|
+
]
|
|
256
|
+
})
|
|
257
|
+
)
|
|
258
|
+
|
|
259
|
+
expect(message.addressTableLookups[0]!.writableIndexes).toEqual([0])
|
|
260
|
+
})
|
|
261
|
+
})
|
|
262
|
+
|
|
263
|
+
describe('no tables (unchanged behaviour)', () => {
|
|
264
|
+
it.each([
|
|
265
|
+
[
|
|
266
|
+
'states omitted',
|
|
267
|
+
request({ instructions: [swapInstruction(ROUTE_ACCOUNTS.slice(0, 3))] })
|
|
268
|
+
],
|
|
269
|
+
[
|
|
270
|
+
'states empty',
|
|
271
|
+
request({
|
|
272
|
+
instructions: [swapInstruction(ROUTE_ACCOUNTS.slice(0, 3))],
|
|
273
|
+
states: []
|
|
274
|
+
})
|
|
275
|
+
]
|
|
276
|
+
])(
|
|
277
|
+
'%s: compiles a V0 message with every account inline',
|
|
278
|
+
async (_, req) => {
|
|
279
|
+
const { message } = await compile(req)
|
|
280
|
+
|
|
281
|
+
expect(message.version).toBe(0)
|
|
282
|
+
expect(message.addressTableLookups).toEqual([])
|
|
283
|
+
expect(message.staticAccountKeys.map(k => k.toBase58())).toEqual(
|
|
284
|
+
expect.arrayContaining(ROUTE_ACCOUNTS.slice(0, 3))
|
|
285
|
+
)
|
|
286
|
+
}
|
|
287
|
+
)
|
|
288
|
+
|
|
289
|
+
it('a table with no addresses adds no lookup', async () => {
|
|
290
|
+
const { message } = await compile(
|
|
291
|
+
request({
|
|
292
|
+
instructions: [swapInstruction(ROUTE_ACCOUNTS.slice(0, 3))],
|
|
293
|
+
states: [table({ addresses: [] })]
|
|
294
|
+
})
|
|
295
|
+
)
|
|
296
|
+
|
|
297
|
+
expect(message.addressTableLookups).toEqual([])
|
|
298
|
+
})
|
|
299
|
+
})
|
|
300
|
+
|
|
301
|
+
describe('table state as it arrives over JSON', () => {
|
|
302
|
+
it.each([
|
|
303
|
+
['a bigint', ACTIVE],
|
|
304
|
+
['a number (a slot JSON can carry exactly)', 280_000_000],
|
|
305
|
+
['a string', '18446744073709551615']
|
|
306
|
+
])('accepts the deactivation slot as %s', async (_, slot) => {
|
|
307
|
+
const tx = await compile(
|
|
308
|
+
request({
|
|
309
|
+
states: [table({ deactivationSlot: slot as unknown as bigint })]
|
|
310
|
+
})
|
|
311
|
+
)
|
|
312
|
+
|
|
313
|
+
expect(tx.message.addressTableLookups).toHaveLength(1)
|
|
314
|
+
})
|
|
315
|
+
|
|
316
|
+
it.each([
|
|
317
|
+
['omitted', undefined],
|
|
318
|
+
['empty', '']
|
|
319
|
+
])('accepts an %s authority', async (_, authority) => {
|
|
320
|
+
const tx = await compile(request({ states: [table({ authority })] }))
|
|
321
|
+
|
|
322
|
+
expect(tx.message.addressTableLookups).toHaveLength(1)
|
|
323
|
+
})
|
|
324
|
+
|
|
325
|
+
it('accepts a valid authority', async () => {
|
|
326
|
+
const tx = await compile(
|
|
327
|
+
request({ states: [table({ authority: keyFor(400) })] })
|
|
328
|
+
)
|
|
329
|
+
|
|
330
|
+
expect(tx.message.addressTableLookups).toHaveLength(1)
|
|
331
|
+
})
|
|
332
|
+
})
|
|
333
|
+
|
|
334
|
+
describe('invalid table state fails with a clear error', () => {
|
|
335
|
+
it('an invalid table key', async () => {
|
|
336
|
+
await expect(
|
|
337
|
+
compile(request({ states: [table(), table({ key: 'not-a-key' })] }))
|
|
338
|
+
).rejects.toThrow('Invalid address lookup table 1 key: not-a-key')
|
|
339
|
+
})
|
|
340
|
+
|
|
341
|
+
it('an invalid address, naming the table and its index', async () => {
|
|
342
|
+
const addresses = [...ROUTE_ACCOUNTS]
|
|
343
|
+
addresses[5] = 'bad'
|
|
344
|
+
await expect(
|
|
345
|
+
compile(request({ states: [table({ addresses })] }))
|
|
346
|
+
).rejects.toThrow('Invalid address lookup table 0 address 5: bad')
|
|
347
|
+
})
|
|
348
|
+
|
|
349
|
+
it('an invalid authority', async () => {
|
|
350
|
+
await expect(
|
|
351
|
+
compile(request({ states: [table({ authority: '0x123' })] }))
|
|
352
|
+
).rejects.toThrow('Invalid address lookup table 0 authority: 0x123')
|
|
353
|
+
})
|
|
354
|
+
|
|
355
|
+
it('addresses that are not an array', async () => {
|
|
356
|
+
await expect(
|
|
357
|
+
compile(
|
|
358
|
+
request({
|
|
359
|
+
states: [table({ addresses: 'nope' as unknown as string[] })]
|
|
360
|
+
})
|
|
361
|
+
)
|
|
362
|
+
).rejects.toThrow(
|
|
363
|
+
'Invalid address lookup table 0: addresses must be an array'
|
|
364
|
+
)
|
|
365
|
+
})
|
|
366
|
+
|
|
367
|
+
it('a deactivation slot that is not an integer', async () => {
|
|
368
|
+
await expect(
|
|
369
|
+
compile(
|
|
370
|
+
request({
|
|
371
|
+
states: [table({ deactivationSlot: 'soon' as unknown as bigint })]
|
|
372
|
+
})
|
|
373
|
+
)
|
|
374
|
+
).rejects.toThrow(
|
|
375
|
+
'Invalid address lookup table 0 deactivation slot: soon'
|
|
376
|
+
)
|
|
377
|
+
})
|
|
378
|
+
})
|
|
379
|
+
})
|
|
@@ -237,4 +237,35 @@ describe('SolanaTransactionBuilder.buildTransferInstructions — generic request
|
|
|
237
237
|
)
|
|
238
238
|
expect(decoded.keys.destination.pubkey.toBase58()).toBe(WU_USDPT_ATA)
|
|
239
239
|
})
|
|
240
|
+
|
|
241
|
+
it('an account with no pubKey fails, naming the instruction and account index', async () => {
|
|
242
|
+
const req: SolanaGenericTransferRequest = {
|
|
243
|
+
feePayer: SENDER,
|
|
244
|
+
blockhash: BLOCKHASH,
|
|
245
|
+
instructions: [
|
|
246
|
+
{
|
|
247
|
+
programId: TOKEN_PROGRAM_ID.toBase58(),
|
|
248
|
+
accounts: [
|
|
249
|
+
{
|
|
250
|
+
shouldFillPubkey: false,
|
|
251
|
+
pubKey: SENDER,
|
|
252
|
+
isSigner: true,
|
|
253
|
+
isWritable: true
|
|
254
|
+
},
|
|
255
|
+
{
|
|
256
|
+
shouldFillPubkey: true,
|
|
257
|
+
pubKey: null,
|
|
258
|
+
isSigner: false,
|
|
259
|
+
isWritable: false
|
|
260
|
+
}
|
|
261
|
+
],
|
|
262
|
+
data: ''
|
|
263
|
+
}
|
|
264
|
+
]
|
|
265
|
+
}
|
|
266
|
+
|
|
267
|
+
await expect(builder.buildTransferInstructions(req)).rejects.toThrow(
|
|
268
|
+
'Account at instruction 0, index 1 has no pubKey and is not fillable'
|
|
269
|
+
)
|
|
270
|
+
})
|
|
240
271
|
})
|
|
@@ -4,8 +4,7 @@ import {
|
|
|
4
4
|
SystemProgram,
|
|
5
5
|
TransactionInstruction,
|
|
6
6
|
TransactionMessage,
|
|
7
|
-
VersionedTransaction
|
|
8
|
-
type AddressLookupTableState
|
|
7
|
+
VersionedTransaction
|
|
9
8
|
} from '@solana/web3.js'
|
|
10
9
|
import type {
|
|
11
10
|
SolanaNativeTransferRequest,
|
|
@@ -14,6 +13,7 @@ import type {
|
|
|
14
13
|
SolanaAccountMeta,
|
|
15
14
|
SolanaTransactionRequest
|
|
16
15
|
} from '@meshconnect/uwc-types'
|
|
16
|
+
import { parseLookupTables } from '@meshconnect/uwc-types'
|
|
17
17
|
import {
|
|
18
18
|
createTransferCheckedInstruction,
|
|
19
19
|
createAssociatedTokenAccountIdempotentInstruction,
|
|
@@ -198,19 +198,9 @@ export class SolanaTransactionBuilder {
|
|
|
198
198
|
return []
|
|
199
199
|
}
|
|
200
200
|
|
|
201
|
-
return (
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
205
|
-
lastExtendedSlotStartIndex: state.lastExtendedStartIndex,
|
|
206
|
-
addresses: [],
|
|
207
|
-
...(state.authority && { authority: new PublicKey(state.authority) })
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return new AddressLookupTableAccount({
|
|
211
|
-
key: new PublicKey(state.key),
|
|
212
|
-
state: currentState
|
|
213
|
-
})
|
|
214
|
-
})
|
|
201
|
+
return parseLookupTables(
|
|
202
|
+
request.states ?? [],
|
|
203
|
+
value => new PublicKey(value)
|
|
204
|
+
).map(table => new AddressLookupTableAccount(table))
|
|
215
205
|
}
|
|
216
206
|
}
|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
// @vitest-environment node
|
|
2
|
+
// Drives the real send path (service → builder → @solana/web3.js, nothing
|
|
3
|
+
// mocked but the wallet) and inspects what the wallet would be asked to sign.
|
|
4
|
+
import { describe, it, expect } from 'vitest'
|
|
5
|
+
import {
|
|
6
|
+
PACKET_DATA_SIZE,
|
|
7
|
+
PublicKey,
|
|
8
|
+
VersionedTransaction
|
|
9
|
+
} from '@solana/web3.js'
|
|
10
|
+
import type { SolanaGenericTransferRequest } from '@meshconnect/uwc-types'
|
|
11
|
+
import { WalletConnectorError } from '@meshconnect/uwc-types'
|
|
12
|
+
import { SolanaTransactionService } from './solana-transaction-service'
|
|
13
|
+
import { bridgedWallet, directWallet } from './__fixtures__/wallets'
|
|
14
|
+
|
|
15
|
+
const NETWORK = 'solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp'
|
|
16
|
+
const PAYER = '5uvp3sU4xEEwXeuR3x3HSn1oVUK7VNWf5TEKJUCoz8rn'
|
|
17
|
+
const PROGRAM = 'JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4'
|
|
18
|
+
|
|
19
|
+
const pubkey = (n: number): string =>
|
|
20
|
+
new PublicKey(
|
|
21
|
+
Uint8Array.from({ length: 32 }, (_, i) => (n + i * 3) % 256)
|
|
22
|
+
).toBase58()
|
|
23
|
+
|
|
24
|
+
const ROUTE = Array.from({ length: 45 }, (_, i) => pubkey(i + 10))
|
|
25
|
+
const TABLE = pubkey(250)
|
|
26
|
+
|
|
27
|
+
// The shape Link builds from a Relay Solana swap step.
|
|
28
|
+
const swapRequest = (withTables: boolean): SolanaGenericTransferRequest => ({
|
|
29
|
+
feePayer: PAYER,
|
|
30
|
+
blockhash: pubkey(77),
|
|
31
|
+
instructions: [
|
|
32
|
+
{
|
|
33
|
+
programId: PROGRAM,
|
|
34
|
+
accounts: [PAYER, ...ROUTE].map((pubKey, index) => ({
|
|
35
|
+
shouldFillPubkey: false,
|
|
36
|
+
pubKey,
|
|
37
|
+
isSigner: index === 0,
|
|
38
|
+
isWritable: index < 4
|
|
39
|
+
})),
|
|
40
|
+
data: Buffer.from([1, 2, 3]).toString('base64')
|
|
41
|
+
}
|
|
42
|
+
],
|
|
43
|
+
...(withTables && {
|
|
44
|
+
states: [
|
|
45
|
+
{
|
|
46
|
+
key: TABLE,
|
|
47
|
+
addresses: ROUTE,
|
|
48
|
+
deactivationSlot: BigInt('18446744073709551615'),
|
|
49
|
+
lastExtendedSlot: 1,
|
|
50
|
+
lastExtendedStartIndex: 0
|
|
51
|
+
}
|
|
52
|
+
]
|
|
53
|
+
})
|
|
54
|
+
})
|
|
55
|
+
|
|
56
|
+
describe('SolanaTransactionService — swap routes with address lookup tables', () => {
|
|
57
|
+
const service = new SolanaTransactionService({})
|
|
58
|
+
|
|
59
|
+
it('bridged wallet: receives a V0 transaction that uses the table and fits in one packet', async () => {
|
|
60
|
+
const { adapter, sent } = bridgedWallet(PAYER)
|
|
61
|
+
|
|
62
|
+
const signature = await service.sendTransaction(
|
|
63
|
+
swapRequest(true),
|
|
64
|
+
adapter,
|
|
65
|
+
NETWORK
|
|
66
|
+
)
|
|
67
|
+
|
|
68
|
+
expect(signature).toBe('sig-bridge')
|
|
69
|
+
expect(sent).toHaveLength(1)
|
|
70
|
+
expect(sent[0]!.length).toBeLessThanOrEqual(PACKET_DATA_SIZE)
|
|
71
|
+
const { message } = VersionedTransaction.deserialize(sent[0]!)
|
|
72
|
+
expect(message.version).toBe(0)
|
|
73
|
+
expect(message.staticAccountKeys[0]!.toBase58()).toBe(PAYER)
|
|
74
|
+
expect(message.addressTableLookups).toHaveLength(1)
|
|
75
|
+
const lookup = message.addressTableLookups[0]!
|
|
76
|
+
expect(lookup.accountKey.toBase58()).toBe(TABLE)
|
|
77
|
+
expect(lookup.writableIndexes).toEqual([0, 1, 2])
|
|
78
|
+
expect(lookup.readonlyIndexes).toHaveLength(ROUTE.length - 3)
|
|
79
|
+
})
|
|
80
|
+
|
|
81
|
+
it('direct wallet: receives the same lookup-table transaction object', async () => {
|
|
82
|
+
const { adapter, sendTransaction } = directWallet(PAYER)
|
|
83
|
+
|
|
84
|
+
const signature = await service.sendTransaction(
|
|
85
|
+
swapRequest(true),
|
|
86
|
+
adapter,
|
|
87
|
+
NETWORK
|
|
88
|
+
)
|
|
89
|
+
|
|
90
|
+
expect(signature).toBe('sig-direct')
|
|
91
|
+
const tx = sendTransaction.mock.calls[0]![0] as VersionedTransaction
|
|
92
|
+
expect(tx).toBeInstanceOf(VersionedTransaction)
|
|
93
|
+
expect(tx.message.addressTableLookups[0]!.accountKey.toBase58()).toBe(TABLE)
|
|
94
|
+
expect(tx.serialize().length).toBeLessThanOrEqual(PACKET_DATA_SIZE)
|
|
95
|
+
})
|
|
96
|
+
|
|
97
|
+
it('without tables the same route is too large and never reaches the wallet', async () => {
|
|
98
|
+
const { adapter } = bridgedWallet(PAYER)
|
|
99
|
+
|
|
100
|
+
await expect(
|
|
101
|
+
service.sendTransaction(swapRequest(false), adapter, NETWORK)
|
|
102
|
+
).rejects.toBeInstanceOf(WalletConnectorError)
|
|
103
|
+
expect(adapter.sendSerializedTransaction).not.toHaveBeenCalled()
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
it('an invalid table is reported as a connector error before the wallet is asked', async () => {
|
|
107
|
+
const { adapter } = bridgedWallet(PAYER)
|
|
108
|
+
const request = swapRequest(true)
|
|
109
|
+
request.states![0]!.key = 'not-a-table'
|
|
110
|
+
|
|
111
|
+
await expect(
|
|
112
|
+
service.sendTransaction(request, adapter, NETWORK)
|
|
113
|
+
).rejects.toThrow('Invalid address lookup table 0 key: not-a-table')
|
|
114
|
+
expect(adapter.sendSerializedTransaction).not.toHaveBeenCalled()
|
|
115
|
+
})
|
|
116
|
+
})
|