erc20 rsk openzeppelin logos

ERC20 Token Development on RSK with OpenZeppelin and Web3.py

Reading Time: 8 minutes

  •  
  • 2
  •  
  • 52
  •  
  •  
  •  
    54
    Shares

Motivation

In my last article, I’ve shown you how to write a Solidity ERC20 Token for the RSK Mainnet, how to import and use OpenZeppelin libraries and contracts, and how to use Truffle to deploy and interact with our contract.

Although we succeeded in our quest and accomplished our objectives using Truffle, eventually this suite might present failures when you are sending transactions, deploying or managing accounts. In our case, while following the previous article instructions, I’ve had problems managing newly created accounts in Truffle and sending transactions.

The first problem that I had was the freezing of part of the funds in SBTC that the RSK Labs Team gave me to test the Mainnet (thanks to Maximiliano). Although I’d unlocked the account, the node asked me anyway for the private key of the account created with (as we have seen in the first article):

For that reason, and for the safety of the few SBTC that I had left, I added the suicide function to the contract in case something went wrong while doing the article.

The second problem appeared when I tried to interact with the already deployed contract. While sending a transaction, Truffle sends the body of it to the node with the data and the rest of the parameters (as I watched live in the rsk.log file). However, after twenty minutes the transaction was never included in any block and neither the receipt, nor its hash, were known to the node.

The way I worked around this was to configure the cors parameter in the node.conf file to allow Metamask to connect to the node. Then, the transaction was sent by encoding the function in question and its parameters to a hex string and entered in the data field. After sending it with the same nonce value as the transaction sent with Truffle, it was finally included.

Don’t get me wrong, the Truffle suite is a great tool for developers, but if you can’t live with some problems every now and then while developing smart contracts, stay focused and continue reading.

The tool that I’ll be discussing here is the Python version of Web3: Web3.py.

Introduction

Since rskj behaves just like an Ethereum node (i.e. it exposes the same JSON-RPC API), we can use other implementations to interact with it. The most popular version of Web3 is the one written in JavaScript (Web3.js). But since November, there has been a higher rate of updates in the Web3.py project, improving its usability dramatically. This version also allows a more friendly interaction experience than Web3.js.

In this article we’ll be discussing deployment and interaction of the same ERC20 Token contract used in the last article, to compare the usability with both tools.

Since rskj 4.2, the RSK Testnet is back online. For that reason, and because we couldn’t use it before, we are going to deploy and test our contracts on that network for this article.

Changing to Python

Web3.py is written in Python3. If you’ve never used Python before, you probably need some packages. Otherwise, when we install Web3.py in our environment, we will have errors during the compilation. We install them with:

Setting up the environment

We want to keep separate the instance of Web3.py from the rest of our personal Python modules. To do so, we need to create a virtual environment of Python with:

Then, we activate the instance with:

In the terminal, we should be able to see the command prompt as:

That means that our virtual environment for Python is now active. Now, we will install the libraries of the project with:

You shouldn’t have any errors.

To improve the interpreter’s appearance, I recommend to install IPython (no, not from the App Store) with:

If we want to have a break and exit the virtual environment, we can do so with:

We are going to use the Test network to deploy our contracts, so feel free to check again the first article in which we’ve discussed how to change networks (and change it yourself! ; ) ).

Now, we have a suitable environment to start our scripts.

Installing SOLC

It’s not Python‘s job to compile our .sol (Solidity contract code). That’s an assignment for our Solidity compiler: Solc. To install the latest stable version of it, we do it with:

With that, we should be able to compile our contracts like this:

where <contract>.sol is the Solidity code file and <output-dir> is where our built ABIs and bytecodes files will be.

Startup Setup

Once we open Python from our virtual environment, we need to import libraries such as Web3 and Json, but before that, there is an issue that must be taken care of. In some cases, even with Python3, the autocomplete with double TAB does not work. To solve this, you need to import some libraries doing the following:

After that, we should have this feature activated. Now, to import the rest of the libraries, we just simply do:

Then we are ready to connect Python to our node, which you’ve already run, I presume. Otherwise, we start it with:

The connection object is instanced in the following way:

where MACHINE and PORT are the IP, or name, and port that we’ve assigned in the node.conf file. By default, it’s localhost:4444.

To test if everything worked successfully, we check the block number in the Python interpreter like this:

Now, we proceed to deploy the contract.

Deployment of the ERC20 Token with Web3.py

We are going to deploy the ERC20 Token contract based on the OpenZeppelin libraries. To be consistent with my previous articles I rename the two accounts as before, typing:

In another terminal, we go to the folder where the contract code is, and then we compile it using Solc like this:

Inside the Compiled folder we find not only the ABI and bytecode of our contract, but also its compiled dependencies. Nevertheless, we only need the ones from the main contract.

To import both files into the Python VM, we can do it like this:

Then, we can create the instance of the contract, giving it the ABI and the bytecode with:

And after that, we create (and deploy!) the contract typing:

We save the hash of the transaction for two reasons: to check the status, address and more parameters, and because we can use it with the function waitForTransactionReceipt to know when the contract is mined.

To calculate the address in which the contract will be deployed, we need the address used for deploying the contract, acc0, and the nonce of the transaction. We have both things, so we import a couple of libraries and then we call the function as following (thanks to M. Stolarz for this finding):

The address is set in our instance like this:

After that, our contract will be written into the blockchain.

One of the faculties of Python is that we can make a script of all the commands and then execute them all together like this:

This means that we can deploy the contract in one line like with Truffle (but knowing what it does! =D ).

Our deployed Token contract of the Test network is now at 0x7C0c436e1E8dCd270a7A306B5AE8A2996f6A25dD .

Interaction with the contract

After we’ve deployed the Token, the instance should be already created to call it easily. To call a function in which the blockchain is not modified, such as a getter, we do:

Where FUNC_CALL is a callable function of the contract that doesn’t modify the blockchain.

If we want to modify the blockchain (to set a value, to do a calculation, etc), the proper syntax is:

Where FUNC_SET is a function of the contract that modifies the blockchain, PARAMETERS are the values that the function takes and TXARG are the arguments for the transaction written like:

We can check that the variable for our name is empty with:

So, as before, if we try to modify the variable without being the owner, acc1, the transaction is unsuccessful.

[Hint: if you get an Internal Server error, your account is probably locked up again. It has a timeout.]

So, if we send the same transaction from acc0 instead of acc1, we obtain:

We see that the status gives us a successful operation.

Now we will focus on the Token’s functions, we check the original balances of our accounts typing:

Now, we make a transfer from the owner account to our second account by executing:

And we get the new balances:

Moving on to next function, we check that acc0 doesn’t have the allowance on behalf of acc1

So, to grant a limit of 10 Tokens to acc0 from acc1, you have to do it with:

Let’s spend on behalf of acc1:

We see that the transaction was successful, as expected.

Finally, we send a transaction to the contract without calling any function, which will active the fallback function (default behaviour):

This will warn us that we did something wrong or that the function doesn’t exist.

Conclusions

To add to the previous article achievements, in this tutorial we have learn how to:

  • Set up a proper developer virtual environment,
  • Install the Solc compiler,
  • Compile our contracts with Solc,
  • Install and configure Python packages,
  • Connect Python to our node,
  • Deploy the compiled contract to the Test network using Python,
  • Interact with the contract using Python,
  • Write automatic Python scripts to load the compiled contract and deploy.

We saw that the syntax of both tools are alike. Truffle is easier to use from scratch because putting the contract code in Solidity is enough to make it deployable from the suite. With two simple commands, your Smart Contract is ready to go. But because we had issues using the Truffle suite when we sent transactions, a change was needed.

The Web3.py implementation is way better for a developer at the time of writing. Not only did it retrieve the funds from a stuck account made in Truffle, but also has a newer version of Web3 and allows non-trivial scripts to be executed. Operations in Truffle may sometimes be more complex than necessary. Yes, maybe you need to set up more packages and libraries from the start, not taking into account that you need to compile the code yourself, but in the long run it seemed to me that this tool was more stable than Truffle to develop contracts.


  •  
  • 2
  •  
  • 52
  •  
  •  
  •  
    54
    Shares