-
Notifications
You must be signed in to change notification settings - Fork 118
/
receipt.ts
59 lines (55 loc) · 1.83 KB
/
receipt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import { OperationContentsAndResult } from '@taquito/rpc';
import BigNumber from 'bignumber.js';
import { flattenOperationResult } from '../operations/operation-errors';
export interface Receipt {
totalFee: BigNumber;
totalGas: BigNumber;
totalStorage: BigNumber;
totalAllocationBurn: BigNumber;
totalOriginationBurn: BigNumber;
totalPaidStorageDiff: BigNumber;
totalStorageBurn: BigNumber;
}
export const receiptFromOperation = (
op: OperationContentsAndResult[],
{ ALLOCATION_BURN, ORIGINATION_BURN } = {
ALLOCATION_BURN: 257,
ORIGINATION_BURN: 257,
}
): Receipt => {
const operationResults = flattenOperationResult({ contents: op });
let totalGas = new BigNumber(0);
let totalStorage = new BigNumber(0);
let totalFee = new BigNumber(0);
let totalOriginationBurn = new BigNumber(0);
let totalAllocationBurn = new BigNumber(0);
let totalPaidStorageDiff = new BigNumber(0);
operationResults.forEach(result => {
totalFee = totalFee.plus(result.fee || 0);
totalOriginationBurn = totalOriginationBurn.plus(
Array.isArray(result.originated_contracts)
? result.originated_contracts.length * ORIGINATION_BURN
: 0
);
totalAllocationBurn = totalAllocationBurn.plus(
'allocated_destination_contract' in result ? ALLOCATION_BURN : 0
);
totalGas = totalGas.plus(result.consumed_gas || 0);
totalPaidStorageDiff = totalPaidStorageDiff.plus(
'paid_storage_size_diff' in result ? Number(result.paid_storage_size_diff) || 0 : 0
);
});
totalStorage = totalStorage
.plus(totalAllocationBurn)
.plus(totalOriginationBurn)
.plus(totalPaidStorageDiff);
return {
totalFee,
totalGas,
totalStorage,
totalAllocationBurn,
totalOriginationBurn,
totalPaidStorageDiff,
totalStorageBurn: new BigNumber(totalStorage.multipliedBy(1000)),
};
};