[Framework Tutorial for Ethereum #4] - Create new assets

[Framework Tutorial for Ethereum #4] - Create new assets

The goal of this Ethereum tutorial series is to show you how to use the 0xcert Framework for building a dapp on the Ethereum blockchain plus creating and managing non-fungible tokens.

(Prefer building on Wanchain? This tutorial should help.)

This episode follows the previous tutorial and assumes you have Ethereum Ropsten node up and running.

In this tutorial series we will:

  • Connect Ethereum with the 0xcert Framework
  • Use the 0xcert Framework for back-end development
  • Build a simple front-end to test the API

At the end of the tutorial series, you will be able to deploy, create, transfer assets on the Ethereum network.

To achieve this, we will use the following technologies:

  • Ethereum Ropsten testnet
  • TypeScript
  • Node.js
  • Express
  • 0xcert Framework
  • jQuery

It is assumed that you know the basics of JavaScript, Node.js, jQuery, HTML, and use of the terminal.

All the code used for the examples can be found on GitHub.

Build your own dapp

Issue/Create new assets

Now that we have an asset ledger, we can start creating new assets. If we look at the documentation for creating assets, we see that we need the ID of asset ledger (which is the contract address that we just copied in the previous step), and asset data that consists of a unique ID, an imprint (asset proof), and the address of the receiver of the asset. Just as before, we will create a POST call where all the data will be available as input parameters:

app.post('/create', async (req, res) => {
  const ledger = AssetLedger.getInstance(provider, req.body.assetLedgerId);
  const mutation = await ledger.createAsset({
    receiverId: req.body.receiverId,
    id: req.body.id,
    imprint: req.body.imprint
  });
  res.send(mutation.id);
});

To test if this works, we will create a simple html page:

<input id="address" type="text" placeholder="Asset ledger Id" /><br/>
<input id="id" type="text" placeholder="Id" /><br/>
<input id="recipient" type="text" placeholder="Recipient address" /><br/>
<input id="imprint" type="text" placeholder="Imprint" /><br/><br/>
<button id="create">Create</button>
<p id="createConsole"></p>


Now we add the JavaScript code that will call the API:

$.ajax({
  contentType: 'application/json'
});
$(function(){
  $('#create').click(function(){
    $.post(
      "http://localhost:3000/create",
      {
        assetLedgerId: $('#address').val(),
        receiverId: $('#recipient').val(),
        id: $('#id').val(),
        imprint: $('#imprint').val()
      },
      function (response) {
        $('#createConsole').html('<a href="https://ropsten.etherscan.io/tx/' + response + '" target="_blank">Check transaction on Etherscan</a>.');
      }
    );
  });
});

Let's reset our server and try if everything works. Here is simple test data that you can use:

Asset ledger ID (contract address): copy from step 3
ID: 1
Recipient address: Let's send it to ourselves, so use your address here
Imprint: 0000000000000000000000000000000000000000000000000000000000000000

Imprint represents the proof of our asset. For this example, we didn't create an imprint since we're just using mock data. If you would like to know how to actually generate a real imprint, read more here.

Note that if you are creating multiple assets, each must have a unique ID, otherwise mutation will fail.

If we did everything well, we should see the transaction on Etherscan. Since we are now the owner of the asset with ID 1, we can transfer it to someone else.



If you have questions about the code or you experience trouble developing your dapp, you can ask our developers for help in our Gitter channel.

What comes next?

Now that we have created our assets via our dapp, we can start transferring them.

Start building

Framework tutorial #1: Run and prepare Geth node for back-end integration
Framework tutorial #2: Set up Express server with Typescript for dapp backend
Framework tutorial #3: Deploy asset ledger
Framework tutorial #5: Transfer assets

Newsletter