Skip to content

Commit

Permalink
partial settleOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
barnjamin committed Jun 20, 2024
1 parent b8c15da commit 4c0dfe2
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 30 deletions.
72 changes: 58 additions & 14 deletions solana/ts/src/protocol/matchingEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -254,15 +254,14 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
yield this.createUnsignedTx({ transaction }, "MatchingEngine.executeFastOrder");
}

async *prepareOrderResponse(
private async _prepareOrderResponseIx(
sender: AnySolanaAddress,
fast: VAA<"FastTransfer:FastMarketOrder">,
finalized: VAA<"FastTransfer:CctpDeposit">,
cctp: {
message: CircleBridge.Message;
attestation: CircleAttestation;
},
lookupTables?: AddressLookupTableAccount[],
) {
const payer = new SolanaAddress(sender).unwrap();

Expand Down Expand Up @@ -292,12 +291,26 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
},
);

const computeIx = ComputeBudgetProgram.setComputeUnitLimit({
units: 300_000,
});
return ix;
}

const transaction = await this.createTx(payer, [ix, computeIx], undefined, lookupTables);
async *prepareOrderResponse(
sender: AnySolanaAddress,
fast: VAA<"FastTransfer:FastMarketOrder">,
finalized: VAA<"FastTransfer:CctpDeposit">,
cctp: {
message: CircleBridge.Message;
attestation: CircleAttestation;
},
lookupTables?: AddressLookupTableAccount[],
) {
const payer = new SolanaAddress(sender).unwrap();
const ix = await this._prepareOrderResponseIx(sender, fast, finalized, cctp);
if (ix === undefined) return;

const computeIx = ComputeBudgetProgram.setComputeUnitLimit({ units: 300_000 });

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

Expand All @@ -311,20 +324,51 @@ export class SolanaMatchingEngine<N extends Network, C extends SolanaChains>
},
lookupTables?: AddressLookupTableAccount[],
) {
const payer = new SolanaAddress(sender).unwrap();

// If the finalized VAA and CCTP message/attestation are passed
// we may try to prepare the order response
// this yields its own transaction
if (finalized && cctp)
yield* this.prepareOrderResponse(sender, fast, finalized, cctp, lookupTables);

const executor = new SolanaAddress(sender).unwrap();
const preparedAddress = this.preparedOrderResponseAddress(keccak256(fast.hash));

const settleIx = await this.settleAuctionCompleteIx({
executor,
preparedOrderResponse: preparedAddress,
});
const digest = keccak256(fast.hash);
const preparedOrderResponse = this.preparedOrderResponseAddress(digest);
const auction = this.auctionAddress(digest);
const fastVaa = coreUtils.derivePostedVaaKey(
this.coreBridgeProgramId(),
Buffer.from(fast.hash),
);

const transaction = await this.createTx(executor, [settleIx], undefined, lookupTables);
const settleIx = await (async () => {
if (finalized && !cctp) {
if (fast.payload.targetChain === "Solana") {
const reservedSequence = this.reservedFastFillSequenceAddress(digest);
return await this.settleAuctionNoneLocalIx({
payer,
reservedSequence,
preparedOrderResponse,
auction,
});
} else {
return this.settleAuctionNoneCctpIx(
{
payer,
fastVaa,
preparedOrderResponse,
},
{ targetChain: toChainId(fast.payload.targetChain) },
);
}
} else {
return await this.settleAuctionCompleteIx({
executor: payer,
preparedOrderResponse,
});
}
})();

const transaction = await this.createTx(payer, [settleIx], undefined, lookupTables);

yield this.createUnsignedTx({ transaction }, "MatchingEngine.settleAuctionComplete");
}
Expand Down
31 changes: 15 additions & 16 deletions solana/ts/tests/01__matchingEngine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4717,18 +4717,22 @@ describe("Matching Engine", function () {
}
})();

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

const ix = await engine.settleAuctionNoneCctpIx({
...accounts,
fastVaa,
preparedOrderResponse,
});
let finalizedVaaAccount = finalizedVaa
? await VaaAccount.fetch(connection, finalizedVaa)
: undefined;

const txs = engine.settleOrder(
accounts.payer,
fastVaaAccount.vaa("FastTransfer:FastMarketOrder"),
finalizedVaaAccount?.vaa("FastTransfer:CctpDeposit"),
);

if (errorMsg !== null) {
return expectIxErr(connection, [computeIx, ix], unwrapSigners(signers), errorMsg);
return expectTxsErr(signers[0], txs, errorMsg);
}

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

await expectIxOk(connection, [computeIx, ix], unwrapSigners(signers));

const fastVaaAccount = await VaaAccount.fetch(connection, fastVaa);
const { fastMarketOrder } = LiquidityLayerMessage.decode(fastVaaAccount.payload());
expect(fastMarketOrder).is.not.undefined;
await expectTxsOk(signers[0], txs);

const finalizedVaaAccount = await VaaAccount.fetch(connection, finalizedVaa);
const {
message: { payload: slowOrderResponse },
} = LiquidityLayerMessage.decode(finalizedVaaAccount.payload()).deposit!;
} = LiquidityLayerMessage.decode(finalizedVaaAccount!.payload()).deposit!;
expect(slowOrderResponse).is.not.undefined;

const fee =
Expand Down

0 comments on commit 4c0dfe2

Please sign in to comment.