Code Monkey home page Code Monkey logo

ethtoken.py's Introduction

ethtoken.py

This is a tiny library leveraging web3.py to make an interface for working with EIP20 tokens on Ethereum. (formerly ERC20)

It is currently in Alpha, with 0 automated tests

Usage

Install

virtualenv -p python3 venv
. venv/bin/activate
pip install --pre ethtoken

Initialize

from ethtoken import token

# Use the ENS name that points to your token contract here:
omg = token("omg.thetoken.eth")

Use standard EIP20 methods

Most EIP20 methods are optional. ethtoken makes no attempt to verify which methods are implemented by a token contract.

Here's an example with all the read functions working:

>>> omg.name()
'OMGToken'

>>> omg.symbol()
'OMG'

>>> omg.decimals()
18

>>> omg.totalSupply()
140245398245132780789239631

# Use the ENS name of the owner address here:
>>> omg.balanceOf('ethereumfoundation.eth')
308744633639977714804

Custom methods

ethtoken has a single custom method not in the EIP20 spec: token_balance.

>>> omg.token_balance("ethereumfoundation.eth")
Decimal('308.744633639977714804')

It returns the balance of an address, with the decimal point shifted according to the decimals() value on the contract. In other words, it is the human-readable number of tokens that the given address owns.

Completely Untested: Transfers

In theory, you could use this to send a token. I haven't even tried it once yet. Just don't use it. If you're going to ignore me, don't blame me if you lose tokens or ether.

This should theoretically transfer 1 giga units from 0x0 to 0xdead. (That's 1 nanotoken, at 18 decimals). Of course, this won't work if you don't control the 0x0 address. (hint: you don't)

from web3 import Web3

>>> omg.transfer(
  '0x000000000000000000000000000000000000dEaD',
  10 ** 9,
  transact={
    'from': '0x0000000000000000000000000000000000000000',
    'gasPrice': Web3.toWei('0.1', 'gwei'),
  },
)

Ownership Disclosure

I own some OmiseGo tokens, because anyone who had some ether during their airdrop got some. I don't have any opinions on the company or token.

ethtoken.py's People

Contributors

carver avatar stefanmendoza avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

ethtoken.py's Issues

Loosen Web3 from 4.0b7 to >=4.0.0,<5.0.0

While working on ethereum/web3.py#1056 and realizing eth-abi needs to have a patch (1.2.1) to include ethereum/eth-abi#100, I also noticed a constraint being violated due to the latest version of ethtoken being pulled in here:

https://github.com/ethereum/web3.py/blob/v4.7.2/setup.py#L26

since the latest version of ethtoken has web3 locked - web3~=4.0b7.

I created a local patch of eth-abi-1.2.1:

[10:53:34 PM] @stefanmendoza ➜  web3.py git:(issue-1086) ✗ git diff
diff --git a/setup.py b/setup.py
index 8c4a8a4..605e0c8 100644
--- a/setup.py
+++ b/setup.py
@@ -24,6 +24,7 @@ extras_require = {
         #"eth-testrpc>=0.8.0",
         #"ethereum-tester-client>=1.1.0",
         "ethtoken",
+        # "ethtoken>=0.0.1-alpha.4,<2.0.0",
         "py-geth>=1.4.0",
         "py-solc>=0.4.0",
         "pytest>=2.7.2",
@@ -70,7 +71,7 @@ setup(
     install_requires=[
         "toolz>=0.9.0,<1.0.0;implementation_name=='pypy'",
         "cytoolz>=0.9.0,<1.0.0;implementation_name=='cpython'",
-        "eth-abi>=1.2.0,<2.0.0",
+        "eth-abi>=1.2.1,<2.0.0",
         "eth-account>=0.2.1,<0.4.0",
         "eth-utils>=1.0.1,<2.0.0",
         "hexbytes>=0.1.0,<1.0.0",
[10:53:35 PM] @stefanmendoza ➜  web3.py git:(issue-1086) ✗ pip list
Package      Version Location                                          
------------ ------- --------------------------------------------------
cytoolz      0.9.0.1 
eth-abi      1.2.1   /Users/stefanmendoza/Github/Forks/ethereum/eth-abi
eth-hash     0.2.0   
eth-typing   1.3.0   
eth-utils    1.2.2   
parsimonious 0.8.0   
pip          18.1    
setuptools   40.4.3  
six          1.11.0  
toolz        0.9.0   
wheel        0.32.1  

and only got this compatability error:

ethtoken 0.0.1a3 has requirement web3~=4.0b7, but you'll have web3 4.7.1 which is incompatible.

so I bumped this up

--- a/setup.py
+++ b/setup.py
@@ -38,7 +38,7 @@ extras_require['dev'] = (
 )
 
 install_requires = [
-    'web3~=4.0b7',
+    'web3>=4.0.0,<5.0.0',
 ]
 if sys.platform == 'win32':
     install_requires.append('pypiwin32')
@@ -46,7 +46,7 @@ if sys.platform == 'win32':
 setup(
     name='ethtoken',
     # NOTE: do not change version manually, use `make release bump=$$PART_TO_BUMP$$` instead
-    version='0.0.1-alpha.3',
+    version='0.0.1-alpha.4',
     description="""Ethereum EIP20 Token Interface""",
     long_description_markdown_filename='README.md',
     author='Jason Carver',

which got rid of the compatibility error. Not sure if we want to open it up to all 4.x, but since we're on 4.7.2 I think we can probably move off the beta now? I'm thinking once we release eth-abi-1.2.1 and potentially open up the version constraints on web3 here that I'll be unblocked on ethereum/web3.py#1056. Thoughts?

web3<5.0.0 requirement conflicts with some other packages.

First, thank you for your work on this. I was wondering what is the issue with the newer releases of web3 that stop you to use it. I also depend on raiden-contracts (which now requires web3=>5.4.0,<6.0.0) for my project and I am getting some warnings about the incompatibilities.

token transaction value vs balanceOf()

Does balanceOf() return the number of tokens for the individual transaction or the total number of tokens that have been transferred to the transaction address?

Feature Request: Batch Calls

It would be super cool to facilitate the possibility of fetching token data (i.e. symbol, decimals) for a given list of token addresses and making a single batched eth_call in on request.

Similar work was done here: Basically input a list of addresses and making a list of request data before making the eth call:

https://github.com/cowprotocol/token-allocation/blob/b969f3e2ad48041f7459fed4d80527bb188f232f/src/fetch/contracts.py#L47-L75

The most relevant code block here was:

        for index, eth_address in enumerate(addresses):
            request_data.append({
                "jsonrpc": "2.0",
                "method": "eth_getCode",
                "params": [eth_address, "latest"],
                "id": index
            })
        response = requests.post(self.node_url, json=request_data)

I would volunteer to implement, but don't have time at the moment, recording this issue here in case someone wants to pick it up.

Cheers.

Add support for Python 3.7, add python_requires to setup.py

ethtoken should support Python 3.7 as currently only Python 3.5 and 3.6 are supported. On a similar vote, the Python version should be locked to >=3.5,<4 with possible consideration for dropping 3.5 support as web3 will be dropping it in 5.x.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.