Skip to content

Commit 4070e94

Browse files
committed
Add erc20 example
1 parent 53f2a9d commit 4070e94

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

docs/examples.rst

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,3 +306,56 @@ Output:
306306
'logs': [AttributeDict({'type': 'mined', 'logIndex': 0, 'transactionIndex': 0, 'transactionHash': HexBytes('0x3ac3518cc59d1698aa03a0bab7fb8191a4ef017aeda7429b11e8c6462b20a62a'), 'blockHash': HexBytes('0x94e07b0b88667da284e914fa44b87d4e7fec39761be51245ef94632a3b5ab9f0'), 'blockNumber': 2, 'address': '0xF2E246BB76DF876Cef8b38ae84130F4F55De395b', 'data': '0x', 'topics': [HexBytes('0x6c2b4666ba8da5a95717621d879a77de725f3d816709b9cbe9f059b8f875e284'), HexBytes('0x00000000000000000000000000000000000000000000000000000000000000ff')]})],
307307
'transactionHash': HexBytes('0x3ac3518cc59d1698aa03a0bab7fb8191a4ef017aeda7429b11e8c6462b20a62a'),
308308
'transactionIndex': 0}
309+
310+
311+
Interacting with an ERC20 Contract
312+
----------------------------------
313+
314+
The ERC20 (formally `EIP20 <https://github.com/ethereum/EIPs/blob/master/EIPS/eip-20.md>`_) token standard is the most widely used standard in Ethereum. Here's how to check the current state of an ERC20 token contract.
315+
316+
First, create your Python object representing the ERC20 contract.
317+
For this example, we'll be inspecting the `Unicorns <https://etherscan.io/token/0x89205a3a3b2a69de6dbf7f01ed13b2108b2c43e7>`_ token contract.
318+
319+
.. code-block:: python
320+
321+
import json
322+
323+
ERC20_ABI = json.loads('[{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_from","type":"address"},{"indexed":true,"name":"_to","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"_owner","type":"address"},{"indexed":true,"name":"_spender","type":"address"},{"indexed":false,"name":"_value","type":"uint256"}],"name":"Approval","type":"event"}]') # noqa: 501
324+
325+
# Address field should be the checksum address at which the ERC20 contract was deployed
326+
erc20 = w3.eth.contract(address='0x89205A3A3b2A69De6Dbf7f01ED13B2108B2c43e7', abi=ERC20_ABI)
327+
328+
Fetch various data about the current state of the ERC20 contract.
329+
330+
.. code-block:: python
331+
332+
name = erc20.functions.name().call()
333+
# -> "Unicorns"
334+
symbol = erc20.functions.symbol().call()
335+
# -> "🦄"
336+
decimals = erc20.functions.decimals().call()
337+
# -> 1
338+
total_supply = erc20.functions.totalSupply().call()
339+
# -> 2038 (at time of writing)
340+
341+
Get the balance of an account address.
342+
343+
.. code-block:: python
344+
345+
balance = erc20.functions.balanceOf('0xD1220A0cf47c7B9Be7A2E6BA89F429762e7b9aDb').call()
346+
# -> 1
347+
348+
Calculate the token count from the token balance of an account address.
349+
350+
.. code-block:: python
351+
352+
from decimal import Decimal
353+
354+
# Most applications expect *exact* results, so using the Decimal library,
355+
# with default precision of 28, is usually sufficient to avoid any rounding error.
356+
count = balance / Decimal(10 ** decimals)
357+
# -> 1
358+
359+
Balance is *always* an integer in the currency's smallest natural unit (i.e. `wei` for ether). Token balance is typically used only for backend calculations.
360+
361+
Token count *may* be a integer or fraction in the currency's primary unit (i.e. `eth` for ether). Token count is typically displayed to the user on the front-end since it is more readable.

0 commit comments

Comments
 (0)