1 | # js-conflux-sdk
|
2 |
|
3 | [![npm](https://img.shields.io/npm/v/js-conflux-sdk.svg)](https://www.npmjs.com/package/js-conflux-sdk)
|
4 | [![npm](https://img.shields.io/npm/dm/js-conflux-sdk.svg)](https://www.npmjs.com/package/js-conflux-sdk)
|
5 | -----------------------
|
6 |
|
7 | JavaScript Conflux Software Development Kit
|
8 |
|
9 | ## Installation
|
10 |
|
11 | `npm install js-conflux-sdk`
|
12 |
|
13 | ## Quick Usage
|
14 |
|
15 | ### Nodejs
|
16 | ```javascript
|
17 | const { Conflux } = require('js-conflux-sdk');
|
18 |
|
19 | async function main() {
|
20 | // initalize a Conflux object
|
21 | const conflux = new Conflux({
|
22 | url: 'http://testnet-jsonrpc.conflux-chain.org:12537',
|
23 | logger: console, // for debug
|
24 | });
|
25 | // get balance
|
26 | const balance = await conflux.getBalance('0x1bd9e9be525ab967e633bcdaeac8bd5723ed4d6b');
|
27 | console.log(balance); // 10098788868004995614504
|
28 | }
|
29 |
|
30 | main();
|
31 | ```
|
32 | require deep nested file/dir
|
33 |
|
34 | ``` javascript
|
35 | const util = require('js-conflux-sdk/src/util');
|
36 | ```
|
37 |
|
38 | ### Frontend
|
39 |
|
40 | #### umd
|
41 | ``` javascript
|
42 | import { Conflux } from 'js-conflux-sdk/dist/js-conflux-sdk.umd.min.js';
|
43 | ```
|
44 |
|
45 | or if your bundler supports the [`browser` field](https://docs.npmjs.com/files/package.json#browser) in `package.json`
|
46 |
|
47 | ``` javascript
|
48 | import { Conflux } from 'js-conflux-sdk';
|
49 | ```
|
50 |
|
51 | or
|
52 |
|
53 | ``` html
|
54 | <script type="text/javascript" src="node_modules/js-conflux-sdk/dist/js-conflux-sdk.umd.min.js"></script>
|
55 | <script type="text/javascript">
|
56 | const conflux = new window.Conflux.Conflux({
|
57 | url: 'http://testnet-jsonrpc.conflux-chain.org:12537',
|
58 | logger: console,
|
59 | });
|
60 | </script>
|
61 | ```
|
62 |
|
63 | ## Examples
|
64 |
|
65 | * [Create conflux instance](https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/0_create_conflux.js)
|
66 | * [Account and balance](https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/1_account_and_balance.js)
|
67 | * [Send transaction](https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/2_send_transaction.js)
|
68 | * [Query epoch block transaction](https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/3_epoch_block_transaction.js)
|
69 | * [Contract deploy and call](https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/4_contract_deploy_and_call.js)
|
70 | * [Contract override](https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/5_contract_override.js)
|
71 |
|
72 | ### Send transaction simple
|
73 |
|
74 | ```js
|
75 | const { Conflux, Drip } = require('js-conflux-sdk');
|
76 |
|
77 | async function main() {
|
78 | const conflux = new Conflux({ url: 'http://testnet-jsonrpc.conflux-chain.org:12537' });
|
79 | const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet
|
80 |
|
81 | const receipt = await conflux.sendTransaction({
|
82 | from: account.address, // sender address which added into conflux.wallet
|
83 | to: ADDRESS, // receiver address
|
84 | value: Drip.fromCFX(0.1), // 0.1 CFX = 100000000000000000 Drip
|
85 | }).executed(); // wait till transaction executed and get receipt
|
86 |
|
87 | console.log(receipt); // outcomeStatus 0 means success
|
88 | /*
|
89 | {
|
90 | "index": 0,
|
91 | "epochNumber": 784649,
|
92 | "outcomeStatus": 0,
|
93 | "gasUsed": "21000",
|
94 | "gasFee": "21000000000000",
|
95 | "blockHash": "0xfa7c6d9d0c8ae436f1c9c785a316ac6cc4db16286eede3dd3d5c6a5a22ad5f9e",
|
96 | "contractCreated": null,
|
97 | "from": "0x1bd9e9be525ab967e633bcdaeac8bd5723ed4d6b",
|
98 | "logs": [],
|
99 | "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
100 | "stateRoot": "0x166cbcbfd747505d91237ccd8f849fd6882ad0e6d8b9923ca3c44891cf2b8753",
|
101 | "to": "0x1ead8630345121d19ee3604128e5dc54b36e8ea6",
|
102 | "transactionHash": "0x50cd13d5f97dd867d4ca65e24eb642f6444c07d6af8143018c558df456f11e63"
|
103 | }
|
104 | */
|
105 | }
|
106 |
|
107 | main();
|
108 | ```
|
109 |
|
110 | ### Send transaction complete
|
111 |
|
112 | For the detail explanation of these fields check [official doc](https://developer.conflux-chain.org/docs/conflux-doc/docs/send_transaction#installation).
|
113 |
|
114 | ```javascript
|
115 | const { Conflux, Drip } = require('js-conflux-sdk');
|
116 |
|
117 | async function main() {
|
118 | const conflux = new Conflux({ url: 'http://testnet-jsonrpc.conflux-chain.org:12537' });
|
119 | const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet
|
120 |
|
121 | const estimate = await conflux.estimateGasAndCollateral({ to, value });
|
122 | const status = await conflux.getStatus();
|
123 |
|
124 | const txHash = await conflux.sendTransaction({
|
125 | from: account.address, // or just `from: account`
|
126 | to: ADDRESS,
|
127 | value: Drip.fromGDrip(100), // 100 GDrip = 100000000000 Drip
|
128 | gas: estimate.gasUsed,
|
129 | storageLimit: estimate.storageCollateralized,
|
130 | chainId: status.chainId,
|
131 | data: null,
|
132 | nonce: await conflux.getNextNonce(ADDRESS),
|
133 | gasPrice: await conflux.getGasPrice(),
|
134 | epochHeight: await conflux.getEpochNumber(),
|
135 | });
|
136 | console.log(txHash);
|
137 |
|
138 | // you might need wait minute here...
|
139 | await new Promise(resolve => setTimeout(resolve, 30*1000));
|
140 |
|
141 | const transaction = await conflux.getTransactionByHash(txHash);
|
142 | console.log(transaction);
|
143 |
|
144 | const receipt = await conflux.getTransactionReceipt(txHash);
|
145 | console.log(receipt);
|
146 | }
|
147 |
|
148 | main();
|
149 | ```
|
150 |
|
151 | ### Deploy contract
|
152 |
|
153 | ```javascript
|
154 | const { Conflux } = require('js-conflux-sdk');
|
155 | const { abi, bytecode } = MINI_ERC20; // see https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/contract/miniERC20.json
|
156 |
|
157 | async function main() {
|
158 | const conflux = new Conflux({ url: 'http://testnet-jsonrpc.conflux-chain.org:12537' });
|
159 | const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet
|
160 | const contract = conflux.Contract({ abi, bytecode });
|
161 |
|
162 | const receipt = await contract.constructor('MiniERC20', 18, 'MC', 10000)
|
163 | .sendTransaction({ from: account })
|
164 | .executed();
|
165 | console.log(receipt);
|
166 | /*
|
167 | {
|
168 | "index": 0,
|
169 | "epochNumber": 318456,
|
170 | "outcomeStatus": 0,
|
171 | "gasUsed": "1054531",
|
172 | "gasFee": "1054531000000000",
|
173 | "blockHash": "0x4a8b07e2694e358af075f7a9e96e78842b77ac2d511e2ab33f6acfff34a5846c",
|
174 | "contractCreated": "0x8a9c270e1a99c05ca90ef0f0008b8f6444cf1a97",
|
175 | "from": "0x1bd9e9be525ab967e633bcdaeac8bd5723ed4d6b",
|
176 | "logs": [],
|
177 | "logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
|
178 | "stateRoot": "0x0940d4870e25bae1e7a5e5d7c19411b41922c025aa3de61aea2be17759673b1a",
|
179 | "to": null,
|
180 | "transactionHash": "0x6f55e67b486b5ef0c658c6d50cb5b89a2a2ddfecc1a1f2e414bbbefe36ef8dd5"
|
181 | }
|
182 | */
|
183 |
|
184 | // create contract address "0x8a9c270e1a99c05ca90ef0f0008b8f6444cf1a97"
|
185 | }
|
186 |
|
187 | main();
|
188 | ```
|
189 |
|
190 | ### Call contract
|
191 |
|
192 | ```javascript
|
193 | const { Conflux } = require('js-conflux-sdk');
|
194 | const { abi } = MINI_ERC20; // see https://github.com/Conflux-Chain/js-conflux-sdk/blob/v1.x/example/contract/miniERC20.json
|
195 |
|
196 | async function main() {
|
197 | const conflux = new Conflux({ url: 'http://testnet-jsonrpc.conflux-chain.org:12537' });
|
198 | const account = conflux.wallet.addPrivateKey(PRIVATE_KEY); // create account instance and add to wallet
|
199 | const contract = conflux.Contract({ abi, address: '0x8a9c270e1a99c05ca90ef0f0008b8f6444cf1a97' });
|
200 |
|
201 | const name = await contract.name(); // call method without arguments
|
202 | console.log(name); // MiniERC20
|
203 | // use can set options by `contract.name().call({ from: account, ... })`
|
204 |
|
205 | const balance = await contract.balanceOf(account.address); // call method with arguments
|
206 | console.log(balance); // "10000" JSBI
|
207 |
|
208 | const txHash = await contract.transfer(ADDRESS, 10).sendTransaction({ from: account }); // send method transaction
|
209 | console.log(txHash); // 0xb31eb095b62bed1ef6fee6b7b4ee43d4127e4b42411e95f761b1fdab89780f1a
|
210 | // use can set options by `contract.transfer(ADDRESS, 10).sendTransaction({ from: account, gasPrice: <number>, ... })`
|
211 | }
|
212 |
|
213 | main();
|
214 | ```
|
215 |
|
216 | ## Document
|
217 |
|
218 | * [API](https://github.com/Conflux-Chain/js-conflux-sdk/blob/master/api.md)
|
219 |
|
220 | ## Change log
|
221 |
|
222 | [see](https://github.com/Conflux-Chain/js-conflux-sdk/tree/master/CHANGE_LOG.md)
|
223 |
|