Home

Create an Asset on Value Chain

This example creates an asset on the Value Chain and publishes it to the Dijets platform. The first step in this process is to create an instance of DijetsJS connected to our Dijets platform endpoint of choice. In this example we're using the local network 12345.

import { Dijets, BN, Buffer } from "dijets"
import {
  AVMAPI,
  KeyChain,
  UTXOSet,
  UnsignedTx,
  Tx,
  InitialStates,
  SECPMintOutput,
  SECPTransferOutput,
} from "dijets/dist/apis/avm"
import {
  PrivateKeyPrefix,
  DefaultLocalGenesisPrivateKey,
} from "dijets/dist/utils"

const ip: string = "localhost"
const port: number = 9650
const protocol: string = "http"
const networkID: number = 12345 // Dijets Mainnet ID is 1, we want to override that default for local network id
const dijets: Dijets = new Dijets(ip, port, protocol, networkID)

Import the Pre-funded Address#

Next, lets get an instance of the Value Chain local keychain. The local network 12345 has a pre-funded address which you can access with the private key PrivateKey-ewoqjP7PxY4yr3iLTpLisriqt94hdyDFNgchSxGGztUrTXtNN. Lastly get the pre-funded address as a Buffer and as a string.

const xchain: AVMAPI = dijets.XChain()
const xKeychain: KeyChain = xchain.keyChain()
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`
xKeychain.importKey(privKey)
const xAddresses: Buffer[] = xchain.keyChain().getAddresses()
const xAddressStrings: string[] = xchain.keyChain().getAddressStrings()

Prepare for the Mint Output#

Now we need to create an empty array for the SECPMintOutput. We also need a threshold and locktime for the outputs which we're going to create. Each Value Chain transaction can contain a memo field of up to 256 bytes of arbitrary data.

const outputs: SECPMintOutput[] = []
const threshold: number = 1
const locktime: BN = new BN(0)
const memo: Buffer = Buffer.from(
  "The Value Chain VM utility method buildCreateAssetTx can be used to create a smart digital asset"
)

Describe the New Asset#

The first step in creating a new asset using DijetsJS is to provide the specifics of the asset. Let's give the asset a name, a ticker symbol, and a denomination.

const name: string = "MimoToken"
const symbol: string = "MTT"
const denomination: number = 3

Set Up Async / Await#

The remaining code will be encapsulated by this main function so that the async / await pattern can be setup and utilised.

const main = async (): Promise<any> => {}
main()

Fetch the UTXO#

Pass the xAddressStrings to xchain.getUTXOs to fetch the UTXO.

const avmUTXOResponse: any = await xchain.getUTXOs(xAddressStrings)
const utxoSet: UTXOSet = avmUTXOResponse.utxos

Creating the Initial State#

We want to mint an asset with 786 units held by the managed key. This sets up the state that will result from the Create Asset transaction.

// Create outputs for the asset's initial state
const amount: BN = new BN(786)
const vcapSecpOutput: SECPTransferOutput = new SECPTransferOutput(
  amount,
  xAddresses,
  locktime,
  threshold
)
const initialStates: InitialStates = new InitialStates()

// Populate the initialStates with the outputs
initialStates.addOutput(vcapSecpOutput)

Create the Mint Output#

We also want to create a SECPMintOutput so that we can mint more of this asset later.

const secpMintOutput: SECPMintOutput = new SECPMintOutput(
  xAddresses,
  locktime,
  threshold
)
outputs.push(secpMintOutput)

Creating the Signed Transaction#

Now that we know what we want an asset to look like, we create a transaction to send to the network. There is a Value Chain VM helper function buildCreateAssetTx() which does just that.

const unsignedTx: UnsignedTx = await xchain.buildCreateAssetTx(
  utxoSet,
  xAddressStrings,
  xAddressStrings,
  initialStates,
  name,
  symbol,
  denomination,
  outputs,
  memo
)

Sign and Issue the Transaction#

Now let's sign the transaction and issue it to the Dijets network. If successful it will return a CB58 (concatenation of the data bytes and a checksum) serialized string for the transaction ID.

info

CB58 is a format used to represent keys, addresses, and other binary values in web wallets and APIs. CB58 is similar to Base58Check but uses a different checksum algorithm.

Now that we have a signed transaction ready to send to the network, let’s issue it!

const tx: Tx = unsignedTx.sign(xKeychain)
const txid: string = await xchain.issueTx(tx)
console.log(`Success! TXID: ${txid}`)

Get the Status of the Transaction#

Now that we sent the transaction to the network, it takes a few seconds to determine if the transaction has gone through. We can get an updated status on the transaction using the transaction ID through the AVM API.

// returns one of: "Accepted", "Processing", "Unknown", and "Rejected"
const status: string = await xchain.getTxStatus(id)

The statuses can be one of Accepted, Processing, Unknown, and Rejected

  • "Accepted" indicates that the transaction has been accepted as valid by the network and executed
  • "Processing" indicates that the transaction is being voted on.
  • "Unknown" indicates that node knows nothing about the transaction, indicating the node doesn’t have it
  • "Rejected" indicates the node knows about the transaction, but it conflicted with an accepted transaction

Identifying the Newly Created Asset#

The Value Chain uses the transaction ID of the transaction which created the asset as the unique identifier for the asset. This unique identifier is henceforth known as the "AssetID" of the asset. When assets are traded around the Value Chain, they always reference the AssetID that they represent.

info

The AssetID of Dijets native cryptocurrency DJTX is wjcHr7ng1qPXeJm5Mh3HzQxqz2S9AGo9UVP78jxh5zfirZxY7

Need some help?

You can join Dijets Support Space for a live chat with one of the team members. Join us on Qowalts.