Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle NoEstimateAvailable text error body #1603

Merged
merged 1 commit into from
Dec 10, 2023
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
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
Loading