posted

0 Comments

Welcome to BeerWars, a demonstration of how to develop a “Dapp” (or blockchain smart contract “distributed application”) on VMware’s blockchain. This app simulates an exchange where people are able to “buy”, “trade”, and “consume” products (say beer). The app tracks the stock of each person as they perform each action.

Prerequisites

To use this demo, you will need two things: a VMware blockchain deployment, and the demo code.

Getting a VMware blockchain deployment

Your blockchain deployment should have been created for you. You will need the deployment address of the deployment.

Getting the demo code

The BeerWars demo is provided as a docker image, which includes the smart contract code, and the necessary libraries for deploying and interacting with the contract on the blockchain.

First, make sure you have Docker installed. Instructions are available for doing this in Windows, Mac, and Linux here: https://store.docker.com/search?offering=community&type=edition

Second, make sure you have an account on Dockerhub. Signing up for an account is free: https://hub.docker.com

With Docker installed, and your Dockerhub login ready, open a terminal, and sign into Dockerhub:

Once logged in, you can launch a new container based on the demo image. The following command starts the container in interactive mode, so that you can use the files and utilities it contains:

You are now inside the Docker container.

If you type ls at the prompt, you will see the contract source file, and some other support files:

Stopping and starting the container (OPTIONAL)

To stop and disconnect from this container, type control-d. If you want to restart this container and pick up where you left off, use the following command, replacing BEERWARS_HASH with the hash found between the @ and the : in the docker prompt that was displayed while you were connected. This was b8136d3deacf in the example above:

You can also start over with a fresh container by executing the docker run command again.

Using the BeerWars demo

You must have the deployment address for an instance of VMware’s blockchain. This is referred to as DEPLOYED_ADDRESS in this document.

In this demo, /source folder is the home directory. All instructions must be executed from within the home directory, unless mentioned otherwise.

Compiling the smart contract

Compile the demo contract to obtain the binary code for the smart contract, using the following command: (NOTE – You could get a few warnings; as long as the two additional files mentioned below are created, you are good to continue)

This will generate .abi and .bin files which can be verified :

Deploying the smart contract

Open Node.js and import the contract setup script.

The console might print ‘undefined’ when a variable is defined; this is not a declaration failure. In order to display the contents loaded into the variable, just type the name of the variable and hit ENTER.

We will now deploy the contract. We will start with three people in our exchange, namely, ‘alpha’, ‘bravo’ and ‘charlie’.

Setup and deploy the smart contract with the following, after replacing DEPLOYED_ADDRESS with the deployment address:

(NOTE – The DEPLOYED_ADDRESS must be of form ‘https://mgmt.blockchain.vmware.com/blockchains/ 80bf7abc-c1ec-4609-90ed-ff68f07d8f64/api/concord/eth’. Note the inclusion of https:// in the beginning and the path /api/concord/eth in the end. Replace USER_NAME and PASSWORD with your credentials. Remember that all these 3 fields are strings are to be enclosed within ’’)

(NOTE – Wait for the console to display ‘Contract has been deployed’. This generally takes upto 5 seconds. As this is an asynchronous call, you might have to hit ENTER once to get the Node.js prompt after the deployed message is printed.)

(NOTE – A promise is created with the expectation of a result in future. Here we store get the address the contract has been deployed and store it in the variable ‘contract_address’. The mention of a ‘debugDomainError’ in the promise does NOT imply failure.)

Obtain the contract instance (used to interact with the smart contract) using the following:

(NOTE – Remember that the console printing ‘undefined’ is expected behavior after initialization. Type the variable name, i.e. contract_instance, and hit ENTER to check its contents.)

Interacting with the smart contract

The following are example invocations of the smart contract method. For ease of use, we have wrapped the smart contract methods with helper methods (in BeerWars.js).

Check the number of beers with a person (say ‘alpha’):

‘bravo’ decides to buy a beer: (NOTE – Here, since we are making changes to the state, a transaction address is returned)

Now, checking the number of beers for ‘bravo’ should return ‘1’:

‘bravo’ decides to give his beer to ‘charlie’:

Now, ‘charlie’ must have the beer originally bought by ‘bravo’:

Let ‘charlie’ drink the beer. But, did ‘charlie’ really drink the beer?

Invoking a method on an invalid person will result in an error:

Welcome ‘delta’ to the party and check the number of beers:

‘alpha’ has to leave the party.