rust-web3
rust-web3 copied to clipboard
How to estimate_gas() for Contract::deploy() ?
Web3 spec describes an API to estimate gas for either calls performed on already deployed Smart Contract instances, or for transaction that do deploy Smart Contracts.
There are two estimate_gas() APIs in rust-web3:
web3.eth().estimate_gas()- which takes aCallRequest.CallRequestrequires the specification of atoparameter which is the address of a Smart Contract that's already deployed.Contract::estimate_gas()- which operates of aContractinstance that also already has an address
I can't seem to find an API that lets me estimate the gas for the deployment of a Smart Contract.
Am I missing something here @tomusdrw ?
I have noticed that there's a TransactionRequest type that makes to an Option<Address>, and if None it'll be a Smart Contract creation: https://github.com/tomusdrw/rust-web3/blob/91639ffee68af98e0ba64e648a30e9edb9adb09a/src/types/transaction_request.rs#L29-L34
I wonder if that should be exposted on estimate_gas() APIs, so that estimate_gas() takes and Enum that's either a CallRequest or a TransactionRequest. Does that make sense?
Yeah, seems that deployment estimation is indeed missing in the API. However I don't think we should change existing web3.eth().estimate_gas() but rather add it to the Contract stuff somehow (probably the builder).
@tomusdrw thanks for confirming!
Okay, I'm going to try to add this API and send a PR ASAP. Maybe you can help me getting in it shape once I have something in reviewable state.
I've been hacking in support for signing transactions in the Contract API, and ran into this issue.
My naive approach doesn't seem to work, so I'm wondering if there's been any progress on this?
@Vypo your solution looks reasonable, why it doesn't seem to work? Note that the value you get is just an estimation, and it might change (even significantly) depending on the actual state it's executed against. In the past usually the idea was to bump the estimate by at least 10% - even if you overestimate there is no extra costs involved, since the unused gas is refunded.
@tomusdrw It seems odd that I have to manually add the 32,000 gas (I assume for contract creation), and even with a 10% overestimate, it still fails. I'll keep playing with it I guess.
@Vypo ah, sorry. I just realised that you are setting to to some address - that won't measure contract creation gas cost, but rather a call to that contract. There has been a change recently to CallRequest type and it should now handle contract creation case as well, see #348