Skip to main content

Simple Web3 multicall 2

Project description

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 "tryBlockAndAggregate" 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-2

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(false, 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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_multicall_2-0.1.0.tar.gz (5.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_multicall_2-0.1.0-py3-none-any.whl (6.1 kB view details)

Uploaded Python 3

File details

Details for the file simple_multicall_2-0.1.0.tar.gz.

File metadata

  • Download URL: simple_multicall_2-0.1.0.tar.gz
  • Upload date:
  • Size: 5.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.1 CPython/3.9.13

File hashes

Hashes for simple_multicall_2-0.1.0.tar.gz
Algorithm Hash digest
SHA256 f783b621e3db0a91a033ca49ae4818ad8cc5d3b741b99eb2d6efff81554bda7f
MD5 5c66ece97c6bb487d776b8f0e3ef305f
BLAKE2b-256 196187c55cc09c20cd58f46b4c4773d55d359454ce0ed2e44f49a7929c62ba50

See more details on using hashes here.

File details

Details for the file simple_multicall_2-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_multicall_2-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 fa7616344531844e8c02f9a471f8b0d549ff42eebd0e3c0013c9e4017b431f5d
MD5 71205c5b27f44b5ab7c7543da1d38c89
BLAKE2b-256 50ac21656290352f8042691c23f44ec15d267a90360b0df88025f59b7ad64c6e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page