1
1
import {
2
2
address ,
3
+ appendTransactionMessageInstructions ,
3
4
createKeyPairSignerFromBytes ,
4
5
createNoopSigner ,
5
6
createSolanaRpc ,
6
7
createSolanaRpcSubscriptions ,
8
+ createTransactionMessage ,
7
9
getBase58Decoder ,
8
10
getSignatureFromTransaction ,
9
11
partiallySignTransactionMessageWithSigners ,
12
+ pipe ,
10
13
sendAndConfirmTransactionFactory ,
14
+ setTransactionMessageFeePayerSigner ,
15
+ setTransactionMessageLifetimeUsingBlockhash ,
11
16
signTransactionMessageWithSigners ,
12
17
} from '@solana/kit' ;
13
18
import { TOKEN_2022_PROGRAM_ADDRESS } from '@solana-program/token-2022' ;
14
19
import {
15
20
findWrappedMintAuthorityPda ,
16
21
findWrappedMintPda ,
17
- multisigOfflineSignWrapTx ,
18
22
combinedMultisigTx ,
19
- createEscrowAccountTx ,
20
- createMintTx ,
23
+ createMint ,
21
24
multisigOfflineSignUnwrap ,
25
+ createEscrowAccount ,
26
+ multisigOfflineSignWrap ,
22
27
} from '../index' ;
23
- import { createTokenAccountTx , getOwnerFromAccount } from '../utilities' ;
28
+ import { createTokenAccount , getOwnerFromAccount } from '../utilities' ;
24
29
25
30
// Replace these consts with your own
26
31
const PAYER_KEYPAIR_BYTES = new Uint8Array ( [
@@ -59,55 +64,77 @@ async function main() {
59
64
const { value : blockhash } = await rpc . getLatestBlockhash ( ) . send ( ) ;
60
65
61
66
// Initialize the wrapped mint
62
- const createMintMessage = await createMintTx ( {
67
+ const createMintHelper = await createMint ( {
63
68
rpc,
64
- blockhash,
65
69
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
66
70
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
67
71
payer,
68
72
idempotent : true ,
69
73
} ) ;
70
- const signedCreateMintTx = await signTransactionMessageWithSigners ( createMintMessage . tx ) ;
71
- await sendAndConfirm ( signedCreateMintTx , { commitment : 'confirmed' } ) ;
72
- const createMintSignature = getSignatureFromTransaction ( signedCreateMintTx ) ;
74
+ const createMintTx = await pipe (
75
+ createTransactionMessage ( { version : 0 } ) ,
76
+ tx => setTransactionMessageFeePayerSigner ( payer , tx ) ,
77
+ tx => setTransactionMessageLifetimeUsingBlockhash ( blockhash , tx ) ,
78
+ tx => appendTransactionMessageInstructions ( createMintHelper . ixs , tx ) ,
79
+ tx => signTransactionMessageWithSigners ( tx ) ,
80
+ ) ;
81
+ await sendAndConfirm ( createMintTx , { commitment : 'confirmed' } ) ;
82
+ const createMintSignature = getSignatureFromTransaction ( createMintTx ) ;
73
83
74
84
console . log ( '======== Create Mint Successful ========' ) ;
75
- console . log ( 'Wrapped Mint:' , createMintMessage . wrappedMint ) ;
76
- console . log ( 'Backpointer:' , createMintMessage . backpointer ) ;
77
- console . log ( 'Funded wrapped mint lamports:' , createMintMessage . fundedWrappedMintLamports ) ;
78
- console . log ( 'Funded backpointer lamports:' , createMintMessage . fundedBackpointerLamports ) ;
85
+ console . log ( 'Wrapped Mint:' , createMintHelper . wrappedMint ) ;
86
+ console . log ( 'Backpointer:' , createMintHelper . backpointer ) ;
87
+ console . log ( 'Funded wrapped mint lamports:' , createMintHelper . fundedWrappedMintLamports ) ;
88
+ console . log ( 'Funded backpointer lamports:' , createMintHelper . fundedBackpointerLamports ) ;
79
89
console . log ( 'Signature:' , createMintSignature ) ;
80
90
81
91
// === Setup accounts needed for wrap ===
82
92
83
93
// Create escrow account that with hold unwrapped tokens
84
- const createEscrowMessage = await createEscrowAccountTx ( {
94
+ const createEscrowHelper = await createEscrowAccount ( {
85
95
rpc,
86
- blockhash,
87
96
payer,
88
97
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
89
98
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
90
99
} ) ;
91
- const signedCreateEscrowTx = await signTransactionMessageWithSigners ( createEscrowMessage . tx ) ;
92
- await sendAndConfirm ( signedCreateEscrowTx , { commitment : 'confirmed' } ) ;
100
+ if ( createEscrowHelper . kind === 'instructions_to_create' ) {
101
+ const createEscrowTx = await pipe (
102
+ createTransactionMessage ( { version : 0 } ) ,
103
+ tx => setTransactionMessageFeePayerSigner ( payer , tx ) ,
104
+ tx => setTransactionMessageLifetimeUsingBlockhash ( blockhash , tx ) ,
105
+ tx => appendTransactionMessageInstructions ( createEscrowHelper . ixs , tx ) ,
106
+ tx => signTransactionMessageWithSigners ( tx ) ,
107
+ ) ;
108
+ await sendAndConfirm ( createEscrowTx , { commitment : 'confirmed' } ) ;
109
+ const createEscrowSignature = getSignatureFromTransaction ( createEscrowTx ) ;
110
+
111
+ console . log ( '======== Create Escrow Successful ========' ) ;
112
+ console . log ( 'Escrow address:' , createEscrowHelper . address ) ;
113
+ console . log ( 'Signature:' , createEscrowSignature ) ;
114
+ } else {
115
+ console . log ( '======== Escrow already exists, skipping creation ========' ) ;
116
+ }
93
117
94
118
// Create recipient account where wrapped tokens will be minted to
95
119
const [ wrappedMint ] = await findWrappedMintPda ( {
96
120
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
97
121
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
98
122
} ) ;
99
- const recipientTokenAccountMessage = await createTokenAccountTx ( {
123
+ const recipientTokenAccountHelper = await createTokenAccount ( {
100
124
rpc,
101
- blockhash,
102
125
payer,
103
126
mint : wrappedMint ,
104
127
tokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
105
128
owner : MULTISIG_SPL_TOKEN_2022 ,
106
129
} ) ;
107
- const signedRecipientAccountTx = await signTransactionMessageWithSigners (
108
- recipientTokenAccountMessage . tx ,
130
+ const recipientTokenAccountTx = await pipe (
131
+ createTransactionMessage ( { version : 0 } ) ,
132
+ tx => setTransactionMessageFeePayerSigner ( payer , tx ) ,
133
+ tx => setTransactionMessageLifetimeUsingBlockhash ( blockhash , tx ) ,
134
+ tx => appendTransactionMessageInstructions ( recipientTokenAccountHelper . ixs , tx ) ,
135
+ tx => signTransactionMessageWithSigners ( tx ) ,
109
136
) ;
110
- await sendAndConfirm ( signedRecipientAccountTx , { commitment : 'confirmed' } ) ;
137
+ await sendAndConfirm ( recipientTokenAccountTx , { commitment : 'confirmed' } ) ;
111
138
112
139
const unwrappedTokenProgram = await getOwnerFromAccount ( rpc , UNWRAPPED_TOKEN_ACCOUNT ) ;
113
140
const [ wrappedMintAuthority ] = await findWrappedMintAuthorityPda ( { wrappedMint } ) ;
@@ -119,14 +146,13 @@ async function main() {
119
146
120
147
// Two signers and the payer sign the transaction independently
121
148
122
- const wrapTxA = multisigOfflineSignWrapTx ( {
149
+ const wrapTxA = await multisigOfflineSignWrap ( {
123
150
payer : createNoopSigner ( payer . address ) ,
124
151
unwrappedTokenAccount : UNWRAPPED_TOKEN_ACCOUNT ,
125
- escrowAccount : createEscrowMessage . keyPair . address ,
126
152
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
127
153
amount : AMOUNT_TO_WRAP ,
128
154
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
129
- recipientWrappedTokenAccount : recipientTokenAccountMessage . keyPair . address ,
155
+ recipientWrappedTokenAccount : recipientTokenAccountHelper . keyPair . address ,
130
156
transferAuthority : MULTISIG_SPL_TOKEN ,
131
157
wrappedMint,
132
158
wrappedMintAuthority,
@@ -136,14 +162,13 @@ async function main() {
136
162
} ) ;
137
163
const signedWrapTxA = await partiallySignTransactionMessageWithSigners ( wrapTxA ) ;
138
164
139
- const wrapTxB = multisigOfflineSignWrapTx ( {
165
+ const wrapTxB = await multisigOfflineSignWrap ( {
140
166
payer : createNoopSigner ( payer . address ) ,
141
167
unwrappedTokenAccount : UNWRAPPED_TOKEN_ACCOUNT ,
142
- escrowAccount : createEscrowMessage . keyPair . address ,
143
168
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
144
169
amount : AMOUNT_TO_WRAP ,
145
170
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
146
- recipientWrappedTokenAccount : recipientTokenAccountMessage . keyPair . address ,
171
+ recipientWrappedTokenAccount : recipientTokenAccountHelper . keyPair . address ,
147
172
transferAuthority : MULTISIG_SPL_TOKEN ,
148
173
wrappedMint,
149
174
wrappedMintAuthority,
@@ -153,14 +178,13 @@ async function main() {
153
178
} ) ;
154
179
const signedWrapTxB = await partiallySignTransactionMessageWithSigners ( wrapTxB ) ;
155
180
156
- const wrapTxC = multisigOfflineSignWrapTx ( {
181
+ const wrapTxC = await multisigOfflineSignWrap ( {
157
182
payer,
158
183
unwrappedTokenAccount : UNWRAPPED_TOKEN_ACCOUNT ,
159
- escrowAccount : createEscrowMessage . keyPair . address ,
160
184
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
161
185
amount : AMOUNT_TO_WRAP ,
162
186
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
163
- recipientWrappedTokenAccount : recipientTokenAccountMessage . keyPair . address ,
187
+ recipientWrappedTokenAccount : recipientTokenAccountHelper . keyPair . address ,
164
188
transferAuthority : MULTISIG_SPL_TOKEN ,
165
189
wrappedMint,
166
190
wrappedMintAuthority,
@@ -192,13 +216,12 @@ async function main() {
192
216
193
217
const { value : unwrapBlockhash } = await rpc . getLatestBlockhash ( ) . send ( ) ;
194
218
195
- const unwrapTxA = multisigOfflineSignUnwrap ( {
219
+ const unwrapTxA = await multisigOfflineSignUnwrap ( {
196
220
payer : createNoopSigner ( payer . address ) ,
197
- unwrappedEscrow : createEscrowMessage . keyPair . address ,
198
221
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
199
222
amount : AMOUNT_TO_WRAP ,
200
223
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
201
- wrappedTokenAccount : recipientTokenAccountMessage . keyPair . address ,
224
+ wrappedTokenAccount : recipientTokenAccountHelper . keyPair . address ,
202
225
recipientUnwrappedToken : UNWRAPPED_TOKEN_ACCOUNT ,
203
226
transferAuthority : MULTISIG_SPL_TOKEN_2022 ,
204
227
wrappedMint,
@@ -209,13 +232,12 @@ async function main() {
209
232
} ) ;
210
233
const signedUnwrapTxA = await partiallySignTransactionMessageWithSigners ( unwrapTxA ) ;
211
234
212
- const unwrapTxB = multisigOfflineSignUnwrap ( {
235
+ const unwrapTxB = await multisigOfflineSignUnwrap ( {
213
236
payer : createNoopSigner ( payer . address ) ,
214
- unwrappedEscrow : createEscrowMessage . keyPair . address ,
215
237
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
216
238
amount : AMOUNT_TO_WRAP ,
217
239
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
218
- wrappedTokenAccount : recipientTokenAccountMessage . keyPair . address ,
240
+ wrappedTokenAccount : recipientTokenAccountHelper . keyPair . address ,
219
241
recipientUnwrappedToken : UNWRAPPED_TOKEN_ACCOUNT ,
220
242
transferAuthority : MULTISIG_SPL_TOKEN_2022 ,
221
243
wrappedMint,
@@ -226,13 +248,12 @@ async function main() {
226
248
} ) ;
227
249
const signedUnwrapTxB = await partiallySignTransactionMessageWithSigners ( unwrapTxB ) ;
228
250
229
- const unwrapTxC = multisigOfflineSignUnwrap ( {
251
+ const unwrapTxC = await multisigOfflineSignUnwrap ( {
230
252
payer : payer ,
231
- unwrappedEscrow : createEscrowMessage . keyPair . address ,
232
253
wrappedTokenProgram : TOKEN_2022_PROGRAM_ADDRESS ,
233
254
amount : AMOUNT_TO_WRAP ,
234
255
unwrappedMint : UNWRAPPED_MINT_ADDRESS ,
235
- wrappedTokenAccount : recipientTokenAccountMessage . keyPair . address ,
256
+ wrappedTokenAccount : recipientTokenAccountHelper . keyPair . address ,
236
257
recipientUnwrappedToken : UNWRAPPED_TOKEN_ACCOUNT ,
237
258
transferAuthority : MULTISIG_SPL_TOKEN_2022 ,
238
259
wrappedMint,
0 commit comments