Module: util

This is the util interface

Methods

(inner) filter_isContained(setA, setB, comparator, isDebug) → {Array}

Check for intersection.

Parameters:
Name Type Description
setA Array

A list

setB Array

Another list

comparator *

Comparator to compare elements of setA and setB

isDebug *

Debug mode

Returns:
Type
Array
Example
const listA = [
  {
    accountAddress: 'testAddress1',
    username: 'user1'
  },
  {
    accountAddress: 'testAddress2',
    username: 'user2'
  },
  {
    accountAddress: 'testAddress3',
    username: 'user3'
  },
]
const listB = [
  {
    username: 'user2'
  }
]
const comparator = function (a, b) { return a.username == b.username; };
const result = util.filter_isContained(listA, listB, comparator);
console.log(result);
// Returns - An array of elements found in Set A that were not included in Set B
// [ { accountAddress: 'testAddress1', username: 'user1' },
// { accountAddress: 'testAddress3', username: 'user3' } ]

(inner) getArgInt(argName, defaultValue) → {Int}

Parse a command line argument as an integer

Parameters:
Name Type Description
argName String

Name of the command line argument

defaultValue Int

Default value to use if argument is not present

Returns:
Type
Int
Example
// Given the command: node index.js --size 10
const result = util.getArgInt("--size", 0);
console.log(result);
// Returns
// 10

(inner) isAddress(address) → {Boolean}

Parameters:
Name Type Description
address String

the given HEX adress

See:
Returns:
Type
Boolean
Example
const simpleStorageSrc = fsUtil.get("SimpleStorage.sol");
const contractArgs = {
  name: "SimpleStorage",
  source: simpleStorageSrc,
  args: {} // Any constructor args would go here. We dont have any.
};
const result = await rest.createContract(stratoUser, contractArgs, options);
// Returns
// true

(inner) isChecksumAddress(address) → {Boolean}

Checks if the given string is a checksummed address

Parameters:
Name Type Description
address String

the given HEX adress

Returns:
Type
Boolean
Example
const result = isChecksumAddress('0x2AE66CDc592e10B60f9097a7b0D3C59fce29876');
console.log(result);
// Returns
// true

(inner) iuid() → {Int}

Generates a unique integer id

Returns:
Type
Int
Example
util.iuid()
// Returns
// 654657412219

(inner) toCsv(array) → {String}

Convert an array to a comma separated string

Parameters:
Name Type Description
array *

List to convert to csv value

Returns:
Type
String
Example
util.toCsv([1, 2, 3, 4])
// Returns
// 1,2,3,4

(inner) uid(prefix, digits) → {Int}

Generates a unique id

Parameters:
Name Type Description
prefix

Optional. The prefix of the unique id to be generated

digits

Optional. The number of digits of the unique id to be generated. Defaults to 6.

Returns:
Type
Int
Example
util.uid()
// Returns
// 100549

(inner) until(predicate, action, options, timeout) → {Object}

Perform action (a promise) until predicate evaluates to true

Parameters:
Name Type Description
predicate *

The function that evaluates to a boolean.

action *

A promise to invoke. Could be a network request.

options *

Ba-rest options

timeout *

The length of time to wait before giving up on the predicate

Returns:
Type
Object
Example
// Consider the following function in the rest module
// async function searchUntil(user, contract, predicate, options) {
//  const action = async o => {
//    return search(user, contract, o);
//   };
//
//   const results = await util.until(predicate, action, options);
//   return results;
// }

// predicate is created: to wait until response is available otherwise throws the error
function predicate(data) {};
const result = await rest.searchUntil(stratoUser, {name: "SimpleStorage"}, predicate, queryOptions);
// Returns
// [ { address: '60fb089dc62858df014819d618aa3f43391ddb9c',
//     chainId: '',
//     block_hash:
//      'fb7edc20a2678ca60024f81d926d1637eb418012beae2fedb7e7c4250406ea82',
//     block_timestamp: '2020-08-04 20:48:18 UTC',
//     block_number: '3',
//     transaction_hash:
//      '32321367696882da2feb9483fa31346f68abf77cc1a01ace6ef5a2b774bc8d38',
//     transaction_sender: '594f19ad4a55d6434711a51f628f22abf4afc55a',
//     transaction_function_name: '',
//     storedData: 10 } ]

(inner) usc(Object) → {Object}

Formats an object's properties

Parameters:
Name Type Description
Object *

An object with properties

Returns:
Type
Object
Example
util.usc({ size: 10, length: 5 })
// Returns
// { _size: 10, _length: 5 }