1
+ import * as splToken from "@solana/spl-token" ;
1
2
import {
2
3
AddressLookupTableAccount ,
3
4
Connection ,
5
+ Keypair ,
4
6
PublicKey ,
5
7
TransactionInstruction ,
6
8
TransactionMessage ,
@@ -10,15 +12,14 @@ import {
10
12
FastTransfer ,
11
13
TokenRouter ,
12
14
} from "@wormhole-foundation/example-liquidity-layer-definitions" ;
13
- import { Chain , Network , Platform } from "@wormhole-foundation/sdk-base" ;
15
+ import { Chain , Network , Platform , toChainId } from "@wormhole-foundation/sdk-base" ;
14
16
import {
15
17
AccountAddress ,
16
18
ChainAddress ,
17
19
ChainsConfig ,
18
20
CircleBridge ,
19
21
Contracts ,
20
22
UnsignedTransaction ,
21
- VAA ,
22
23
} from "@wormhole-foundation/sdk-definitions" ;
23
24
import {
24
25
AnySolanaAddress ,
@@ -88,8 +89,60 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
88
89
yield this . createUnsignedTx ( { transaction } , "TokenRouter.Initialize" ) ;
89
90
}
90
91
91
- getInitialAuctionFee ( ) : Promise < bigint > {
92
- throw new Error ( "Method not implemented." ) ;
92
+ async * prepareMarketOrder (
93
+ sender : AnySolanaAddress ,
94
+ amount : bigint ,
95
+ redeemer : ChainAddress < Chain > ,
96
+ minAmountOut ?: bigint ,
97
+ redeemerMessage ?: Uint8Array ,
98
+ preparedOrder ?: Keypair ,
99
+ ) {
100
+ const payer = new SolanaAddress ( sender ) . unwrap ( ) ;
101
+
102
+ // assume sender token is the usdc mint address
103
+ const senderToken = splToken . getAssociatedTokenAddressSync ( this . mint , payer ) ;
104
+
105
+ // Where we'll write the prepared order
106
+ preparedOrder = preparedOrder ?? Keypair . generate ( ) ;
107
+
108
+ const [ approveIx , prepareIx ] = await this . prepareMarketOrderIx (
109
+ {
110
+ payer,
111
+ senderToken,
112
+ preparedOrder : preparedOrder . publicKey ,
113
+ } ,
114
+ {
115
+ amountIn : amount ,
116
+ minAmountOut : minAmountOut !== undefined ? minAmountOut : null ,
117
+ targetChain : toChainId ( redeemer . chain ) ,
118
+ redeemer : Array . from ( redeemer . address . toUniversalAddress ( ) . toUint8Array ( ) ) ,
119
+ redeemerMessage : redeemerMessage ? Buffer . from ( redeemerMessage ) : Buffer . from ( "" ) ,
120
+ } ,
121
+ ) ;
122
+
123
+ // TODO: fix prepareMarketOrderIx to not return null at all
124
+ const ixs = [ ] ;
125
+ if ( approveIx ) ixs . push ( approveIx ) ;
126
+ ixs . push ( prepareIx ) ;
127
+
128
+ const transaction = this . createTx ( payer , ixs ) ;
129
+ yield this . createUnsignedTx (
130
+ { transaction, signers : [ preparedOrder ] } ,
131
+ "TokenRouter.PrepareMarketOrder" ,
132
+ ) ;
133
+ }
134
+
135
+ async * closePreparedOrder ( sender : AnySolanaAddress , order : AnySolanaAddress ) {
136
+ const payer = new SolanaAddress ( sender ) . unwrap ( ) ;
137
+ const preparedOrder = new SolanaAddress ( order ) . unwrap ( ) ;
138
+
139
+ const ix = await this . closePreparedOrderIx ( {
140
+ preparedOrder,
141
+ preparedBy : payer ,
142
+ } ) ;
143
+
144
+ const transaction = this . createTx ( payer , [ ix ] ) ;
145
+ yield this . createUnsignedTx ( { transaction } , "TokenRouter.ClosePreparedOrder" ) ;
93
146
}
94
147
95
148
placeMarketOrder (
@@ -101,6 +154,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
101
154
) : AsyncGenerator < UnsignedTransaction < N , C > , any , unknown > {
102
155
throw new Error ( "Method not implemented." ) ;
103
156
}
157
+
104
158
placeFastMarketOrder < RC extends Chain > (
105
159
amount : bigint ,
106
160
chain : RC ,
@@ -113,6 +167,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
113
167
) : AsyncGenerator < UnsignedTransaction < N , C > , any , unknown > {
114
168
throw new Error ( "Method not implemented." ) ;
115
169
}
170
+
116
171
redeemFill (
117
172
vaa : FastTransfer . VAA ,
118
173
cctp : CircleBridge . Attestation ,
0 commit comments