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

Commit d1e071f

Browse files
committed
replace MatchingEngine with protocol impl
1 parent 9577f84 commit d1e071f

File tree

8 files changed

+190
-62
lines changed

8 files changed

+190
-62
lines changed

evm/ts/src/MatchingEngine/evm.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { ChainId, asChainId } from "@wormhole-foundation/sdk-base";
22
import { ethers } from "ethers";
3-
import { RouterEndpoint, LiveAuctionData, MatchingEngine, RedeemParameters } from ".";
3+
import { AbstractMatchingEngine, LiveAuctionData, RedeemParameters, RouterEndpoint } from ".";
44
import { LiquidityLayerTransactionResult } from "..";
55
import {
66
IMatchingEngine,
@@ -11,7 +11,7 @@ import {
1111
IWormhole__factory,
1212
} from "../types";
1313

14-
export class EvmMatchingEngine implements MatchingEngine<ethers.ContractTransaction> {
14+
export class MatchingEngine implements AbstractMatchingEngine<ethers.ContractTransaction> {
1515
contract: IMatchingEngine;
1616
circle: ITokenMessenger;
1717

@@ -39,39 +39,39 @@ export class EvmMatchingEngine implements MatchingEngine<ethers.ContractTransact
3939
return this.connection;
4040
}
4141

42-
connect(connection: ethers.Provider): EvmMatchingEngine {
43-
return new EvmMatchingEngine(connection, this.address, this.circleBridge);
42+
connect(connection: ethers.Provider): MatchingEngine {
43+
return new MatchingEngine(connection, this.address, this.circleBridge);
4444
}
4545

46-
async addRouterEndpoint(
46+
async addRouterEndpointTx(
4747
chain: number,
4848
endpoint: RouterEndpoint,
4949
domain: number,
5050
): Promise<ethers.ContractTransaction> {
5151
return this.contract.addRouterEndpoint.populateTransaction(chain, endpoint, domain);
5252
}
5353

54-
async placeInitialBid(
54+
async placeInitialBidTx(
5555
fastTransferVaa: Buffer | Uint8Array,
5656
feeBid: bigint | ethers.BigNumberish,
5757
): Promise<ethers.ContractTransaction> {
5858
return this.contract.placeInitialBid.populateTransaction(fastTransferVaa, feeBid);
5959
}
6060

61-
async improveBid(
61+
async improveBidTx(
6262
auctionId: Buffer | Uint8Array,
6363
feeBid: bigint | ethers.BigNumberish,
6464
): Promise<ethers.ContractTransaction> {
6565
return this.contract.improveBid.populateTransaction(auctionId, feeBid);
6666
}
6767

68-
async executeFastOrder(
68+
async executeFastOrderTx(
6969
fastTransferVaa: Buffer | Uint8Array,
7070
): Promise<ethers.ContractTransaction> {
7171
return this.contract.executeFastOrder.populateTransaction(fastTransferVaa);
7272
}
7373

74-
async executeSlowOrderAndRedeem(
74+
async executeSlowOrderAndRedeemTx(
7575
fastTransferVaa: Buffer | Uint8Array,
7676
params: RedeemParameters,
7777
): Promise<ethers.ContractTransaction> {

evm/ts/src/MatchingEngine/index.ts

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,10 @@ export type RouterEndpoint = {
2424
mintRecipient: string | Buffer | Uint8Array;
2525
};
2626

27-
export abstract class MatchingEngine<PreparedTransactionType extends PreparedInstruction> {
27+
export abstract class AbstractMatchingEngine<PreparedTransactionType extends PreparedInstruction> {
2828
abstract get address(): string;
2929

30-
abstract addRouterEndpoint(
30+
abstract addRouterEndpointTx(
3131
chain: number,
3232
endpoint: RouterEndpoint,
3333
domain: number,
@@ -37,21 +37,21 @@ export abstract class MatchingEngine<PreparedTransactionType extends PreparedIns
3737

3838
abstract auctionStatus(auctionId: Buffer | Uint8Array): Promise<bigint>;
3939

40-
abstract placeInitialBid(
40+
abstract placeInitialBidTx(
4141
fastTransferVaa: Buffer | Uint8Array,
4242
feeBid: bigint,
4343
): Promise<PreparedTransactionType>;
4444

45-
abstract improveBid(
45+
abstract improveBidTx(
4646
auctionId: Buffer | Uint8Array,
4747
feeBid: bigint,
4848
): Promise<PreparedTransactionType>;
4949

50-
abstract executeFastOrder(
50+
abstract executeFastOrderTx(
5151
fastTransferVaa: Buffer | Uint8Array,
5252
): Promise<PreparedTransactionType>;
5353

54-
abstract executeSlowOrderAndRedeem(
54+
abstract executeSlowOrderAndRedeemTx(
5555
fastTransferVaa: Buffer | Uint8Array,
5656
params: RedeemParameters,
5757
): Promise<PreparedTransactionType>;

evm/ts/src/TokenRouter/index.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { deserialize, CircleBridge, VAA } from "@wormhole-foundation/sdk-definitions";
2-
import { LiquidityLayerTransactionResult, PreparedInstruction } from "..";
31
import { encoding } from "@wormhole-foundation/sdk-base";
4-
import { FastTransfer } from "@wormhole-foundation/example-liquidity-layer-definitions";
2+
import { CircleBridge, VAA, deserialize } from "@wormhole-foundation/sdk-definitions";
3+
import { LiquidityLayerTransactionResult, PreparedInstruction } from "..";
54
export * from "./evm";
65

76
export type FastTransferParameters = {

evm/ts/src/protocol/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./tokenRouter";
2+
export * from "./matchingEngine";
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
import {
2+
MatchingEngine,
3+
TokenRouter,
4+
} from "@wormhole-foundation/example-liquidity-layer-definitions";
5+
import { Chain, Network, encoding, toChainId } from "@wormhole-foundation/sdk-base";
6+
import {
7+
AccountAddress,
8+
CircleBridge,
9+
Contracts,
10+
UnsignedTransaction,
11+
VAA,
12+
serialize,
13+
} from "@wormhole-foundation/sdk-definitions";
14+
import {
15+
AnyEvmAddress,
16+
EvmAddress,
17+
EvmChains,
18+
EvmUnsignedTransaction,
19+
} from "@wormhole-foundation/sdk-evm";
20+
import { ethers } from "ethers";
21+
import { MatchingEngine as _MatchingEngine } from "../MatchingEngine";
22+
23+
export class EvmMatchingEngine<N extends Network, C extends EvmChains>
24+
extends _MatchingEngine
25+
implements MatchingEngine<N, C>
26+
{
27+
constructor(
28+
readonly network: N,
29+
readonly chain: C,
30+
provider: ethers.Provider,
31+
readonly contracts: Contracts & MatchingEngine.Addresses,
32+
) {
33+
super(provider, contracts.matchingEngine, contracts.cctp.tokenMessenger);
34+
}
35+
registerRouter<RC extends Chain>(
36+
sender: AccountAddress<C>,
37+
chain: RC,
38+
cctpDomain: number,
39+
router: AccountAddress<RC>,
40+
tokenAccount?: AccountAddress<C> | undefined,
41+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
42+
throw new Error("Method not implemented.");
43+
}
44+
updateRouter<RC extends Chain>(
45+
sender: AccountAddress<C>,
46+
chain: RC,
47+
cctpDomain: number,
48+
router: AccountAddress<RC>,
49+
tokenAccount?: AccountAddress<C> | undefined,
50+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
51+
throw new Error("Method not implemented.");
52+
}
53+
disableRouter<RC extends Chain>(
54+
sender: AccountAddress<C>,
55+
chain: RC,
56+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
57+
throw new Error("Method not implemented.");
58+
}
59+
setPause(
60+
sender: AccountAddress<C>,
61+
pause: boolean,
62+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
63+
throw new Error("Method not implemented.");
64+
}
65+
setConfiguration(config: {
66+
enabled: boolean;
67+
maxAmount: bigint;
68+
baseFee: bigint;
69+
initAuctionFee: bigint;
70+
}): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
71+
throw new Error("Method not implemented.");
72+
}
73+
placeInitialOffer(
74+
sender: AccountAddress<C>,
75+
vaa: VAA<"FastTransfer:FastMarketOrder">,
76+
offerPrice: bigint,
77+
totalDeposit?: bigint | undefined,
78+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
79+
throw new Error("Method not implemented.");
80+
}
81+
improveOffer(
82+
sender: AccountAddress<C>,
83+
vaa: VAA<"FastTransfer:FastMarketOrder">,
84+
offer: bigint,
85+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
86+
throw new Error("Method not implemented.");
87+
}
88+
executeFastOrder(
89+
sender: AccountAddress<C>,
90+
vaa: VAA<"FastTransfer:FastMarketOrder">,
91+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
92+
throw new Error("Method not implemented.");
93+
}
94+
prepareOrderResponse(
95+
sender: AccountAddress<C>,
96+
vaa: VAA<"FastTransfer:FastMarketOrder">,
97+
deposit: VAA<"FastTransfer:CctpDeposit">,
98+
cctp: CircleBridge.Attestation,
99+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
100+
throw new Error("Method not implemented.");
101+
}
102+
settleOrder(
103+
sender: AccountAddress<C>,
104+
fast: VAA<"FastTransfer:FastMarketOrder">,
105+
deposit?: VAA<"FastTransfer:CctpDeposit"> | undefined,
106+
cctp?: CircleBridge.Attestation | undefined,
107+
): AsyncGenerator<UnsignedTransaction<N, C>, any, unknown> {
108+
throw new Error("Method not implemented.");
109+
}
110+
111+
//async *redeemFill(
112+
// sender: AnyEvmAddress,
113+
// vaa: VAA<"FastTransfer:CctpDeposit"> | VAA<"FastTransfer:FastFill">,
114+
// cctp?: CircleBridge.Attestation,
115+
//) {
116+
// const from = new EvmAddress(sender).unwrap();
117+
// const txReq = await this.redeemFillTx({
118+
// encodedWormholeMessage: serialize(vaa),
119+
// circleBridgeMessage: cctp ? CircleBridge.serialize(cctp.message) : new Uint8Array(),
120+
// circleAttestation: cctp ? encoding.hex.decode(cctp.attestation!) : new Uint8Array(),
121+
// });
122+
// yield this.createUnsignedTx({ ...txReq, from }, "TokenRouter.redeemFill");
123+
//}
124+
125+
private createUnsignedTx(
126+
txReq: ethers.TransactionRequest,
127+
description: string,
128+
parallelizable: boolean = false,
129+
): UnsignedTransaction<N, C> {
130+
return new EvmUnsignedTransaction(
131+
txReq,
132+
this.network,
133+
this.chain,
134+
description,
135+
parallelizable,
136+
);
137+
}
138+
}

evm/ts/src/testing/env.ts

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { TokenRouter } from "@wormhole-foundation/example-liquidity-layer-definitions";
2-
import { Platform } from "@wormhole-foundation/sdk-base";
2+
import { Chain, Platform, toChain } from "@wormhole-foundation/sdk-base";
3+
import { toUniversal } from "@wormhole-foundation/sdk-definitions";
34
//@ts-ignore
45
import { parse as envParse } from "envfile";
56
import * as fs from "fs";
@@ -15,7 +16,7 @@ export type LiquidityLayerEnv = {
1516
tokenRouterAddress: string;
1617
tokenRouterMintRecipient?: string;
1718
feeRecipient?: string;
18-
matchingEngineChain: string;
19+
matchingEngineChain: Chain;
1920
matchingEngineAddress: string;
2021
matchingEngineMintRecipient: string;
2122
matchingEngineDomain?: string;
@@ -67,7 +68,7 @@ export function parseLiquidityLayerEnvFile(envPath: string): LiquidityLayerEnv {
6768
tokenRouterAddress: contents.TOKEN_ROUTER_ADDRESS,
6869
tokenRouterMintRecipient: contents.TOKEN_ROUTER_MINT_RECIPIENT,
6970
feeRecipient: contents.FEE_RECIPIENT_ADDRESS,
70-
matchingEngineChain: contents.MATCHING_ENGINE_CHAIN,
71+
matchingEngineChain: toChain(parseInt(contents.MATCHING_ENGINE_CHAIN)),
7172
matchingEngineAddress: contents.MATCHING_ENGINE_ADDRESS,
7273
matchingEngineMintRecipient: contents.MATCHING_ENGINE_MINT_RECIPIENT,
7374
matchingEngineDomain: contents.MATCHING_ENGINE_DOMAIN,
@@ -77,13 +78,15 @@ export function parseLiquidityLayerEnvFile(envPath: string): LiquidityLayerEnv {
7778
export function toContractAddresses(env: LiquidityLayerEnv): TokenRouter.Addresses {
7879
return {
7980
tokenRouter: env.tokenRouterAddress,
80-
matchingEngine: env.tokenMessengerAddress,
81+
matchingEngine: toUniversal(env.matchingEngineChain, env.matchingEngineAddress)
82+
.toNative(env.matchingEngineChain)
83+
.toString(),
8184
coreBridge: env.wormholeAddress,
8285
cctp: {
8386
tokenMessenger: env.tokenMessengerAddress,
84-
// TODO
87+
usdcMint: env.tokenAddress,
88+
// TODO: needed?
8589
messageTransmitter: "",
86-
usdcMint: "",
8790
wormhole: "",
8891
wormholeRelayer: "",
8992
},

evm/ts/tests/03__marketOrder.ts

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
import { encoding, toChainId } from "@wormhole-foundation/sdk-base";
2-
import { signAndSendWait } from "@wormhole-foundation/sdk-connect";
1+
import { TokenRouter } from "@wormhole-foundation/example-liquidity-layer-definitions";
2+
import { encoding } from "@wormhole-foundation/sdk-base";
3+
import { CircleBridge, deserialize, toNative } from "@wormhole-foundation/sdk-definitions";
34
import { expect } from "chai";
45
import { ethers } from "ethers";
5-
import { EvmTokenRouter, OrderResponse, errorDecoder } from "../src";
6+
import { EvmTokenRouter, OrderResponse, decodedOrderResponse } from "../src";
67
import {
78
CircleAttester,
89
GuardianNetwork,
@@ -19,9 +20,6 @@ import {
1920
tryNativeToUint8Array,
2021
} from "../src/testing";
2122
import { IERC20__factory } from "../src/types";
22-
import { TokenRouter } from "@wormhole-foundation/example-liquidity-layer-definitions";
23-
import { CircleBridge, deserialize, toNative } from "@wormhole-foundation/sdk-definitions";
24-
import { EvmNativeSigner } from "@wormhole-foundation/sdk-evm/dist/cjs";
2523

2624
const CHAIN_PATHWAYS: ValidNetwork[][] = [
2725
["Ethereum", "Avalanche"],
@@ -160,16 +158,8 @@ describe("Market Order Business Logic -- CCTP to CCTP", () => {
160158
const usdc = IERC20__factory.connect(toEnv.tokenAddress, toProvider);
161159
const balanceBefore = await usdc.balanceOf(toWallet.address);
162160

163-
const vaa = deserialize(
164-
"FastTransfer:CctpDeposit",
165-
orderResponse.encodedWormholeMessage,
166-
);
167-
const [msg] = CircleBridge.deserialize(orderResponse.circleBridgeMessage);
168-
const cctpMsg: CircleBridge.Attestation = {
169-
message: msg,
170-
attestation: encoding.hex.encode(orderResponse.circleAttestation),
171-
};
172-
const txs = toTokenRouter.redeemFill(toWallet.address, vaa, cctpMsg);
161+
const { vaa, cctp } = decodedOrderResponse(orderResponse);
162+
const txs = toTokenRouter.redeemFill(toWallet.address, vaa, cctp);
173163
const receipt = await signSendMineWait(txs, toSigner);
174164

175165
const balanceAfter = await usdc.balanceOf(toWallet.address);

0 commit comments

Comments
 (0)