Skip to content

Commit f1ca94a

Browse files
committed
fix: check btc balance before start order
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
1 parent 0936784 commit f1ca94a

File tree

1 file changed

+22
-14
lines changed

1 file changed

+22
-14
lines changed

sdk/src/gateway/client.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -755,6 +755,14 @@ export class GatewayApiClient {
755755
const { onrampQuote, params } = executeQuoteParams;
756756
const quote = onrampQuote!;
757757

758+
const esploraClient = new EsploraClient(this.chain.id === bob.id ? Network.mainnet : Network.signet);
759+
760+
// TODO: refactor to construct the PSBT instead since it may fund from other inputs
761+
const availableBtcBalance = await esploraClient.getBalance(quote.bitcoinAddress);
762+
if (availableBtcBalance.total < BigInt(quote.satoshis)) {
763+
throw new Error(`Insufficient BTC balance in address ${quote.bitcoinAddress}. Required: ${formatBtc(BigInt(quote.satoshis))}`);
764+
}
765+
758766
const { uuid, psbtBase64, bitcoinAddress, satoshis, opReturnHash } = await this.startOnrampOrder(
759767
quote,
760768
params
@@ -1001,8 +1009,8 @@ export class GatewayApiClient {
10011009
: (base as NonNullable<typeof base>) // failed
10021010
: (base as NonNullable<typeof base>) // success
10031011
: order.strategyAddress // pending
1004-
? (output as NonNullable<typeof output>)
1005-
: (base as NonNullable<typeof base>);
1012+
? (output as NonNullable<typeof output>)
1013+
: (base as NonNullable<typeof base>);
10061014
}
10071015
const getTokenAddress = (): string => {
10081016
return getFinal(order.baseTokenAddress, order.outputTokenAddress);
@@ -1046,12 +1054,12 @@ export class GatewayApiClient {
10461054
return !hasEnoughConfirmations
10471055
? { confirmed: false, data }
10481056
: order.status
1049-
? order.strategyAddress
1050-
? order.outputTokenAddress
1051-
? { success: true, data }
1052-
: { success: false, data }
1053-
: { success: true, data }
1054-
: { pending: true, data };
1057+
? order.strategyAddress
1058+
? order.outputTokenAddress
1059+
? { success: true, data }
1060+
: { success: false, data }
1061+
: { success: true, data }
1062+
: { pending: true, data };
10551063
},
10561064
};
10571065
});
@@ -1111,12 +1119,12 @@ export class GatewayApiClient {
11111119
},
11121120
outputToken: outputToken
11131121
? {
1114-
symbol: outputToken.symbol,
1115-
address: outputToken.address,
1116-
logo: outputToken.logoURI,
1117-
decimals: outputToken.decimals,
1118-
chain: chainName,
1119-
}
1122+
symbol: outputToken.symbol,
1123+
address: outputToken.address,
1124+
logo: outputToken.logoURI,
1125+
decimals: outputToken.decimals,
1126+
chain: chainName,
1127+
}
11201128
: null,
11211129
};
11221130
});

0 commit comments

Comments
 (0)