Skip to content
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 18 additions & 9 deletions packages/sui-indexer/src/redeem-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,6 @@ export class RedeemService {
const availableUtxos = await this.storage.getAvailableUtxos(req.setup_id);
const selectedUtxos = selectUtxos(availableUtxos, req.amount);

// TODO: we should continue only if our solution is better than the existing one - in case
// someone frontrun us.

if (!selectedUtxos) {
logger.warn({
msg: "Insufficient UTXOs for request",
Expand All @@ -338,14 +335,26 @@ export class RedeemService {
return;
}

try {
const client = this.getSuiClient(req.sui_network);
const txDigest = await client.proposeRedeemUtxos({
const client = this.getSuiClient(req.sui_network);
const proposalArgs = {
redeemId: req.redeem_id,
utxoIds: selectedUtxos.map((u) => u.nbtc_utxo_id),
nbtcPkg: req.nbtc_pkg,
nbtcContract: req.nbtc_contract,
};

const wouldSucceed = await client.dryRunProposeUtxos(proposalArgs);
if (!wouldSucceed) {
logger.info({
msg: "Dry-run failed, existing proposal is better or equal",
redeemId: req.redeem_id,
utxoIds: selectedUtxos.map((u) => u.nbtc_utxo_id),
nbtcPkg: req.nbtc_pkg,
nbtcContract: req.nbtc_contract,
ourUtxoCount: selectedUtxos.length,
});
return;
}

try {
const txDigest = await client.proposeRedeemUtxos(proposalArgs);

logger.info({
msg: "Proposed UTXOs for redeem request",
Expand Down
23 changes: 23 additions & 0 deletions packages/sui-indexer/src/redeem-sui-client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export interface SuiClientCfg {
export interface SuiClient {
ikaClient(): IkaClient;
proposeRedeemUtxos(args: ProposeRedeemCall): Promise<string>;
dryRunProposeUtxos(args: ProposeRedeemCall): Promise<boolean>;
solveRedeemRequest(args: SolveRedeemCall): Promise<string>;
finalizeRedeem(args: FinalizeRedeemCall): Promise<string>;
requestIkaPresigns(count: number): Promise<string[]>;
Expand Down Expand Up @@ -142,6 +143,28 @@ class SuiClientImp implements SuiClient {
return result.digest;
}

async dryRunProposeUtxos(args: ProposeRedeemCall): Promise<boolean> {
const tx = new Transaction();

tx.add(
nBTCContractModule.proposeUtxos({
package: args.nbtcPkg,
arguments: {
contract: args.nbtcContract,
redeemId: args.redeemId,
utxoIds: args.utxoIds.map((u) => BigInt(u)),
},
}),
);

const result = await this.#sui.devInspectTransactionBlock({
transactionBlock: tx,
sender: this.signer.toSuiAddress(),
});

return !result.error;
}

async solveRedeemRequest(args: SolveRedeemCall): Promise<string> {
const tx = new Transaction();

Expand Down