rust-web3 icon indicating copy to clipboard operation
rust-web3 copied to clipboard

How to estimate_gas() for Contract::deploy() ?

Open 0x-r4bbit opened this issue 6 years ago • 7 comments

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 a CallRequest. CallRequest requires the specification of a to parameter which is the address of a Smart Contract that's already deployed.
  • Contract::estimate_gas() - which operates of a Contract instance 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 ?

0x-r4bbit avatar Jun 24 '19 10:06 0x-r4bbit

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?

0x-r4bbit avatar Jun 24 '19 10:06 0x-r4bbit

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 avatar Jun 24 '19 11:06 tomusdrw

@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.

0x-r4bbit avatar Jun 24 '19 12:06 0x-r4bbit

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 avatar May 21 '20 19:05 Vypo

@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 avatar May 22 '20 07:05 tomusdrw

@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 avatar May 22 '20 17:05 Vypo

@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

tomusdrw avatar May 25 '20 11:05 tomusdrw