Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

murnicitawa task4 inery #1490

Open
wants to merge 1 commit into
base: task4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions murnicitawa/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
## Prerequisites

- Node.js: This script uses the `eosjs` library, which requires Node.js. You can download the latest version of Node.js from the official website (https://nodejs.org/).
- EOS node: This script interacts with an EOS node through its HTTP API. You will need to have an EOS node running and its HTTP API endpoint URL available.

## Setting up the environment

1. Clone the repository or download the script.
2. Create a file named `.env` in the same directory as the script.
3. In the `.env` file, define the following environment variables:
- `NODE_URL`: The URL of the HTTP API endpoint of your EOS node.
- `PRIVATE_KEY`: The private key of the account that will sign and broadcast the transaction.
- `INERY_ACCOUNT`: Your Inery account name.
- `TOKEN`: The token you created in your Inery account.
4. Install the dependencies by running `npm install` in the terminal.

## Running the script

1. Open a terminal and navigate to the directory where the script is located.
2. Run the script with the following command: `node ./inerynoderpc.mjs` or `npm run MC-RPC`

## Expected output

If the script runs successfully, you should see the signed transaction object and the console output of the `transfer` action of the `inery.token` contract, depending on the script. If an error occurs, the error message will be printed to the console.
128 changes: 128 additions & 0 deletions murnicitawa/inerynoderpc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { Api, JsonRpc, JsSignatureProvider } from 'ineryjs'
import * as dotenv from 'dotenv'
import axios from 'axios'

dotenv.config()

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 token = process.env.TOKEN
const signature = new JsSignatureProvider([private_key]);

const api = new Api({
rpc: json_rpc,
signatureProvider: signature
})


var lastBlockNum = 0;
var lastFiveProducers = [];

// Get Last Block Info For Latest Block Number
await GetLastBlockInfo();

// If The Last Block Info Was Retrieved Succesfully
if (lastBlockNum != 0) {
// Get Last 5 Producers Account
await FillLastProducers();

// If The Last Producers Were Successfully Imported
if(lastFiveProducers.length > 0)
{
// Send Sample Token To Last Producers
lastFiveProducers.forEach(async item => {
await SendToken(item);
});
}

}
async function GetLastBlockInfo() {
try {

let get_info = {
method: 'post',
url: url + '/v1/chain/get_info',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
};

await axios(get_info)
.then((response) => {
lastBlockNum = response.data["head_block_num"];
})
.catch((error) => {
console.log(error);
});
} catch (error) {
console.log(error)
}
}

async function FillLastProducers() {
for (let index = (lastBlockNum - 4); index <= lastBlockNum; index++) {
await GetBlockInfo(index);
}
}

async function GetBlockInfo(block_num) {

try {

let data = JSON.stringify({
"block_num_or_id": block_num.toString()
});

let get_block = {
method: 'post',
url: url + '/v1/chain/get_block',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: data
};

await axios(get_block)
.then((response) => {
lastFiveProducers.push(response.data["master"]);
})
.catch((error) => {
console.log(error);
});
}
catch (error) {
console.log(error)
}
}

async function SendToken(to) {
try {
const result = await api.transact({
actions: [{
account: 'inery.token',
name: 'transfer',
authorization: [{
actor: actor,
permission: 'active',
}],
data: {
from: account,
to: to,
quantity: '0.0001 ' + token,
memo: 'Here 0.0001 ' + token + ' for you :)',
}
}]
});

console.log(result);
} catch (error) {
console.log(error)
}
}
17 changes: 17 additions & 0 deletions murnicitawa/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "murnicitawa",
"version": "1.0.0",
"description": "",
"type": "module",
"scripts": {
"MC-RPC": "node ./inerynoderpc.mjs"
},
"dependencies": {
"axios": "^1.2.2",
"dotenv": "^16.0.3",
"ineryjs": "github:inery-blockchain/ineryjs"
},
"keywords": [],
"author": "murnicitawa",
"license": "ISC"
}