Skip to main content

Multicall library for aggregating web3py contract calls

Project description

web3mc - multicall library for web3py

Based on makerdao's multicall contractand brownie implementation with batching and asynchronous support. Works directly with web3py contract functions and parameters

Installation

pip install web3mc

Quickstart

Basic usage

(this is default value if empty - set by web3py)

export WEB3_HTTP_PROVIDER_URI=http://localhost:8545
from web3.auto import w3
from web3mc.auto import multicall

abi = [{"constant": True, "inputs": [], "name": "name", "outputs": [{"name": "", "type": "string"}], "payable": False,
        "stateMutability": "view", "type": "function", },
       {"constant": True, "inputs": [], "name": "symbol", "outputs": [{"name": "", "type": "string"}], "payable": False,
        "stateMutability": "view", "type": "function", },
       {"constant": True, "inputs": [{"name": "", "type": "address"}], "name": "balanceOf",
        "outputs": [{"name": "", "type": "uint256"}], "payable": False, "stateMutability": "view", "type": "function"}]

weth_erc20 = w3.eth.contract("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", abi=abi)

calls = [weth_erc20.functions.name(), weth_erc20.functions.symbol(), weth_erc20.functions.balanceOf("vitalik.eth")]

result = multicall.aggregate(calls)
print(result)  # ['Wrapped Ether', 'WETH', 26992040046283229929]

Call multiple contracts with same abi (implementation)

...
weth_erc20 = w3.eth.contract("0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", abi=abi)

calls = [weth_erc20.functions.name(),weth_erc20.functions.symbol(),weth_erc20.functions.balanceOf("vitalik.eth")] * 2
# WBTC, USDC
addresses = ["0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"] * 3 + ["0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599"] * 3

result = multicall.aggregate(calls, use_try=True, addresses=addresses)  # tryAggregate()
print(result)  # ['Wrapped BTC', 'WBTC', 0, 'USD Coin', 'USDC', 396267093705]

Parameters

Environment variable

  • WEB3_HTTP_PROVIDER_URI - can be overwritten directly (from web3py)

Custom parameters

from web3mc import Multicall

multicall = Multicall(
    provider_url="<your custom provider url>",  # Overrides env parameter
    batch=100,  # can lead to overflow
    max_retries=3,  # retries without use_try (aggregate function in contract)
    gas_limit=15_000_000,  # gas limit for calls
    _semaphore=1000,  # max concurrent coroutines, change carefully!
)

Testing

Install dependencies, make sure you set WEB3_HTTP_PROVIDER_URI environment variable

pytest tests

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

web3mc-0.1.13.tar.gz (12.9 kB view details)

Uploaded Source

Built Distribution

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

web3mc-0.1.13-py3-none-any.whl (13.1 kB view details)

Uploaded Python 3

File details

Details for the file web3mc-0.1.13.tar.gz.

File metadata

  • Download URL: web3mc-0.1.13.tar.gz
  • Upload date:
  • Size: 12.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.4 Linux/6.11.0-1018-azure

File hashes

Hashes for web3mc-0.1.13.tar.gz
Algorithm Hash digest
SHA256 f24f22621c11b904d09656e8226a9fb8a578a225b052a6b2e94fce3f64b4e78a
MD5 fb9f586f3313000d39fda7a5012d1676
BLAKE2b-256 28254bcf61f8dfc96fd0e582f28f3b1e2df69e2e910bced36feddd58c4dfdfad

See more details on using hashes here.

File details

Details for the file web3mc-0.1.13-py3-none-any.whl.

File metadata

  • Download URL: web3mc-0.1.13-py3-none-any.whl
  • Upload date:
  • Size: 13.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.5.1 CPython/3.10.4 Linux/6.11.0-1018-azure

File hashes

Hashes for web3mc-0.1.13-py3-none-any.whl
Algorithm Hash digest
SHA256 6d7e9561b33519de5cb19bb0d1b987e7316c9cc80104d1aab661cb4a91d11fd6
MD5 c07762eca8aecc28edd854f060c798f7
BLAKE2b-256 80f21fe2500e040d60ade9175f81ffc613de0c02eb4c2e79f6254c3b2a6caa74

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