Skip to content
This repository was archived by the owner on Jun 16, 2025. It is now read-only.

Commit 7f05805

Browse files
committed
adding prepare order
1 parent c2af1f9 commit 7f05805

File tree

3 files changed

+119
-149
lines changed

3 files changed

+119
-149
lines changed

solana/ts/src/protocol/tokenRouter.ts

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1+
import * as splToken from "@solana/spl-token";
12
import {
23
AddressLookupTableAccount,
34
Connection,
5+
Keypair,
46
PublicKey,
57
TransactionInstruction,
68
TransactionMessage,
@@ -10,15 +12,14 @@ import {
1012
FastTransfer,
1113
TokenRouter,
1214
} 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";
1416
import {
1517
AccountAddress,
1618
ChainAddress,
1719
ChainsConfig,
1820
CircleBridge,
1921
Contracts,
2022
UnsignedTransaction,
21-
VAA,
2223
} from "@wormhole-foundation/sdk-definitions";
2324
import {
2425
AnySolanaAddress,
@@ -88,8 +89,47 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
8889
yield this.createUnsignedTx({ transaction }, "TokenRouter.Initialize");
8990
}
9091

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 ? 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+
);
93133
}
94134

95135
placeMarketOrder(
@@ -101,6 +141,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
101141
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
102142
throw new Error("Method not implemented.");
103143
}
144+
104145
placeFastMarketOrder<RC extends Chain>(
105146
amount: bigint,
106147
chain: RC,
@@ -113,6 +154,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
113154
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
114155
throw new Error("Method not implemented.");
115156
}
157+
116158
redeemFill(
117159
vaa: FastTransfer.VAA,
118160
cctp: CircleBridge.Attestation,

0 commit comments

Comments
 (0)