Files
hpcore/examples/nodejs_contract/echo_contract.js
Ravin Perera 332e5a4750 User input json/bson format improvements. (#174)
* Removed hex encoding in json input container.
* Refactored client lib exports.
2020-11-28 21:24:35 +05:30

49 lines
1.5 KiB
JavaScript

const { HotPocketContract } = require("./hp-contract-lib");
const fs = require('fs');
// HP smart contract is defined as a function which takes HP ExecutionContext as an argument.
// HP considers execution as complete, when this function completes and all the callbacks are complete.
const echoContract = async (ctx) => {
// We just save execution timestamp as an example state file change.
if (!ctx.readonly)
fs.appendFileSync("exects.txt", "ts:" + ctx.timestamp + "\n");
// This will return after all user messages are processed.
await ctx.users.onMessage(async (user, buf) => {
// This user's pubkey can be accessed from 'user.pubKey'
// A reply message can be sent to the user by 'user.send(msg)'
const msg = buf.toString();
if (msg == "ts") {
await user.send(fs.readFileSync("exects.txt"));
}
else {
await user.send("Echoing: " + msg);
}
});
// Get list of all users who are connected.
// ctx.users.get();
// Get the user identified by public key.
// ctx.users.find("<PubkeyHex>");
// Get list of all peers in the cluster.
// ctx.peers.get();
// Get the peer identified by public key.
// ctx.peers.find("<PubkeyHex>");
// Peer messages example.
// if (!ctx.readonly) {
// ctx.peers.onMessage((peer, msg) => {
// console.log(msg + " from " + peer.pubKey);
// })
// await ctx.peers.send("Hello");
// }
}
const hpc = new HotPocketContract();
hpc.init(echoContract);