Skip to content

Commit 0a8b276

Browse files
authored
fix: ensure contract call receipt exists when decoding logs (#3871)
* ensure call receipt exists at getAllDecodedLogs * add test case * add changeset * fix typo
1 parent 03b151b commit 0a8b276

File tree

3 files changed

+61
-4
lines changed

3 files changed

+61
-4
lines changed

.changeset/bright-steaks-wave.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/account": patch
3+
---
4+
5+
fix: ensure contract call receipt exists when decoding logs

packages/account/src/providers/transaction-response/getAllDecodedLogs.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,9 @@ export function getAllDecodedLogs<T = unknown>(opts: {
4444
(r) => r.type === ReceiptType.Call && r.id === ZeroBytes32
4545
) as TransactionResultCallReceipt;
4646

47-
mainContract = firstCallReceipt.to;
47+
if (firstCallReceipt) {
48+
mainContract = firstCallReceipt.to;
49+
}
4850
}
4951

5052
return receipts.reduce(

packages/fuel-gauge/src/revert-error.test.ts

Lines changed: 53 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,17 @@
11
import { ErrorCode, FuelError } from '@fuel-ts/errors';
22
import { expectToThrowFuelError } from '@fuel-ts/errors/test-utils';
33
import type { TransactionResultReceipt } from 'fuels';
4-
import { bn, getRandomB256, ContractFactory } from 'fuels';
5-
import { launchTestNode } from 'fuels/test-utils';
4+
import { bn, getRandomB256, ContractFactory, Wallet } from 'fuels';
5+
import { launchTestNode, TestAssetId } from 'fuels/test-utils';
66

7-
import { RevertErrorFactory, TokenContract, TokenContractFactory } from '../test/typegen';
7+
import {
8+
CallTestContractFactory,
9+
RevertErrorFactory,
10+
TokenContract,
11+
TokenContractFactory,
12+
} from '../test/typegen';
813

14+
import { fundAccount } from './predicate/utils/predicate';
915
import { launchTestContract } from './utils';
1016

1117
function launchContract() {
@@ -299,4 +305,48 @@ describe('Revert Error Testing', () => {
299305
)
300306
);
301307
});
308+
309+
it('should properly decode error when contract CALL receipt is not available', async () => {
310+
using launched = await launchTestNode({
311+
contractsConfigs: [{ factory: CallTestContractFactory }],
312+
});
313+
const {
314+
provider,
315+
wallets: [adminWallet],
316+
contracts: [contract],
317+
} = launched;
318+
319+
const transferAmount = 100;
320+
const baseAssetId = await provider.getBaseAssetId();
321+
322+
const wallet = Wallet.generate({ provider });
323+
324+
await fundAccount(adminWallet, wallet, 500_000);
325+
326+
contract.account = wallet;
327+
328+
// Contract call requires an amount of asset A
329+
const scope = contract.functions.return_context_amount().callParams({
330+
forward: [transferAmount, TestAssetId.A.value],
331+
});
332+
333+
const request = await scope.getTransactionRequest();
334+
335+
request.gasLimit = bn(100_000);
336+
request.maxFee = bn(100_000);
337+
338+
const baseAssetResources = await wallet.getResourcesToSpend([
339+
{ amount: transferAmount, assetId: baseAssetId },
340+
]);
341+
342+
// Funding the transaction only with the base asset
343+
request.addResources(baseAssetResources);
344+
345+
const res = await wallet.sendTransaction(request);
346+
347+
await expectToThrowFuelError(() => res.waitForResult(), {
348+
code: ErrorCode.SCRIPT_REVERTED,
349+
message: expect.stringMatching(`The transaction reverted with reason: "NotEnoughBalance".`),
350+
});
351+
});
302352
});

0 commit comments

Comments
 (0)