|
| 1 | +# Reading blockchain |
| 2 | + |
| 3 | +Reading blockchain state only requires an instance of `JsonRpc` connected to a node. |
| 4 | + |
| 5 | +```javascript |
| 6 | +const { JsonRpc } = require('eosjs'); |
| 7 | +const fetch = require('node-fetch'); // node only; not needed in browsers |
| 8 | +const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch }); |
| 9 | +``` |
| 10 | + |
| 11 | +## Examples |
| 12 | + |
| 13 | +### Get table rows |
| 14 | + |
| 15 | +Get the first 10 token balances of account _testacc_. |
| 16 | + |
| 17 | +```javascript |
| 18 | +const resp = await rpc.get_table_rows({ |
| 19 | + json: true, // Get the response as json |
| 20 | + code: 'eosio.token', // Contract that we target |
| 21 | + scope: 'testacc' // Account that owns the data |
| 22 | + table: 'accounts' // Table name |
| 23 | + limit: 10, // maximum number of rows that we want to get |
| 24 | +}); |
| 25 | + |
| 26 | +console.log(resp.rows); |
| 27 | +``` |
| 28 | +Output: |
| 29 | + |
| 30 | +```json |
| 31 | +{ |
| 32 | + "rows": [{ |
| 33 | + "balance": "100.0000 HAK" |
| 34 | + } |
| 35 | + ], |
| 36 | + "more": false |
| 37 | +} |
| 38 | +``` |
| 39 | + |
| 40 | +### Get one row by index |
| 41 | + |
| 42 | +```javascript |
| 43 | +const resp = await rpc.get_table_rows({ |
| 44 | + json: true, // Get the response as json |
| 45 | + code: 'contract', // Contract that we target |
| 46 | + scope: 'contract' // Account that owns the data |
| 47 | + table: 'profiles' // Table name |
| 48 | + lower_bound: 'testacc' // Table primary key value |
| 49 | + limit: 1, // Here we limit to 1 to get only the |
| 50 | +}); |
| 51 | +console.log(resp.rows); |
| 52 | +``` |
| 53 | +Output: |
| 54 | + |
| 55 | +```json |
| 56 | +{ |
| 57 | + "rows": [{ |
| 58 | + "user": "testacc", |
| 59 | + "age": 21, |
| 60 | + "surname": "Martin" |
| 61 | + } |
| 62 | + ], |
| 63 | + "more": false |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Get one row by secondary index |
| 68 | + |
| 69 | +```javascript |
| 70 | +const resp = await rpc.get_table_rows({ |
| 71 | + json: true, // Get the response as json |
| 72 | + code: 'contract', // Contract that we target |
| 73 | + scope: 'contract' // Account that owns the data |
| 74 | + table: 'profiles' // Table name |
| 75 | + table_key: 'age' // Table secondaray key name |
| 76 | + lower_bound: 21 // Table secondary key value |
| 77 | + limit: 1, // Here we limit to 1 to get only the |
| 78 | +}); |
| 79 | +console.log(resp.rows); |
| 80 | +``` |
| 81 | +Output: |
| 82 | + |
| 83 | +```json |
| 84 | +{ |
| 85 | + "rows": [{ |
| 86 | + "user": "testacc", |
| 87 | + "age": 21, |
| 88 | + "surname": "Martin" |
| 89 | + } |
| 90 | + ], |
| 91 | + "more": false |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +### Get currency balance |
| 96 | + |
| 97 | +```javascript |
| 98 | +console.log(await rpc.get_currency_balance('eosio.token', 'testacc', 'HAK')); |
| 99 | +``` |
| 100 | +Output: |
| 101 | + |
| 102 | +```json |
| 103 | +[ "1000000000.0000 HAK" ] |
| 104 | +``` |
| 105 | + |
| 106 | +### Get account info |
| 107 | + |
| 108 | +```javascript |
| 109 | +console.log(await rpc.get_account('testacc')); |
| 110 | +``` |
| 111 | +Output: |
| 112 | + |
| 113 | +```json |
| 114 | +{ "account_name": "testacc", |
| 115 | + "head_block_num": 1079, |
| 116 | + "head_block_time": "2018-11-10T00:45:53.500", |
| 117 | + "privileged": false, |
| 118 | + "last_code_update": "1970-01-01T00:00:00.000", |
| 119 | + "created": "2018-11-10T00:37:05.000", |
| 120 | + "ram_quota": -1, |
| 121 | + "net_weight": -1, |
| 122 | + "cpu_weight": -1, |
| 123 | + "net_limit": { "used": -1, "available": -1, "max": -1 }, |
| 124 | + "cpu_limit": { "used": -1, "available": -1, "max": -1 }, |
| 125 | + "ram_usage": 2724, |
| 126 | + "permissions": |
| 127 | + [ { "perm_name": "active", "parent": "owner", "required_auth": [] }, |
| 128 | + { "perm_name": "owner", "parent": "", "required_auth": [] } ], |
| 129 | + "total_resources": null, |
| 130 | + "self_delegated_bandwidth": null, |
| 131 | + "refund_request": null, |
| 132 | + "voter_info": null } |
| 133 | +``` |
| 134 | + |
| 135 | +### Get block |
| 136 | + |
| 137 | +```javascript |
| 138 | +console.log(await rpc.get_block(1)); |
| 139 | +``` |
| 140 | +Output: |
| 141 | + |
| 142 | +```json |
| 143 | +{ "timestamp": "2018-06-01T12:00:00.000", |
| 144 | + "producer": "", |
| 145 | + "confirmed": 1, |
| 146 | + "previous": "0000000000000000000000000000000000000000000000000000000000000000", |
| 147 | + "transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000", |
| 148 | + "action_mroot": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f", |
| 149 | + "schedule_version": 0, |
| 150 | + "new_producers": null, |
| 151 | + "header_extensions": [], |
| 152 | + "producer_signature": "SIG_K1_111111111111111111111111111111111111111111111111111111111111111116uk5ne", |
| 153 | + "transactions": [], |
| 154 | + "block_extensions": [], |
| 155 | + "id": "00000001bcf2f448225d099685f14da76803028926af04d2607eafcf609c265c", |
| 156 | + "block_num": 1, |
| 157 | + "ref_block_prefix": 2517196066 } |
| 158 | +``` |
0 commit comments