Using Azure Blockchain Development Kit with OpenZeppelin Contracts

The Azure Blockchain Development Kit for Ethereum extension for Visual Studio Code (VS Code) supports OpenZeppelin Contracts. :rocket:

You can read all the details in the Microsoft announcement:


The following guide will walk through using the Azure Blockchain Development Kit for Ethereum with OpenZeppelin Contracts to create an ERC20 token, including deploying and interacting.

Prerequisites

If you haven’t already, install VS Code as per the instructions on the website:

Setup

Install Azure Blockchain Development Kit for Ethereum extension by selecting install from the website:
https://marketplace.visualstudio.com/items?itemName=AzBlockchain.azure-blockchain

Create a new project

Create an empty directory for your new project e.g. mkdir demo
Open VS Code
From the VS Code Command Palette (View menu -> Command Palette), type Azure Blockchain: New Solidity Project
Then Select type of solidity project choose Create basic project
Then choose the empty directory you created previously.
This will do a truffle unbox of Azure-Samples/Blockchain-Ethereum-Template so could be a good time to make a cup of tea.

Add OpenZeppelin Contracts

Select an existing contract in the contracts directory e.g. Migrations.sol and open the context menu (e.g. in Windows right click).
Choose Add contracts from OpenZeppelin
Then Select category for downloading and choose Tokens/ERC20
This will download the OpenZeppelin Contracts for ERC20 Tokens in contracts/openZeppelin.
The contracts in contracts/openZeppelin are read only so that you can’t save any changes.

Create an ERC20 Token

We will now create a simple ERC20 token where the total supply will be issued to the deployer of the contract.

In the contracts directory create a file named SimpleToken.sol.
Paste the following smart contract code into SimpleToken.sol.
We can see that the contract imports from the OpenZeppelin contracts we just added to our project.

pragma solidity ^0.5.0;

import "./openZeppelin/token/ERC20/ERC20.sol";
import "./openZeppelin/token/ERC20/ERC20Detailed.sol";

/**
 * @title SimpleToken
 * @dev Very simple ERC20 Token example, where all tokens are pre-assigned to the creator.
 * Note they can later distribute these tokens as they wish using `transfer` and other
 * `ERC20` functions.
 */
contract SimpleToken is ERC20, ERC20Detailed {

    /**
     * @dev Constructor that gives msg.sender all of existing tokens.
     */
    constructor () public ERC20Detailed("SimpleToken", "SIM", 18) {
        _mint(msg.sender, 10000 * (10 ** uint256(decimals())));
    }
}

Migrations

In the migrations directory, we can replace the contents of 2_deploy_contract.js with the following:

var SimpleToken = artifacts.require("SimpleToken");
module.exports = deployer => {
    deployer.deploy(SimpleToken);
};

We don’t need 99_deploy_openzeppelin.js so this can be deleted.

Deploy

First start a local blockchain in a terminal (ganache-cli -d).

From the Command Palette, type Azure Blockchain: Deploy Contracts (or select one of the contracts in the contracts directory e.g. SimpleToken.sol and from the context menu choose Deploy Contracts).

You will be asked Some contracts have parameters required for deploy. Do you want to specify them?. You can select No and delete the newly created 99_deploy_openzeppelin.js.

Then select development 127.0.0.1:8545 to deploy to a local test network (ganache-cli)

Interact

Select SimpleToken.sol in the contracts directory and from the context menu choose Show Smart Contract Interaction Page
This will display a Smart Contract UI for the SimpleToken.

Currently read functions that take parameters such as balanceOf and allowance are not supported.

Change the Contract Action to transfer.
Set the recipient to an address e.g. 0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0
Set the amount e.g. 23
Press the Execute button to create a transaction.


Next steps

We can also deploy to public networks such as public testnets or mainnet using Infura.

See the Infura blog post for instructions:

4 Likes

Thanks for this guide, confirming it works.
Do we know when they will begin supporting other functions e.g. balanceOf allowance

1 Like

Hi @pkr,

The Smart Contract Interaction Page is nice.
I don’t know when they will support read functions that take parameters. I will see if I can find out.

1 Like

transfer works, is there a way to look up the balance of the account where the tokens were sent?

Hi @pkr,

In VS Code in a terminal you could run truffle console, get the token and then check the balance of an account.

I don’t know if there is an easier way of doing this, if you find one, let the community know.

$ truffle console
truffle(development)> token = await SimpleToken.deployed()
undefined
truffle(development)> (await token.balanceOf("0xFFcf8FDEE72ac11b5c542428B35EEF5769C409f0")).toString()
'23'

Sorry I haven’t been following the threads over here, but love this feedback. We primarily surfaced the functions and weren’t taking into account the public variables “wrappers” to allow you to get these public properties. Our general error there was about complex data structures. We support the intrinsic variables (uint, string, etc.) but structures and reference types were a bit more work. I will bump this back to our work stream to continue work. We have been getting a bit more feedback that folks are using this interaction page more.

Thanks again for the feedback!

1 Like

Hi @windozer,

Thank you for the awesome job with the Blockchain Development Kit for Ethereum and the support for OpenZeppelin Contracts.

I have submitted a review and would encourage the community reading this to do the same: https://marketplace.visualstudio.com/items?itemName=AzBlockchain.azure-blockchain&ssr=false#review-details

I have created some issues based on my experiences. Really appreciate the work that you are doing. :pray:

Very cool, much thanks for this feedback, we will get this resolved. Always happy to support the OpenZeppelin community!

1 Like