simple-multicall


Namesimple-multicall JSON
Version 0.1.5 PyPI version JSON
download
home_page
SummarySimple Web3 multicall
upload_time2022-08-30 20:56:51
maintainer
docs_urlNone
authorIgor Lapshin
requires_python
licenseMIT
keywords multicall web3
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Simple Web3 Multicall

A multicall for use with pure Web3.py library.
It uses MakerDao Multicall smart contract by default,
but also can use any custom multicall smart contract 
that implements "aggregate" function.

Main urge for developmenet such a package was when I was needed to use multicall in my project 
based on Web3.py, but found only libraries which work with Brownie, not on pure Web3.py.


    pip install simple-multicall


## Package Content

- class Multicall - The main multicall class that executes a multicall itself.

- constants file makerdao_multicall - contains a default MakerDao multicall smart contract ABI and a dictionary of addresses of multicall contracts deployed on different chains. 


### class Multicall

The main multicall class. Should be initialized by passing a Web3 instance with specified chain RPC provider.

Has two methods: 

*call* - executes a multicall

*create_call* - prepare a tuple for a list of calls to be passed to a *call* method


**Attributes**
    
*w3* : Web3 class instanse

*chain*: str
The name of one of defined chains 
where MakerDao Multicall smart contract is deployed.
Can be one of the followings

- 'mainnet'
- 'kovan'
- 'rinkeby'
- 'goerli'
- 'ropsten'
- 'xdai'
- 'polygon'
- 'mumbai'
- 'bsc-mainnet'
- 'bsc-testnet'


*custom_address*: str - An address of custom multicall smart contract. 
If specified, MakerDao Multicall smart contract will be omited.


*custom_abi*: str - An ABI of custom multicall smart contract.
If omited, MakerDao Multicall smart contract ABI will be used.


**Methods**

*call()* - Executes multicall for specified list of smart contracts functions.

Parameters:

calls: list(tuple) - list of tuples (target contract address, encoded function name with parameters)
Can be easy prepared via using Multicall.create_cal' method.

Returns:

tuple(block number, list(results)) for default MakerDao multicall. May vary for custom multicalls.


*create_call()* - Prepares a tuple for passing to Multicall.call list.

Parameters:

contract: web3.eth.Contract - A web3.eth.Contract instance of a contract 
that is to be called via multicall.

fn_name: str - The name of a contract function to be called.

args: list - a list of arguments to be passed to a called contract function.

Returns:

tuple(target contract address, encoded function name with parameters)


## Example

Multicall of three ERC20 tokens of a specified EthDev *address*.

Importing Web3

    >>> from web3 import Web3

Defining constants: ERC20 token ABI token mainnet addresses.

    # ERC20 ABI string is cropped for readability
    >>> ERC20_ABI = '[{"constant":true,"inputs":[],"name":"name", ...

    >>> USDT_ADDRESS = Web3.toChecksumAddress('0xdAC17F958D2ee523a2206206994597C13D831ec7')
    >>> USDC_ADDRESS = Web3.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')
    >>> BNB_ADDRESS = Web3.toChecksumAddress('0xB8c77482e45F1F44dE1745F52C74426C631bDD52')

Initializing web3 instance.

    >>> ETH_PROVIDER_URL = 'https://rpc.ankr.com/eth'
    >>> w3 = Web3(Web3.HTTPProvider(ETH_PROVIDER_URL))    
    
Creating token Web3 contracts.

    >>> USDT = w3.eth.contract(address=USDT_ADDRESS, abi=ERC20_ABI)
    >>> USDC = w3.eth.contract(address=USDC_ADDRESS, abi=ERC20_ABI)
    >>> BNB = w3.eth.contract(address=BNB_ADDRESS, abi=ERC20_ABI)
  
Target user address.

    >>> address = '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'

Initializing Multicall for mainnet.

    >>> from simple_multicall import Multicall
    >>> 
    >>> multicall = Multicall(w3, 'mainnet')

Creating a list of calls via 'Multicall.create_call()' method.

    >>> calls = [
    ...     multicall.create_call(USDT, 'balanceOf', [address]),
    ...     multicall.create_call(USDC, 'balanceOf', [address]),
    ...     multicall.create_call(BNB, 'balanceOf', [address])
    ... ]

Executing multicall.

    >>> result= multicall.call(calls)

    >>> result
    [15442332, [b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\    
    x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00?D#\xde' b'\x00\x00\x00\x00\x00\
    x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\
    x00\x00\x00;\x9a\xf1\x10', b'\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x0
    0\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02HA>\xf1\xd3\xf2\x00\x00']]

Interpreting results.

    >>> print('Block number: ', result[0])
    Block number:  15442332

    >>> print('USDT: ', int(result[1][0].hex(), 16) / 10 ** 6)
    USDT:  1061.430238

    >>> print('USDC: ', int(result[1][1].hex(), 16) / 10 ** 6)
    USDC:  1000.01

    >>> print('BNB: ', int(result[1][2].hex(), 16) / 10 ** 18)
    BNB:  42.1

            

Raw data

            {
    "_id": null,
    "home_page": "",
    "name": "simple-multicall",
    "maintainer": "",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "multicall,web3",
    "author": "Igor Lapshin",
    "author_email": "igorlapshin@list.ru",
    "download_url": "https://files.pythonhosted.org/packages/c8/fd/9da63dbc821e82f884889b0cbc4173892d45ade89064b0c89a63962bff6d/simple_multicall-0.1.5.tar.gz",
    "platform": null,
    "description": "# Simple Web3 Multicall\n\nA multicall for use with pure Web3.py library.\nIt uses MakerDao Multicall smart contract by default,\nbut also can use any custom multicall smart contract \nthat implements \"aggregate\" function.\n\nMain urge for developmenet such a package was when I was needed to use multicall in my project \nbased on Web3.py, but found only libraries which work with Brownie, not on pure Web3.py.\n\n\n    pip install simple-multicall\n\n\n## Package Content\n\n- class Multicall - The main multicall class that executes a multicall itself.\n\n- constants file makerdao_multicall - contains a default MakerDao multicall smart contract ABI and a dictionary of addresses of multicall contracts deployed on different chains. \n\n\n### class Multicall\n\nThe main multicall class. Should be initialized by passing a Web3 instance with specified chain RPC provider.\n\nHas two methods: \n\n*call* - executes a multicall\n\n*create_call* - prepare a tuple for a list of calls to be passed to a *call* method\n\n\n**Attributes**\n    \n*w3* : Web3 class instanse\n\n*chain*: str\nThe name of one of defined chains \nwhere MakerDao Multicall smart contract is deployed.\nCan be one of the followings\n\n- 'mainnet'\n- 'kovan'\n- 'rinkeby'\n- 'goerli'\n- 'ropsten'\n- 'xdai'\n- 'polygon'\n- 'mumbai'\n- 'bsc-mainnet'\n- 'bsc-testnet'\n\n\n*custom_address*: str - An address of custom multicall smart contract. \nIf specified, MakerDao Multicall smart contract will be omited.\n\n\n*custom_abi*: str - An ABI of custom multicall smart contract.\nIf omited, MakerDao Multicall smart contract ABI will be used.\n\n\n**Methods**\n\n*call()* - Executes multicall for specified list of smart contracts functions.\n\nParameters:\n\ncalls: list(tuple) - list of tuples (target contract address, encoded function name with parameters)\nCan be easy prepared via using Multicall.create_cal' method.\n\nReturns:\n\ntuple(block number, list(results)) for default MakerDao multicall. May vary for custom multicalls.\n\n\n*create_call()* - Prepares a tuple for passing to Multicall.call list.\n\nParameters:\n\ncontract: web3.eth.Contract - A web3.eth.Contract instance of a contract \nthat is to be called via multicall.\n\nfn_name: str - The name of a contract function to be called.\n\nargs: list - a list of arguments to be passed to a called contract function.\n\nReturns:\n\ntuple(target contract address, encoded function name with parameters)\n\n\n## Example\n\nMulticall of three ERC20 tokens of a specified EthDev *address*.\n\nImporting Web3\n\n    >>> from web3 import Web3\n\nDefining constants: ERC20 token ABI token mainnet addresses.\n\n    # ERC20 ABI string is cropped for readability\n    >>> ERC20_ABI = '[{\"constant\":true,\"inputs\":[],\"name\":\"name\", ...\n\n    >>> USDT_ADDRESS = Web3.toChecksumAddress('0xdAC17F958D2ee523a2206206994597C13D831ec7')\n    >>> USDC_ADDRESS = Web3.toChecksumAddress('0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48')\n    >>> BNB_ADDRESS = Web3.toChecksumAddress('0xB8c77482e45F1F44dE1745F52C74426C631bDD52')\n\nInitializing web3 instance.\n\n    >>> ETH_PROVIDER_URL = 'https://rpc.ankr.com/eth'\n    >>> w3 = Web3(Web3.HTTPProvider(ETH_PROVIDER_URL))    \n    \nCreating token Web3 contracts.\n\n    >>> USDT = w3.eth.contract(address=USDT_ADDRESS, abi=ERC20_ABI)\n    >>> USDC = w3.eth.contract(address=USDC_ADDRESS, abi=ERC20_ABI)\n    >>> BNB = w3.eth.contract(address=BNB_ADDRESS, abi=ERC20_ABI)\n  \nTarget user address.\n\n    >>> address = '0xde0B295669a9FD93d5F28D9Ec85E40f4cb697BAe'\n\nInitializing Multicall for mainnet.\n\n    >>> from simple_multicall import Multicall\n    >>> \n    >>> multicall = Multicall(w3, 'mainnet')\n\nCreating a list of calls via 'Multicall.create_call()' method.\n\n    >>> calls = [\n    ...     multicall.create_call(USDT, 'balanceOf', [address]),\n    ...     multicall.create_call(USDC, 'balanceOf', [address]),\n    ...     multicall.create_call(BNB, 'balanceOf', [address])\n    ... ]\n\nExecuting multicall.\n\n    >>> result= multicall.call(calls)\n\n    >>> result\n    [15442332, [b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\    \n    x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00?D#\\xde' b'\\x00\\x00\\x00\\x00\\x00\\\n    x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\\n    x00\\x00\\x00;\\x9a\\xf1\\x10', b'\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x0\n    0\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x00\\x02HA>\\xf1\\xd3\\xf2\\x00\\x00']]\n\nInterpreting results.\n\n    >>> print('Block number: ', result[0])\n    Block number:  15442332\n\n    >>> print('USDT: ', int(result[1][0].hex(), 16) / 10 ** 6)\n    USDT:  1061.430238\n\n    >>> print('USDC: ', int(result[1][1].hex(), 16) / 10 ** 6)\n    USDC:  1000.01\n\n    >>> print('BNB: ', int(result[1][2].hex(), 16) / 10 ** 18)\n    BNB:  42.1\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Simple Web3 multicall",
    "version": "0.1.5",
    "project_urls": null,
    "split_keywords": [
        "multicall",
        "web3"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "64db1463b178850c591de3c745a124e42c0e11821ee0469e633aa543e161f8d7",
                "md5": "2368ca2ea1ea21357a3997b14b758aa0",
                "sha256": "08a0e9c8ab1542d1301e40c85b4a32a32c399c3e4ed7e0d0820193cba12f0a22"
            },
            "downloads": -1,
            "filename": "simple_multicall-0.1.5-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "2368ca2ea1ea21357a3997b14b758aa0",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6260,
            "upload_time": "2022-08-30T20:56:49",
            "upload_time_iso_8601": "2022-08-30T20:56:49.661829Z",
            "url": "https://files.pythonhosted.org/packages/64/db/1463b178850c591de3c745a124e42c0e11821ee0469e633aa543e161f8d7/simple_multicall-0.1.5-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "c8fd9da63dbc821e82f884889b0cbc4173892d45ade89064b0c89a63962bff6d",
                "md5": "bb755d26ef36370c4c3b91572ba8f5b8",
                "sha256": "253e33dfddb9f8a61bca91e2e486050c9f5a92524a2a7a81221736817a867ef9"
            },
            "downloads": -1,
            "filename": "simple_multicall-0.1.5.tar.gz",
            "has_sig": false,
            "md5_digest": "bb755d26ef36370c4c3b91572ba8f5b8",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5489,
            "upload_time": "2022-08-30T20:56:51",
            "upload_time_iso_8601": "2022-08-30T20:56:51.164064Z",
            "url": "https://files.pythonhosted.org/packages/c8/fd/9da63dbc821e82f884889b0cbc4173892d45ade89064b0c89a63962bff6d/simple_multicall-0.1.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2022-08-30 20:56:51",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "simple-multicall"
}
        
Elapsed time: 0.20343s