Skip to content
This repository was archived by the owner on Aug 2, 2022. It is now read-only.

Commit be370fb

Browse files
authored
Merge pull request #462 from EOSIO/develop
EOSJS v20.0.0-beta3
2 parents 68272dd + 251387f commit be370fb

28 files changed

+674
-891
lines changed

.babelrc

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,14 @@
66
}
77
}],
88
"stage-1"
9-
]
9+
],
10+
"plugins": [
11+
[
12+
"transform-runtime",
13+
{
14+
"polyfill": false,
15+
"regenerator": true
16+
}
17+
]
18+
]
1019
}

.npmrc.template

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
//registry.npmjs.org/:_authToken=${NPM_AUTH_TOKEN}

.travis.yml

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,30 @@
1+
env:
2+
global:
3+
PUBLISH_NPM_LATEST_FROM="master"
14
sudo: false
25
language: node_js
36
node_js:
4-
- '10.0.0'
7+
- '10.0.0'
58
before_install:
6-
- npm install -g typescript
9+
- npm i -g [email protected]
10+
- yarn global add typescript
11+
- yarn global add webpack
12+
before_script:
13+
- source ./scripts/is_latest.sh
714
script:
8-
- npm run lint
9-
- npm run test
10-
15+
- yarn run lint
16+
- yarn run test
17+
deploy:
18+
- provider: script
19+
skip_cleanup: true
20+
script:
21+
- ./scripts/publish-edge.sh
22+
on:
23+
branch: develop
24+
- provider: script
25+
skip_cleanup: true
26+
script:
27+
- ./scripts/publish-tag.sh $PUBLISH_NPM_LATEST_FROM
28+
on:
29+
tags: true
30+
condition: $TRAVIS_IS_LATEST_TAG = true # sourced from ./scripts/is_latest.sh

README.md

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,33 +10,43 @@ Documentation can be found [here](https://eosio.github.io/eosjs)
1010

1111
## Installation
1212

13-
### NodeJS
13+
### NodeJS Dependency
1414

15-
`npm install eosjs@beta`
15+
`npm install eosjs@beta` or `yarn add eosjs@beta`
1616

17-
## Basic Usage
17+
### Browser Distribution
18+
19+
Clone this repository locally then run `npm run build-web` or `yarn build-web`. The browser distribution will be located in `dist-web` and can be directly copied into your project repository. The `dist-web` folder contains minified bundles ready for production, along with source mapped versions of the library for debugging. For full browser usage examples, [see the documentation](https://eosio.github.io/eosjs/guides/1.-Browsers.html).
1820

19-
### Browser
21+
## Import
22+
23+
### ES Modules
2024

2125
Importing using ES6 module syntax in the browser is supported if you have a transpiler, such as Babel.
2226
```js
23-
import { Api, JsonRpc, RpcError, JsSignatureProvider } from 'eosjs';
27+
import { Api, JsonRpc, RpcError } from 'eosjs';
28+
29+
import JsSignatureProvider from 'eosjs/dist/eosjs-jssig'; // development only
2430
```
2531

26-
### NodeJS
32+
### CommonJS
2733

28-
Importing using commonJS syntax is supported by node out of the box.
34+
Importing using commonJS syntax is supported by NodeJS out of the box.
2935
```js
30-
const { Api, JsonRpc, RpcError, JsSignatureProvider } = require('eosjs');
36+
const { Api, JsonRpc, RpcError } = require('eosjs');
37+
const JsSignatureProvider = require('eosjs/dist/eosjs-jssig'); // development only
3138
const fetch = require('node-fetch'); // node only; not needed in browsers
32-
const { TextDecoder, TextEncoder } = require('text-encoding'); // node, IE11 and IE Edge Browsers
39+
const { TextEncoder, TextDecoder } = require('util'); // node only; native TextEncoder/Decoder
40+
const { TextEncoder, TextDecoder } = require('text-encoding'); // React Native, IE11, and Edge Browsers only
3341
```
3442

35-
### SignatureProvider
43+
## Basic Usage
44+
45+
### Signature Provider
3646

37-
SignatureProvider holds private keys and is responsible for signing transactions.
47+
The Signature Provider holds private keys and is responsible for signing transactions.
3848

39-
***Using the default JsSignatureProvider in the browser is not secure and should only be used for development purposes. Use a secure vault outside of the context of the webpage to ensure security when signing transactions in production***
49+
***Using the JsSignatureProvider in the browser is not secure and should only be used for development purposes. Use a secure vault outside of the context of the webpage to ensure security when signing transactions in production***
4050

4151
```js
4252
const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // useraaaaaaaa
@@ -47,10 +57,10 @@ const signatureProvider = new JsSignatureProvider([defaultPrivateKey]);
4757

4858
Open a connection to JSON-RPC, include `fetch` when on NodeJS.
4959
```js
50-
const rpc = new JsonRpc('http://127.0.0.1:8000', { fetch });
60+
const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });
5161
```
5262

53-
### API Constructor
63+
### API
5464

5565
Include textDecoder and textEncoder when using in browser.
5666
```js
@@ -59,6 +69,8 @@ const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), te
5969

6070
### Sending a transaction
6171

72+
`transact()` is used to sign and push transactions onto the blockchain with an optional configuration object parameter. This parameter can override the default value of `broadcast: true`, and can be used to fill TAPOS fields given `blocksBehind` and `expireSeconds`. Given no configuration options, transactions are expected to be unpacked with TAPOS fields (`expiration`, `ref_block_num`, `ref_block_prefix`) and will automatically be broadcast onto the chain.
73+
6274
```js
6375
(async () => {
6476
const result = await api.transact({
@@ -100,16 +112,10 @@ try {
100112
...
101113
```
102114
103-
## Browsers
104-
105-
After running `npm run build-web` or `yarn build-web`, the browser distribution will be located in `dist`. For full browser usage examples, [see the documentation](https://eosio.github.io/eosjs/static/3.-Browsers.html).
106-
107-
## How it works
108-
109-
`transact()` is used to sign and push transactions onto the blockchain with an optional configuration object parameter. This parameter can override the default value of `broadcast: true`, and can be used to fill TAPOS fields given `blocksBehind` and `expireSeconds`. Given no configuration options, transactions are expected to be unpacked with TAPOS fields (`expiration`, `ref_block_num`, `ref_block_prefix`) and will automatically be broadcast onto the chain.
110-
111-
112115
## Running Tests
113116
114-
### Automated Test Suite
117+
### Automated Unit Test Suite
115118
`npm run test` or `yarn test`
119+
120+
### Web Integration Test Suite
121+
Run `npm run build-web` to build the browser distrubution then open `src/tests/web.html` in the browser of your choice. The file should run through 6 tests, relaying the results onto the webpage with a 2 second delay after each test. The final 2 tests should relay the exceptions being thrown onto the webpage for an invalid transaction and invalid rpc call.

docs/2.-Transaction-Examples.md

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,26 @@
1-
## Example: Buy ram
1+
# Transactions
2+
3+
To be able to send transactions and trigger actions on the blockchain, you must have an instance of `Api`.
4+
5+
The signature provider must contains the private keys corresponsing to the actors and permissions of the actions.
6+
7+
```javascript
8+
const { Api, JsonRpc } = require('eosjs');
9+
const JsSignatureProvider = require('eosjs/dist/eosjs-jssig'); // development only
10+
const fetch = require('node-fetch'); // node only; not needed in browsers
11+
const { TextDecoder, TextEncoder } = require('text-encoding'); // node, IE11 and IE Edge Browsers
12+
13+
const privateKeys = [privateKey1];
14+
15+
const signatureProvider = new JsSignatureProvider(privateKeys);
16+
const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch });
17+
const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() });
18+
19+
```
20+
21+
## Examples
22+
23+
### Buy ram
224

325
```javascript
426
const result = await api.transact({
@@ -21,7 +43,7 @@ const result = await api.transact({
2143
});
2244
```
2345

24-
## Example: Stake
46+
### Stake
2547

2648
```javascript
2749
const result = await api.transact({
@@ -71,7 +93,7 @@ const result = await api.transact({
7193
});
7294
```
7395

74-
## Example: Create New Account (multiple actions)
96+
### Create New Account (multiple actions)
7597

7698
```javascript
7799
const result = await api.transact({

docs/3.-Browsers.md

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,9 @@ Reuse the `api` object for all transactions; it caches ABIs to reduce network us
88
```html
99
<pre style="width: 100%; height: 100%; margin:0px; "></pre>
1010

11-
<script src='dist-web/eosjs-api-debug.js'></script>
12-
<script src='dist-web/eosjs-jsonrpc-debug.js'></script>
13-
<script src='dist-web/eosjs-rpcerror-debug.js'></script>
14-
<script src='dist-web/eosjs-jssig-debug.js'></script>
11+
<script src='dist-web/eosjs-api.js'></script>
12+
<script src='dist-web/eosjs-jsonrpc.js'></script>
13+
<script src='dist-web/eosjs-jssig.js'></script>
1514
<script>
1615
let pre = document.getElementsByTagName('pre')[0];
1716
const defaultPrivateKey = "5JtUScZK2XEp3g9gh7F8bwtPTRAkASmNrrftmx4AxDKD5K4zDnr"; // useraaaaaaaa
@@ -43,12 +42,16 @@ Reuse the `api` object for all transactions; it caches ABIs to reduce network us
4342
pre.textContent += '\n\nTransaction pushed!\n\n' + JSON.stringify(result, null, 2);
4443
} catch (e) {
4544
pre.textContent = '\nCaught exception: ' + e;
46-
if (e instanceof eosjs_rpcerror.default)
45+
if (e instanceof eosjs_jsonrpc.RpcError)
4746
pre.textContent += '\n\n' + JSON.stringify(e.json, null, 2);
4847
}
4948
})();
5049
</script>
5150
```
5251

52+
## Debugging
53+
54+
If you would like readable source files for debugging, change the file reference to the `-debug.js` files inside `dist-web/debug` directory. These files should only be used for development as they are over 10 times as large as the minified versions, and importing the debug versions will increase loading times for the end user.
55+
5356
## IE11 and Edge Support
5457
If you need to support IE11 or Edge you will also need to install a text-encoding polyfill as eosjs Signing is dependent on the TextEncoder which IE11 and Edge do not provide. Pass the TextEncoder and TextDecoder to the API constructor as demonstrated in the [ES 2015 example](#node-es-2015). Refer to the documentation here https://github.com/inexorabletash/text-encoding to determine the best way to include it in your project.
Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,158 @@
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

Comments
 (0)