Skip to main content

Quick start

Let’s make a simple custom contract. First, create npm project and install the dependencies:

npm init
yarn add assemblyscript -D
yarn add jest -D

npx asinit .

yarn add idena-sdk-as@0.0.29

After project initialization, you will have the following project folder structure:

image

  • assembly: folder with contract source code
  • build: folder for compiled files
  • node_modules: dependencies
  • tests: folder with tests
  • asconfig.json: config file for the asc compiler

Next, update asconfig.json file so that the asc compiler uses the transformation of the contract code with idena-sdk-bindgen. For this you need to add the line:

"extends": "idena-sdk-as/asconfig.json"

image

You are ready to write the contract code using idena-sdk-as. Exported class in the assembly/index.ts is the entry point for executing the contract.

As an example, see the code of hello-world contract.

Building

To build the contracts use asbuild:

yarn asb

As a result, a file will be created:

/build/release/<your-project-name>.wasm

Testing

For testing, we recommend using jest. Run the command to install it:

yarn add jest -D

Update the "test" value in your project package.json file in the scripts section as follows:

"test":"jest"

image

In the tests folder, create the index.test.js file.

See example of tests for hello-world contract.

To execute the contract code and run the test, you need to install and run Idena node emulator idena-contract-runner. You can build it yourself or download the executable file.

idena-sdk-as contains additional classes and methods for testing, see idena-sdk-tests.

To run tests, use the command:

yarn test

Deploying

To deploy your contract you can use rpc.idena.io.

  • Connect to the Idena node
  • Enter your coinbase address
  • Select the compiled .wasm contract file
  • Enter unique nonce
  • Specify args for the contract constructor if needed
  • You can call the contract_estimateDeploy method to calcaulte maxfee parameter

image

{
"method": "contract_Deploy",
"params": [
{
"from": "0x6899...",
"code": "0x0061736d01...",
"nonce": "0x01",
"args": [],
"maxFee": 2
}
],
"id": 1,
"key": "..."
}

Response example:

{
"jsonrpc": "2.0",
"id": 1,
"result": "0xdeb3..."
}

As a result you will get a hash of DeployContractTx transaction. To get the address of the deployed contract, call bcn_txReceipt method for this transaction hash.