@@ -9,16 +9,19 @@ import {
9
9
VersionedTransaction ,
10
10
} from "@solana/web3.js" ;
11
11
import {
12
+ FastMarketOrder ,
12
13
FastTransfer ,
14
+ Message ,
13
15
TokenRouter ,
14
16
} from "@wormhole-foundation/example-liquidity-layer-definitions" ;
15
- import { Chain , Network , Platform , toChainId } from "@wormhole-foundation/sdk-base" ;
17
+ import { Chain , ChainId , Network , Platform , toChainId } from "@wormhole-foundation/sdk-base" ;
16
18
import {
17
19
AccountAddress ,
18
20
ChainAddress ,
19
21
ChainsConfig ,
20
22
CircleBridge ,
21
23
Contracts ,
24
+ UniversalAddress ,
22
25
UnsignedTransaction ,
23
26
} from "@wormhole-foundation/sdk-definitions" ;
24
27
import {
@@ -73,6 +76,10 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
73
76
} ) ;
74
77
}
75
78
79
+ private usdcMint ( wallet : PublicKey ) {
80
+ return splToken . getAssociatedTokenAddressSync ( this . mint , wallet ) ;
81
+ }
82
+
76
83
async * initialize (
77
84
owner : AnySolanaAddress ,
78
85
ownerAssistant : AnySolanaAddress ,
@@ -89,47 +96,79 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
89
96
yield this . createUnsignedTx ( { transaction } , "TokenRouter.Initialize" ) ;
90
97
}
91
98
92
- async * prepareMarketOrder (
99
+ private makeFastMarketOrder (
93
100
sender : AnySolanaAddress ,
94
- amount : bigint ,
95
- redeemer : ChainAddress < Chain > ,
96
- minAmountOut ?: bigint ,
97
- redeemerMessage ?: Uint8Array ,
98
- preparedOrder ?: Keypair ,
99
- ) {
101
+ order : Partial < FastMarketOrder > ,
102
+ ) : FastMarketOrder {
103
+ const senderAddress = new SolanaAddress ( sender ) . toUniversalAddress ( ) ;
104
+ const o : FastMarketOrder = {
105
+ // TODO: from auction params? take as args?
106
+ maxFee : order . maxFee ?? 0n ,
107
+ initAuctionFee : order . initAuctionFee ?? 0n ,
108
+ deadline : order . deadline ?? 0 ,
109
+ // TODO: specify which params we need, not just partial
110
+ amountIn : order . amountIn ! ,
111
+ minAmountOut : order . minAmountOut ! ,
112
+ targetChain : order . targetChain ! ,
113
+ redeemer : order . redeemer ! ,
114
+ // TODO: which of these can we assume? any
115
+ sender : order . sender ?? senderAddress ,
116
+ refundAddress : order . refundAddress ?? senderAddress ,
117
+ redeemerMessage : order . redeemerMessage ?? new Uint8Array ( ) ,
118
+ } ;
119
+
120
+ return o ;
121
+ }
122
+
123
+ private async _prepareMarketOrderIxs (
124
+ sender : AnySolanaAddress ,
125
+ order : TokenRouter . OrderRequest ,
126
+ prepareTo ?: Keypair ,
127
+ ) : Promise < [ TransactionInstruction [ ] , Keypair [ ] ] > {
100
128
const payer = new SolanaAddress ( sender ) . unwrap ( ) ;
101
129
102
- // assume sender token is the usdc mint address
103
- const senderToken = splToken . getAssociatedTokenAddressSync ( this . mint , payer ) ;
130
+ // TODO: assumes sender token is the usdc mint address
131
+ const senderToken = this . usdcMint ( payer ) ;
104
132
105
133
// Where we'll write the prepared order
106
- preparedOrder = preparedOrder ?? Keypair . generate ( ) ;
134
+ prepareTo = prepareTo ?? Keypair . generate ( ) ;
107
135
108
136
const [ approveIx , prepareIx ] = await this . prepareMarketOrderIx (
109
137
{
110
138
payer,
111
139
senderToken,
112
- preparedOrder : preparedOrder . publicKey ,
140
+ preparedOrder : prepareTo . publicKey ,
113
141
} ,
114
142
{
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 ( "" ) ,
143
+ amountIn : order . amountIn ,
144
+ minAmountOut : order . minAmountOut !== undefined ? order . minAmountOut : null ,
145
+ targetChain : toChainId ( order . targetChain ) ,
146
+ redeemer : Array . from ( order . redeemer . toUint8Array ( ) ) ,
147
+ redeemerMessage : order . redeemerMessage
148
+ ? Buffer . from ( order . redeemerMessage )
149
+ : Buffer . from ( "" ) ,
120
150
} ,
121
151
) ;
122
152
123
- // TODO: fix prepareMarketOrderIx to not return null at all
153
+ // TODO: fix prepareMarketOrderIx to not return null at all?
124
154
const ixs = [ ] ;
125
155
if ( approveIx ) ixs . push ( approveIx ) ;
126
156
ixs . push ( prepareIx ) ;
127
157
158
+ return [ ixs , [ prepareTo ] ] ;
159
+ }
160
+
161
+ async * prepareMarketOrder (
162
+ sender : AnySolanaAddress ,
163
+ order : TokenRouter . OrderRequest ,
164
+ prepareTo ?: Keypair ,
165
+ ) {
166
+ const payer = new SolanaAddress ( sender ) . unwrap ( ) ;
167
+
168
+ const [ ixs , signers ] = await this . _prepareMarketOrderIxs ( sender , order , prepareTo ) ;
169
+
128
170
const transaction = this . createTx ( payer , ixs ) ;
129
- yield this . createUnsignedTx (
130
- { transaction, signers : [ preparedOrder ] } ,
131
- "TokenRouter.PrepareMarketOrder" ,
132
- ) ;
171
+ yield this . createUnsignedTx ( { transaction, signers } , "TokenRouter.PrepareMarketOrder" ) ;
133
172
}
134
173
135
174
async * closePreparedOrder ( sender : AnySolanaAddress , order : AnySolanaAddress ) {
@@ -139,20 +178,57 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
139
178
const ix = await this . closePreparedOrderIx ( {
140
179
preparedOrder,
141
180
preparedBy : payer ,
181
+ orderSender : payer ,
142
182
} ) ;
143
183
144
184
const transaction = this . createTx ( payer , [ ix ] ) ;
185
+
145
186
yield this . createUnsignedTx ( { transaction } , "TokenRouter.ClosePreparedOrder" ) ;
146
187
}
147
188
148
- placeMarketOrder (
149
- amount : bigint ,
150
- redeemer : ChainAddress < Chain > ,
151
- redeemerMessage : Uint8Array ,
152
- minAmountOut ?: bigint | undefined ,
153
- refundAddress ?: AccountAddress < C > | undefined ,
189
+ async * placeMarketOrder (
190
+ sender : AnySolanaAddress ,
191
+ order : TokenRouter . OrderRequest | AnySolanaAddress ,
192
+ prepareTo ?: Keypair ,
154
193
) : AsyncGenerator < UnsignedTransaction < N , C > , any , unknown > {
155
- throw new Error ( "Method not implemented." ) ;
194
+ const payer = new SolanaAddress ( sender ) . unwrap ( ) ;
195
+
196
+ let ixs : TransactionInstruction [ ] = [ ] ;
197
+ let signers : Keypair [ ] = [ ] ;
198
+ let preparedOrder : PublicKey ;
199
+ let targetChain : ChainId | undefined ;
200
+
201
+ if ( TokenRouter . isOrderRequest ( order ) ) {
202
+ prepareTo = prepareTo ?? Keypair . generate ( ) ;
203
+
204
+ const combined = false ; // TODO how to choose?
205
+ if ( combined ) {
206
+ const [ ixs , signers ] = await this . _prepareMarketOrderIxs ( sender , order , prepareTo ) ;
207
+ ixs . push ( ...ixs ) ;
208
+ signers . push ( ...signers ) ;
209
+ } else {
210
+ yield * this . prepareMarketOrder ( sender , order , prepareTo ) ;
211
+ }
212
+
213
+ preparedOrder = prepareTo . publicKey ;
214
+ targetChain = toChainId ( order . targetChain ) ;
215
+ } else {
216
+ preparedOrder = new SolanaAddress ( order ) . unwrap ( ) ;
217
+ }
218
+
219
+ const ix = await this . placeMarketOrderCctpIx (
220
+ {
221
+ payer,
222
+ preparedOrder,
223
+ preparedBy : payer ,
224
+ } ,
225
+ // TODO: add cctpDomain fn
226
+ { targetChain } , //,destinationDomain: 3 },
227
+ ) ;
228
+ ixs . push ( ix ) ;
229
+
230
+ const transaction = this . createTx ( payer , ixs ) ;
231
+ yield this . createUnsignedTx ( { transaction, signers } , "TokenRouter.PlaceMarketOrder" ) ;
156
232
}
157
233
158
234
placeFastMarketOrder < RC extends Chain > (
0 commit comments