diff --git a/aprameth/README.md b/aprameth/README.md new file mode 100644 index 000000000..d36b173dd --- /dev/null +++ b/aprameth/README.md @@ -0,0 +1,43 @@ +## Prerequisite + +### Instal curl +``` +sudo apt-get install curl +``` + +### NodeJS & NPM +- [Windows](https://nodejs.org/en/download/) Included NPM packages +- Linux: +``` +curl -fsSL https://deb.nodesource.com/setup_19.x | sudo -E bash - &&\ +sudo apt-get install -y nodejs +``` + + + +## How to run? + +**1. Change directory to `aprameth`** + +```shell +cd ./aprameth +``` + + +**2. Install dependencies** + +```shell +npm install +``` + +**3. Create & edit `.env` file** +``` +cp .env-sample .env +``` + + +**4. Run the script** + +``` +npm run solution +``` \ No newline at end of file diff --git a/aprameth/package.json b/aprameth/package.json new file mode 100644 index 000000000..61e771131 --- /dev/null +++ b/aprameth/package.json @@ -0,0 +1,19 @@ +{ + "name": "aprameth-rpc-transaction", + "version": "1.0.0", + "description": "A sample RPC API push transaction on Inery blockchain", + "main": "./solution.mjs", + "scripts": { + "solution": "node ./solution.mjs" + }, + "author": "DiscoverMyself", + "license": "MIT", + "repository": { + "type": "git", + "url": "https://github.com/DiscoverMyself/inery-testnet-faucet-tasks" + }, + "homepage": "https://github.com/DiscoverMyself/inery-testnet-faucet-tasks", + "dependencies": { + "ineryjs": "github:inery-blockchain/ineryjs" + } +} \ No newline at end of file diff --git a/aprameth/solution.mjs b/aprameth/solution.mjs new file mode 100644 index 000000000..1243b9b84 --- /dev/null +++ b/aprameth/solution.mjs @@ -0,0 +1,151 @@ +import { Api, JsonRpc, RpcError, JsSignatureProvider } from 'ineryjs/dist/index.js' +const url = process.env.NODE_URL + +const json_rpc = new JsonRpc(url) +const private_key = process.env.PRIVATE_KEY; + +const account = process.env.INERY_ACCOUNT +const actor = process.env.INERY_ACCOUNT +const signature = new JsSignatureProvider([private_key]); + +const api = new Api({ + rpc: json_rpc, + signatureProvider: signature +}) + +async function create(id, user, data){ + try{ + const tx = await api.transact({ + actions:[ + { + account, + name:"create", + authorization:[ + { + actor, + permission:"active" + } + ], + data:{ + id, user, data + } + } + ] + },{broadcast:true,sign:true}) + + + console.log("=======================================================================") + console.log("===================== CREATE transaction details ======================") + console.log("=======================================================================") + console.log(tx, "\n") + console.log("Response from contract :", tx.processed.action_traces[0].console) + console.log("\n") + }catch(error){ + console.log(error) + } +} + +async function read(id){ + try{ + const tx = await api.transact({ + actions:[ + { + account, + name:"read", + authorization:[ + { + actor, + permission:"active" + } + ], + data:{ + id + } + } + ] + },{broadcast:true,sign:true}) + + console.log("=======================================================================") + console.log("===================== READ transaction details ========================") + console.log("=======================================================================") + console.log(tx, "\n") + console.log("Response from contract :", tx.processed.action_traces[0].console) + console.log("\n") + }catch(error){ + console.log(error) + } +} + +async function update(id, data){ + try{ + const tx = await api.transact({ + actions:[ + { + account, + name:"update", + authorization:[ + { + actor, + permission:"active" + } + ], + data:{ + id, data + } + } + ] + },{broadcast:true,sign:true}) + + + console.log("=======================================================================") + console.log("===================== UPDATE transaction details ======================") + console.log("=======================================================================") + console.log(tx, "\n") + console.log("Response from contract :", tx.processed.action_traces[0].console) + console.log("\n") + }catch(error){ + console.log(error) + } +} + +async function destroy(id){ + try{ + const tx = await api.transact({ + actions:[ + { + account, + name:"destroy", + authorization:[ + { + actor, + permission:"active" + } + ], + data:{ + id + } + } + ] + },{broadcast:true,sign:true}) + + + console.log("=======================================================================") + console.log("===================== DESTROY transaction details =====================") + console.log("=======================================================================") + console.log(tx, "\n") + console.log("Response from contract :", tx.processed.action_traces[0].console) + console.log("\n") + }catch(error){ + console.log(error) + } +} + + +async function main(id, user, data){ + await create(id, user, data) + await read(id) + await update(id, data) + await destroy(id) +} + +main(1, account, "CRUD Transaction via JSON RPC") \ No newline at end of file