Skip to content

Instantly share code, notes, and snippets.

@Blauyourmind
Last active July 25, 2022 13:08
Show Gist options
  • Save Blauyourmind/c4f8e0dd33c2779884c0a0fdc49f2c4e to your computer and use it in GitHub Desktop.
Save Blauyourmind/c4f8e0dd33c2779884c0a0fdc49f2c4e to your computer and use it in GitHub Desktop.
import os
from typing import List, Optional
from web3 import Web3
from eth_abi import decode_abi
from dotenv import load_dotenv
load_dotenv()
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 = "0xDeaDbeefdEAdbeefdEadbEEFdeadbeEFdEaDbeeF"
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
# ============= Example: NFT Snapshot =============
web3 = Web3(Web3.HTTPProvider(os.getenv("MAINNET_ALCHEMY_URL")))
abi = """
[
{
"inputs": [
{
"internalType": "address",
"name": "_NFTAddress",
"type": "address"
},
{
"internalType": "uint256[]",
"name": "_tokenIds",
"type": "uint256[]"
}
],
"name": "snapshot",
"outputs": [
{
"internalType": "address[]",
"name": "",
"type": "address[]"
}
],
"stateMutability": "view",
"type": "function"
}
]
"""
runtime_bytecode = "0x608060405234801561001057600080fd5b506004361061002b5760003560e01c806352fd269e14610030575b600080fd5b61004a6004803603810190610045919061027b565b610060565b604051610057919061036f565b60405180910390f35b606060008383905067ffffffffffffffff811115610081576100806104c8565b5b6040519080825280602002602001820160405280156100af5781602001602082028036833780820191505090505b50905060005b848490508110156101c2578573ffffffffffffffffffffffffffffffffffffffff16636352211e8686848181106100ef576100ee610499565b5b905060200201356040518263ffffffff1660e01b81526004016101129190610391565b60206040518083038186803b15801561012a57600080fd5b505afa15801561013e573d6000803e3d6000fd5b505050506040513d601f19601f82011682018060405250810190610162919061024e565b82828151811061017557610174610499565b5b602002602001019073ffffffffffffffffffffffffffffffffffffffff16908173ffffffffffffffffffffffffffffffffffffffff168152505080806101ba90610421565b9150506100b5565b50809150509392505050565b6000813590506101dd81610510565b92915050565b6000815190506101f281610510565b92915050565b60008083601f84011261020e5761020d6104fc565b5b8235905067ffffffffffffffff81111561022b5761022a6104f7565b5b60208301915083602082028301111561024757610246610501565b5b9250929050565b6000602082840312156102645761026361050b565b5b6000610272848285016101e3565b91505092915050565b6000806000604084860312156102945761029361050b565b5b60006102a2868287016101ce565b935050602084013567ffffffffffffffff8111156102c3576102c2610506565b5b6102cf868287016101f8565b92509250509250925092565b60006102e783836102f3565b60208301905092915050565b6102fc816103e5565b82525050565b600061030d826103bc565b61031781856103d4565b9350610322836103ac565b8060005b8381101561035357815161033a88826102db565b9750610345836103c7565b925050600181019050610326565b5085935050505092915050565b61036981610417565b82525050565b600060208201905081810360008301526103898184610302565b905092915050565b60006020820190506103a66000830184610360565b92915050565b6000819050602082019050919050565b600081519050919050565b6000602082019050919050565b600082825260208201905092915050565b60006103f0826103f7565b9050919050565b600073ffffffffffffffffffffffffffffffffffffffff82169050919050565b6000819050919050565b600061042c82610417565b91507fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff82141561045f5761045e61046a565b5b600182019050919050565b7f4e487b7100000000000000000000000000000000000000000000000000000000600052601160045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052603260045260246000fd5b7f4e487b7100000000000000000000000000000000000000000000000000000000600052604160045260246000fd5b600080fd5b600080fd5b600080fd5b600080fd5b600080fd5b610519816103e5565b811461052457600080fd5b5056fea2646970667358221220c62a290c8207e8a7025b3b514e767daee70b0aeb16d4ae4d1d744400b8e120de64736f6c63430008070033"
res = virtual_contract_call(
web3_interface=web3,
abi=abi,
runtime_bytecode=runtime_bytecode,
fn_name="snapshot",
fn_args=["0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", [*range(1, 10000)]],
)
snapshot = decode_abi(["address[]"], bytes.fromhex(res.hex()[2:]))
print(snapshot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment