Skip to content

Commit e48b0e9

Browse files
feat: add receipts with the operations summary (#3658)
* feat: add receipts handling to transaction operations * . * test: add tests for merging transaction operation receipts * refactor: expose `mergeOperations` function for transaction operations * refactor: improve mergeOperations function formatting and readability * ci: add changeset for incremental Fuels package update * feat: send receipts with operations summary for account and recipes packages * test: add tests for adding operations with receipt merging * refactor: make `mergeOperations` function private * test: improve formatting of operation receipts test case * Atualizar o rotten-pillows-scream.md Co-authored-by: Peter Smith <[email protected]> --------- Co-authored-by: Peter Smith <[email protected]>
1 parent 0429278 commit e48b0e9

File tree

4 files changed

+105
-0
lines changed

4 files changed

+105
-0
lines changed

.changeset/rotten-pillows-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@fuel-ts/account": patch
3+
---
4+
5+
feat: add receipts with the operations summary

packages/account/src/providers/transaction-summary/operations.test.ts

+92
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ describe('operations', () => {
7373
assetId: ZeroBytes32,
7474
},
7575
],
76+
receipts: [MOCK_RECEIPT_CALL],
7677
};
7778

7879
const receipts = [
@@ -266,6 +267,7 @@ describe('operations', () => {
266267
chain: ChainName.ethereum,
267268
type: 1,
268269
},
270+
receipts: [MOCK_RECEIPT_MESSAGE_OUT],
269271
};
270272

271273
const operations = getWithdrawFromFuelOperations({
@@ -324,6 +326,7 @@ describe('operations', () => {
324326
assetId: '0x0000000000000000000000000000000000000000000000000000000000000000',
325327
},
326328
],
329+
receipts: [MOCK_RECEIPT_TRANSFER_OUT],
327330
},
328331
{
329332
name: OperationName.contractCall,
@@ -342,6 +345,7 @@ describe('operations', () => {
342345
assetId: ZeroBytes32,
343346
},
344347
],
348+
receipts: [MOCK_RECEIPT_CALL],
345349
},
346350
];
347351

@@ -400,6 +404,7 @@ describe('operations', () => {
400404
const operationsCallNoAmount: Operation = {
401405
...expected,
402406
assetsSent: undefined,
407+
receipts: [{ ...MOCK_RECEIPT_CALL, amount: bn(0) }],
403408
};
404409

405410
const operations = getOperations({
@@ -822,6 +827,93 @@ describe('operations', () => {
822827
expect(operationsAddedSameContractCall.length).toEqual(1);
823828
expect(operationsAddedSameContractCall[0].calls?.length).toEqual(2);
824829
});
830+
831+
it('should merge receipts when adding operations', () => {
832+
const receipt1: TransactionResultReceipt = {
833+
type: ReceiptType.Transfer,
834+
to: '0xabc',
835+
amount: bn(100),
836+
assetId: '0x0',
837+
id: '0x123',
838+
pc: bn(0),
839+
is: bn(0),
840+
};
841+
842+
const receipt2: TransactionResultReceipt = {
843+
type: ReceiptType.Transfer,
844+
to: '0xdef',
845+
amount: bn(200),
846+
assetId: '0x0',
847+
id: '0x456',
848+
pc: bn(0),
849+
is: bn(0),
850+
};
851+
852+
const op1: Operation = {
853+
name: OperationName.transfer,
854+
receipts: [receipt1],
855+
};
856+
857+
const op2: Operation = {
858+
name: OperationName.transfer,
859+
receipts: [receipt2],
860+
};
861+
862+
const operations = addOperation([op1], op2);
863+
expect(operations[0].receipts).toHaveLength(2);
864+
expect(operations[0].receipts).toContainEqual(receipt1);
865+
expect(operations[0].receipts).toContainEqual(receipt2);
866+
});
867+
868+
it('should not duplicate receipts when adding operations', () => {
869+
const receipt: TransactionResultReceipt = {
870+
type: ReceiptType.Transfer,
871+
to: '0xabc',
872+
amount: bn(100),
873+
assetId: '0x0',
874+
id: '0x123',
875+
pc: bn(0),
876+
is: bn(0),
877+
};
878+
879+
const op1: Operation = {
880+
name: OperationName.transfer,
881+
receipts: [receipt],
882+
};
883+
884+
const op2: Operation = {
885+
name: OperationName.transfer,
886+
receipts: [receipt],
887+
};
888+
889+
const operations = addOperation([op1], op2);
890+
expect(operations[0].receipts).toHaveLength(1);
891+
expect(operations[0].receipts?.[0]).toEqual(receipt);
892+
});
893+
894+
it('should handle operations without receipts', () => {
895+
const op1: Operation = {
896+
name: OperationName.transfer,
897+
};
898+
899+
const op2: Operation = {
900+
name: OperationName.transfer,
901+
receipts: [
902+
{
903+
type: ReceiptType.Transfer,
904+
to: '0xabc',
905+
amount: bn(100),
906+
assetId: '0x0',
907+
id: '0x123',
908+
pc: bn(0),
909+
is: bn(0),
910+
},
911+
],
912+
};
913+
914+
const operations = addOperation([op1], op2);
915+
expect(operations[0].receipts).toHaveLength(1);
916+
});
825917
});
826918

827919
describe('isType', () => {

packages/account/src/providers/transaction-summary/operations.ts

+7
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,10 @@ function mergeOperations(existing: Operation, toAdd: Operation): Operation {
196196
...existing,
197197
assetsSent: mergeAssetsSent(existing, toAdd),
198198
calls: mergeCalls(existing, toAdd),
199+
receipts: [
200+
...(existing.receipts || []),
201+
...(toAdd.receipts?.filter((r) => !existing.receipts?.some((er) => er === r)) || []),
202+
],
199203
};
200204
}
201205

@@ -252,6 +256,7 @@ export function getWithdrawFromFuelOperations({
252256
assetId: baseAssetId,
253257
},
254258
],
259+
receipts: [receipt],
255260
});
256261

257262
return newWithdrawFromFuelOps;
@@ -332,6 +337,7 @@ function processCallReceipt(
332337
},
333338
assetsSent: getAssetsSent(receipt),
334339
calls,
340+
receipts: [receipt],
335341
},
336342
];
337343
}
@@ -421,6 +427,7 @@ function extractTransferOperationFromReceipt(
421427
amount,
422428
},
423429
],
430+
receipts: [receipt],
424431
};
425432
}
426433

packages/account/src/providers/transaction-summary/types.ts

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ export type Operation = {
118118
to?: OperationTransactionAddress;
119119
assetsSent?: Array<OperationCoin>;
120120
calls?: Array<OperationFunctionCall>;
121+
receipts?: TransactionResultReceipt[];
121122
};
122123

123124
/**

0 commit comments

Comments
 (0)