Deploying ERC-1155 tokens with Enjin Coin via GraphiQL or Unity 3D.

[From the Archives] I finally was able to get some test Ether and test ENJ on the Kovan network last night, so today I was able to work on figuring out how to deploy some ERC-1155 tokens to my dev wallet.

[From the Archives]

I finally was able to get some test Ether and test ENJ on the Kovan network last night, so today I was able to work on figuring out how to deploy some ERC-1155 tokens to my dev wallet.

Since the process is a little complicated, I thought it would be best to document my process for those that may run across this in the future along with my future self when I forget how to do this.  Buckle up and get your crypto dev hard hats on because we are going in deep.

First I will take you through the process via the GraphiQL endpoint, then I will go through the easy way with the Unity 3D Enjin SDK that was recently released.  


First off you will need some art, and a place to host some JSON files.  I recommend using a 512x512 pixel PNG image as when I used a size of 1000x1000 pixels, it never seemed to come through in the wallet.  

I originally tried hosting the files in a git repo on github, but ended up moving over to Imgur and MyJSON.com.  A funny thing happened while creating my first token, I originally tried using jsonbin, but they seemed to fall over and go offline today while I was working with it [note to the admins there, hopefully that wasn't me, sorry about that].  

So we have our cool image for the tokens, and a place to host them. Now we need to build a JSON file to point out the metadata correctly.  I used a nifty tool built by a dev in the Enjin Devs Telegram channel to build mine.  Here's the link: https://metadata-editor.enj1155.com/

The GraphiQL Way

You will need an account on the Dev site for Enjin, so get that setup and remember your password as you will need it for the GraphiQL login.  You will also need to setup your game under the tab Applications.

app

Once that is all set, open the Kovan GraphiQL endpoint: https://kovan.cloud.enjin.io/graphiql

Then authenticate with the following query on the left side:

query login{
  EnjinOauth (
    email: "YOUR_SUPER_EMAIL",
    password: "YOUR_SUPER_PASSWORD"
  ) {
    id,
    name,
    email,
    access_tokens
  }
}

Then on the right it will return with your access token.  In the dev docs, it explains how to throw your access token in to a cookie, but I did not have to do that.  

You will also need to link your app to the dev wallet on your tablet or phone in addition to allowing it to spend ENJ for you.  The docs were pretty straight forward on that, so I won't replicate them [plus I didn't take screenshots, sorry]: https://kovan.cloud.enjin.io/docs/wallet-setup

Ok, now you have been authenticated, and your dev wallet is linked to the app. Remember your ID as you will need it for later queries.  It is the 'identity_id' used later.

If you need to get it again you can run the 'viewIdentities' query again:

query viewIdentities{
  EnjinIdentities (
    pagination: {
      page: 1,
      limit: 50
    }
  ) {
    id
    app {
      name
    }
    linking_code
    enj_allowance
    ethereum_address
  }
}

Now on to the fun stuff.

First you need to create a token request, then you mint the tokens, then view your newly minted token, and finally update the metadata.  The process seemed a bit odd, but that's how I got it working.

Creating a token request:

Here was my first attempt.  Well really, something like fifth attempt, but that was due to me not knowing what ID to put in.

[note: 9999 is where you put your ID]

mutation createTokenRequest{
  CreateEnjinRequest (
    identity_id: 9999,
    type: CREATE,
    create_token_data: {
      name: "The Utopia Machines Logo",
      totalSupply: 10,
      initialReserve: 5,
      supplyModel: FIXED,
      meltValue: "10000000000000000000",
      meltFeeRatio: 5000,
      transferable: PERMANENT,
            transferFeeSettings: {
        type: PER_TRANSFER,
        token_id: "0",
        value: "10000000000000000000"
      }
      nonFungible: false
    }
  ) {
    id,
    encoded_data
  }
}

So let's break down the confusing parts.  The meltValue and transferFeeSettings.  You will notice I put 19 zeros behind the 1 on both of those settings. "10,000,000,000,000,000,000"!

That means it takes 10 ENJ to transfer AND I also locked up 10 ENJ in to each token. WHOOPS!  Good thing I started on the testnet, eh? I will get the locked up ENJ back after melting them, just a steep learning curve, so pay attention before you go to production.

Mint Your Token

Next you will need to actually mint some tokens in to reality.

In the recipient_address_array, put your dev wallet ENJ address, and in the value_array put how many you'd like to mint.  You can add other addresses and values to the arrays, but I just kept it simple and sent them to myself.

[note: Remember we are on the Kovan testnet, so use KENJ and KETH]

mutation mintFungibleItems {
  CreateEnjinRequest (
    identity_id: 9999,
    type: MINT,
    mint_token_data: {
      token_id: "1800000000000354",
      recipient_address_array: [
        "YOUR_ETH_ADDRESS"
      ]
      value_array: [
        5
      ]
    }
  ) {
    id,
    encoded_data
  }
}

That will return what looks like some contract bytecode if you are used to working with Ethereum.  The dev wallet will notify you of the changes and you will need to confirm them.  After a few minutes they should show up in your wallet, trust me, just go get a coffee refill and don't sit there swiping down like a mad man like me.

Get the Token ID

Now we need to get the token_id, so we can set the icon and metadata correctly.  You will search by the name of the token you just created.

query viewTokens{
  EnjinTokens (
    name: "The Utopia Machines Logo",
    pagination: {
      page: 1,
      limit: 50
    }
  ) {
    token_id
    name
    creator
    meltValue
    meltFeeRatio
    meltFeeMaxRatio
    supplyModel
    totalSupply
    circulatingSupply
    reserve
    transferable
    nonFungible
    blockHeight
    markedForDelete
    created_at
    updated_at
    availableToMint
    itemURI
  }
}

That returns a bunch of info about your newly minted token in addition to the token_id :

{
  "data": {
    "EnjinTokens": [
      {
        "token_id": "1800000000000354",
        "name": "The Utopia Machines Logo",
        "creator": "YOUR_ETH_ADDRESS",
        "meltValue": "10000000000000000000",
        "meltFeeRatio": 5000,
        "meltFeeMaxRatio": 5000,
        "supplyModel": "FIXED",
        "totalSupply": "10",
        "circulatingSupply": "0",
        "reserve": "5",
        "transferable": "PERMANENT",
        "nonFungible": false,
        "blockHeight": 10741328,
        "markedForDelete": false,
        "created_at": {
          "date": "2019-04-14 14:11:24.000000",
          "timezone_type": 3,
          "timezone": "UTC"
        },
        "updated_at": {
          "date": "2019-04-14 14:11:24.000000",
          "timezone_type": 3,
          "timezone": "UTC"
        },
        "availableToMint": "10",
        "itemURI": ""
      }
    ]
  }
}

Update the metadata

Now that we have the token_id we can update the metadata so that the image and properties show up.

mutation setItemURI{
  CreateEnjinRequest (
    identity_id: 9999,
    type: SET_ITEM_URI,
    set_item_uri_data: {
      token_id: "1800000000000354",
      token_index: 0,
      item_uri: "YOUR_FANCY_JSON_HOST"
    }
  ) {
    id,
    encoded_data
  }
}

That will return what looks like some contract bytecode again if you are used to working with Ethereum.  The dev wallet will notify you of the changes and you will need to confirm them.

After confirming the transactions, I ran the viewTokens query again to see my itemURI updated to where the JSON file is hosted.

Finally, after working with the GraphiQL endpoint, you will have yourself a fancy ERC-1155 token:

token

Deploy a ERC-1155 Token with Unity 3D

So, if you've followed along this far you will realize that this is hard, and you will want an easier way to do it.  

There is.  

Just setup Unity, go to the Asset Store, install the Enjin SDK into your project and then open it up [Window > Enjin SDK].

If you got this far, your wallet will already be linked so you will just login and magically all your settings will be there. You will need to just go to CryptoItems and create one.

unity3d

Here is the Blue Key I made for my game GameGuy using the Unity SDK:

Blue Key

I would highly recommend using the Unity SDK if your are going to be issuing a bunch of tokens as the process is a lot more streamlined than the GraphiQL endpoint.  I am looking forward to a time when there is a plugin for the Godot engine as that will make integrating my future games a lot easier.

All in all a fantastic experience this morning as I gained a ton of knowledge.  I look forward to integrating GameGuy with Enjin Coin so that players will get some special treats while playing.