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
    }

}