1
- import { TransactionMessageWithBlockhashLifetime } from '../blockhash' ;
2
- import { TransactionMessageWithDurableNonceLifetime } from '../durable-nonce' ;
1
+ import { Address } from '@solana/addresses' ;
2
+ import { pipe } from '@solana/functional' ;
3
+
4
+ import { setTransactionMessageLifetimeUsingBlockhash , TransactionMessageWithBlockhashLifetime } from '../blockhash' ;
5
+ import { CompilableTransactionMessage } from '../compilable-transaction-message' ;
6
+ import { createTransactionMessage } from '../create-transaction-message' ;
7
+ import {
8
+ setTransactionMessageLifetimeUsingDurableNonce ,
9
+ TransactionMessageWithDurableNonceLifetime ,
10
+ } from '../durable-nonce' ;
11
+ import { AdvanceNonceAccountInstruction } from '../durable-nonce-instruction' ;
12
+ import { setTransactionMessageFeePayer } from '../fee-payer' ;
3
13
import {
4
14
appendTransactionMessageInstruction ,
5
15
appendTransactionMessageInstructions ,
@@ -9,6 +19,9 @@ import {
9
19
import { BaseTransactionMessage } from '../transaction-message' ;
10
20
11
21
type IInstruction = BaseTransactionMessage [ 'instructions' ] [ number ] ;
22
+ type InstructionA = IInstruction & { identifier : 'A' } ;
23
+ type InstructionB = IInstruction & { identifier : 'B' } ;
24
+ type InstructionC = IInstruction & { identifier : 'C' } ;
12
25
13
26
// [DESCRIBE] appendTransactionMessageInstruction
14
27
{
@@ -18,6 +31,56 @@ type IInstruction = BaseTransactionMessage['instructions'][number];
18
31
const newMessage = appendTransactionMessageInstruction ( null as unknown as IInstruction , message ) ;
19
32
newMessage satisfies BaseTransactionMessage & { some : 1 } ;
20
33
}
34
+
35
+ // It concatenates the instruction types
36
+ {
37
+ const message = null as unknown as { instructions : [ InstructionA ] ; version : 0 } ;
38
+ const newMessage = appendTransactionMessageInstruction ( null as unknown as InstructionB , message ) ;
39
+ newMessage . instructions satisfies readonly [ InstructionA , InstructionB ] ;
40
+ // @ts -expect-error Wrong order.
41
+ newMessage . instructions satisfies readonly [ InstructionB , InstructionA ] ;
42
+ // @ts -expect-error Not readonly.
43
+ newMessage . instructions satisfies [ InstructionA , InstructionB ] ;
44
+ }
45
+
46
+ // It adds instruction types to base transaction messages
47
+ {
48
+ const message = null as unknown as BaseTransactionMessage ;
49
+ const newMessage = appendTransactionMessageInstruction ( null as unknown as InstructionA , message ) ;
50
+ newMessage . instructions satisfies readonly [ ...IInstruction [ ] , InstructionA ] ;
51
+ }
52
+
53
+ // It keeps the blockhash lifetime type safety.
54
+ {
55
+ const feePayer = null as unknown as Address ;
56
+ const blockhash = null as unknown as Parameters < typeof setTransactionMessageLifetimeUsingBlockhash > [ 0 ] ;
57
+ const message = pipe (
58
+ createTransactionMessage ( { version : 0 } ) ,
59
+ m => setTransactionMessageFeePayer ( feePayer , m ) ,
60
+ m => setTransactionMessageLifetimeUsingBlockhash ( blockhash , m ) ,
61
+ m => appendTransactionMessageInstruction ( null as unknown as InstructionA , m ) ,
62
+ ) ;
63
+
64
+ message satisfies CompilableTransactionMessage ;
65
+ message satisfies BaseTransactionMessage & TransactionMessageWithBlockhashLifetime ;
66
+ message . instructions satisfies readonly [ InstructionA ] ;
67
+ }
68
+
69
+ // It keeps the durable nonce lifetime type safety.
70
+ {
71
+ const feePayer = null as unknown as Address ;
72
+ const nonceConfig = null as unknown as Parameters < typeof setTransactionMessageLifetimeUsingDurableNonce > [ 0 ] ;
73
+ const message = pipe (
74
+ createTransactionMessage ( { version : 0 } ) ,
75
+ m => setTransactionMessageFeePayer ( feePayer , m ) ,
76
+ m => setTransactionMessageLifetimeUsingDurableNonce ( nonceConfig , m ) ,
77
+ m => appendTransactionMessageInstruction ( null as unknown as InstructionA , m ) ,
78
+ ) ;
79
+
80
+ message satisfies CompilableTransactionMessage ;
81
+ message satisfies BaseTransactionMessage & TransactionMessageWithDurableNonceLifetime ;
82
+ message . instructions satisfies readonly [ AdvanceNonceAccountInstruction , InstructionA ] ;
83
+ }
21
84
}
22
85
23
86
// [DESCRIBE] appendTransactionMessageInstructions
@@ -28,6 +91,30 @@ type IInstruction = BaseTransactionMessage['instructions'][number];
28
91
const newMessage = appendTransactionMessageInstructions ( null as unknown as IInstruction [ ] , message ) ;
29
92
newMessage satisfies BaseTransactionMessage & { some : 1 } ;
30
93
}
94
+
95
+ // It concatenates the instruction types
96
+ {
97
+ const message = null as unknown as { instructions : [ InstructionA ] ; version : 0 } ;
98
+ const newMessage = appendTransactionMessageInstructions (
99
+ [ null as unknown as InstructionB , null as unknown as InstructionC ] ,
100
+ message ,
101
+ ) ;
102
+ newMessage . instructions satisfies readonly [ InstructionA , InstructionB , InstructionC ] ;
103
+ // @ts -expect-error Wrong order.
104
+ newMessage . instructions satisfies readonly [ InstructionC , InstructionB , InstructionA ] ;
105
+ // @ts -expect-error Not readonly.
106
+ newMessage . instructions satisfies [ InstructionA , InstructionB , InstructionC ] ;
107
+ }
108
+
109
+ // It adds instruction types to base transaction messages
110
+ {
111
+ const message = null as unknown as BaseTransactionMessage ;
112
+ const newMessage = appendTransactionMessageInstructions (
113
+ [ null as unknown as InstructionA , null as unknown as InstructionB ] ,
114
+ message ,
115
+ ) ;
116
+ newMessage . instructions satisfies readonly [ ...IInstruction [ ] , InstructionA , InstructionB ] ;
117
+ }
31
118
}
32
119
33
120
// [DESCRIBE] prependTransactionMessageInstruction
@@ -56,6 +143,58 @@ type IInstruction = BaseTransactionMessage['instructions'][number];
56
143
const newMessage = prependTransactionMessageInstruction ( null as unknown as IInstruction , message ) ;
57
144
newMessage satisfies BaseTransactionMessage & TransactionMessageWithBlockhashLifetime & { some : 1 } ;
58
145
}
146
+
147
+ // It concatenates the instruction types
148
+ {
149
+ const message = null as unknown as { instructions : [ InstructionA ] ; version : 0 } ;
150
+ const newMessage = prependTransactionMessageInstruction ( null as unknown as InstructionB , message ) ;
151
+ newMessage . instructions satisfies readonly [ InstructionB , InstructionA ] ;
152
+ // @ts -expect-error Wrong order.
153
+ newMessage . instructions satisfies readonly [ InstructionA , InstructionB ] ;
154
+ // @ts -expect-error Not readonly.
155
+ newMessage . instructions satisfies [ InstructionB , InstructionA ] ;
156
+ }
157
+
158
+ // It adds instruction types to base transaction messages
159
+ {
160
+ const message = null as unknown as BaseTransactionMessage ;
161
+ const newMessage = prependTransactionMessageInstruction ( null as unknown as InstructionA , message ) ;
162
+ newMessage . instructions satisfies readonly [ InstructionA , ...IInstruction [ ] ] ;
163
+ }
164
+
165
+ // It keeps the blockhash lifetime type safety.
166
+ {
167
+ const feePayer = null as unknown as Address ;
168
+ const blockhash = null as unknown as Parameters < typeof setTransactionMessageLifetimeUsingBlockhash > [ 0 ] ;
169
+ const message = pipe (
170
+ createTransactionMessage ( { version : 0 } ) ,
171
+ m => setTransactionMessageFeePayer ( feePayer , m ) ,
172
+ m => setTransactionMessageLifetimeUsingBlockhash ( blockhash , m ) ,
173
+ m => prependTransactionMessageInstruction ( null as unknown as InstructionA , m ) ,
174
+ ) ;
175
+
176
+ message satisfies CompilableTransactionMessage ;
177
+ message satisfies BaseTransactionMessage & TransactionMessageWithBlockhashLifetime ;
178
+ message . instructions satisfies readonly [ InstructionA ] ;
179
+ }
180
+
181
+ // It removes the durable nonce lifetime type safety but keep the nonce instruction.
182
+ {
183
+ const feePayer = null as unknown as Address ;
184
+ const nonceConfig = null as unknown as Parameters < typeof setTransactionMessageLifetimeUsingDurableNonce > [ 0 ] ;
185
+ const message = pipe (
186
+ createTransactionMessage ( { version : 0 } ) ,
187
+ m => setTransactionMessageFeePayer ( feePayer , m ) ,
188
+ m => setTransactionMessageLifetimeUsingDurableNonce ( nonceConfig , m ) ,
189
+ m => prependTransactionMessageInstruction ( null as unknown as InstructionA , m ) ,
190
+ ) ;
191
+
192
+ message . instructions satisfies readonly [ InstructionA , AdvanceNonceAccountInstruction ] ;
193
+ // @ts -expect-error No longer a durable nonce lifetime.
194
+ message satisfies CompilableTransactionMessage ;
195
+ // @ts -expect-error No longer a durable nonce lifetime.
196
+ message satisfies BaseTransactionMessage & TransactionMessageWithDurableNonceLifetime ;
197
+ }
59
198
}
60
199
61
200
// [DESCRIBE] prependTransactionMessageInstructions
@@ -76,4 +215,28 @@ type IInstruction = BaseTransactionMessage['instructions'][number];
76
215
// @ts -expect-error The durable nonce transaction message type should be stripped.
77
216
newMessage satisfies TransactionMessageWithDurableNonceLifetime ;
78
217
}
218
+
219
+ // It concatenates the instruction types
220
+ {
221
+ const message = null as unknown as { instructions : [ InstructionA ] ; version : 0 } ;
222
+ const newMessage = prependTransactionMessageInstructions (
223
+ [ null as unknown as InstructionB , null as unknown as InstructionC ] ,
224
+ message ,
225
+ ) ;
226
+ newMessage . instructions satisfies readonly [ InstructionB , InstructionC , InstructionA ] ;
227
+ // @ts -expect-error Wrong order.
228
+ newMessage . instructions satisfies readonly [ InstructionA , InstructionC , InstructionB ] ;
229
+ // @ts -expect-error Not readonly.
230
+ newMessage . instructions satisfies [ InstructionB , InstructionC , InstructionA ] ;
231
+ }
232
+
233
+ // It adds instruction types to base transaction messages
234
+ {
235
+ const message = null as unknown as BaseTransactionMessage ;
236
+ const newMessage = prependTransactionMessageInstructions (
237
+ [ null as unknown as InstructionA , null as unknown as InstructionB ] ,
238
+ message ,
239
+ ) ;
240
+ newMessage . instructions satisfies readonly [ InstructionA , InstructionB , ...IInstruction [ ] ] ;
241
+ }
79
242
}
0 commit comments