0

I am new to blockchain and have recently setup a hardhat project and written a smart contract for minting nfts. I am more comfortable with python so have started using web3.py for deploying and interacting the scripts. I had two questions:

  1. Is there any limitations to using python with blockchain over javascript especially in a production environment, and if so is it worth switching to javascript sooner rather than later.

  2. Whenever I use the web3.py library I seem to be running into the same error of AttributeError: 'Web3' object has no attribute '...', prompting me to keep changing the code even though online tutorials seem to show it works. Is there a problem on my end in terms of how it's installed or is it just that the library is being updated continually. Here is an example of some of my code:

from web3 import Web3
from dotenv import load_dotenv
import os
import json

# Load environment variables
load_dotenv()
api_url = os.getenv('API_URL')
private_key = os.getenv('PRIVATE_KEY')

# Connect to the network
w3 = Web3(Web3.HTTPProvider(api_url))

# Ensure connection is successful
assert w3.isConnected()

# Set up your account
account = w3.eth.account.privateKeyToAccount(private_key)
w3.eth.default_account = account.address

# Load contract ABI and bytecode
with open('blockchain/artifacts/contracts/Mint721A.sol/Mint721A.json', 'r') as file:
    contract_json = json.load(file)  # load the json file
    contract_abi = contract_json['abi']  # extract the abi
    contract_bytecode = contract_json['bytecode']  # extract the bytecode

# Create the contract in Python
Contract = w3.eth.contract(abi=contract_abi, bytecode=contract_bytecode)

# Build transaction
construct_txn = Contract.constructor("MyToken", "MTK", "https://mytokenbaseuri.com/").buildTransaction({
    'from': account.address,
    'nonce': w3.eth.getTransactionCount(account.address),
    'gas': 2000000,
    'gasPrice': w3.toWei('50', 'gwei')
})

# Sign the transaction
signed = account.signTransaction(construct_txn)

# Send the transaction
tx_hash = w3.eth.sendRawTransaction(signed.rawTransaction)

# Wait for the transaction to be mined
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)

print(f'Contract deployed at address: {tx_receipt.contractAddress}')

I tried changing IsConnected to is_connected and it works, but then similar errors occur for buildTransaction, and getTransactionCount. Since I am using online sources of code, I am surprised by these errors, and not sure if there is something wrong with how im importing library.

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.