Search code examples
pythonblockchainsolidityweb3py

TypeError: 'type' object is not subscriptable web3.py


Hey I am getting TypeError: 'type' object is not subscriptable when trying to print SimpleStorage.I am using env, my python version is 3.10.0 n the library is web3.py idk if this is enough this is my first q on stakeoverflow

from solcx import compile_standard, install_solc
import json
from web3 import Web3

with open("./SimpleStorage.sol", "r") as file:
    simple_storage_file = file.read()
print("Installing...")
install_solc("0.6.0")

compiled_sol = compile_standard(
    {
        "language": "Solidity",
        "sources": {"SimpleStorage.sol": {"content": simple_storage_file}},
        "settings": {
            "outputSelection": {
                "*": {
                    "*": ["abi", "metadata", "evm.bytecode", "evm.bytecode.sourceMap"]
                }
            }
        },
    },
    solc_version="0.6.0",
)


with open("compiled_code.json", "w") as file:
    json.dump(compiled_sol, file)

# get the bytecode

bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
    "bytecode"
]["object"]

# get abi
abi = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["abi"]

w3 = Web3(Web3.HTTPProvider["http://127.0.0.1:7545"])
chain_id = 5777
my_address = "0x4bAB8D6D8b8959B1B7DaBd40B39eDFA8C07144C8"
private_key = "0xa5311c1f5d113053b3cfd4ef7ad1375a4030825d2f6bdf4131f28ae2331e1b6c"


# Create the contract in python

SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
print(SimpleStorage)

Solution

  • use () instead of []. Using [] treats it as a subscriptable object/container, meaning they contain other objects. Like strings, lists, tuples, and dictionaries.

    w3 = Web3(Web3.HTTPProvider("http://127.0.0.1:7545"))