Topic discussion checklist
- Run
$ fuelup show
and add the output below
- Detailed steps to recreate issue
- Link full scope of the codebase pertaining to your issue
- Any other relevant information
Hey guys, I am having an issue calculating the network fee for the array of transactions I get from the GraphQL main net API.
I use the same query mentioned in Fuel graphql docs with first 906 transactions, to get the array of transactions for an address on the main net
I then calculate the network fee using the following code on the array of inputs and outputs for each transaction:
const totalInputs = inputs.reduce((total: number, input: any) => {
if (input.amount) {
return total + parseInt(input.amount);
}
return total;
}, 0);
const totalOutputs = outputs.reduce((total: number, output: any) => {
// added typename condition from chat gpt
if (output.amount && output.__typename !== "ChangeOutput") {
return total + parseInt(output.amount);
}
return total;
}, 0);
const networkFee = (totalInputs - totalOutputs) / 10 ** 9;
return networkFee;
The network fee I get for transactions does not match with the one on block explorer, though for many transactions it matches with the amount of output of __typename = ChangeOutput mentioned on the block explorer
I just want to know the correct method to retrieve the network fee for all the transactions and add them up to show the total gas cost for the user
To calculate the network fee correctly, you must account for all inputs and outputs, particularly the ChangeOutput. You may need to exclude the ‘ChangeOutput’ parameter from the output because it is not included in the fee. Could you please try this approach and let me know if it works for you?
const totalInputs = inputs.reduce((total: number, input: any) => {
if (input.amount) {
return total + parseInt(input.amount, 10);
}
return total;
}, 0);
const totalOutputs = outputs.reduce((total: number, output: any) => {
if (output.amount && output.__typename !== "ChangeOutput") {
return total + parseInt(output.amount, 10);
}
return total;
}, 0);
const networkFee = (totalInputs - totalOutputs) / 10 ** 9;
return networkFee;
By this method, Sometimes the network fee is negative, it cannot be negative right?
Hey Nazeeh the solution is working for all transactions other than ones which contain an asset other than eth
Since their amount is very high in value, dividing by 10 ** 9 doesn’t decrease the value
eg:
{
__typename:"VariableOutput",
to:"0x72de7a66f3f6e5a7bcb994f0a187acb7f3f3b2a498c394ceefb885afe666dffe",
amount:"47165316817532158",
assetId:"0xb3424e9d0540e4e37e23513b00365e3c439b815810bd7e543c8eb4f7519e9646",
name:"DUCKY Coin",
symbol:"DUCKY",
icon:null,
contractId:"0x81d5964bfbb24fd994591cc7d0a4137458d746ac0eb7ececb9a9cf2ae966d942",
decimals:9,
suspicious:false
}
how to deal with such transactions
When dealing with assets other than ETH, you must account for their specific decimal precision. Each asset may have a different number of decimals, which influences how you calculate and display their values.
Each asset will include a decimal property. Use this to change the division factor for each asset. Instead of dividing by 10 ** 9 for all assets, use each asset’s decimal property to determine the appropriate division factor.
you can try this modified code
const totalInputs = inputs.reduce((total: number, input: any) => {
if (input.amount) {
return total + parseInt(input.amount, 10);
}
return total;
}, 0);
const totalOutputs = outputs.reduce((total: number, output: any) => {
if (output.amount && output.__typename !== "ChangeOutput") {
return total + parseInt(output.amount, 10);
}
return total;
}, 0);
// Adjust the division factor based on the asset's decimals
const divisionFactor = 10 ** (outputs[0]?.decimals || 9); // Default to 9 if decimals not specified
const networkFee = (totalInputs - totalOutputs) / divisionFactor;
return networkFee;
lmk how this goes