Lesson 4: Web3.py Simple Storage - when I run "python deploy.py" I get a "SyntaxError: invalid syntax" message #1031
-
Hi there! I've followed the steps detailed here: I set my private key and addressed the variables to connect with ganache and here's my deploy.py file: import json
from web3 import Web3
# In the video, we forget to `install_solc`
# from solcx import compile_standard
from solcx import compile_standard, install_solc
import os
from dotenv import load_dotenv
load_dotenv()
with open("./SimpleStorage.sol", "r") as file:
simple_storage_file = file.read()
# We add these two lines that we forgot from the video!
print("Installing...")
install_solc("0.6.0")
# Solidity source code
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 bytecode
bytecode = compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["evm"][
"bytecode"
]["object"]
# get abi
abi = json.loads(
compiled_sol["contracts"]["SimpleStorage.sol"]["SimpleStorage"]["metadata"]
)["output"]["abi"]
# w3 = Web3(Web3.HTTPProvider(os.getenv("RINKEBY_RPC_URL")))
# chain_id = 4
#
# For connecting to ganache
w3 = Web3(Web3.HTTPProvider("http://0.0.0.0:8545"))
chain_id = 1337
my_address = "0x907c508f6bd46FE03630F03A768c2Eed34D0D0FD"
private_key = "0x82b605cec45cbbf9ff3f7deac74c18cfc63a999771e5cb61c2422290f8582f93"
# Create the contract in Python
SimpleStorage = w3.eth.contract(abi=abi, bytecode=bytecode)
# Get the latest transaction
nonce = w3.eth.getTransactionCount(my_address)
# Submit the transaction that deploys the contract
transaction = SimpleStorage.constructor().buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce,
}
)
# Sign the transaction
signed_txn = w3.eth.account.sign_transaction(transaction, private_key=private_key)
print("Deploying Contract!")
# Send it!
tx_hash = w3.eth.send_raw_transaction(signed_txn.rawTransaction)
# Wait for the transaction to be mined, and get the transaction receipt
print("Waiting for transaction to finish...")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_hash)
print(f"Done! Contract deployed to {tx_receipt.contractAddress}")
# Working with deployed Contracts
simple_storage = w3.eth.contract(address=tx_receipt.contractAddress, abi=abi)
print(f"Initial Stored Value {simple_storage.functions.retrieve().call()}")
greeting_transaction = simple_storage.functions.store(15).buildTransaction(
{
"chainId": chain_id,
"gasPrice": w3.eth.gas_price,
"from": my_address,
"nonce": nonce + 1,
}
)
signed_greeting_txn = w3.eth.account.sign_transaction(
greeting_transaction, private_key=private_key
)
tx_greeting_hash = w3.eth.send_raw_transaction(signed_greeting_txn.rawTransaction)
print("Updating stored Value...")
tx_receipt = w3.eth.wait_for_transaction_receipt(tx_greeting_hash)
print(simple_storage.functions.retrieve().call()) When I run python deploy.py, Visual Studio returns:
I would be very grateful for any assistance. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments 4 replies
-
Could you try copy pasting from the github repo code? I don't see anything glaring... |
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
Beta Was this translation helpful? Give feedback.
-
It would appear to me that there is an issue with Visual Studio Code for Mac: In the end, I installed "Python Environment Manager" v1.0.3 in Visual Studio Code I then needed to run:
It finally worked!
So the issue was indeed with VSC using the wrong version of python and it was thanks to the Python Environment Manager that I was finally able to get it to work Many thanks for all your assistance. |
Beta Was this translation helpful? Give feedback.
It would appear to me that there is an issue with Visual Studio Code for Mac:
"On macOS, selecting venv interpreter actually selects system interpreter #13603"
microsoft/vscode-python#13603
In the end, I installed "Python Environment Manager" v1.0.3 in Visual Studio Code
and then I had to chose the correct VirtualEnvWrapper:
I then needed to run:
It finally worked!
So the issue was indeed with VSC using the wrong v…