Skip to main content

Easy way to work with Multicall package

Project description

Multicallable

Easy way to work with Multicall package

Installation

pip install -U multicallable

Usage

Importing Web3

>>> from web3 import Web3

Initializing Web3 instance

>>> ETH_RPC_URL = 'https://rpc.ankr.com/eth'
>>> w3 = Web3(Web3.HTTPProvider(ETH_RPC_URL))

Importing Multicallable

>>> from multicallable import Multicallable

Initializing Multicallable instance for Deus token

# ERC20 ABI string is cropped for readability
>>> ERC20_ABI = '[{"constant":true,"inputs":[],"name":"name", ...
>>> DEUS_TOKEN = '0xDE5ed76E7c05eC5e4572CfC88d1ACEA165109E44'
>>> deus = Multicallable(DEUS_TOKEN, ERC20_ABI, w3)

Calling balanceOf function for a list of addresses

>>> addresses = [
...     '0xa345c89c07fEB0A13937eecE0204327271904cB7',
...     '0xF493284a730e3D561Bf81f52991AF0C8D9227C35',
...     '0x19dceFD603ea112CF547Cdddb8D68f61c6F4c73C',
...     '0x633cBf6347ddddb5fEc65ad803b4e0B282ADdBd7',
... ]
>>> deus.balanceOf(addresses).call()
[3955776201653330000000,
 1499972538000000000000,
 334010000000000000000,
 135760891050327000000]

Get more details for call

>>> addresses = [
...     '0xa345c89c07fEB0A13937eecE0204327271904cB7',
...     '0xF493284a730e3D561Bf81f52991AF0C8D9227C35',
...     '0x19dceFD603ea112CF547Cdddb8D68f61c6F4c73C',
...     '0x633cBf6347ddddb5fEc65ad803b4e0B282ADdBd7',
... ]
>>> deus.balanceOf(addresses).detailed_call()
[{'block_number': 54040756,
  'result': [3955776201653330000000,
             1499972538000000000000,
             334010000000000000000,
             135760891050327000000]}]

Ignore failed calls

If require_success is True, all calls must return true, otherwise the multicall fails.
The default value is True.

>>> contract_address = '0x15BB7787Be4E03E6Caa09D2fF502564D3eD67Ec2'
>>> contract_abi = '[{"inputs":[{"internalType":"uint256","name":"num","type":"uint256"}],"name":"getNum","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"pure","type":"function"}]'
>>> mc = Multicallable(contract_address, contract_abi, w3)
>>> mc.getNum(list(range(7))).call(require_success=True)
Traceback (most recent call last):
.
.
.
web3.exceptions.ContractLogicError: execution reverted: Multicall3: call failed

>>> mc.getNum(list(range(7))).call(require_success=False)
[ValueError('Bad number!'),
 2,
 3,
 4,
 5,
 ValueError('Bad number!'),
 7]

Change number of buckets

Set n as the number of buckets for calling Multicall contract for large number of calls.
The default value is 1.

>>> result = mc.getNum(list(range(70000))).call(require_success=False)
Traceback (most recent call last):
.
.
.
ValueError: {'code': -32000, 'message': 'out of gas'}

>>> result = mc.getNum(list(range(70000))).call(require_success=False, n=100)
>>> len(result)
70000

Show progress bar

Use progress_bar=True to show progress bar while sending buckets.

>>> result = mc.getNum(list(range(70000))).call(n=100, progress_bar=True, require_success=False)
    ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 100/100 buckets
>>> len(result)
70000

Use with custom Multicall

It is possible to pass a Multicall instance with custom address and abi to Multicallable

>>> from multicallable.multicall import Multicall
>>> multicall = Multicall(w3, custom_address, custom_abi)
>>> mc = Multicallable(contract_address, contract_abi, multicall=multicall)

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

multicallable-5.1.0.tar.gz (7.1 kB view details)

Uploaded Source

Built Distribution

multicallable-5.1.0-py3-none-any.whl (8.5 kB view details)

Uploaded Python 3

File details

Details for the file multicallable-5.1.0.tar.gz.

File metadata

  • Download URL: multicallable-5.1.0.tar.gz
  • Upload date:
  • Size: 7.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.7.16

File hashes

Hashes for multicallable-5.1.0.tar.gz
Algorithm Hash digest
SHA256 7116d6a2802f6a485d51e2cf4f96486d31b88b5c7bd8fbd79696fb66e0e8c0d3
MD5 5e3da647530be15e298e30ec217d1fa4
BLAKE2b-256 b70af59f80f47662374aa40ee2910f7a63f9ac871d7e76deb3cd10ee8a0afb04

See more details on using hashes here.

File details

Details for the file multicallable-5.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for multicallable-5.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 233e42f048b196f036a0ebb9526bc0d64a1a58a514456abc3bcb3cc2af4cba11
MD5 f48e59bd7bc9107589b8c4d1670c1d17
BLAKE2b-256 248b7852930a74fb5c4ad4caeeee00a2ca76d5783dba2b9dadd8aaece70093a7

See more details on using hashes here.

Supported by

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