JS library for building browser apps & NodeJS services that interact with Loom DAppChains
Branch: master
Clone or download
gakonst add redelegate bindings (#218)
* add redelegate bindings
* Remove redundant asyncs
Latest commit 0bfa3f9 Feb 19, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
e2e_support Fix formatting / null privkey Nov 13, 2018
scripts Distributing .d.ts files Dec 10, 2018
src add redelegate bindings (#218) Feb 19, 2019
.babelrc Setup bundling for browser environments with Webpack May 11, 2018
.editorconfig Start implementing the next version May 11, 2018
.env.test.example Run all contract tests with all the RPC clients Jun 9, 2018
.gitignore Added plasma-cash test on e2e dir Nov 11, 2018
.npmignore Add e2e_plasma_cash_test.sh to .npmingore Nov 12, 2018
.prettierignore Add npm scripts to generate protobufs, and run tests in node & browser May 11, 2018
.prettierrc Start implementing the next version May 11, 2018
.travis.yml Override read URL for PlasmaChain production cluster (#211) Feb 7, 2019
.travis_before_install.sh Testing e2e travis Sep 21, 2018
.travis_e2e_test.sh Bump loom build to 762 Jan 24, 2019
LICENSE Ship `loom.proto` in the NPM package May 27, 2018
README.md Updated readme json rpc calls Jan 17, 2019
e2e_plasma_cash_test.sh Query nonce from write URL instead of read URL (#189) Jan 7, 2019
e2e_tests.sh Query nonce from write URL instead of read URL (#189) Jan 7, 2019
es2017.tsconfig.json Add debugging instructions & an ES2017 target for development Jul 5, 2018
package.json Version 1.42.0 Feb 15, 2019
tsconfig.json Added plasma-cash test on e2e dir Nov 11, 2018
tslint.json Disable annoying member ordering linter rule May 23, 2018
webpack.config.js Bump loom / minor changes in createUser Oct 24, 2018
webpack.e2e.test.config.js Fix PlasmaDB cleanup Oct 19, 2018
webpack.unit.tests.config.js Fix PlasmaDB cleanup Oct 19, 2018
yarn.lock Add ethers.js Nov 9, 2018

README.md

Loom.js Build Status

JS library for building browser apps & NodeJS services that interact with Loom DAppChains.

Install

NodeJS

# NPM
npm install loom-js
# Yarn
yarn add loom-js

Browser

<script src="https://cdn.jsdelivr.net/npm/loom-js@1.15.0/dist/loom.umd.js"></script>

Debugging

loom-js uses the debug package for logging.

  • To inspect the logs when running in the browser set localStorage.debug = '*'
  • To inspect the logs when running in NodeJS set the DEBUG env var to *

See docs for debug package if you wish to filter the log messages.

Development

# build for Node
yarn build
# build for Browser
yarn build:browser
# build for Node while targeting ES2017 (more readable than ES5)
yarn build:dev
# run unit tests on Node & Browser
yarn test
# auto-format source files
yarn format
# run unit tests in Node
yarn test:node
# run unit tests in Electon
yarn test:browser
# run e2e test in Node (local DAppChain node must be running first)
yarn e2e:node
# run e2e test in Electron (local DAppChain node must be running first)
yarn e2e:browser

The e2e test environment can be configured by changing .env.test (see .env.test.example for default values).

Debugging

To debug loom-js while using it another project:

  1. Clone the Git repo from https://github.com/loomnetwork/loom-js
  2. Inside the loom-js repo run:
    yarn link
    yarn build:dev
  3. In your project repo run yarn link loom-js.
  4. Debug your project normally.

yarn build:dev will compile the TypeScript source to target ES2017, which requires a lot less transformation of async/await constructs than the ES5 code released on NPM.

Regenerating Protobufs

Whenever you change src/proto/loom.proto, or src/tests/tests.proto you will need to regenerate the corresponding *_pb.js by running:

yarn proto

The shell must be able to locate the protoc binary, which can be obtained from https://github.com/google/protobuf/releases

Custom Middlewares

Install the loom-js beta version: yarn add loom-js@beta

LoomProvider takes a custom function that lets you customize what middlewares it runs. For example, we have an experimental nonce caching middleware that has more advanced nonce tracking. You can add it as such:

const setupMiddlewareFn = function(client, privateKey) {
  const publicKey = CryptoUtils.publicKeyFromPrivateKey(privateKey)
  return [new CachedNonceTxMiddleware(publicKey, client), new SignedTxMiddleware(privateKey)]
}

const loomProviderObj = new LoomProvider(client, privateKey, setupMiddlewareFn)

More examples here: https://github.com/loomnetwork/loom-js/blob/master/src/tests/e2e/loom-provider-web3-middlewares-tests.ts

JSON RPC supported calls

LoomProvider combined with Web3 supports the following JSON RPC calls

Current implemented

  • eth_accounts
  • eth_blockNumber
  • eth_call
  • eth_estimateGas
  • eth_gasPrice
  • eth_getBlockByHash
  • eth_getBlockByNumber
  • eth_getCode
  • eth_getFilterChanges
  • eth_getLogs
  • eth_getTransactionByHash
  • eth_getTransactionReceipt
  • eth_newBlockFilter
  • eth_newFilter
  • eth_newPendingTransactionFilter
  • eth_sendTransaction
  • eth_subscribe
  • eth_uninstallFilter
  • eth_unsubscribe
  • net_version

Should

  • eth_getFilterLogs (Returns an array of all logs matching filter with given id)

Maybe

  • net_peerCount (Returns the current ethereum protocol version)
  • eth_syncing (Returns an object with data about the sync status or false)
  • eth_coinbase (Returns the client coinbase address)
  • web3_clientVersion (Returns the current client version)
  • eth_getTransactionCount (Returns the number of transactions sent from an address)
  • eth_getBlockTransactionCountByHash (Returns the number of transactions in a block from a block matching the given block hash)
  • eth_getBlockTransactionCountByNumber (Returns the number of transactions in a block matching the given block number)

Custom Loom provider methods

Install the loom-js beta version: yarn add loom-js@beta

Sometimes the project requires few functions that aren't provided directly from LoomProvider in this case is possible to add a custom Eth RPC method. You can add it as such:

const loomProviderObj = new LoomProvider(client, privateKey)
loomProviderObj.addCustomMethod('eth_balance', payload => {
  return '0x1'
})

The first param should be the Ethereum RPC call method to add on LoomProvider and the second parameter a callback function with the result

Overwrite Loom provider methods

Install the loom-js beta version: yarn add loom-js@beta

If the project requires to overwrite a RPC function that already exists on LoomProvider. You can add it as such:

loomProvider.overwriteMethod('eth_estimateGas', payload => {
  return '0x123'
})

The first param should be the Ethereum RPC call method to overwrite on LoomProvider and the second parameter a callback function with the result

License

BSD 3-Clause, see LICENSE file for details.