Skip to content

Commit 7b84543

Browse files
feruzmrelativityboy
authored andcommitted
JS-T: Account reputation (#94)
* resolves #31, account reputation * typo fix
1 parent 3b03eb2 commit 7b84543

9 files changed

Lines changed: 12085 additions & 0 deletions

File tree

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Interpret account reputation
2+
3+
_In this tutorial, we will show how to interpret account reputation_
4+
5+
This tutorial runs on the main Steem blockchain. And accounts queried are real users with reputation.
6+
7+
## Intro
8+
9+
This tutorial will show the method of capturing a queried tag name and matching it to the Steem. We are using the `call` function provided by the `dsteem` library to pull accounts from the Steem blockchain. A simple HTML interface is used to both capture the string query as well as display the completed search.
10+
11+
## steps
12+
13+
1. [**App setup**](#app-setup) Configuration of `dsteem` to use the proper connection and network.
14+
2. [**Search account**](#search-account) Collecting the relevant search criteria
15+
3. [**Interpret account reputation**](#run-reputation) Running the search and interpreting reputation.
16+
4. [**Output**](#output) Displaying the results
17+
18+
#### 1. App setup <a name="app-setup"></a>
19+
20+
Below we have `dsteem` pointing to the production network with the proper chainId, addressPrefix, and endpoint. There is a `public/app.js` file which holds the Javascript segment of this tutorial. In the first few lines we define the configured library and packages:
21+
22+
```javascript
23+
const dsteem = require('dsteem');
24+
let opts = {};
25+
//connect to production server
26+
opts.addressPrefix = 'STM';
27+
opts.chainId =
28+
'0000000000000000000000000000000000000000000000000000000000000000';
29+
//connect to server which is connected to the network/production
30+
const client = new dsteem.Client('https://api.steemit.com');
31+
```
32+
33+
#### 2. Search account <a name="search-account"></a>
34+
35+
Collecting of the search criteria happens via an HTML input. The form can be found in the `index.html` file. The values are pulled from that screen with the below:
36+
37+
```javascript
38+
const max = 5;
39+
window.submitAcc = async () => {
40+
const accSearch = document.getElementById('username').value;
41+
```
42+
43+
#### 3. Interpret account reputation <a name="run-reputation"></a>
44+
45+
In order to get accounts, we run the search with the `search field` and `maximum` list items as parameters.
46+
47+
```javascript
48+
const _accounts = await client.database.call('lookup_accounts',[accSearch, max]);
49+
```
50+
51+
The result of the search is an array of accounts. After that we use `get_accounts` to pull account data from Steem.
52+
53+
```javascript
54+
const acc = await client.database.call('get_accounts',[_accounts]);
55+
```
56+
57+
And we loop through each account to convert their `reputation` to human readable format with following function:
58+
59+
```javascript
60+
function log10(str) {
61+
const leadingDigits = parseInt(str.substring(0, 4));
62+
const log = Math.log(leadingDigits) / Math.LN10 + 0.00000001;
63+
const n = str.length - 1;
64+
return n + (log - parseInt(log));
65+
}
66+
67+
export const repLog10 = rep2 => {
68+
if (rep2 == null) return rep2;
69+
let rep = String(rep2);
70+
const neg = rep.charAt(0) === '-';
71+
rep = neg ? rep.substring(1) : rep;
72+
73+
let out = log10(rep);
74+
if (isNaN(out)) out = 0;
75+
out = Math.max(out - 9, 0); // @ -9, $0.50 earned is approx magnitude 1
76+
out = (neg ? -1 : 1) * out;
77+
out = out * 9 + 25; // 9 points per magnitude. center at 25
78+
// base-line 0 to darken and < 0 to auto hide (grep rephide)
79+
out = parseInt(out);
80+
return out;
81+
};
82+
```
83+
84+
#### 4. Output <a name="output"></a>
85+
86+
After each account's reputation is interpreted we can then display them on screen with readable reputation.
87+
88+
```javascript
89+
//disply list of account names and reputation with line breaks
90+
for (var i = 0; i < _accounts.length; i++) {
91+
_accounts[i] = `${_accounts[i]} - ${repLog10(acc[i].reputation)}`;
92+
}
93+
document.getElementById('accList').innerHTML = _accounts.join('<br/>');
94+
```
95+
96+
That's it!
97+
98+
### To run this tutorial
99+
100+
1. clone this repo
101+
1. `cd tutorials/20_account_reputation`
102+
1. `npm i`
103+
1. `npm i`
104+
1. `npm run dev-server` or `npm run start`
105+
1. After a few moments, the server should be running at [http://localhost:3000/](http://localhost:3000/)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const Koa = require('koa');
2+
const app = new Koa();
3+
const serve = require('koa-static');
4+
app.use(serve('./public'));
5+
6+
app.listen(3000);
7+
8+
console.log('listening on port 3000');

0 commit comments

Comments
 (0)