Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
4 changes: 2 additions & 2 deletions sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@gobob/bob-sdk",
"version": "4.2.8",
"version": "4.2.9",
"main": "dist/index.js",
"types": "dist/index.d.ts",
"scripts": {
Expand Down Expand Up @@ -52,4 +52,4 @@
"viem": "^2.33.2",
"global": "^4.4.0"
}
}
}
96 changes: 78 additions & 18 deletions sdk/src/gateway/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -755,6 +755,16 @@ export class GatewayApiClient {
const { onrampQuote, params } = executeQuoteParams;
const quote = onrampQuote!;

const esploraClient = new EsploraClient(this.chain.id === bob.id ? Network.mainnet : Network.signet);

Comment on lines +758 to +759
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Fix incorrect network parameter in EsploraClient constructor

The EsploraClient constructor expects a string literal ('mainnet' | 'signet' | 'testnet' | 'regtest'), but you're passing Network.mainnet and Network.signet which are enums from bitcoin-address-validation. This type mismatch will cause runtime errors.

Apply this fix:

-const esploraClient = new EsploraClient(this.chain.id === bob.id ? Network.mainnet : Network.signet);
+const esploraClient = new EsploraClient(this.chain.id === bob.id ? 'mainnet' : 'signet');
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const esploraClient = new EsploraClient(this.chain.id === bob.id ? Network.mainnet : Network.signet);
const esploraClient = new EsploraClient(this.chain.id === bob.id ? 'mainnet' : 'signet');
🤖 Prompt for AI Agents
In sdk/src/gateway/client.ts around lines 758-759, the EsploraClient is being
constructed with Network.mainnet/Network.signet (enum values) but the
constructor expects string literals ('mainnet'|'signet'|'testnet'|'regtest');
replace the enum usage with the corresponding string literals (e.g. use
this.chain.id === bob.id ? 'mainnet' : 'signet') so the correct string is passed
to EsploraClient, and update any type annotations if necessary to reflect the
string union.

// TODO: refactor to construct the PSBT instead since it may fund from other inputs
const availableBtcBalance = await esploraClient.getBalance(params.fromUserAddress!);
if (availableBtcBalance.total < BigInt(quote.satoshis)) {
throw new Error(
`Insufficient BTC balance in address ${quote.bitcoinAddress}. Required: ${formatBtc(BigInt(quote.satoshis))}, Got: ${formatBtc(BigInt(availableBtcBalance.total))}`
);
}

const { uuid, psbtBase64, bitcoinAddress, satoshis, opReturnHash } = await this.startOnrampOrder(
quote,
params
Expand Down Expand Up @@ -795,25 +805,75 @@ export class GatewayApiClient {
this.fetchOfframpRegistryAddress(),
]);

const [allowance, decimals] = await publicClient.multicall({
allowFailure: false,
contracts: [
{
address: tokenAddress,
abi: erc20Abi,
functionName: 'allowance',
args: [params.fromUserAddress as Address, offrampRegistryAddress],
},
{
address: tokenAddress,
abi: erc20Abi,
functionName: 'decimals',
},
],
});
const accountAddress = walletClient.account?.address ?? (params.fromUserAddress as Address);

// Check ETH balance and estimate gas for both potential transactions
const [
ethBalance,
feeValues,
gasPrice,
approvalGasEstimate,
createOrderGasEstimate,
[allowance, decimals],
] = await Promise.all([
publicClient.getBalance({
address: accountAddress,
}),
publicClient.estimateFeesPerGas(),
publicClient.getGasPrice(),
publicClient.estimateContractGas({
address: tokenAddress,
abi: erc20Abi,
functionName: 'approve',
args: [offrampRegistryAddress, maxUint256],
account: walletClient.account,
}),
publicClient.estimateContractGas({
address: offrampRegistryAddress,
abi: offrampOrder.offrampABI,
functionName: offrampOrder.offrampFunctionName,
args: offrampOrder.offrampArgs,
account: walletClient.account,
}),
publicClient.multicall({
allowFailure: false,
contracts: [
{
address: tokenAddress,
abi: erc20Abi,
functionName: 'allowance',
args: [params.fromUserAddress as Address, offrampRegistryAddress],
},
{
address: tokenAddress,
abi: erc20Abi,
functionName: 'decimals',
},
],
}),
]);

const fee = feeValues.maxFeePerGas ?? gasPrice;
const approvalGasCost = approvalGasEstimate * fee;
const createOrderGasCost = createOrderGasEstimate * fee;

if (decimals < 8) {
throw new Error('Tokens with less than 8 decimals are not supported');
}
const multiplier = 10n ** BigInt(decimals - 8);
const requiredAmount = BigInt(quote.amountLockInSat) * multiplier;
const needsApproval = requiredAmount > allowance;

// Calculate total gas cost needed
const totalGasCost = needsApproval ? approvalGasCost + createOrderGasCost : createOrderGasCost;

if (ethBalance < totalGasCost) {
throw new Error(
`Insufficient ETH balance for gas fees. Required: ${totalGasCost} wei, Available: ${ethBalance} wei`
);
}

const multiplier = 10 ** (decimals - 8);
if (BigInt(quote.amountLockInSat * multiplier) > allowance) {
if (needsApproval) {
const { request } = await publicClient.simulateContract({
account: walletClient.account,
address: tokenAddress,
Expand Down
8 changes: 8 additions & 0 deletions sdk/test/gateway.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,14 @@ describe('Gateway Tests', () => {
const mockPublicClient = {
multicall: () => Promise.resolve([10_000n, 8]),
readContract: () => Promise.resolve(10_000n),
getBalance: () => Promise.resolve(1000000000000000000n), // 1 ETH in wei
estimateContractGas: () => Promise.resolve(21000n),
estimateFeesPerGas: () =>
Promise.resolve({
maxFeePerGas: 20000000000n,
maxPriorityFeePerGas: 1000000000n,
}),
getGasPrice: () => Promise.resolve(20000000000n),
simulateContract: () => Promise.resolve({ request: '🎉' }),
waitForTransactionReceipt: () =>
Promise.resolve('0x35f5bca7f984f4ed97888944293b979f3abb198a5716d04e10c6bdc023080075'),
Expand Down
Loading