Home

Intrachain Transfers

Optional Prerequisites:#

  1. You have completed the tutorial for "How to run a Dijets Node"

  2. You have completed the tutorial for "Funding a Test Account"

  3. You have some balance of Test DJT on any of the Value, Utility or Method Chain.

  4. You have Go language installed on your system with $GOBIN set to be in your path.

Introduction#

Please note that this article shows how to transfer DJT programmatically between any two chains on Dijets.

Getting Started#

To use DijetsJS, you can clone the repo:


_10
git clone https://github.com/Dijets-Inc/dijetsJs.git

or add it to an existing project:


_10
yarn add --dev dijetsJs

For this tutorial we will use ts-node to run the example scripts directly from the dijetsJS directory.

In order to send DJT, you need to have some DJT. You can use a pre-funded account on local network or get testnet DJT from the Dijets Faucet, which is an easy way to get to play around with Dijets. After getting comfortable with your code, you can run the code on Mainnet once you have made the necessary/required changes.

Transferring DJT Using DijetsJS#

The easiest way to transfer DJT between chains is to use DijetsJs which is a programmatic way to access and move DJT.

DijetsJs allows you to create and sign transactions locally which is why it is the recommended way to transfer DJT between chains. We are moving away from using Dijets Node's keystore because it requires you to keep your keys on a full node which makes them a target for malicious hackers and a security concern.

Example Code#

Following files can be found under the examples directory of DijetsJs project.

Transfer From >> ToExportImport
Value Chain >> Utility ChainValue Chain: Export DJT to Utility ChainUtility Chain : Import DJT from Value Chain
Value Chain >> Method ChainValue Chain : Export DJT to Method ChainMethod Chain : Import DJT from Value Chain
Method Chain >> Value ChainMethod Chain : Export DJT to Value ChainValue Chain : Import DJT from Method Chain
Method Chain >> Utility ChainMethod Chain : Export DJT to Utility ChainUtility Chain : Import DJT from Method Chain
Utility Chain >> Value ChainUtility Chain : Export DJT to Value ChainValue Chain : Import DJT from Utility Chain
Utility Chain >> Method ChainUtility Chain : Export DJT to Method ChainMethod Chain : Import DJT from Utility Chain

Transaction Fees#

Transaction fees are fixed on Value Chain and Method Chain, while dynamic yet (extremely economical) on Utility Chain. When transferring tokens on Utility Chain, please take fee into consideration in calculating total amount to be transferred.

Testnet Workflow#

This tutorial uses Value Chain to Utility Chain transfers as an example. Transferring between other chains are very similar processes.

Transfer from the Value Chain to the Utility Chain#

To transfer a specified amount of tokens from Value Chain to Utility Chain, the token needs to be first exported from the Value Chain to the atomic memory, from where it is then imported into the Utility Chain.

Export DJT From Value Chain to Utility Chain

Select the examples/avm folder to view the DijetsJS Value Chain examples. To export DJT from the Value Chain to the Utility Chain, select avm/buildExportTx-cchain-djtx.ts.

Private Key

Locate this line in the file


_10
const privKey: string = `${PrivateKeyPrefix}${DefaultLocalGenesisPrivateKey}`

and replace this with a private key that you control.


_10
const privKey: string = "<YOUR-PRIVATE-KEY-HERE>"

Network Setting

The following settings work when using a local node started with --network-id=testnet:


_10
const ip: string = "localhost"
_10
const port: number = 9650
_10
const protocol: string = "http"
_10
const networkID: number = 5

However, to connect directly to the Dijets Testnet API server, the following changes are needed:


_10
const ip: string = "testnet.dijets.io"
_10
const port: number = 443
_10
const protocol: string = "https"
_10
const networkID: number = 5

Depending on the networkID passed in when instantiating Dijets, the encoded addresses used will have a distinctive Human Readable Part(HRP) per each network.

Example Address: 5 - X-test19rknw8l0grnfunjrzwxlxync6zrlu33yxqzg0h

For Dijets Testnet, 4 is the correct value to use.


_10
const networkID: number = 4

Set the Correct Amount To Send:

By default the scripts send the wallet's entire DJT balance:


_10
const balance: BN = new BN(getBalanceResponse.balance)
_10
const amount: BN = balance.sub(fee)

To send a different amount, please replace the code above with the following. Below sets a new value of 0.01 DJT (10000000 Gwei). Value is set in Gwei format where 1,000,000,000 Gwei = 1 DJT


_10
const value: BN = new BN("10000000")
_10
const amount: BN = value.sub(fee)

Run the export script:


_10
ts-node examples/avm/buildExportTx-cchain-djtx.ts

This returns:


_10
Success! TXID: 2uQvMcPZjmPXAyvz9cdKBphDDSmnxxx3vsUrxqpj3U92hsfQcc

Verify the Transaction

You can now pass this txID 2uQvMcPZjmPXAyvz9cdKBphDDSmnxxx3vsUrxqpj3U92hsfQcc into examples/avm/getTx.ts, plus other similar network settings, then you can run


_10
ts-node examples/avm/getTx.ts

which returns:


_21
{
_21
unsignedTx: {
_21
networkID: 5,
_21
blockchainID: '2JVSBoinj9C2J33VntvzYtVJNZdN2NKiwwKjcumHUWEb5DbBrm',
_21
outputs: [ [Object] ],
_21
inputs: [ [Object], [Object] ],
_21
memo: '0x41564d207574696c697479206d6574686f64206275696c644578706f7274547820746f206578706f7274204156415820746f2074686520432d436861696e2066726f6d2074686520582d436861696e',
_21
destinationChain: 'yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp',
_21
exportedOutputs: [ [Object] ]
_21
},
_21
credentials: [
_21
{
_21
fxID: 'spdxUxVJQbX85MGxMHbKw1sHxMnSqJ3QBzDyDYEP3h6TLuxqQ',
_21
credential: [Object]
_21
},
_21
{
_21
fxID: 'spdxUxVJQbX85MGxMHbKw1sHxMnSqJ3QBzDyDYEP3h6TLuxqQ',
_21
credential: [Object]
_21
}
_21
]
_21
}

Import the DJT Token From Value Chain to Utility Chain

Select the examples/evm folder to view the DijetsJS Utility Chain examples. To import DJT to the Utility Chain from the Value Chain, select evm/buildImportTx-xchain.ts

Copy the network setting from above into evm/buildImportTx-xchain.ts.

Navigate to this part of the code and ensure that the cHexAddress(Your Utility Chain wallet address) and private key are correct:


_10
const cHexAddress: string = "<YOUR-CCHAIN-WALLET-ADDRESS-HERE>"
_10
const privKey: string = "<YOUR-PRIVATE-KEY-HERE>"

Run the import script:


_10
ts-node examples/evm/buildImportTx-xchain.ts

This returns:


_10
Success! TXID: 2uQvMcPZjmPXAyvz9cdKBphDDSmnxxx3vsUrxqpj3U92hsfQcc

A "Success" message with a Transaction ID signifies the completion of a transfer! You've transferred DJT from the Value Chain to Utility Chain!

Transfer from the Utility Chain to the Value Chain#

To return the DJT back to the Value Chain, you need to do the transfer in the opposite direction.

Export the DJT Token From Utility Chain to Value Chain

Select the examples/evm folder to view the DijetsJS Utility Chain examples. To export DJT from the Value Chain to the Utility Chain, select evm/buildExportTx-xchain-djtx.ts.

Make necessary changes as above for private key and network settings.

You can change the amount of DJT to send by editing the BN variable: djtxAmount. The sample code assigns this as 1e7 or 10000000 (0.01 DJT)

The fee here will only be for exporting the asset. The import fees will be deducted from the UTXOs present on the Exported Atomic Memory, a memory location where UTXOs stay after getting exported but before being imported.


_10
let djtxAmount: BN = new BN(1e7)
_10
let fee: BN = baseFee.div(new BN(1e9))
_10
fee = fee.add(new BN(1e6))

Run the export script:


_10
dijetsjs $ ts-node examples/evm/buildExportTx-xchain-djtx.ts
_10
Success! TXID: UAez3DTv26qmhKKFDvmQTayaXTPAVahHenDKe6xnUMhJbKuxc

Import the DJT Token From Utility Chain to Value Chain

Before we run the example import script, we need to make some changes to the code:

  1. Change the Network Setting to meet Dijets Testnet network requirements.
  2. Import your Private Key by following the steps listed here.
  3. Run the Script!

_10
dijetsjs $ ts-node examples/avm/buildImportTx-cchain.ts
_10
Success! TXID: Sm6Ec2GyguWyG3Li1pARmTpaZ6qLEPuVAHV8QBGL9JWwWAEgM

Mainnet Workflow#

Dijets Testnet workflow above can be adapted to Mainnet with the following modifications:

  • The correct private key.
  • Network setting should be to a Mainnet node, either a local node on Mainnet or Dijets Mainnet API server where dijets.ukwest.cloudapp.azure.com should be used for the ip.
  • const networkID: number = 1 based on this.
  • Set the correct amount to send.
  • The correct receiving address.

Local Workflow#

Start the Local Network#

Follow Create a Local Test Network to start a 5-node local network. Make sure that you get one of the port number by following this. In this tutorial, we will assume one of the ports is 30301.

Locate the Example Code and Make Necessary Changes#

Most of the code are already set to run it on a local network. Do check the following values to make sure they are correct.


_10
const ip: string = "localhost"
_10
const port: number = 30301 // Change this to the correct value
_10
const protocol: string = "http"
_10
const networkID: number = 1337

Then run the export and import scripts to transfer tokens across chains.

Next steps#

At this stage you have a fully functional application!