mirror of
https://github.com/EvernodeXRPL/hp-devkit.git
synced 2026-04-29 15:37:58 +00:00
Added new contract templates (#31)
This commit is contained in:
115
docker/code-templates/nodejs/starter-client/_projname_.js
Normal file
115
docker/code-templates/nodejs/starter-client/_projname_.js
Normal file
@@ -0,0 +1,115 @@
|
||||
const readline = require('readline');
|
||||
const HotPocket = require('hotpocket-js-client');
|
||||
|
||||
async function clientApp() {
|
||||
|
||||
const userKeyPair = await HotPocket.generateKeys();
|
||||
const client = await HotPocket.createClient(['wss://localhost:8081'], userKeyPair);
|
||||
|
||||
// Establish HotPocket connection.
|
||||
if (!await client.connect()) {
|
||||
console.log('Connection failed.');
|
||||
return;
|
||||
}
|
||||
|
||||
console.log('HotPocket Connected.');
|
||||
|
||||
// start listening for stdin
|
||||
const rl = readline.createInterface({
|
||||
input: process.stdin,
|
||||
output: process.stdout
|
||||
});
|
||||
|
||||
// On ctrl + c we should close HP connection gracefully.
|
||||
rl.on('SIGINT', () => {
|
||||
console.log('SIGINT received...');
|
||||
rl.close();
|
||||
client.close();
|
||||
});
|
||||
|
||||
// This will get fired if HP server disconnects unexpectedly.
|
||||
client.on(HotPocket.events.disconnect, () => {
|
||||
console.log('Disconnected');
|
||||
rl.close();
|
||||
});
|
||||
|
||||
// This will get fired when contract sends an output.
|
||||
client.on(HotPocket.events.contractOutput, (r) => {
|
||||
r.outputs.forEach(output => {
|
||||
handleOutput(output, r.ledgerSeqNo);
|
||||
});
|
||||
});
|
||||
|
||||
const handleOutput = (output, ledgerSeqNo) => {
|
||||
if (output.type == "statResult") {
|
||||
console.log(`(ledger:${ledgerSeqNo})>> ${output.data}`);
|
||||
}
|
||||
else if (output.type == "dataResult") {
|
||||
console.log(`(ledger:${ledgerSeqNo})>> ${output.data}`);
|
||||
}
|
||||
else if (output.type == "error") {
|
||||
console.log(`(ledger:${ledgerSeqNo})>> Error: ${output.error}`);
|
||||
}
|
||||
else {
|
||||
console.log("Unknown contract output.");
|
||||
}
|
||||
}
|
||||
|
||||
console.log("Ready to accept inputs.");
|
||||
|
||||
const input_pump = () => {
|
||||
rl.question('', async (inp) => {
|
||||
let input;
|
||||
if (inp.startsWith("stat")) {
|
||||
// Get contract status by read request and contract input.
|
||||
// This output means
|
||||
// Read request - contract is deployed and excitable.
|
||||
// Contract input - node are in sync.
|
||||
if (inp.startsWith("stat contract")) {
|
||||
input = await client.submitContractInput(JSON.stringify({
|
||||
type: "stat"
|
||||
}));
|
||||
}
|
||||
else if (inp.startsWith("stat read")) {
|
||||
const output = await client.submitContractReadRequest(JSON.stringify({
|
||||
type: "stat"
|
||||
}));
|
||||
handleOutput(output, (await client.getStatus()).ledgerSeqNo);
|
||||
}
|
||||
else {
|
||||
const res = await client.getStatus();
|
||||
console.log(res);
|
||||
}
|
||||
}
|
||||
else if (inp.startsWith("set ")) {
|
||||
input = await client.submitContractInput(JSON.stringify({
|
||||
type: "set",
|
||||
data: inp.substr(4)
|
||||
}));
|
||||
}
|
||||
else if (inp.startsWith("get")) {
|
||||
|
||||
// Read only inputs can be sent as read requests, So it'll be quick.
|
||||
const output = await client.submitContractReadRequest(JSON.stringify({
|
||||
type: "get"
|
||||
}));
|
||||
|
||||
handleOutput(output, (await client.getStatus()).ledgerSeqNo);
|
||||
}
|
||||
else {
|
||||
console.log("Invalid command. [set <data>] or [get] expected.")
|
||||
}
|
||||
|
||||
if (input) {
|
||||
const submission = await input.submissionStatus;
|
||||
if (submission.status != "accepted")
|
||||
console.log("Submission failed. reason: " + submission.reason);
|
||||
}
|
||||
|
||||
input_pump();
|
||||
})
|
||||
}
|
||||
input_pump();
|
||||
}
|
||||
|
||||
clientApp();
|
||||
6
docker/code-templates/nodejs/starter-client/package.json
Normal file
6
docker/code-templates/nodejs/starter-client/package.json
Normal file
@@ -0,0 +1,6 @@
|
||||
{
|
||||
"name": "_projname_",
|
||||
"dependencies": {
|
||||
"hotpocket-js-client": "^0.5.6"
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user