A library and commands to interact with RSK and Ethereum nodes
Keep it simple, and learn
One of the concessions that I had to make to my inclination to have few dependencies in this type of projects, was the need to include two libraries that allow me to sign and encode transactions, something very common with these nodes, where in production it is recommended not to have the private keys on the node itself, but sign the transactions from the external application. This is the main use case to have more dependencies in a project like this.
const rskapi = require('rskapi'); // Connecting to a local node, maybe RSK regtest const client = rskapi.client('http://localhost:4444'); /** operations with the node **/ const number = await client.number(); // best block number const bestBlock = await client.block(number); // retrieve best block
But I found very useful to have sample commands: Command-line programs that executes tasks, and save results in a configuration file. See samples/commands folder.
Let’s run an example. From this folder, execute:
node sethost https://public-node.testnet.rsk.co:443
This is the RSK public node for testnet (see RSK public nodes). In my library, you MUST specify the port (443) if you are using https transport.
You need an account. Create a random one and associate it with a logical name:
node genaccount root
The configuration file
config.json have the data of the account. This approach is for experimental tasks, not for production. The generated public address is shown, then you could add funds using
RSK testnet faucet.
You can retrieve the balance (in weis) using
node getbalance root
You can create new accounts with:
node genaccount alice node genaccount bob node genaccount charlie
You can transfer weis from one account to another:
node transfer root alice 1000000000000 node transfer root bob 1000000000000
Retrieve all the balance:
The sample also includes some smart contracts in
contracts folder and their compilation result (using truffle) already available in
build/contracts folder. So, without truffle, you even can deploy the contract, e.g. Counter, to an instance, using the funds of an account to pay the deployment:
node deploy root counter1 Counter
counter1 is the logical name of the new account just created with the deployed contract.
You can query the contract:
node call root counter1 counter()
and invoke it to change its status
node invoke root counter1 increment() node invoke root counter1 increment() node invoke root counter1 add(uint256) 40 node call root counter1 counter()
I’m using some older versions of these commands and library in others of my projects, like RskUtils, EthMeta and DeFiProt.
Other available objects
There are other objects that can be use from the library. One is the host: A client is only a wrapper of a host, that it is in charge of some formatting and validations, not to be discussed here (I’m thinking of some internal refactors).
You can get an host in two ways:
// from a client const client = rskapi.client('http://localhost:4444'); const host = client.host(); // or directly const host = rskapi.host('http://localhost:4444');
If you need to invoke a particular JSON RPC method not directly supported by a client or host, you can use the provider object:
const provider = host.provider(); // I should add too client.provider()
that is an instance of simplejsonrpc project.
I will add some additional functions to clients, like log retrieval and code retrieval. I plan to add the commands too. And I will add some programming examples, with more intensive tasks, like sending many transactions from differents accounts, deploying and invoking tokens, etc.
I’m having fun :-)