Skip to content

Commit

Permalink
adding prepare order
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Jun 21, 2024
1 parent c2af1f9 commit 7f05805
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 149 deletions.
50 changes: 46 additions & 4 deletions solana/ts/src/protocol/tokenRouter.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import * as splToken from "@solana/spl-token";
import {
AddressLookupTableAccount,
Connection,
Keypair,
PublicKey,
TransactionInstruction,
TransactionMessage,
Expand All @@ -10,15 +12,14 @@ import {
FastTransfer,
TokenRouter,
} from "@wormhole-foundation/example-liquidity-layer-definitions";
import { Chain, Network, Platform } from "@wormhole-foundation/sdk-base";
import { Chain, Network, Platform, toChainId } from "@wormhole-foundation/sdk-base";
import {
AccountAddress,
ChainAddress,
ChainsConfig,
CircleBridge,
Contracts,
UnsignedTransaction,
VAA,
} from "@wormhole-foundation/sdk-definitions";
import {
AnySolanaAddress,
Expand Down Expand Up @@ -88,8 +89,47 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
yield this.createUnsignedTx({ transaction }, "TokenRouter.Initialize");
}

getInitialAuctionFee(): Promise<bigint> {
throw new Error("Method not implemented.");
async *prepareMarketOrder(
sender: AnySolanaAddress,
amount: bigint,
redeemer: ChainAddress<Chain>,
minAmountOut?: bigint,
redeemerMessage?: Uint8Array,
preparedOrder?: Keypair,
) {
const payer = new SolanaAddress(sender).unwrap();

// assume sender token is the usdc mint address
const senderToken = splToken.getAssociatedTokenAddressSync(this.mint, payer);

// Where we'll write the prepared order
preparedOrder = preparedOrder ?? Keypair.generate();

const [approveIx, prepareIx] = await this.prepareMarketOrderIx(
{
payer,
senderToken,
preparedOrder: preparedOrder.publicKey,
},
{
amountIn: amount,
minAmountOut: minAmountOut ? minAmountOut : null,
targetChain: toChainId(redeemer.chain),
redeemer: Array.from(redeemer.address.toUniversalAddress().toUint8Array()),
redeemerMessage: redeemerMessage ? Buffer.from(redeemerMessage) : Buffer.from(""),
},
);

// TODO: fix prepareMarketOrderIx to not return null at all
const ixs = [];
if (approveIx) ixs.push(approveIx);
ixs.push(prepareIx);

const transaction = this.createTx(payer, ixs);
yield this.createUnsignedTx(
{ transaction, signers: [preparedOrder] },
"TokenRouter.PrepareMarketOrder",
);
}

placeMarketOrder(
Expand All @@ -101,6 +141,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
throw new Error("Method not implemented.");
}

placeFastMarketOrder<RC extends Chain>(
amount: bigint,
chain: RC,
Expand All @@ -113,6 +154,7 @@ export class SolanaTokenRouter<N extends Network, C extends SolanaChains>
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
throw new Error("Method not implemented.");
}

redeemFill(
vaa: FastTransfer.VAA,
cctp: CircleBridge.Attestation,
Expand Down
Loading

0 comments on commit 7f05805

Please sign in to comment.