Logo

Home

Tutorial 1Create Wallet

Tutorial 2Avatars and Smart Token

Tutorial 3Identifiable Token

Tutorial 2 - Avatars and Metaverse Smart Tokens (MST)

In this tutorial you will learn how to

Introduction

Avatars are digital representations of personal identities – digital identitites (DIDs) – that exist on the Metaverse Blockchain. Avatars have unique alphanumeric symbols. An Avatar is attached to exactly one Metaverse address (i.e there can only be at most one Avatar per address). Transfer of tokens can be done by referring to Avatars instead of addresses.

For more info on Avatars, visit this article.

Metaverse Smart Token (MST) is Metaverse’s fungible token standard. MSTs can be seen as sub-currencies on the Metaverse Blockchain. It costs 10 ETP to create an MST. The creation and issuance of an MST must be done through an Avatar (i.e. to issue a token, you can’t just use a Metaverse address that’s not already tied to an Avatar). Again, ETP and other Metaverse Smart Tokens (MSTs) can be sent to Avatars instead of addresses.

There are other things an Avatar can or cannot do and such limitations are defined through a system of Certificates. There are three types of certificates on Metaverse: Domain Certificate, Secondary Issue Certificate and Naming Certificate. For more details on what they do, check out this article.

Hands-on Tutorial

First let’s create an html front-end

<!DOCTYPE html>
<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Tutorial 2</title>
  </head>
  <body>

    <h3> Register Avatar </h3>
    <input placeholder="Avatar Name"></input>
    <label>Avatar Address</label> <select></select>
    <button>Register</button>

    <h3> Issue MST </h3>
    <input placeholder="symbol"></input> <br>
    <input placeholder="max_supply"></input>
    <button>Issue</button>

    <h3> Balances </h3>
    <table>
      <tr>
        <th>MST</th>
        <th>Issuer</th>
        <th>Amount</th>
      </tr>
    </table>

    <h3> Send MST </h3>
    <label></label><select></select>
    <input placeholder ="sendTo"></input><br>
    <input placeholder = "amount"></input><br>

  </body>
</html>

Now, create a function to register your Avatar. Remember, you’ll have to initialize the wallet object first (see the previous tutorial).

async function registerAvatar(avatar_name,avatar_address) {
    let change_address = avatar_address
    let height = await blockchain.height()
    let txs = await blockchain.addresses.txs([avatar_address])
    let utxos = await Metaverse.output.calculateUtxo(txs.transactions, [avatar_address]) //Get all utxo for the avatar address
    let result = await Metaverse.output.findUtxo(utxos, {}, height, 100000000) //Collect utxo to pay for the fee of 1 ETP
    let tx = await Metaverse.transaction_builder.issueDid(result.utxo, avatar_address, avatar_name, change_address, result.change, 80000000, 'testnet')
    tx= await wallet.sign(tx)
    tx = await tx.encode()
    tx = await tx.toString('hex')
    tx = await blockchain.transaction.broadcast(tx)
}

Create a function to look up an avatar address

function getAvatar(avatar) {
  return blockchain.avatar.get(avatar);
}

Create a function to issue an MST

async function issueMST(issuer,symbol,max_supply,decimalPrecision,description){
  let issuingAddress = await getAvatar(issuer)
  let recipient_address = issuingAddress;
  let change_address = issuingAddress;
  let height = await blockchain.height()
  console.log(issuingAddress)
  let txs = await blockchain.addresses.txs(wallet.getAddresses())
  let utxos = await Metaverse.output.calculateUtxo(txs.transactions, wallet.getAddresses()) //Get all utxo
  let result = await Metaverse.output.findUtxo(utxos, {}, height, 1000000000) //Collect utxo to pay for the fee of 10 ETP
  let tx = await Metaverse.transaction_builder.issueAsset(result.utxo, recipient_address, symbol, max_supply, precision, issuer, description, 0,false, change_address, result.change,true,0,'testnet',null,undefined)
  tx = await wallet.sign(tx)
  tx = await tx.encode()
  tx = await tx.toString('hex')
  tx = await blockchain.transaction.broadcast(tx)
  console.log(tx);
}

Create a function to get your MST balance

async function getBalances(){
  let wallet = await Metaverse.wallet.fromMnemonic(mnemonic, 'testnet')
  let height = await blockchain.height()
  let addresses = wallet.getAddresses()
  let txs = await blockchain.addresses.txs(addresses)
  let utxo = await Metaverse.output.calculateUtxo(txs.transactions, wallet.getAddresses())
  let balances = await blockchain.balance.all(utxo, wallet.getAddresses(), height)
  console.log(balances.MST)
}

Create a function to transfer MSTs

async function transferMST(amount,recipient_address,MSTSymbol) {
  amount = parseInt(amount)
  recipient_address = await getAvatar(recipient_address)
  let target = {};
  target[MSTSymbol] = amount
  console.log(target)
  change_address = wallet.getAddress(0)
  let height = await blockchain.height()
  let txs = await blockchain.addresses.txs(wallet.getAddresses())
  let utxos = await Metaverse.output.calculateUtxo(txs.transactions, wallet.getAddresses()) //Get all utxo
  let result = await Metaverse.output.findUtxo(utxos, target, height) //Collect utxo for given target
  let tx = await Metaverse.transaction_builder.send(result.utxo, recipient_address, undefined, target, change_address, result.change)
  tx = await wallet.sign(tx)
  tx = await tx.encode()
  tx = await blockchain.transaction.broadcast(tx.toString('hex'))
  console.log(tx)
}

Connect to Your DApp

To interact with metaversejs in your webapp, you need to reference metaversejs in your HTML.

<script type="text/javascript" src="/dist/metaverse.min.js"></script>

Also reference your tut2.js file.

<script type="text/javascript" src="tut2.js"></script>

Verify that you have connected metaverse to the webapp by opening the browser console and typing “Metaverse”. You should see the Metaverse object come up.

Next connect elements to the js functions and you’re done!

What’s Next?

Continue with the next tutorial and learn how to work with Metaverse Identifiable Token (MIT)