Web3.py: New contract method execution option: generating the transaction dictionary

Created on 15 Nov 2017  路  24Comments  路  Source: ethereum/web3.py

  • Version: 4

What was wrong?

There is no convenient way to prepare a contract transaction for offline signing.

Specifically, the prepared transaction should work smoothly with w3.eth.account.signTransaction().

How can it be fixed?

New contract method preparation API

Add a new way to prepare a contract function transaction. It would look something like:

# new API
transaction_dict = myContract.functions.myMethod().asdict()

# existing APIs
signed = w3.eth.account.signTransaction(transaction_dict, private_key)
w3.eth.sendRawTransaction(signed.rawTransaction)

asdict should accept a dictionary that defines transaction fields, just like call/transact/etc, and return an AttributeDict.

Fields included

Besides the natural fields, like to and data, this must also generate other fields like:

  • nonce
  • chainId
  • gas
  • gasPrice

If they are not supplied as arguments to asdict(), then they should be inferred before returning. signTransaction will not attempt to infer these values, so they must be explicitly added.

API Naming

asdict() is okay, but isn't necessarily the obvious winner.

Some alternatives:

  • myContract.functions.myMethod().todict()
  • myContract.functions.myMethod().tofields()
  • myContract.functions.myMethod().prepare()
  • myContract.functions.myMethod().generate()

Crossover issue

Depending on whether #419 is already implemented, and how it's implemented, this might require a bit of extra work to expose asdict in both ways.

TODOs

  • [x] choose naming
  • [x] implement new API
  • [x] document in docs/
  • [x] reference this method in signTransaction docs docs/web3.eth.account.rst
    ~- [ ] Support for #419 - if implementation available~

Most helpful comment

@monokh i just remitted payment. the gitcoinco bot didn't comment because of issues resultant from the migration of this repo from @pipermerriam 's github handle to the @ethereum handle. nevertheless, payment has been remitted and https://gitcoin.co/funding/details?url=https://github.com/pipermerriam/web3.py/issues/430 is updated!

congrats! 馃帀

All 24 comments

suggest names with the word transaction in them

  • myContract.myMethod().prepareTransaction()
  • myContract.myMethod().asTransactionDict()
  • myContract.myMethod().generateTransaction()

Yeah, I was feeling grumbly about how long it got. Especially when you add in the prep word (which I just realized I forgot to do. Now updated in the top.)

With prep word:

  • myContract.functions.myMethod().prepareTransaction()
  • myContract.functions.myMethod().asTransactionDict()
  • myContract.functions.myMethod().generateTransaction()

Not sure I like it but worth considering offering a shortcut API as myContract.f.myMethod().prepareTransaction().

Also, for methods that take no arguments, we should try to support myContract.myMethod.call().

__This issue now has a funding of 1.0 ETH (329.52 USDT) attached to it.__ To view or claim this funding, click here.

Hi, I'm willing to work on this :slightly_smiling_face:. I've done offline TX signing with web3.js but it had been a bit of a pain due to needed other libraries for TX signing. I have python experience so interested in how I can shape this 馃槂 Expect a merge request coming your way soon

The PR for this is ready now ( #444 ). Any chance you could have a look @carver @pipermerriam ? Thanks 馃檪

let me know how it looks folks and i can take a look at paying out the bounty. @monokh looks like you submitted for review

Sorry it took me a while to get to this review (Thanksgiving). I just posted some comments. Once we get them resolved, I'd love for the bounty to be paid.

Is prepareTransaction() your favorite option @monokh ? It still feels a bit long to me, but if it's your favorite, I won't object.

@carver it is quite long but not much longer than sendTransaction. I actually feel the word prepare is a bit misguided. It doesn't clearly imply that something is also being returned. For comparison, web3.js calls this encodeABI and in pre 1.0 it's getData which I personally feel is more clear.

@carver also, I've left comments on the discussion on the PR however it seems to not be visible to others. All my comments are marked as "Pending":
image
Is it visible to you? I'm just wondering if it's blocked or something

@monokh my concern with using encodeABI here is that it only describes a part of what this function does. There are a bunch of other ideas for the naming higher in the thread. Do you like any of them better than prepareTransaction or have any other suggestions?

No, we can't see the comments while they are pending. There should be a Submit button floating around somewhere on the PR for you to release the pending comments.

@carver definitely agree that encodeABI isn't suitable
other alternatives also suitable look like
.asTransactionDict()
and
generateTransaction()

Cool, another option to add to the mix: buildTransaction()

buildTransaction()
liking that one, shorter and more descriptive. Will change to that!

just checking in here. @monokh -- let me know if you need anything from me. can pay out bounty as soon as the approved code is merged

@carver sorry, im slow. should i be paying out the bounty yet?

the PR is approved and merged now 馃帀
thanks for the patience @carver @pipermerriam had a great time working on this 馃槂
@owocki i'm not very familiar with the process but i've submitted a claim for the issue that's being picked up now. It's still waiting to be included in a block :slightly_frowning_face: hopefully kitties will let us through soon

@owocki yes, the bounty can be paid out :) Looks like it might need a 21 gwei gas price to get through...

kittens attack

those darn cryptokitties are lucky they're cute! remitting now

@monokh can you send me the txid for your claim? looks like there are several pending against the smart contract now

@owocki 0x687f2c24ada361291e290ad0fe264d80303f25c474f5f9d269562ec7b6966f0c
Still not seeing it tagged as Claimed on gitcoin though so may be doing something wrong.

@monokh just synced the issue at https://gitcoin.co/funding/details?url=https://github.com/pipermerriam/web3.py/issues/430 with web3.

and submitted an acceptance for your work on this. payment should be remitted once https://etherscan.io/tx/0xa0db386627cf9a636eca4e1d8ce75629ae6e0d1a6cbba2fb81c7679a2c329911 clears

@monokh i just remitted payment. the gitcoinco bot didn't comment because of issues resultant from the migration of this repo from @pipermerriam 's github handle to the @ethereum handle. nevertheless, payment has been remitted and https://gitcoin.co/funding/details?url=https://github.com/pipermerriam/web3.py/issues/430 is updated!

congrats! 馃帀

Was this page helpful?
0 / 5 - 0 ratings