# Asimov Javascript SDK

This document describes how to develop js script to test and deploy contracts on Asimov using high level Javascript API.

# Intall Node.js/NPM

Before running js script, you need to install node.js and npm. Follow instructions on nodejs.org to finish the installation.

# Install Asimov JS SDK

Run npm install @asimovdev/asimovjs to install Asimov JS SDK in your npm project. We provide high level Javascript API through JS SDK. Please visit Javascript API to read api specifications.

# Tutorial

We provide a tutorial.tc.js to cover the most used functions in JS SDK, including send transaction, submit template, deploy contract, execute/vote on contract methods, read contract states and check transaction status.

The smart contract test against in the above script is tutorial.sol.

# Load Modules

Load modules provided by Asimov JS SDK, including Templates, Contracts, Transactions and Setting.

var Templates = require('@asimovdev/asimov-cli/lib/index').Templates
var Contracts = require('@asimovdev/asimov-cli/lib/index').Contracts
var Transactions = require('@asimovdev/asimov-cli/lib/index').Transactions
var Setting = require('@asimovdev/asimov-cli/lib/index').Setting
1
2
3
4

# Setup Setting

Call Setting.init() to setup the rpc server and private key.

let setting = Setting.getInstance()

setting.init({
  url: 'https://test-rpc.asimov.network',
  privateKey: '4cda3c4ac8d4f70850dd2cf468cacf207c8c10736af9ee1ade84de450a5d4f97'
})
1
2
3
4
5
6

We leave other configurations as default in Setting module.

Note private key is not mandentary. You only need to provide it when developing js script. You don't need to provide it when developing web dapps. AsiLink will be invoked in that case. Please read Tutorial - Web DApp.

# Send Transaction

Call Transactions.send() to send a normal transaction on Asimov.

  let transactions = new Transactions()

  console.log('send tx')
  var txId = await transactions.send({
    address: "0x6611f5fa2927e607d3452753d3a41e24a23e0b947f",
    amount: 1
  })
  LoopCheck(txId, 10)
  console.log('transaction ' + txId + ' is confirmed on chain')
1
2
3
4
5
6
7
8
9

Note the unit of amount we use in this script is Satoshi instead of Coin. As a result, 1 represents 0.00000001 Asim.

# Submit Template

Call Templates.submitTemplate() to submit a contract template to Asimov blockchain.

Call Transactions.fetch() to make sure the transaction is confirmed on chain before moving on to next step.

  let templates = new Templates()

  console.log('submit template')
  var txId2 = await templates.submitTemplate({
    path: source,
    templateName: 'tutorial_' + Date.now(),
    contractName: 'Tutorial'
  })

  let receipt = await LoopFetch(txId2, 10)

  if (receipt && receipt.status == 0) {
    console.log("failed to submit template")
    return
  } else {
    console.log("submit template " + txId2 + " successfully")
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# Deploy Contract

Call Templates.deployContract() to deploy a contract instance to Asimov blockchain.

Call Transactions.fetch() to make sure the transaction is confirmed on chain before moving on to next step.

  console.log('deploy contract')
  let txId3 = await templates.deployContract({
    templateId: txId2,
    constructorArguments: ["tutorial_" + Date.now()]
  })

  let deployReceipt = await LoopFetch(txId3, 10)

  if (deployReceipt && deployReceipt.status == 0) {
    console.log("failed to deploy contract")
    return
  } else {
    console.log("deploy contract " + deployReceipt.contractAddress + " successfully")
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# Execute Contract Methods

Call Contracts.execute() to mint asset, transfer asset and burn asset.

Call Transactions.fetch() to make sure the contract execution is successful. Call Contracts.read() to verify the contract state after execution.

  let contracts = new Contracts()
  console.log('execute contract method - mint')
  let txId4 = await contracts.execute({
    address: deployReceipt.contractAddress,
    method: "mint",
    args: [10000000000]
  })

  let executeResult = await LoopFetch(txId4, 10)
  checkExecuteResult(executeResult);

  var assettype;
  console.log('read contract state')
  if (executeResult.status == 1) {
    let at = await contracts.read({
      address: deployReceipt.contractAddress,
      method: "assettype",
    })
    assettype = intTo24Hex(at)
    console.log("assettype: " + assettype)
  }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

Only mint is shown in this document. Please check tutorial.tc.js for full testcases.

# Vote Contract Methods

Call Contracts.vote() to vote using the utxo asset mint in the above step.

Call Transactions.fetch() to make sure the vote is successful.

  console.log('vote contract method')
  let txId5 = await contracts.vote({
    address: deployReceipt.contractAddress,
    method: "vote",
    args: [1],
    voteValue: 0,
    assetType: assettype
  })

  let voteReceipt = await LoopFetch(txId5, 10)
  if (voteReceipt.status == 1) {
    console.log("vote successfully")
  }
1
2
3
4
5
6
7
8
9
10
11
12
13