diff --git a/docs.json b/docs.json new file mode 100644 index 000000000..7cf928535 --- /dev/null +++ b/docs.json @@ -0,0 +1,46 @@ +{ + "name": "eosjs", + "generators": [ + { + "name": "collate_markdown", + "options": { + "docs_dir": "docs" + } + }, + { + "name": "typedoc", + "options": { + "theme": "markdown", + "exclude": ["**/index*","**/*.test.ts","**/rpc-web*"], + "ignoreCompilerErrors": true, + "includeDeclarations": false, + "excludeExternals": true, + "excludeProtected": true, + "excludePrivate": true, + "typescript": { + "include": [ + "src/**/*.js" + ] + } + } + } + ], + "filters": [ + { + "name": "search_replace", + "options": { + "search": "numeric.md#", + "replace": "#", + "filepattern": "numeric.md" + } + }, + { + "name": "search_replace", + "options": { + "search": "serialize.md#", + "replace": "#", + "filepattern": "serialize.md" + } + } + ] +} diff --git a/docs/00_introduction.md b/docs/00_introduction.md new file mode 100644 index 000000000..b8a654291 --- /dev/null +++ b/docs/00_introduction.md @@ -0,0 +1,8 @@ +`eosjs` is a Javascript library which provides an API for integrating with EOSIO-based blockchains using the [EOSIO Nodeos RPC API](https://developers.eos.io/eosio-nodeos/reference). The documentation for `eosjs` is structured in the following way: + +* [Installation](02_installation.md) explains how to install `eosjs` using `npm` or `yarn`. +* [Basic Usage](basic-usage/) provides information related to importing `eosjs` in various Javascript environments. The [basic-usage](basic-usage/03_basic-usage.md) document specifically provides brief explanations of the components provided by `eosjs` as well as their typical use cases. +* [FAQ](faq/) provides answers to frequently asked questions surrounding the `eosjs` software. +* [How-To Guides](how-to-guides/) provides how-tos on everything from getting block information to proposing and signing multi-sig transactions. +* [Troubleshooting](troubleshooting/) provides possible exceptions encountered when developing with `eosjs` and their most common causes. +* [Technical Overview](01_technical-overview.md) provides a high-level overview of how `eosjs` works. \ No newline at end of file diff --git a/docs/01_technical-overview.md b/docs/01_technical-overview.md new file mode 100644 index 000000000..27ba182e1 --- /dev/null +++ b/docs/01_technical-overview.md @@ -0,0 +1,23 @@ +As stated in the [introduction](00_introduction.md), `eosjs` integrates with EOSIO-based blockchains using the [EOSIO Nodeos RPC API](https://developers.eos.io/eosio-nodeos/reference). + +In general, there are two objects that are used to interact with a blockchain via `eosjs`: the `JsonRpc` object, and the `Api` object. + +## JsonRpc +The `JsonRpc` object is typically used when signing is not necessary. Some examples include [getting block information](how-to-guides/00_how-to-get-block-information.md), [getting transaction information](how-to-guides/02_how-to-get-transaction-information.md), or [getting table information](how-to-guides/09_how-to-get-table-information.md). + +The requests made by the `JsonRpc` object will either use a built-in `fetch` library, or [the `fetch` library passed in by the user](basic-usage/01_commonjs.md) to issue requests to the endpoint specified when instantiating the `JsonRpc` object. When the various methods ([get_abi](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L66), [get_account](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L71), [get_block_header_state](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L76), etc) of the `JsonRpc` object are invoked, the calls are delegated to the `JsonRpc` object's [fetch function](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L42-L63), which in turn, delegate the requests to the `fetch` library. + +## Api +The `Api` object is typically used when transacting on an EOSIO-based blockchain. Some examples include [staking](how-to-guides/03_how-to-stake.md), [creating an account](how-to-guides/05_how-to-create-an-account.md), or [proposing multi-sig transactions](how-to-guides/13_how-to-propose-a-multisig-transaction.md). + +The typical use of the `Api` object is to call its [`transact` method](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-api.ts#L214-L254). This method performs a number of steps depending on the input passed to it: + +* The `transact` method first checks if the **chainId** was set in the `Api` constructor, and if not, uses the [`JsonRpc` object's](#jsonrpc) [`get_info`](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L101) method to retrieve the **chainId**. +* The `transact` method then checks if the `blocksBehind` and `expiration` fields are set and well-formed in the [optional configuration object, as specified in *How to Submit a Transaction*](how-to-guides/01_how-to-submit-a-transaction.md#). + * If so, the block *blocksBehind* the head block retrieved from [`JsonRpc`'s `get_info`](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L101) is set as the reference block and the transaction header is serialized using this reference block and the `expiration` field. +* The `transact` method then checks if the appropriate TAPOS fields are present in the transaction ([they can either be specified directly in the transaction or in the optional configuration object](how-to-guides/01_how-to-submit-a-transaction.md#)) and throws an Error if not. +* The necessary `abi`s for a transaction are then retrieved for the case when `transact` is expected to sign the transaction. +* The `actions` are serialized using the `eosjs-serialize` `ser` object. +* The entire transaction is then [serialized](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-api.ts#L154-L166), also using the `eosjs-serialize` `ser` object. +* The transaction is then optionally signed, using the `signatureProvider`, the previously retrieved `abi`s, the private keys of the `signatureProvider`, and the `chainId`. +* The transaction is then optionally broadcasted using `JsonRpc`'s [`push_transaction`](https://github.com/EOSIO/eosjs/blob/master/src/eosjs-jsonrpc.ts#L187). \ No newline at end of file diff --git a/docs/02_installation.md b/docs/02_installation.md new file mode 100644 index 000000000..4c0b4e9ac --- /dev/null +++ b/docs/02_installation.md @@ -0,0 +1,9 @@ +`eosjs` can be installed via [`yarn`](https://yarnpkg.com/en/) +```javascript +yarn add eosjs +``` + +or [`npm`](https://www.npmjs.com/) +```javascript +npm install eosjs +``` \ No newline at end of file diff --git a/docs/2.-Transaction-Examples.md b/docs/2.-Transaction-Examples.md deleted file mode 100644 index d59dbd9d3..000000000 --- a/docs/2.-Transaction-Examples.md +++ /dev/null @@ -1,178 +0,0 @@ -# Transactions - -To send transactions and trigger actions on the blockchain, you must have an instance of `Api`. - -The signature provider must contain the private keys corresponding to the actors and permission requirements of the actions being executed. - - -## CommonJS - -```javascript -const { Api, JsonRpc } = require('eosjs'); -const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig'); // development only -const fetch = require('node-fetch'); // node only -const { TextDecoder, TextEncoder } = require('util'); // node only -const { TextEncoder, TextDecoder } = require('text-encoding'); // React Native, IE11, and Edge Browsers only - -const privateKeys = [privateKey1]; - -const signatureProvider = new JsSignatureProvider(privateKeys); -const rpc = new JsonRpc('http://127.0.0.1:8888', { fetch }); -const api = new Api({ rpc, signatureProvider, textDecoder: new TextDecoder(), textEncoder: new TextEncoder() }); -``` - -## ES Modules - -```javascript -import { Api, JsonRpc } from 'eosjs'; -import { JsSignatureProvider } from 'eosjs/dist/eosjs-jssig'; // development only - -const privateKeys = [privateKey1]; - -const signatureProvider = new JsSignatureProvider(privateKeys); -const rpc = new JsonRpc('http://127.0.0.1:8888'); -const api = new Api({ rpc, signatureProvider }) -``` - -## Examples - -### Buy ram - -```javascript -const result = await api.transact({ - actions: [{ - account: 'eosio', - name: 'buyrambytes', - authorization: [{ - actor: 'useraaaaaaaa', - permission: 'active', - }], - data: { - payer: 'useraaaaaaaa', - receiver: 'useraaaaaaaa', - bytes: 8192, - }, - }] -}, { - blocksBehind: 3, - expireSeconds: 30, -}); -``` - -### Stake - -```javascript -const result = await api.transact({ - actions: [{ - account: 'eosio', - name: 'delegatebw', - authorization: [{ - actor: 'useraaaaaaaa', - permission: 'active', - }], - data: { - from: 'useraaaaaaaa', - receiver: 'useraaaaaaaa', - stake_net_quantity: '1.0000 SYS', - stake_cpu_quantity: '1.0000 SYS', - transfer: false, - } - }] -}, { - blocksBehind: 3, - expireSeconds: 30, -}); -``` - -## Example: Unstake - -```javascript -const result = await api.transact({ - actions: [{ - account: 'eosio', - name: 'undelegatebw', - authorization: [{ - actor: 'useraaaaaaaa', - permission: 'active', - }], - data: { - from: 'useraaaaaaaa', - receiver: 'useraaaaaaaa', - unstake_net_quantity: '1.0000 SYS', - unstake_cpu_quantity: '1.0000 SYS', - transfer: false, - } - }] -}, { - blocksBehind: 3, - expireSeconds: 30, -}); -``` - -### Create New Account (multiple actions) - -```javascript -const result = await api.transact({ - actions: [{ - account: 'eosio', - name: 'newaccount', - authorization: [{ - actor: 'useraaaaaaaa', - permission: 'active', - }], - data: { - creator: 'useraaaaaaaa', - name: 'mynewaccount', - owner: { - threshold: 1, - keys: [{ - key: 'PUB_R1_6FPFZqw5ahYrR9jD96yDbbDNTdKtNqRbze6oTDLntrsANgQKZu', - weight: 1 - }], - accounts: [], - waits: [] - }, - active: { - threshold: 1, - keys: [{ - key: 'PUB_R1_6FPFZqw5ahYrR9jD96yDbbDNTdKtNqRbze6oTDLntrsANgQKZu', - weight: 1 - }], - accounts: [], - waits: [] - }, - }, - }, - { - account: 'eosio', - name: 'buyrambytes', - authorization: [{ - actor: 'useraaaaaaaa', - permission: 'active', - }], - data: { - payer: 'useraaaaaaaa', - receiver: 'mynewaccount', - bytes: 8192, - }, - }, - { - account: 'eosio', - name: 'delegatebw', - authorization: [{ - actor: 'useraaaaaaaa', - permission: 'active', - }], - data: { - from: 'useraaaaaaaa', - receiver: 'mynewaccount', - stake_net_quantity: '1.0000 SYS', - stake_cpu_quantity: '1.0000 SYS', - transfer: false, - } - }] -}, { - blocksBehind: 3, - expireSeconds: 30, -}); -``` diff --git a/docs/4.-Reading blockchain-Examples.md b/docs/4.-Reading blockchain-Examples.md deleted file mode 100644 index ec3a96170..000000000 --- a/docs/4.-Reading blockchain-Examples.md +++ /dev/null @@ -1,164 +0,0 @@ -# Reading blockchain - -Reading blockchain state only requires an instance of `JsonRpc` connected to a node. - -```javascript -const { JsonRpc } = require('eosjs'); -const fetch = require('node-fetch'); // node only; not needed in browsers -const rpc = new JsonRpc('http://localhost:8888', { fetch }); -``` - -## Examples - -### Get table rows - -Get the first 10 token balances of account _testacc_. - -```javascript -const resp = await rpc.get_table_rows({ - json: true, // Get the response as json - code: 'eosio.token', // Contract that we target - scope: 'testacc', // Account that owns the data - table: 'accounts', // Table name - limit: 10, // Maximum number of rows that we want to get - reverse: false, // Optional: Get reversed data - show_payer: false, // Optional: Show ram payer -}); - -console.log(resp.rows); -``` -Output: - -```json -{ - "rows": [{ - "balance": "100.0000 HAK" - } - ], - "more": false -} -``` - -### Get one row by index - -```javascript -const resp = await rpc.get_table_rows({ - json: true, // Get the response as json - code: 'contract', // Contract that we target - scope: 'contract', // Account that owns the data - table: 'profiles', // Table name - lower_bound: 'testacc', // Table primary key value - limit: 1, // Here we limit to 1 to get only the - reverse: false, // Optional: Get reversed data - show_payer: false, // Optional: Show ram payer -}); -console.log(resp.rows); -``` -Output: - -```json -{ - "rows": [{ - "user": "testacc", - "age": 21, - "surname": "Martin" - } - ], - "more": false -} -``` - -### Get one row by secondary index - -```javascript -const resp = await rpc.get_table_rows({ - json: true, // Get the response as json - code: 'contract', // Contract that we target - scope: 'contract', // Account that owns the data - table: 'profiles', // Table name - table_key: 'age', // Table secondaray key name - lower_bound: 21, // Table secondary key value - limit: 1, // Here we limit to 1 to get only row - reverse: false, // Optional: Get reversed data - show_payer: false, // Optional: Show ram payer -}); -console.log(resp.rows); -``` -Output: - -```json -{ - "rows": [{ - "user": "testacc", - "age": 21, - "surname": "Martin" - } - ], - "more": false -} -``` - -### Get currency balance - -```javascript -console.log(await rpc.get_currency_balance('eosio.token', 'testacc', 'HAK')); -``` -Output: - -```json -[ "1000000000.0000 HAK" ] -``` - -### Get account info - -```javascript -console.log(await rpc.get_account('testacc')); -``` -Output: - -```json -{ "account_name": "testacc", - "head_block_num": 1079, - "head_block_time": "2018-11-10T00:45:53.500", - "privileged": false, - "last_code_update": "1970-01-01T00:00:00.000", - "created": "2018-11-10T00:37:05.000", - "ram_quota": -1, - "net_weight": -1, - "cpu_weight": -1, - "net_limit": { "used": -1, "available": -1, "max": -1 }, - "cpu_limit": { "used": -1, "available": -1, "max": -1 }, - "ram_usage": 2724, - "permissions": - [ { "perm_name": "active", "parent": "owner", "required_auth": [] }, - { "perm_name": "owner", "parent": "", "required_auth": [] } ], - "total_resources": null, - "self_delegated_bandwidth": null, - "refund_request": null, - "voter_info": null } -``` - -### Get block - -```javascript -console.log(await rpc.get_block(1)); -``` -Output: - -```json -{ "timestamp": "2018-06-01T12:00:00.000", - "producer": "", - "confirmed": 1, - "previous": "0000000000000000000000000000000000000000000000000000000000000000", - "transaction_mroot": "0000000000000000000000000000000000000000000000000000000000000000", - "action_mroot": "cf057bbfb72640471fd910bcb67639c22df9f92470936cddc1ade0e2f2e7dc4f", - "schedule_version": 0, - "new_producers": null, - "header_extensions": [], - "producer_signature": "SIG_K1_111111111111111111111111111111111111111111111111111111111111111116uk5ne", - "transactions": [], - "block_extensions": [], - "id": "00000001bcf2f448225d099685f14da76803028926af04d2607eafcf609c265c", - "block_num": 1, - "ref_block_prefix": 2517196066 } -``` diff --git a/docs/3.-Browsers.md b/docs/basic-usage/00_browser.md similarity index 72% rename from docs/3.-Browsers.md rename to docs/basic-usage/00_browser.md index 35a485069..90275f30a 100644 --- a/docs/3.-Browsers.md +++ b/docs/basic-usage/00_browser.md @@ -1,16 +1,14 @@ -# Browsers - -## Usage -`npm run build-web` or `yarn build-web` - -Reuse the `api` object for all transactions; it caches ABIs to reduce network usage. Only call `new eosjs_api.Api(...)` once. - +To use `eosjs` in a browser run `npm run build-web` or `yarn build-web`. This will create the `dist-web` folder and web distribution modules. ```html
+``` + +To cache ABIs and reduce network usage, reuse the `api` object for all transactions. This implies you should only call `new eosjs_api.Api(...)` once. +```html