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

Commit 4cf3221

Browse files
committed
replacing more ix with tx
1 parent 7a9bc69 commit 4cf3221

File tree

8 files changed

+353
-377
lines changed

8 files changed

+353
-377
lines changed

solana/Anchor.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@ cluster = "Localnet"
3131
wallet = "ts/tests/keys/pFCBP4bhqdSsrWUVTgqhPsLrfEdChBK17vgFM7TxjxQ.json"
3232

3333
[scripts]
34-
test-local = "npx ts-mocha -p ./tsconfig.anchor.json -t 1000000 --bail --full-trace --exit ts/tests/0[0-9]*.ts"
34+
test-local = "npx ts-mocha -p ./tsconfig.anchor.json -t 1000000 --bail --full-trace --exit ts/tests/01*.ts"
35+
# test-local = "npx ts-mocha -p ./tsconfig.anchor.json -t 1000000 --bail --full-trace --exit ts/tests/0[0-9]*.ts"
3536
test-upgrade-fork = "npx ts-mocha -p ./tsconfig.anchor.json -t 1000000 --bail --full-trace --exit ts/tests/1[0-9]*.ts"
3637

3738
[test]

solana/ts/src/matchingEngine/index.ts

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import {
5656
} from "./state";
5757
import { ChainId, toChainId, isChainId, Chain } from "@wormhole-foundation/sdk-base";
5858
import { programDerivedAddresses } from "./pdas";
59+
import { FastTransfer } from "@wormhole-foundation/example-liquidity-layer-definitions";
5960

6061
export const PROGRAM_IDS = [
6162
"MatchingEngine11111111111111111111111111111",
@@ -682,21 +683,15 @@ export class MatchingEngineProgram {
682683
}
683684

684685
routerEndpointComposite(addr: PublicKey): { endpoint: PublicKey } {
685-
return {
686-
endpoint: addr,
687-
};
686+
return { endpoint: addr };
688687
}
689688

690689
liquidityLayerVaaComposite(vaa: PublicKey): { vaa: PublicKey } {
691-
return {
692-
vaa,
693-
};
690+
return { vaa };
694691
}
695692

696693
usdcComposite(mint?: PublicKey): { mint: PublicKey } {
697-
return {
698-
mint: mint ?? this.mint,
699-
};
694+
return { mint: mint ?? this.mint };
700695
}
701696

702697
localTokenRouterComposite(tokenRouterProgram: PublicKey): {
@@ -1719,7 +1714,6 @@ export class MatchingEngineProgram {
17191714
}
17201715
const {
17211716
custodian,
1722-
routerEndpoint: toRouterEndpoint,
17231717
coreMessage,
17241718
cctpMessage,
17251719
coreBridgeConfig,

solana/ts/src/protocol/matchingEngine.ts

Lines changed: 83 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import {
2+
ComputeBudgetProgram,
23
Connection,
34
PublicKey,
45
TransactionInstruction,
@@ -17,8 +18,10 @@ import {
1718
Contracts,
1819
UnsignedTransaction,
1920
VAA,
21+
keccak256,
2022
} from "@wormhole-foundation/sdk-definitions";
2123
import {
24+
AnySolanaAddress,
2225
SolanaAddress,
2326
SolanaChains,
2427
SolanaPlatform,
@@ -67,11 +70,11 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
6770
}
6871

6972
async *initialize(
70-
owner: AccountAddress<C>,
71-
ownerAssistant: AccountAddress<C>,
72-
feeRecipient: AccountAddress<C>,
73+
owner: AnySolanaAddress,
74+
ownerAssistant: AnySolanaAddress,
75+
feeRecipient: AnySolanaAddress,
7376
params: AuctionParameters,
74-
mint?: AccountAddress<C>,
77+
mint?: AnySolanaAddress,
7578
) {
7679
const ix = await this.initializeIx(
7780
{
@@ -86,22 +89,24 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
8689
yield this.createUnsignedTx({ transaction }, "MatchingEngine.initialize");
8790
}
8891

89-
async *setPause(sender: AccountAddress<C>, pause: boolean) {
92+
async *setPause(sender: AnySolanaAddress, pause: boolean) {
9093
const payer = new SolanaAddress(sender).unwrap();
9194
const ix = await this.setPauseIx({ ownerOrAssistant: payer }, pause);
9295
const transaction = await this.createTx(payer, [ix]);
9396
yield this.createUnsignedTx({ transaction }, "MatchingEngine.setPause");
9497
}
9598

9699
async *registerRouter<RC extends Chain>(
97-
sender: AccountAddress<C>,
100+
sender: AnySolanaAddress,
98101
chain: RC,
99102
cctpDomain: number,
100103
router: AccountAddress<RC>,
101-
tokenAccount?: AccountAddress<C>,
104+
tokenAccount?: AnySolanaAddress,
102105
) {
103106
const ownerOrAssistant = new SolanaAddress(sender).unwrap();
104-
const mintRecipient = tokenAccount?.toUniversalAddress().toUint8Array() ?? null;
107+
const mintRecipient = tokenAccount
108+
? new SolanaAddress(tokenAccount).toUniversalAddress().toUint8Array()
109+
: null;
105110
const ix = await this.addCctpRouterEndpointIx(
106111
{ ownerOrAssistant },
107112
{
@@ -116,6 +121,40 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
116121
yield this.createUnsignedTx({ transaction }, "MatchingEngine.registerRouter");
117122
}
118123

124+
async *updateRouter<RC extends Chain>(
125+
sender: AnySolanaAddress,
126+
chain: RC,
127+
cctpDomain: number,
128+
router: AccountAddress<RC>,
129+
tokenAccount?: AnySolanaAddress,
130+
) {
131+
const owner = new SolanaAddress(sender).unwrap();
132+
const mintRecipient = tokenAccount
133+
? new SolanaAddress(tokenAccount).toUniversalAddress().toUint8Array()
134+
: null;
135+
136+
const ix = await this.updateCctpRouterEndpointIx(
137+
{ owner },
138+
{
139+
chain: toChainId(chain),
140+
cctpDomain: cctpDomain,
141+
address: Array.from(router.toUniversalAddress().toUint8Array()),
142+
mintRecipient: mintRecipient ? Array.from(mintRecipient) : null,
143+
},
144+
);
145+
146+
const transaction = await this.createTx(owner, [ix]);
147+
yield this.createUnsignedTx({ transaction }, "MatchingEngine.updateRouter");
148+
}
149+
150+
async *disableRouter<RC extends Chain>(sender: AnySolanaAddress, chain: RC) {
151+
const owner = new SolanaAddress(sender).unwrap();
152+
const ix = await this.disableRouterEndpointIx({ owner }, toChainId(chain));
153+
154+
const transaction = await this.createTx(owner, [ix]);
155+
yield this.createUnsignedTx({ transaction }, "MatchingEngine.disableRouter");
156+
}
157+
119158
async *setConfiguration(config: {
120159
enabled: boolean;
121160
maxAmount: bigint;
@@ -126,7 +165,7 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
126165
}
127166

128167
async *placeInitialOffer(
129-
sender: AccountAddress<C>,
168+
sender: AnySolanaAddress,
130169
vaa: FastTransfer.VAA,
131170
offerPrice: bigint,
132171
totalDeposit?: bigint,
@@ -144,26 +183,48 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
144183
);
145184

146185
const transaction = await this.createTx(payer, ixs);
147-
yield this.createUnsignedTx(
148-
{ transaction: transaction },
149-
"MatchingEngine.placeInitialOffer",
150-
);
186+
yield this.createUnsignedTx({ transaction }, "MatchingEngine.placeInitialOffer");
151187
}
152188

153-
improveOffer(
154-
id: Uint8Array,
155-
bid: bigint,
156-
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
157-
throw new Error("Method not implemented.");
189+
async *improveOffer(sender: AnySolanaAddress, vaa: FastTransfer.VAA, offer: bigint) {
190+
const participant = new SolanaAddress(sender).unwrap();
191+
const auction = this.auctionAddress(keccak256(vaa.hash));
192+
193+
const ixs = await this.improveOfferIx({ participant, auction }, { offerPrice: offer });
194+
195+
const transaction = await this.createTx(participant, ixs);
196+
yield this.createUnsignedTx({ transaction }, "MatchingEngine.improveOffer");
158197
}
159-
executeFastOrder(
160-
vaa: FastTransfer.VAA,
161-
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
198+
199+
async *executeFastOrder(sender: AnySolanaAddress, vaa: FastTransfer.VAA) {
200+
const payer = new SolanaAddress(sender).unwrap();
201+
202+
const fastVaa = coreUtils.derivePostedVaaKey(
203+
this.coreBridgeProgramId(),
204+
Buffer.from(vaa.hash),
205+
);
206+
207+
const ix = await this.executeFastOrderCctpIx({
208+
payer,
209+
fastVaa,
210+
});
211+
212+
const computeIx = ComputeBudgetProgram.setComputeUnitLimit({
213+
units: 300_000,
214+
});
215+
216+
const transaction = await this.createTx(payer, [ix, computeIx]);
217+
yield this.createUnsignedTx({ transaction }, "MatchingEngine.improveOffer");
218+
}
219+
220+
async *settleAuctionComplete() {
162221
throw new Error("Method not implemented.");
163222
}
164-
settleAuctionComplete(): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
223+
224+
settleAuction(): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
165225
throw new Error("Method not implemented.");
166226
}
227+
167228
getAuctionGracePeriod(): Promise<number> {
168229
throw new Error("Method not implemented.");
169230
}

solana/ts/src/testing/mock.ts

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,14 @@
11
import { Connection, Keypair } from "@solana/web3.js";
2-
import { ethers } from "ethers";
3-
import { LiquidityLayerMessage } from "../common";
4-
import { CORE_BRIDGE_PID, GUARDIAN_KEY, MOCK_GUARDIANS } from "./consts";
5-
import { postVaa, getBlockTime } from "./utils";
2+
import { Chain, Network } from "@wormhole-foundation/sdk-base";
3+
import { SignAndSendSigner, toUniversal } from "@wormhole-foundation/sdk-definitions";
64
import { mocks } from "@wormhole-foundation/sdk-definitions/testing";
7-
import { signAndSendWait } from "@wormhole-foundation/sdk-connect";
8-
import { SolanaWormholeCore, utils as coreUtils } from "@wormhole-foundation/sdk-solana-core";
9-
import { Chain, Network, contracts } from "@wormhole-foundation/sdk-base";
10-
import {
11-
SignAndSendSigner,
12-
buildConfig,
13-
serialize,
14-
toUniversal,
15-
} from "@wormhole-foundation/sdk-definitions";
165
import { SolanaAddress, SolanaSendSigner } from "@wormhole-foundation/sdk-solana";
6+
import { utils as coreUtils } from "@wormhole-foundation/sdk-solana-core";
7+
import { ethers } from "ethers";
8+
import { LiquidityLayerMessage } from "../common";
179
import { VaaAccount } from "../wormhole";
10+
import { CORE_BRIDGE_PID, GUARDIAN_KEY, MOCK_GUARDIANS } from "./consts";
11+
import { getBlockTime, postVaa } from "./utils";
1812

1913
export type SDKSigner<N extends Network> = SolanaSendSigner<N, "Solana">;
2014

@@ -56,15 +50,14 @@ export async function postLiquidityLayerVaav2(
5650

5751
const vaa = MOCK_GUARDIANS.addSignatures(published, [0]);
5852

59-
await postVaa(connection, payer, Buffer.from(serialize(vaa)));
60-
61-
const address = coreUtils.derivePostedVaaKey(CORE_BRIDGE_PID, Buffer.from(vaa.hash));
53+
const { address } = await postVaa(connection, payer, vaa);
6254
const account = await VaaAccount.fetch(connection, address);
6355

6456
return { address, account };
6557
}
6658

67-
// TODO: return VaaAccount, too
59+
// TODO: Replace any invocations of this function with postLiquidityLayerVaav2
60+
// then rename back to postLiquidityLayerVaa
6861
export async function postLiquidityLayerVaa(
6962
connection: Connection,
7063
payer: Keypair,
@@ -92,7 +85,7 @@ export async function postLiquidityLayerVaa(
9285
);
9386
const vaa = guardians.addSignatures(published, [0]);
9487

95-
await postVaa(connection, payer, Buffer.from(serialize(vaa)));
88+
await postVaa(connection, payer, vaa);
9689

9790
return coreUtils.derivePostedVaaKey(CORE_BRIDGE_PID, Buffer.from(vaa.hash));
9891
}

solana/ts/src/testing/utils.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,9 @@ import { execSync } from "child_process";
1616
import { Err, Ok } from "ts-results";
1717
import { CORE_BRIDGE_PID, USDC_MINT_ADDRESS } from "./consts";
1818
import { SolanaSendSigner, SolanaUnsignedTransaction } from "@wormhole-foundation/sdk-solana";
19-
import { SolanaWormholeCore } from "@wormhole-foundation/sdk-solana-core";
19+
import { SolanaWormholeCore, utils as coreUtils } from "@wormhole-foundation/sdk-solana-core";
2020
import { SignAndSendSigner as SdkSigner, signAndSendWait } from "@wormhole-foundation/sdk-connect";
21-
import { UniversalAddress, deserialize } from "@wormhole-foundation/sdk-definitions";
21+
import { UniversalAddress, VAA, deserialize } from "@wormhole-foundation/sdk-definitions";
2222
import { Chain, Network } from "@wormhole-foundation/sdk-base";
2323

2424
export function toUniversalAddress(address: number[] | Buffer | Array<number>): UniversalAddress {
@@ -198,21 +198,25 @@ async function debugSendAndConfirmTransaction(
198198
export async function postVaa(
199199
connection: Connection,
200200
payer: Keypair | SdkSigner<Network, "Solana">,
201-
vaaBuf: Buffer,
201+
vaa: VAA,
202202
coreBridgeAddress?: PublicKey,
203203
) {
204+
coreBridgeAddress ??= CORE_BRIDGE_PID;
205+
204206
const core = new SolanaWormholeCore("Devnet", "Solana", connection, {
205-
coreBridge: (coreBridgeAddress ?? CORE_BRIDGE_PID).toString(),
207+
coreBridge: coreBridgeAddress.toString(),
206208
});
207209

208210
const signer =
209211
payer instanceof Keypair
210212
? new SolanaSendSigner(connection, "Solana", payer, false, {})
211213
: payer;
212214

213-
const txs = core.postVaa(signer.address(), deserialize("Uint8Array", vaaBuf));
215+
const txs = core.postVaa(signer.address(), vaa);
216+
const address = coreUtils.derivePostedVaaKey(coreBridgeAddress, Buffer.from(vaa.hash));
217+
const txids = await signAndSendWait(txs, signer);
214218

215-
return await signAndSendWait(txs, signer);
219+
return { txids, address };
216220
}
217221

218222
export function loadProgramBpf(artifactPath: string, keypath: string): PublicKey {

solana/ts/src/wormhole/index.ts

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ import {
88
toChainId,
99
} from "@wormhole-foundation/sdk-base";
1010
import {
11+
PayloadLiteral,
1112
VAA,
1213
createVAA,
1314
deserialize,
1415
keccak256,
1516
layoutItems,
17+
serialize,
1618
} from "@wormhole-foundation/sdk-definitions";
1719
export * from "./spy";
1820

@@ -85,10 +87,17 @@ export class VaaAccount {
8587
}
8688
}
8789

88-
vaa(): VAA<"Uint8Array"> {
89-
if (this._encodedVaa !== undefined) return this._encodedVaa.vaa;
90-
if (this._postedVaaV1 !== undefined) return this._postedVaaV1;
91-
throw new Error("impossible: vaa() failed");
90+
vaa<P extends PayloadLiteral = "Uint8Array">(payload?: P): VAA<P> {
91+
const vaa =
92+
this._encodedVaa !== undefined
93+
? this._encodedVaa.vaa
94+
: this._postedVaaV1 !== undefined
95+
? this._postedVaaV1
96+
: undefined;
97+
98+
if (!vaa) throw new Error("impossible: vaa() failed");
99+
100+
return (payload ? deserialize(payload, serialize(vaa)) : vaa) as VAA<P>;
92101
}
93102

94103
emitterInfo(): EmitterInfo {

0 commit comments

Comments
 (0)