Skip to content

Commit

Permalink
Inery task 4 solution: aprameth
Browse files Browse the repository at this point in the history
  • Loading branch information
DiscoverMyself authored Dec 24, 2022
1 parent 726e653 commit e400ed2
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 0 deletions.
43 changes: 43 additions & 0 deletions aprameth/README.md
Original file line number Diff line number Diff line change
@@ -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
```
19 changes: 19 additions & 0 deletions aprameth/package.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
151 changes: 151 additions & 0 deletions aprameth/solution.mjs
Original file line number Diff line number Diff line change
@@ -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")

0 comments on commit e400ed2

Please sign in to comment.