banteg / multicall.py

aggregate results of multiple smart contract calls into one
MIT License
246 stars 106 forks source link

Getting Unauthorized while calling multicall with node provider secured with username & password #67

Closed ashrita-systango closed 4 months ago

ashrita-systango commented 1 year ago

I am trying to do multicall with node provider which is secured with username & password. I have full archived node setup for ETHEREUM. Please suggest how to resolve this problem

With this I am getting below error:

aiohttp.client_exceptions.ClientResponseError: 401, message='Unauthorized', url=URL("<node provider url>")

Below is the sample code for ETH chain:

import requests from web3 import Web3 from web3.middleware import geth_poa_middleware from multicall import Call, Multicall

addresses = [{'walletAddress': '0xee03B2D7E7903012d5d9238139De657493c69C7F', 'tokenAddress': '0x3819f64f282bf135d62168c1e513280daf905e06'}]

block_number = 16889104 GET_BALANCE_FN = "balanceOf(address)(uint256)"

def call_back_obj(success, value): if success is True: return value else: return False

node_provider = 'eth_node_provider_url'

username = 'my_username' password = 'my_password'

session = requests.Session() session.auth = (username, password)

def get_web3_instance(): w3 = Web3(Web3.HTTPProvider(node_provider, session=session)) w3.middleware_onion.inject(geth_poa_middleware, layer=0) return w3

_w3 = get_web3_instance()

def token_balance_handler(addresses, block_number=None): calls = [] for address in addresses: contract_addr = address.get("tokenAddress") wallet_addr = address.get("walletAddress") calls.append( Call( contract_addr, [GET_BALANCE_FN, (wallet_addr)], [[f"{wallet_addr}-{contract_addr}", call_back_obj]], ) ) return Multicall( calls, _w3=_w3, block_id=block_number, require_success=False )

print(token_balance_handler(addresses, block_number)())

BobTheBuidler commented 1 year ago

Looks like you'll need to build support for copying creds to this method here: https://github.com/banteg/multicall.py/blob/fae20da53b960f4f94cb467276901bbc7c7e17b7/multicall/utils.py#L38

If this is a big need for you, go ahead and put together a PR. I can review and merge it for you.

BobTheBuidler commented 4 months ago

going to close this due to no activity. Feel free to reopen if this is still an issue.