In protocol 003_PsddFKi3, how can the gas and storage usage be calculated without doing a dry-run in a node?
3 Answers
There's no other way, you have to simulate what the operation does if you want to estimate this value, and this is exactly what a dry run does. You could try to reimplement it outside of a node, but the approach would be the same.
-
1That's true. If doing it outside of the node the same logic must be used as in the dry run. For operations that doesn't involve parameters or script, I guess a simplified logic could be used in the client, in order to reduce the traffic to the node.– KlassareFeb 1, 2019 at 18:06
According to the official documentation available here you can get the gas and storage cost of the operation you plan to broadcast to the network by passing the parameter --dry-run
to the client node
Quoting the doc it mentions
Another important use of validation is to determine gas and storage limits. The node first simulates the execution of a Michelson program and takes trace of the amount of gas and storage. Then the client sends the transaction with the right limits for gas and storage based on that indicated by the node. This is why we were able to submit transactions without specifying this limits, they were computed for us.
So officially there's indeed no alternative way short of implementing your own validator.
- Install Taquito in node project using
npm install --save @taquito/taquito
, - Install Taquito signer package using
npm install --save @taquito/signer
- Copy below code in javascript file say
index.js
, - Run
$ node index.js
Code:
const TezosToolkit = require('@taquito/taquito').TezosToolkit;
const tezos = new TezosToolkit("Place Tezos node RPC URL here");
const InMemorySigner = require('@taquito/signer').InMemorySigner;
tezos.setProvider({ signer: new InMemorySigner("Place the private key of sender here") });
const input = {
"destination" : "Place destination address here",
"amount" : "Place amount to be sent",
"source" : "Place sender address here",
"senderPrivateKey" : "Place sender private key here"
}
const estimate = async function(input){
const estimated = await tezos.estimate
.transfer({ to: input.destination, amount: input.amount })
.then((est) => {
const estimated = {
"gasLimit" : est.gasLimit.toString(),
"storageLimit" : est.storageLimit.toString(),
"suggestedFee" : est.suggestedFeeMutez.toString()
}
return estimated;
})
.catch((error) => {
console.log("ERROR: Estimation failed!!",error);
});
return estimated;
}
console.log(await estimate);
Gas limit, Storage limit and fee will print in the command-line interface. Thank you.