Skip to content

EN DApps Reference

Andrey Nedobylsky edited this page Oct 10, 2019 · 12 revisions

Description of modules and available objects

Objects

logger

Require path: modules/logger

logger provides standardized output to the console.

Property: disable - true - disable message output; false - enable output


new Logger([prefix])

Args:

  • prefix (string) – Messages prefix

getPrefix()

Returns: Prefix value


log(type, data)

Args:

  • type (string) – Message type
  • data (string) – Message

info(data)

Args:

  • data (string) – Info message

init(data)

Args:

  • data (string) – Init message

error(data)

Args:

  • data (string) – Error message

fatal(data)

Args:

  • data (string) – Fatal error message

warning(data)

Args:

  • data (string) – Warning message

Example:

const DApp = require(global.PATH.mainDir + '/app/DApp');
const logger = new (require(global.PATH.mainDir + '/modules/logger'))("TestDApp");

class TestDApp extends DApp {
    init(){
        logger.info('Test DApp ready!'); //Prints "Fri, 01 Feb 2019 14:12:48 GMT Info:  TestDApp: Test DApp ready"
    }

}

assert

Require path: modules/testing/assert

The implementation is identical to the implementation inside EcmaContract environment.

Provides functionality for checking any conditions. Failure to comply with these conditions leads to the throwing of an exception and termination of the contract/DApp call with an error. For the contracts the state is rolled back to the state before the call.


Assertion for statement is equals to true

assert(statement[, message])

Args:

  • statement – Checking statement
  • message – Message if statement are incorrect

Throws: Exception


Check is element defined

defined(statement[, message])

Args:

  • statement – Checking statement
  • message – Message if statement are incorrect

Throws: Exception


Checks is a > b

gt(a, b[, message])

Args:

  • a – First element
  • b – Second element
  • message – Message if statement are incorrect

Throws: Exception


Checks is a < b

lt(a, b[, message])

Args:

  • a – First element
  • b – Second element
  • message – Message if statement are incorrect

Throws: Exception


Assertion for statement is equals to true. Alias for assert

true(statement[, message])

Args:

  • statement – Checking statement
  • message – Message if statement are incorrect

Throws: Exception


Assertion for statement is equals to false.

false(statement[, message])

Args:

  • statement – Checking statement
  • message – Message if statement are incorrect

Throws: Exception


Example:

class Test extends Contract {
    init(){
        super.init();
        assert.gt(100,200,'100 can\'t be greather than 200');
    }

}

instanceStorage

Require path: modules/instanceStorage

Provides access to created global object instances by keys.


Get element from storage

get(key)

Args:

  • key – Instance key

Returns: Instance object or null


Put element to storage

put(key, object)

Args:

  • key – Instance key
  • object – Instance object

By default, instanceStorage contains the following entities:

  • Blockchain - the main instance of the blockchain object
  • config - instance of the configuration file object
  • wallet - current wallet
  • blocks - block data storage
  • blockHandler - instance of the block handler object
  • frontend - an instance of the RPC/API interface and frontend object
  • transactor - instance of the transactions control object
  • cryptography - an instance of a cryptography object
  • blockchainObject - blockchain object alias

Optional entities:

  • ecmaContract - instance of the smart contract object (if enabled)
  • dapp - instance of the decentralized application object (if enabled)
  • terminating - shutdown flag (true/false)

Example:

const DApp = require(global.PATH.mainDir + '/app/DApp');
const storj = require(global.PATH.mainDir + '/modules/instanceStorage');

class TestDApp extends DApp {
    init(){
        console.log(storj.get('config').p2pPort); //6015
    }

}

Connectors

DApp connectors are wrapper classes that implement a simplified interface for interacting with certain types of contracts.

ContractConnector

Require path: modules/smartContracts/connectors/ContractConnector

Base abstract class for connectors. Allows you to register methods and their aliases.

TokenContractConnector

Require path: modules/smartContracts/connectors/TokenContractConnector

A connector class that implements interaction with a token contract for DApp


new TokenContractConnector(EcmaContractsInstance, contractAddress)

Args:

  • EcmaContractsInstance– Instance of EcmaContracts object
  • contractAddress (string) - Connection contract address

async balanceOf(address)

Args:

  • address– Address for checking balance

Returns: address tokens balance


async totalSupply()

Returns: tokens total supply


Create token transaction

async transfer(to, amount)

Args:

  • to (string) – Address for sending tokens
  • amount (string) – Amount of tokens in transaction

Burn tokens

async burn(amount)

Args:

  • amount (string) – Amount for burning

Mint new tokens

async mint(amount)

Args:

  • amount (string) – Amount for minting

Get contract info

async contract()

Returns: contract info object


Executing a method from another pay contract

async pay(address, method, txAmount, args)

Args:

  • address (string) – Address of contract for paying
  • method (string) – Paying method
  • txAmount (string) – Transaction amount
  • args (array) – Method calling arguments

Returns: Block object with transaction


Example:

const DApp = require(global.PATH.mainDir + '/app/DApp');
const TokenContractConnector = require(global.PATH.mainDir + '/modules/smartContracts/connectors/TokenContractConnector');

class TestDApp extends DApp {
    async init(){
        let mainToken = new TokenContractConnector(this.ecmaContract, this.getMasterContractAddress());

        console.log(await mainToken.balanceOf(this.getCurrentWallet().id)); //Prints balance of current wallet

        //Calls processPayment method from contract SOME_CONTRACT_ADDRESS with arguments ['Hello'] with payments state and send 1 token to contract
        await mainToken.pay(SOME_CONTRACT_ADDRESS, "processPayment", '1', ['Hello']);
    }