Skip to main content

Easy way to work with Multicall package

Project description

Multicallable: Simplified Interface for Multicall

Multicallable provides a streamlined way to work with the Multicall package, allowing you to batch multiple contract calls into a single request.

Installation

Install the package using the following command:

pip install -U multicallable

Getting Started

Initialize Web3 or AsyncWeb3

First, import the Web3 library and set up a Web3 instance. The setup differs depending on whether you are using synchronous or asynchronous operations.

For synchronous operations:

from web3 import Web3

# Specify Ethereum RPC URL
ETH_RPC_URL = 'https://rpc.ankr.com/eth'

# Initialize Web3 instance
w3 = Web3(Web3.HTTPProvider(ETH_RPC_URL))

For asynchronous operations:

from web3 import AsyncWeb3

# Initialize AsyncWeb3 instance
w3 = AsyncWeb3(AsyncWeb3.AsyncHTTPProvider(ETH_RPC_URL))

Import and Initialize Multicallable

Next, import the Multicallable class and initialize it for a specific token:

from multicallable import Multicallable

# Truncated ERC20 ABI for demonstration
ERC20_ABI = '[{"constant":true,"inputs":[],"name":"name", ...'

# sample token contract address
TOKEN = '0xDE5ed76E7c05eC5e4572CfC88d1ACEA165109E44'

# Initialize Multicallable instance
multicallable = Multicallable(TOKEN, ERC20_ABI, w3)

AsyncMulticallable: The Asynchronous Alternative

For asynchronous use-cases, AsyncMulticallable is available. Unlike Multicallable, its constructor is empty, and it includes an asynchronous setup function that takes the same parameters:

from multicallable import AsyncMulticallable

# Initialize AsyncMulticallable instance
async_multicallable = AsyncMulticallable()
await async_multicallable.setup(TOKEN, ERC20_ABI, w3)  # Make sure w3 is an AsyncWeb3 instance

Basic Operations

Querying Multiple Balances

For synchronous operations:

addresses = [
    # List of addresses
]
balances = multicallable.balanceOf(addresses).call()

For asynchronous operations:

addresses = [
    # List of addresses
]
balances = await async_multicallable.balanceOf(addresses).call()

Detailed Call Information

For synchronous operations:

detailed_info = multicallable.balanceOf(addresses).detailed_call()

For asynchronous operations:

detailed_info = await async_multicallable.balanceOf(addresses).detailed_call()

Advanced Features

Handling Failed Calls

By default, all calls must succeed for the batch call to return successfully. Use require_success=False to allow partial success:

mc = Multicallable(contract_address, contract_abi, w3)
partial_result = mc.getNum(list(range(7))).call(require_success=False)

Batching Large Number of Calls

For large number of calls, you can specify the number of buckets using the n parameter:

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

Progress Indicator

Enable a progress bar for better visibility into the batch processing:

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

Custom Multicall Instance

You can also use a custom Multicall instance with a custom address and ABI:

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-6.0.2.tar.gz (10.2 kB view details)

Uploaded Source

Built Distribution

multicallable-6.0.2-py3-none-any.whl (12.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: multicallable-6.0.2.tar.gz
  • Upload date:
  • Size: 10.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.5

File hashes

Hashes for multicallable-6.0.2.tar.gz
Algorithm Hash digest
SHA256 a9e490c8272c2cdce1c9caf763928ed1c7ead59a2daf5a41f76447c5f6cc7fb0
MD5 f8d557bfa3a7602621a72c60c2842f5f
BLAKE2b-256 12eb4cde6f264688a8b38cb81681e389632541552fb10b2cd491d0f691246433

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for multicallable-6.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7f762dab7c7e44ec346dd63ebdfb503be20a75fbb6b739560c600719fe708f20
MD5 3e401c6b8888b8ed74c53ee6bc76632b
BLAKE2b-256 b98269f97e109da9838b4f07a80cbb69c8903e167895d3d6602d5672610128df

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