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

Commit b55c9fb

Browse files
committed
adding prepare order
1 parent c2af1f9 commit b55c9fb

File tree

5 files changed

+150
-182
lines changed

5 files changed

+150
-182
lines changed

solana/ts/src/common/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,7 @@ export async function reclaimCctpMessageIx(
4444
const { payer, cctpMessage: messageSentEventData } = accounts;
4545

4646
return messageTransmitter.reclaimEventAccountIx(
47-
{
48-
payee: payer,
49-
messageSentEventData,
50-
},
47+
{ payee: payer, messageSentEventData },
5148
cctpAttestation,
5249
);
5350
}

solana/ts/src/protocol/tokenRouter.ts

Lines changed: 59 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,60 @@ 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 !== 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");
93146
}
94147

95148
placeMarketOrder(
@@ -101,6 +154,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
101154
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
102155
throw new Error("Method not implemented.");
103156
}
157+
104158
placeFastMarketOrder<RC extends Chain>(
105159
amount: bigint,
106160
chain: RC,
@@ -113,6 +167,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
113167
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
114168
throw new Error("Method not implemented.");
115169
}
170+
116171
redeemFill(
117172
vaa: FastTransfer.VAA,
118173
cctp: CircleBridge.Attestation,

solana/ts/src/tokenRouter/state/PreparedOrder.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import { BN } from "@coral-xyz/anchor";
22
import { PublicKey } from "@solana/web3.js";
3-
import { Keccak } from "sha3";
4-
import { Uint64, uint64ToBN } from "../../common";
53

64
export type OrderType = {
75
market?: {

0 commit comments

Comments
 (0)