You'll need a version of node that supports async/await. You should be ok if your version number is greater than 8.
My version is 8.6 (nothing special about this version...):
node --version
v8.6.0
I recommend that you download the Long Term Support version (8.9.3):
You can test whether async/await is supported or not by entering into the node REPL:
$ node
Then create an async function:
> async () => { }
[AsyncFunction]
If for some reason you need to run hydrajs on a platform that does not support async/await, please create an issue.
Note on Code Editor
For modern JavaScript development, you really owe it to yourself to try VSCode. hydrachainjs comes with static type definitions for its API, and with VSCode you get some of the most useful IDE features (e.g. type-accurate autocomplete) without the UX bloat:
It is recommended to try TypeScript too. JavaScript is in fact an extremely powerful language. TypeScript is the sobered-up version, yet retaining the same dynamism and expressivity that JavaScript developers love.
Setup The HRC20 CLI Project
Let's clone the NodeJS project to the directory mytoken-js:
async function balanceOf(owner) {
const res = await myToken.call("balanceOf", [owner])
const balance = res.outputs[0]
console.log(`balance:`, balance.toNumber())
}
The arguments to balanceOf are passed in as an array.
Send VS Call
Confusingly, there are two ways to invoke a method: send and call. These two names are inherited from Ethereum. A more descriptive way to name them is perhaps to call send "commit" and call "query".
call (or "query"): executes contract code on your own local hyrad node as a "simulation", returning results, but not changing the blockchain. This is free.
send (or "commit"): creates an actual transaction that would execute code globally on the network, changing the blockchain. This costs gas.
Next, we are going to mint some new tokens using hydrajs. And because minting token changes the blockchain, we'll use send.
Mint Tokens With Send
The mint command creates new tokens by using send to create a new transaction. Then it waits for that transaction to confirm:
Note that the API does not require the _from address. It is assumed that msg.sender is the source of the token balance to transfer from.
Ah, msg.sender, our old nemesis.
As we've learned in The Owner UTXO Address, HYDRA doesn't really have the idea of an "account". The msg.sender is the address of whatever UTXO that was used to pay for the transaction.
To act as dcb3...9e94, we need to explicitly specify an UTXO that has the same address. We can do this by using the senderAddress option.
node index.js transfer \
qdgznat81MfTHZUrQrLZDZteAx212X4Wjj \
9d748f98e65c6875dbed7bfb6ffbeca426ff9cc6 \
100
transfer tx: a1ba017b3974b98bf9c8edc824c3abc0ce17678a14e7cfac94b5900a290bdd07
✔ confirm transfer
Note that we MUST specify the senderAddress using base58 address format. We'll fix this in the future.
We can then verify that 9d74...9cc6 had indeed received the tokens:
The CappedToken contract defines a few events. The Transfer event is emitted whenever fund is moved from one account to another (also when minting new tokens). The Transfer event:
event Transfer(
address indexed from,
address indexed to,
uint256 value
);
Let's use qtumjs to subscribe to the stream of contract events, so we can react in a timely manner when a transfer occurs. The code:
async function streamEvents() {
console.log("Subscribed to contract events")
console.log("Ctrl-C to terminate events subscription")
myToken.onLog((entry) => {
console.log(entry)
}, { minconf: 1 })
}
Let's see it in action. Launch the events subscriber:
node index.js events
Subscribed to contract events
Ctrl-C to terminate events subscription
The program hangs there waiting for new events. In another terminal, mint more tokens:
node index.js mint dcd32b87270aeb980333213da2549c9907e09e94 10000
mint tx: c0e3007178a1b9e05b33e770f7a0e7d084f2d06732658524be042dc0e9864cc4
Wait for a bit for confirmations. In the events terminal, you should see both Mint and Transfer events printed out: