Skip to content

Commit

Permalink
fix: handle NoEstimateAvailable text error body
Browse files Browse the repository at this point in the history
  • Loading branch information
zone117x authored and janniks committed Dec 10, 2023
1 parent 9af3f5a commit 802487d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
13 changes: 11 additions & 2 deletions packages/transactions/src/builders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -209,9 +209,18 @@ export async function estimateTransaction(
const response = await derivedNetwork.fetchFn(url, options);

if (!response.ok) {
const body = await response.json().catch(() => ({}));
const body = await response.text().then(str => {
try {
return JSON.parse(str);
} catch (error) {
return str;
}
});

if (body?.reason === 'NoEstimateAvailable') {
if (
body?.reason === 'NoEstimateAvailable' ||
(typeof body === 'string' && body.includes('NoEstimateAvailable'))
) {
throw new NoEstimateAvailableError(body?.reason_data?.message ?? '');
}

Expand Down
15 changes: 14 additions & 1 deletion packages/transactions/tests/builder.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1245,6 +1245,19 @@ test('Estimate transaction fee fallback', async () => {
const resultEstimateFee = await estimateTransactionFeeWithFallback(tx, testnet);
expect(resultEstimateFee).toBe(201n);

// Test with plain-text response
// http://localhost:3999/v2/fees/transaction
fetchMock.once(
`Estimator RPC endpoint failed to estimate tx TokenTransfer: NoEstimateAvailable`,
{ status: 400 }
);

// http://localhost:3999/v2/fees/transfer
fetchMock.once('1');

const resultEstimateFee2 = await estimateTransactionFeeWithFallback(tx, testnet);
expect(resultEstimateFee2).toBe(201n);

// http://localhost:3999/v2/fees/transaction
fetchMock.once(
`{"error":"Estimation could not be performed","reason":"NoEstimateAvailable","reason_data":{"message":"No estimate available for the provided payload."}}`,
Expand All @@ -1257,7 +1270,7 @@ test('Estimate transaction fee fallback', async () => {
const doubleRate = await estimateTransactionFeeWithFallback(tx, testnet);
expect(doubleRate).toBe(402n);

expect(fetchMock.mock.calls.length).toEqual(6);
expect(fetchMock.mock.calls.length).toEqual(8);
});

test('Single-sig transaction byte length must include signature', async () => {
Expand Down

1 comment on commit 802487d

@vercel
Copy link

@vercel vercel bot commented on 802487d Dec 10, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.