UNPKG

2.88 kBMarkdownView Raw
1# ETH RPC
2
3![](https://github.com/romfrolov/eth-json-rpc/workflows/build/badge.svg) [![npm version](https://badge.fury.io/js/eth-json-rpc.svg)](https://badge.fury.io/js/eth-json-rpc) [![Coverage Status](https://coveralls.io/repos/github/romfrolov/eth-json-rpc/badge.svg?branch=master)](https://coveralls.io/github/romfrolov/eth-json-rpc?branch=master) [![install size](https://packagephobia.now.sh/badge?p=eth-json-rpc)](https://packagephobia.now.sh/result?p=eth-json-rpc)
4
5Lightweight wrapper library on top of [Ethereum JSON RPC](https://github.com/ethereum/wiki/wiki/JSON-RPC).
6
7## Quick start
8
9```bash
10npm install eth-json-rpc
11```
12
13```js
14const ethRpc = require('eth-json-rpc')('https://mainnet.infura.io');
15
16(async () => {
17
18 // Get block number.
19 const blockNumber = await ethRpc.eth.blockNumber();
20
21 console.log(blockNumber); // 7280000
22
23 // Call contract method.
24 const totalSupply = await ethRpc.eth.call({methodSignature: 'totalSupply()', to: CONTRACT_ADDRESS});
25
26 console.log(totalSupply); // 0x00000000000000000000000000000000000000000000d3c21bcecceda1000000
27
28 // Send transaction to contract.
29 const transactionHash = await ethRpc.eth.transaction({methodSignature: 'mint(uint256)', to: CONTRACT_ADDRESS, args: [100], privateKey: PRIVATE_KEY});
30
31 console.log(transactionHash); // 0x36af4c76dd7f2a204b1a340fb6327ae8ff9e2efe2f974b054d3a36314635a10c
32
33})();
34```
35
36## Features
37
38- lightweight
39- support of batch RPC requests
40- minimum level of abstraction layers
41- ease of work with contracts
42
43## API
44
45### eth
46
47- `call` - Call contract method.
48- `transaction` - Create transaction object, sign transaction, serialize transaction, send transaction.
49- `gasPrice` - Get gas price.
50- `getCode` - Get code at address.
51- `getTransactionReceipt` - Get transaction receipt.
52- `getTransactionCount` - Get number of transactions the address sent.
53- `blockNumber` - Get number of the latest block.
54- `getBlock` - Get block by number.
55- `getLogs` - Get logs from blocks.
56- `getBlocks` - Get blocks with logs in a batch RPC request.
57- `getBlocksFromArray` - Get blocks with logs in a batch RPC request with optional consistency.
58
59### utils
60
61- `isZeroAddress` - Check whether address is zero or not.
62- `isValidAddress` - Validate address.
63- `getMethodOutputParameters` - Get method output parameter types from contract ABI.
64- `decodeRawOutput` - Decode raw data returned from the contract call.
65
66For a complete documentation visit [documentation](#documentation) section.
67
68## Documentation
69
70```bash
71npm run docs # generate docs
72npm run http-docs # start HTTP server serving docs
73```
74
75## Tests
76
77```bash
78npm test
79```
80
81## License
82
83The eth-json-rpc library is licensed under the GNU GENERAL PUBLIC LICENSE, which can be found in this repository in the `LICENSE` file.
84
85## Acknowledgments
86
87- This library was written with support of [Wings Project](https://wings.ai)