简体   繁体   中英

State Overrides: OverflowError: Python int too large to convert to C ssize_t

I've written some simple state override functions in a contract that I'd like to use to query specific data about a contract. The idea is that we call a series of view functions and aggregate the outputs "on chain" before returning the output all at once

However, I'm seeing the following error. Any idea where I'm going wrong?

OverflowError: Python int too large to convert to C ssize_t

Contract code

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract NftSnapshot {

  function snapshotUsers(address _nftAddress, uint256[] calldata _tokenIds) public view returns (address[] memory) {
    address[] memory owners = new address[](_tokenIds.length);
    for (uint256 i; i < _tokenIds.length; i++) {
      owners[i] = IERC721(_nftAddress).ownerOf(_tokenIds[i]);
    }
    return owners;
  }

  function snapshotTokenMetadata(address _nftAddress, uint256[] calldata _tokenIds) public view returns (string[] memory) {
    string[] memory tokens = new string[](_tokenIds.length);
    for (uint256 i; i < _tokenIds.length; i++) {
      tokens[i] = IERC721Metadata(_nftAddress).tokenURI(_tokenIds[i]);
    }
    return tokens;
  }

}

Python code

from typing import List, Optional
from web3 import Web3
from eth_abi import decode_abi
import time

def virtual_contract_call(
    web3_interface: Web3,
    abi: str,
    runtime_bytecode: str,
    fn_name: str,
    fn_args: List,
    latest_block: Optional[int] = None,
) -> List:

    virtual_contract_address = "0x37F5eCc33e530D495C336866d7471FF9ad1C2469"
    contract = web3.eth.contract(address=virtual_contract_address, abi=abi)

    function_call_payload = contract.encodeABI(fn_name, fn_args)

    state_override = {virtual_contract_address: {"code": runtime_bytecode}}

    block_number = web3_interface.eth.getBlock(
        "latest" if not latest_block else latest_block
    ).number

    res = web3.eth.call(
        {"to": virtual_contract_address, "data": function_call_payload},
        block_number,
        state_override=state_override,
    )

    return res


web3 = Web3(Web3.HTTPProvider("https://eth-mainnet.g.alchemy.com/v2/ZpQFH9fKeo5GUpbViaz_VkhyqaWJUfhF"))

abi = """
[
   {
      "inputs":[
         {
            "internalType":"address",
            "name":"_nftAddress",
            "type":"address"
         },
         {
            "internalType":"uint256[]",
            "name":"_tokenIds",
            "type":"uint256[]"
         }
      ],
      "name":"snapshotTokenMetadata",
      "outputs":[
         {
            "internalType":"string[]",
            "name":"",
            "type":"string[]"
         }
      ],
      "stateMutability":"view",
      "type":"function"
   },
   {
      "inputs":[
         {
            "internalType":"address",
            "name":"_nftAddress",
            "type":"address"
         },
         {
            "internalType":"uint256[]",
            "name":"_tokenIds",
            "type":"uint256[]"
         }
      ],
      "name":"snapshotUsers",
      "outputs":[
         {
            "internalType":"address[]",
            "name":"",
            "type":"address[]"
         }
      ],
      "stateMutability":"view",
      "type":"function"
   }
]
"""

runtime_bytecode = "0x608060405234801561001057600080fd5b5061097b806100206000396000f3fe608060405234801561001057600080fd5b50600436106100365760003560e01c806342e9bdc31461003b578063b3930b121461006b575b600080fd5b6100556004803603810190610050919061040b565b61009b565b6040516100629190610529565b60405180910390f35b6100856004803603810190610080919061040b565b6101fa565b604051610092919061069d565b60405180910390f35b606060008383905067ffffffffffffffff8111156100bc576100bb6106bf565b5b6040519080825280602002602001820160405280156100ea5781602001602082028036833780820191505090505b50905060005b848490508110156101ee578573ffffffffffffffffffffffffffffffffffffffff16636352211e86868481811061012a576101296106ee565b5b905060200201356040518263ffffffff1660e01b815260040161014d9190610736565b602060405180830381865afa15801561016a573d6000803e3d6000fd5b505050506040513d601f19601f8201168201806040525081019061018e9190610766565b8282815181106101a1576101a06106ee565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff168152505080806101e6906107c2565b9150506100f0565b50809150509392505050565b606060008383905067ffffffffffffffff81111561021b5761021a6106bf565b5b60405190808252806020026020018201604052801561024e57816020015b60608152602001906001900390816102395790505b50905060005b84849050811015610328578573ffffffffffffffffffffffffffffffffffffffff1663c87b56dd86868481811061028e5761028d6106ee565b5b905060200201356040518263ffffffff1660e01b81526004016102b19190610736565b600060405180830381865afa1580156102ce573d6000803e3d6000fd5b505050506040513d6000823e3d601f19601f820116820180604052508101906102f791906108fc565b82828151811061030a576103096106ee565b5b60200260200101819052508080610320906107c2565b915050610254565b50809150509392505050565b6000604051905090565b600080fd5b600080fd5b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b600061037382610348565b9050919050565b61038381610368565b811461038e57600080fd5b50565b6000813590506103a08161037a565b92915050565b600080fd5b600080fd5b600080fd5b60008083601f8401126103cb576103ca6103a6565b5b8235905067ffffffffffffffff8111156103e8576103e76103ab565b5b602083019150836020820283011115610404576104036103b0565b5b9250929050565b6000806000604084860312156104245761042361033e565b5b600061043286828701610391565b935050602084013567ffffffffffffffff81111561045357610452610343565b5b61045f868287016103b5565b92509250509250925092565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b6104a081610368565b82525050565b60006104b28383610497565b60208301905092915050565b6000602082019050919050565b60006104d68261046b565b6104e08185610476565b93506104eb83610487565b8060005b8381101561051c57815161050388826104a6565b975061050e836104be565b9250506001810190506104ef565b5085935050505092915050565b6000602082019050818103600083015261054381846104cb565b905092915050565b600081519050919050565b600082825260208201905092915050565b6000819050602082019050919050565b600081519050919050565b600082825260208201905092915050565b60005b838110156105b1578082015181840152602081019050610596565b60008484015250505050565b6000601f19601f8301169050919050565b60006105d982610577565b6105e38185610582565b93506105f3818560208601610593565b6105fc816105bd565b840191505092915050565b600061061383836105ce565b905092915050565b6000602082019050919050565b60006106338261054b565b61063d8185610556565b93508360208202850161064f85610567565b8060005b8581101561068b578484038952815161066c8582610607565b94506106778361061b565b925060208a01995050600181019050610653565b50829750879550505050505092915050565b600060208201905081810360008301526106b78184610628565b905092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b6000819050919050565b6107308161071d565b82525050565b600060208201905061074b6000830184610727565b92915050565b6000815190506107608161037a565b92915050565b60006020828403121561077c5761077b61033e565b5b600061078a84828501610751565b91505092915050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b60006107cd8261071d565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82036107ff576107fe610793565b5b600182019050919050565b600080fd5b610818826105bd565b810181811067ffffffffffffffff82111715610837576108366106bf565b5b80604052505050565b600061084a610334565b9050610856828261080f565b919050565b600067ffffffffffffffff821115610876576108756106bf565b5b61087f826105bd565b9050602081019050919050565b600061089f61089a8461085b565b610840565b9050828152602081018484840111156108bb576108ba61080a565b5b6108c6848285610593565b509392505050565b600082601f8301126108e3576108e26103a6565b5b81516108f384826020860161088c565b91505092915050565b6000602082840312156109125761091161033e565b5b600082015167ffffffffffffffff8111156109305761092f610343565b5b61093c848285016108ce565b9150509291505056fea26469706673582212203d2b80cfef255ffcf4a801de03f43a508578aa570e95a8a26064bbc3e4df44da64736f6c63430008110033"

res = virtual_contract_call(
    web3_interface=web3,
    abi=abi,
    runtime_bytecode=runtime_bytecode,
    fn_name="snapshotUsers",
    fn_args=[Web3.toChecksumAddress("0x60e4d786628fea6478f785a6d7e704777c86a7c6"), [*range(1, 10000)]], #MAYC NFT contract
)

snapshot = decode_abi(["address[]"], bytes.fromhex(res.hex()[2:]))
print(snapshot)

Here's a quick repo I spun up if you'd like to get deeper into the scaffolding

https://github.com/chumbalayaa/state-override-functions

Full backtrace

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<string>", line 102, in <module>
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_abi/codec.py", line 181, in decode_abi
    return decoder(stream)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_abi/decoding.py", line 127, in __call__
    return self.decode(stream)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_utils/functional.py", line 45, in inner
    return callback(fn(*args, **kwargs))
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_abi/decoding.py", line 173, in decode
    yield decoder(stream)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_abi/decoding.py", line 127, in __call__
    return self.decode(stream)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_abi/decoding.py", line 144, in decode
    stream.push_frame(start_pos)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_abi/decoding.py", line 95, in push_frame
    self.seek_in_frame(0)
  File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/eth_abi/decoding.py", line 84, in seek_in_frame
    self.seek(self._total_offset + pos, *args, **kwargs)
OverflowError: Python int too large to convert to C ssize_t

the int is too large, thats what it says

The integer is too large for the Python compiler to compile it down to C code.
There are various methods to determine the max size / max value of a Python int.

Python 2.x: sys.maxint
Python 3.x: sys.maxsize

If you really need a number that long, you should check out NumPy Data Types such as numpy.ulonglong for storing large numbers.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM