docs: make it clear that npm vs npx is either or
[crowdnode.js/.git] / README.md
1 # CrowdNode Node.js SDK
2
3 CrowdNode allows you to become a partial MNO - staking Dash to earn interest,
4 participate in voting, etc.
5
6 The CrowdNode Node.js SDK enables you to build Web-based flows and
7 cross-platform CLI tools to privately manage staking using CrowdNode's KYC-free
8 Blockchain API.
9
10 # Install
11
12 ## Node.js
13
14 You must have [node.js](https://webinstall.dev/node) installed:
15
16 ```bash
17 # Mac, Linux
18 curl https://webinstall.dev/node | bash
19 export PATH="${HOME}/.local/opt/node:$PATH"
20 ```
21
22 ```pwsh
23 # Windows
24 curl.exe -A MS https://webinstall.dev/node | powershell
25 PATH %USERPROFILE%\.local\opt\node;%PATH%
26 ```
27
28 ## CrowdNode SDK
29
30 ```bash
31 npm install --save crowdnode@v1
32 ```
33
34 # API
35
36 The SDK also provides Type Hinting via JSDoc (compatible with TypeScript / tsc
37 without any transpiling).
38
39 ## QuickStart
40
41 The CrowdNode SDK uses Dashcore to create raw transactions and broadcasts them
42 as Instant Send via the Dash Insight API. It uses Dash Insight WebSockets to
43 listen for responses from the CrowdNode hotwallet.
44
45 A simple CrowdNode application may look like this:
46
47 ```js
48 "use strict";
49
50 let Fs = require("fs").promises;
51 let CrowdNode = require("crowdnode");
52
53 async function main() {
54   let keyfile = process.argv[2];
55
56   // a wallet pre-loaded with about Đ0.001
57   let wif = await Fs.readFile(keyfile, "utf8");
58   wif = wif.trim();
59
60   // Initializes API info, such as hotwallets
61   await CrowdNode.init({ insightBaseUrl: "https://insight.dash.org/" });
62
63   let hotwallet = CrowdNode.main.hotwallet;
64   await CrowdNode.signup(wif, hotwallet);
65   await CrowdNode.accept(wif, hotwallet);
66   await CrowdNode.deposit(wif, hotwallet);
67
68   console.info("Congrats! You're staking!");
69 }
70
71 main().catch(function (err) {
72   console.error("Fail:");
73   console.error(err.stack || err);
74   process.exit(1);
75 });
76 ```
77
78 There are also a number of utility functions which are not exposed as public
79 APIs, but which you could learn from in [crowdnode-cli](/bin/crowdnode.js).
80
81 ## Constants
82
83 ```js
84 CrowdNode.offset = 20000;
85 CrowdNode.duffs = 100000000;
86 CrowdNode.depositMinimum = 10000;
87
88 CrowdNode.requests = {
89   acceptTerms: 65536,
90   offset: 20000,
91   signupForApi: 131072,
92   toggleInstantPayout: 4096,
93   withdrawMin: 1,
94   withdrawMax: 1000,
95 };
96
97 CrowdNode.responses = {
98   PleaseAcceptTerms: 2,
99   WelcomeToCrowdNodeBlockChainAPI: 4,
100   DepositReceived: 8,
101   WithdrawalQueued: 16,
102   WithdrawalFailed: 32,
103   AutoWithdrawalEnabled: 64,
104   AutoWithdrawalDisabled: 128,
105 };
106 ```
107
108 ## Usage
109
110 ### Manage Stake
111
112 ```js
113 await CrowdNode.init({ insightBaseUrl: "https://insight.dash.org" });
114
115 CrowdNode.main.baseUrl; // "https://app.crowdnode.io"
116 CrowdNode.main.hotwallet; // "XjbaGWaGnvEtuQAUoBgDxJWe8ZNv45upG2"
117
118 await CrowdNode.status(pubAddress, hotwallet);
119 /*
120  * {
121  *   signup: 0, // seconds since unix epoch
122  *   accept: 0,
123  *   deposit: 0,
124  * }
125  */
126
127 await CrowdNode.signup(wif, hotwallet);
128 /** @type SocketPayment
129  * {
130  *   "address": "Xj00000000000000000000000000000000",
131  *   "satoshis": 20002, // PleaseAcceptTerms
132  *   "timestamp": 1655634136000,
133  *   "txid": "xxxx...",
134  *   "txlock": true
135  * }
136  */
137
138 await CrowdNode.accept(wif, hotwallet);
139 /** @type SocketPayment
140  * {
141  *   "address": "Xj00000000000000000000000000000000",
142  *   "satoshis": 20004, // WelcomeToCrowdNodeBlockChainAPI
143  *   "timestamp": 1655634138000,
144  *   "txid": "xxxx...",
145  *   "txlock": true
146  * }
147  */
148
149 await CrowdNode.deposit(wif, hotwallet, (amount = 0));
150 /** @type SocketPayment
151  * {
152  *   "address": "Xj00000000000000000000000000000000",
153  *   "satoshis": 20008, // DepositReceived
154  *   "timestamp": 1655634142000,
155  *   "txid": "xxxx...",
156  *   "txlock": true
157  * }
158  */
159
160 await CrowdNode.withdrawal(wif, hotwallet, permil);
161 /** @type SocketPayment
162  * {
163  *   "address": "Xj00000000000000000000000000000000",
164  *   "satoshis": 20016, // WithdrawalQueued
165  *   "timestamp": 1657634142000,
166  *   "txid": "xxxx...",
167  *   "txlock": true
168  * }
169  */
170 ```
171
172 ### HTTP RPC
173
174 ```js
175 await CrowdNode.http.GetBalance(pubAddr);
176 /** @type CrowdNodeBalance
177  * {
178  *   "DashAddress": "Xj00000000000000000000000000000000",
179  *   "TotalBalance": 0.01292824,
180  *   "TotalActiveBalance": 0,
181  *   "TotalDividend": 0,
182  *   "UpdatedOn": "2022-06-19T08:06:19.11",
183  *   "UpdateOnUnixTime": 1655625979
184  * }
185  */
186
187 await CrowdNode.http.GetFunds(pubAddr);
188 await CrowdNode.http.GetFundsFrom(pubAddr, secondsSinceEpoch);
189 /*
190  *  [
191  *    {
192  *      "FundingType": 1,
193  *      "Amount": 0.00810218,
194  *      "Time": 1655553336,
195  *      "TimeReceived": 1655553336,
196  *      "TxId": "e5a...",
197  *      "Status": 32,
198  *      "Comment": null,
199  *      "TimeUTC": "2022-06-18T11:55:36",
200  *      "Id": 3641556,
201  *      "UpdatedOn": "2022-06-18T12:04:15.1233333"
202  *    }
203  *  ]
204  */
205
206 await CrowdNode.http.IsAddressInUse(pubAddr);
207 /**
208  * {
209  *   "inUse": true,
210  *   "DashAddress": "Xj00000000000000000000000000000000"
211  * }
212  */
213 ```
214
215 ### Messages (Voting, etc)
216
217 ```js
218 await CrowdNode.http.GetMessages(pubAddr);
219 /**
220  * []
221  */
222
223 await CrowdNode.http.SetEmail(wif, email, sig);
224 await CrowdNode.http.Vote(wif, gobjectHash, vote, sig);
225 await CrowdNode.http.SetReferral(wif, referralId, sig);
226 ```
227
228 ```js
229 await CrowdNode.http.FundsOpen(pub);
230 /* ${baseUrl}/FundsOpen/${pub} */
231
232 await CrowdNode.http.VotingOpen(pub);
233 /* ${baseUrl}/VotingOpen/${pub} */
234 ```
235
236 # CLI Documentation
237
238 See <https://github.com/dashhive/crowdnode.js/tree/main/cli>.
239
240 # Official CrowdNode Docs
241
242 <https://knowledge.crowdnode.io/en/articles/5963880-blockchain-api-guide>