Lab 3: Deployment and interactions with smart-contracts#
In this lab, we are going to interact with out contract from python.
Interacting with our first contract from lab 2#
In this section, we are going to call our contract from lab 2 directly from python. To do so, we need two things: - The contract’s address, to now where to call the functions - A way to know what functions are available and how to call them
The contract ABI#
As it is, your python program has no idea how to call your contract. Especially, what functions are available.
To do so, we need to provide it your contract’s Application Binary Interface (ABI). You’re contract ABI is a file that list all available functions and what parameters you need to provide. Given a contract ABI, python is (with the web3 library) available to generate every function calls to your contract, so you can easily create a python application that can interact the contract.
To get your contract’s ABI, you need to have it compiled first on Remix. Go on the Solidity compiler window, and click on ABI to copy the contract ABI. Paste it in a new file called “ABI.json”.
Tip
Use version 0.8.0 of the Solidity compiler
Task
Try to have a look at this file and try to find where your functions have been defined.
The contract address#
Now that we have a way to tell python what functions are in our contract, we need to tell it where to actually send the transactions. Remember that a contract has an address where you can call it.
To get a contract adress, you need to deploy it first. This should have been done in the previous lab. You can go on the deployment window and click on your deloyed contract to copy its address.
Task
Get the provided python script for this lab and modify the CONTRACT_ADDRESS variable to paste your own contract
address. Try to run it. It should print the value stored in the name variable in your deployed contract.
Tip
If it does not print anything after “It returns:”, make sure you have actually called “setName” once. You can still do it in Remix.
Complete the script#
Task
Now, try to implement calls to the various functions you have in your contract.
Question
Some functions (setName, setCapacity) needs to be called with transact(), some (getName, getCapacity) needs to be called with call(). Can you guess why and what are the differences between those functions?
Tip
If you want to make your program a bit more interactive, you can use the “input” python built in function to ask the user for an input while executing the script. This can be nice for the setName and setCapacity functions!
The documentation can be found here: https://web3py.readthedocs.io/en/v5/contracts.html#id3.