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

Commit 8fce9d7

Browse files
committed
partial settleOrder
1 parent 889e14e commit 8fce9d7

File tree

2 files changed

+73
-30
lines changed

2 files changed

+73
-30
lines changed

solana/ts/src/protocol/matchingEngine.ts

Lines changed: 58 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -254,15 +254,14 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
254254
yield this.createUnsignedTx({ transaction }, "MatchingEngine.executeFastOrder");
255255
}
256256

257-
async *prepareOrderResponse(
257+
private async _prepareOrderResponseIx(
258258
sender: AnySolanaAddress,
259259
fast: VAA<"FastTransfer:FastMarketOrder">,
260260
finalized: VAA<"FastTransfer:CctpDeposit">,
261261
cctp: {
262262
message: CircleBridge.Message;
263263
attestation: CircleAttestation;
264264
},
265-
lookupTables?: AddressLookupTableAccount[],
266265
) {
267266
const payer = new SolanaAddress(sender).unwrap();
268267

@@ -292,12 +291,26 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
292291
},
293292
);
294293

295-
const computeIx = ComputeBudgetProgram.setComputeUnitLimit({
296-
units: 300_000,
297-
});
294+
return ix;
295+
}
298296

299-
const transaction = await this.createTx(payer, [ix, computeIx], undefined, lookupTables);
297+
async *prepareOrderResponse(
298+
sender: AnySolanaAddress,
299+
fast: VAA<"FastTransfer:FastMarketOrder">,
300+
finalized: VAA<"FastTransfer:CctpDeposit">,
301+
cctp: {
302+
message: CircleBridge.Message;
303+
attestation: CircleAttestation;
304+
},
305+
lookupTables?: AddressLookupTableAccount[],
306+
) {
307+
const payer = new SolanaAddress(sender).unwrap();
308+
const ix = await this._prepareOrderResponseIx(sender, fast, finalized, cctp);
309+
if (ix === undefined) return;
310+
311+
const computeIx = ComputeBudgetProgram.setComputeUnitLimit({ units: 300_000 });
300312

313+
const transaction = await this.createTx(payer, [ix, computeIx], undefined, lookupTables);
301314
yield this.createUnsignedTx({ transaction }, "MatchingEngine.prepareOrderResponse");
302315
}
303316

@@ -311,20 +324,51 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
311324
},
312325
lookupTables?: AddressLookupTableAccount[],
313326
) {
327+
const payer = new SolanaAddress(sender).unwrap();
328+
314329
// If the finalized VAA and CCTP message/attestation are passed
315330
// we may try to prepare the order response
331+
// this yields its own transaction
316332
if (finalized && cctp)
317333
yield* this.prepareOrderResponse(sender, fast, finalized, cctp, lookupTables);
318334

319-
const executor = new SolanaAddress(sender).unwrap();
320-
const preparedAddress = this.preparedOrderResponseAddress(keccak256(fast.hash));
321-
322-
const settleIx = await this.settleAuctionCompleteIx({
323-
executor,
324-
preparedOrderResponse: preparedAddress,
325-
});
335+
const digest = keccak256(fast.hash);
336+
const preparedOrderResponse = this.preparedOrderResponseAddress(digest);
337+
const auction = this.auctionAddress(digest);
338+
const fastVaa = coreUtils.derivePostedVaaKey(
339+
this.coreBridgeProgramId(),
340+
Buffer.from(fast.hash),
341+
);
326342

327-
const transaction = await this.createTx(executor, [settleIx], undefined, lookupTables);
343+
const settleIx = await (async () => {
344+
if (finalized && !cctp) {
345+
if (fast.payload.targetChain === "Solana") {
346+
const reservedSequence = this.reservedFastFillSequenceAddress(digest);
347+
return await this.settleAuctionNoneLocalIx({
348+
payer,
349+
reservedSequence,
350+
preparedOrderResponse,
351+
auction,
352+
});
353+
} else {
354+
return this.settleAuctionNoneCctpIx(
355+
{
356+
payer,
357+
fastVaa,
358+
preparedOrderResponse,
359+
},
360+
{ targetChain: toChainId(fast.payload.targetChain) },
361+
);
362+
}
363+
} else {
364+
return await this.settleAuctionCompleteIx({
365+
executor: payer,
366+
preparedOrderResponse,
367+
});
368+
}
369+
})();
370+
371+
const transaction = await this.createTx(payer, [settleIx], undefined, lookupTables);
328372

329373
yield this.createUnsignedTx({ transaction }, "MatchingEngine.settleAuctionComplete");
330374
}

solana/ts/tests/01__matchingEngine.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4717,18 +4717,22 @@ describe("Matching Engine", function () {
47174717
}
47184718
})();
47194719

4720-
const computeIx = ComputeBudgetProgram.setComputeUnitLimit({
4721-
units: 300_000,
4722-
});
4720+
const fastVaaAccount = await VaaAccount.fetch(connection, fastVaa);
4721+
const { fastMarketOrder } = LiquidityLayerMessage.decode(fastVaaAccount.payload());
4722+
expect(fastMarketOrder).is.not.undefined;
47234723

4724-
const ix = await engine.settleAuctionNoneCctpIx({
4725-
...accounts,
4726-
fastVaa,
4727-
preparedOrderResponse,
4728-
});
4724+
let finalizedVaaAccount = finalizedVaa
4725+
? await VaaAccount.fetch(connection, finalizedVaa)
4726+
: undefined;
4727+
4728+
const txs = engine.settleOrder(
4729+
accounts.payer,
4730+
fastVaaAccount.vaa("FastTransfer:FastMarketOrder"),
4731+
finalizedVaaAccount?.vaa("FastTransfer:CctpDeposit"),
4732+
);
47294733

47304734
if (errorMsg !== null) {
4731-
return expectIxErr(connection, [computeIx, ix], unwrapSigners(signers), errorMsg);
4735+
return expectTxsErr(signers[0], txs, errorMsg);
47324736
}
47334737

47344738
// If we are at this point, we require that prepareOrderResponseForTest be called. So the
@@ -4744,16 +4748,11 @@ describe("Matching Engine", function () {
47444748
feeRecipientToken,
47454749
);
47464750

4747-
await expectIxOk(connection, [computeIx, ix], unwrapSigners(signers));
4748-
4749-
const fastVaaAccount = await VaaAccount.fetch(connection, fastVaa);
4750-
const { fastMarketOrder } = LiquidityLayerMessage.decode(fastVaaAccount.payload());
4751-
expect(fastMarketOrder).is.not.undefined;
4751+
await expectTxsOk(signers[0], txs);
47524752

4753-
const finalizedVaaAccount = await VaaAccount.fetch(connection, finalizedVaa);
47544753
const {
47554754
message: { payload: slowOrderResponse },
4756-
} = LiquidityLayerMessage.decode(finalizedVaaAccount.payload()).deposit!;
4755+
} = LiquidityLayerMessage.decode(finalizedVaaAccount!.payload()).deposit!;
47574756
expect(slowOrderResponse).is.not.undefined;
47584757

47594758
const fee =

0 commit comments

Comments
 (0)