Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 6 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,10 @@ npm install --save crowdnode@v1
## CrowdNode Browser SDK

```html
<script src="https://unpkg.com/@root/[email protected]/urequest.js"></script>
<script src="https://unpkg.com/dashsight@1.x/dashrequest.js"></script>
<script src="https://unpkg.com/[email protected]/dashtx.js"></script>
<script src="https://unpkg.com/dashkeys@1.x/dashkeys.js"></script>
<script src="https://unpkg.com/[email protected]/dashsight.js"></script>
<script src="https://unpkg.com/[email protected]/dashsocket.js"></script>
<script src="https://unpkg.com/@dashevo/[email protected]"></script>
<script src="https://unpkg.com/[email protected]/dashapi.js"></script>
<script src="https://unpkg.com/[email protected]/crowdnode.js"></script>
```
Expand Down Expand Up @@ -124,7 +123,7 @@ APIs, but which you could learn from in [crowdnode-cli](/bin/crowdnode.js).

```js
CrowdNode.offset = 20000;
CrowdNode.duffs = 100000000;
CrowdNode.satoshis = 100000000;
CrowdNode.depositMinimum = 100000;

CrowdNode.requests = {
Expand Down Expand Up @@ -188,8 +187,8 @@ await CrowdNode.accept(wif, hotwallet);
* }
*/

// amount given in DUFFs
await CrowdNode.deposit(wif, hotwallet, (amount = 0));
// satoshis (a.k.a. duffs) is the base unit of DASH
await CrowdNode.deposit(wif, hotwallet, (satoshis = 0));
/** @type SocketPayment
* {
* "address": "Xj00000000000000000000000000000000",
Expand Down Expand Up @@ -282,7 +281,7 @@ await CrowdNode.http.VotingOpen(pub);
| Term | Description |
| ------------- | ------------------------------------------------------------- |
| addr | your Dash address (Base58Check-encoded Pay-to-PubKey Address) |
| amount | the integer value of "Duffs" (Đ/100000000) |
| satoshis | the base unit of DASH (a.k.a. "Duffs") Đ/100000000 |
| permil | 1/1000, 1‰, or 0.1% - between 1 and 1000 (0.1% to 100.0%) |
| ./privkey.wif | the file path to your staking key in WIF (Base58Check) format |

Expand Down
139 changes: 68 additions & 71 deletions bin/crowdnode.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ let Prompt = require("./_prompt.js");
let Qr = require("./_qr-node.js");
let Ws = require("dashsight/ws");

let Dashcore = require("@dashevo/dashcore-lib");
let Dashcore = require("../dashcore-lit.js");

const DONE = "✅";
const TODO = "ℹ️";
const NO_SHADOW = "NONE";
const DUFFS = 100000000;
const SATOSHIS = 100000000;

let shownDefault = false;
let qrWidth = 2 + 33 + 2;
// Sign Up Fees:
// 0.00236608 // required for signup
// 0.00002000 // TX fee estimate
// 0.00238608 // minimum recommended amount
// 0.00238608 // minimum recommended DASH
// Target:
// 0.01000000
let signupOnly = CrowdNode.requests.signupForApi + CrowdNode.requests.offset;
Expand Down Expand Up @@ -472,20 +472,20 @@ async function stakeDash(
extra += acceptDeposit;
}

let desiredAmountDash = args.shift() || "0.5";
let effectiveDuff = toDuff(desiredAmountDash);
effectiveDuff += extra;
let desiredDashAmount = args.shift() || "0.5";
let effectiveSats = toDuff(desiredDashAmount);
effectiveSats += extra;

let balanceInfo = await dashApi.getInstantBalance(addr);
effectiveDuff -= balanceInfo.balanceSat;
effectiveSats -= balanceInfo.balanceSat;

if (effectiveDuff > 0) {
effectiveDuff = roundDuff(effectiveDuff, 3);
let effectiveDash = toDash(effectiveDuff);
if (effectiveSats > 0) {
effectiveSats = roundDuff(effectiveSats, 3);
let effectiveDash = toDash(effectiveSats);
await plainLoadAddr({
addr,
effectiveDash,
effectiveDuff,
effectiveSats,
});
}

Expand Down Expand Up @@ -663,7 +663,7 @@ async function mustGetAddr({ defaultAddr }, args) {
return [addr, name];
}
//let pk = new Dashcore.PrivateKey(wif);
//let addr = pk.toAddress().toString();
//let addr = (await pk.toAddress()).toString();
return [addr, name];
}

Expand Down Expand Up @@ -796,8 +796,8 @@ async function generateKey({ defaultKey, plainText }, args) {
//@ts-ignore - TODO submit JSDoc PR for Dashcore
let pk = new Dashcore.PrivateKey();

let addr = pk.toAddress().toString();
let plainWif = pk.toWIF();
let addr = (await pk.toAddress()).toString();
let plainWif = await pk.toWIF();

let wif = plainWif;
if (!plainText) {
Expand Down Expand Up @@ -1259,7 +1259,7 @@ async function maybeReadKeyFileRaw(filepath, opts) {
}

let pk = new Dashcore.PrivateKey(privKey);
let pub = pk.toAddress().toString();
let pub = (await pk.toAddress()).toString();

return {
addr: pub,
Expand Down Expand Up @@ -1326,7 +1326,7 @@ async function setDefault(_, args) {
let filepath = Path.join(keysDir, keyname);
let wif = await maybeReadKeyFile(filepath);
let pk = new Dashcore.PrivateKey(wif);
let pub = pk.toAddress().toString();
let pub = (await pk.toAddress()).toString();

console.info("set", defaultWifPath, pub);
await Fs.writeFile(defaultWifPath, pub, "utf8");
Expand Down Expand Up @@ -1439,7 +1439,7 @@ async function getAllBalances({ dashApi, defaultAddr }, args) {

/*
let pk = new Dashcore.PrivateKey(wif);
let pub = pk.toAddress().toString();
let pub = (await pk.toAddress()).toString();
if (`${pub}.wif` !== wifname) {
// sanity check
warns.push({
Expand Down Expand Up @@ -1625,47 +1625,47 @@ async function listManagedKeynames() {
async function loadAddr({ defaultAddr, insightBaseUrl }, args) {
let [addr] = await mustGetAddr({ defaultAddr }, args);

let desiredAmountDash = parseFloat(args.shift() || "0");
let desiredAmountDuff = Math.round(desiredAmountDash * DUFFS);
let desiredDashAmount = parseFloat(args.shift() || "0");
let desiredSatoshis = Math.round(desiredDashAmount * SATOSHIS);

let effectiveDuff = desiredAmountDuff;
let effectiveSats = desiredSatoshis;
let effectiveDash = "";
if (!effectiveDuff) {
effectiveDuff = CrowdNode.stakeMinimum + signupTotal + feeEstimate;
effectiveDuff = roundDuff(effectiveDuff, 3);
effectiveDash = toDash(effectiveDuff);
if (!effectiveSats) {
effectiveSats = CrowdNode.stakeMinimum + signupTotal + feeEstimate;
effectiveSats = roundDuff(effectiveSats, 3);
effectiveDash = toDash(effectiveSats);
}

await plainLoadAddr({ addr, effectiveDash, effectiveDuff, insightBaseUrl });
await plainLoadAddr({ addr, effectiveDash, effectiveSats, insightBaseUrl });

return;
}

/**
* 1000 to Round to the nearest mDash
* ex: 0.50238108 => 0.50300000
* @param {Number} effectiveDuff
* @param {Number} effectiveSats
* @param {Number} numDigits
*/
function roundDuff(effectiveDuff, numDigits) {
function roundDuff(effectiveSats, numDigits) {
let n = Math.pow(10, numDigits);
let effectiveDash = toDash(effectiveDuff);
effectiveDuff = toDuff(
let effectiveDash = toDash(effectiveSats);
effectiveSats = toDuff(
(Math.ceil(parseFloat(effectiveDash) * n) / n).toString(),
);
return effectiveDuff;
return effectiveSats;
}

/**
* @param {Object} opts
* @param {String} opts.addr
* @param {String} opts.effectiveDash
* @param {Number} opts.effectiveDuff
* @param {Number} opts.effectiveSats
* @param {String} opts.insightBaseUrl
*/
async function plainLoadAddr({ addr, effectiveDash, effectiveDuff }) {
async function plainLoadAddr({ addr, effectiveDash, effectiveSats }) {
console.info(``);
showQr(addr, effectiveDuff);
showQr(addr, effectiveSats);
console.info(``);
console.info(
`Send Đ${effectiveDash} to your staking key via the QR above, or its address:`,
Expand Down Expand Up @@ -1718,16 +1718,16 @@ async function transferBalance(
//
// Ex:
// crowdnode transfer {source} {dest}
// crowdnode transfer {source} {dest} {amount}
// crowdnode transfer {dest} {amount}
// crowdnode transfer {source} {dest} {dash-amount}
// crowdnode transfer {dest} {dash-amount}
// crowdnode transfer {dest}
//
// To disambiguate, we check if the second argument is an amount.
// To disambiguate, we check if the second argument is a dash-amount.
if (3 === args.length) {
getAddrArgs = args;
} else if (2 === args.length) {
let maybeAmount = parseFloat(args[1]);
let isAddr = isNaN(maybeAmount);
let maybeDashAmount = parseFloat(args[1]);
let isAddr = isNaN(maybeDashAmount);
if (isAddr) {
getAddrArgs = args;
}
Expand All @@ -1737,17 +1737,17 @@ async function transferBalance(
let keyname = args.shift() || "";
let newAddr = await wifFileToAddr(keyname);
let dashAmount = parseFloat(args.shift() || "0");
let duffAmount = Math.round(dashAmount * DUFFS);
let satoshis = Math.round(dashAmount * SATOSHIS);
let tx;
if (duffAmount) {
tx = await dashApi.createPayment(wif, newAddr, duffAmount);
if (satoshis) {
tx = await dashApi.createPayment(wif, newAddr, satoshis);
} else {
tx = await dashApi.createBalanceTransfer(wif, newAddr);
}
if (duffAmount) {
let dashAmountStr = toDash(duffAmount);
if (satoshis) {
let dashAmountStr = toDash(satoshis);
console.info(
`Transferring ${duffAmount} (Đ${dashAmountStr}) to ${newAddr}...`,
`Transferring ${satoshis} (Đ${dashAmountStr}) to ${newAddr}...`,
);
} else {
console.info(`Transferring balance to ${newAddr}...`);
Expand Down Expand Up @@ -1922,18 +1922,18 @@ async function depositDash({ dashApi, defaultAddr, noReserve }, args) {

// deposit what the user asks, or all that we have,
// or all that the user deposits - but at least 2x the reserve
let desiredAmountDash = parseFloat(args.shift() || "0");
let desiredAmountDuff = Math.round(desiredAmountDash * DUFFS);
let effectiveAmount = desiredAmountDuff;
if (!effectiveAmount) {
effectiveAmount = balanceInfo.balanceSat - reserve;
let desiredDashAmount = parseFloat(args.shift() || "0");
let desiredSatoshis = Math.round(desiredDashAmount * SATOSHIS);
let effectiveSats = desiredSatoshis;
if (!effectiveSats) {
effectiveSats = balanceInfo.balanceSat - reserve;
}
let needed = Math.max(reserve * 2, effectiveAmount + reserve);
let needed = Math.max(reserve * 2, effectiveSats + reserve);

if (balanceInfo.balanceSat < needed) {
let ask = 0;
if (desiredAmountDuff) {
ask = desiredAmountDuff + reserve + -balanceInfo.balanceSat;
if (desiredSatoshis) {
ask = desiredSatoshis + reserve + -balanceInfo.balanceSat;
}
await collectDeposit(addr, ask);
balanceInfo = await dashApi.getInstantBalance(addr);
Expand All @@ -1946,18 +1946,16 @@ async function depositDash({ dashApi, defaultAddr, noReserve }, args) {
return;
}
}
if (!desiredAmountDuff) {
effectiveAmount = balanceInfo.balanceSat - reserve;
if (!desiredSatoshis) {
effectiveSats = balanceInfo.balanceSat - reserve;
}

let effectiveDash = toDash(effectiveAmount);
console.info(
`Initiating deposit of ${effectiveAmount} (Đ${effectiveDash})...`,
);
let effectiveDash = toDash(effectiveSats);
console.info(`Initiating deposit of ${effectiveSats} (Đ${effectiveDash})...`);

let wif = await maybeReadKeyPaths(name, { wif: true });

await CrowdNode.deposit(wif, hotwallet, effectiveAmount);
await CrowdNode.deposit(wif, hotwallet, effectiveSats);
state.deposit = DONE;
console.info(` ${state.deposit} DepositReceived`);
return;
Expand Down Expand Up @@ -2004,7 +2002,7 @@ async function withdrawDash({ dashApi, defaultAddr, insightBaseUrl }, args) {
let filepath = Path.join(keysDir, wifname);
let wif = await maybeReadKeyFile(filepath);
let paid = await CrowdNode.withdraw(wif, hotwallet, permil);
//let paidFloat = (paid.satoshis / DUFFS).toFixed(8);
//let paidFloat = (paid.satoshis / SATOSHIS).toFixed(8);
//let paidInt = paid.satoshis.toString().padStart(9, "0");
console.info(`API Response: ${paid.api}`);
return;
Expand Down Expand Up @@ -2038,12 +2036,11 @@ async function wifFileToAddr(name) {
}

let pk = new Dashcore.PrivateKey(privKey);
let pub = pk.toPublicKey().toAddress().toString();
let pub = (await pk.toPublicKey().toAddress()).toString();
return pub;
}

/**
* @param {String} insightBaseUrl
* @param {String} addr
*/
async function collectSignupFees(addr) {
Expand Down Expand Up @@ -2071,16 +2068,16 @@ async function collectSignupFees(addr) {
/**
* @param {String} insightBaseUrl
* @param {String} addr
* @param {Number} duffAmount
* @param {Number} satoshis
*/
async function collectDeposit(addr, duffAmount) {
async function collectDeposit(addr, satoshis) {
console.info(``);
showQr(addr, duffAmount);
showQr(addr, satoshis);

let depositMsg = `Please send what you wish to deposit to ${addr}`;
if (duffAmount) {
let dashAmount = toDash(duffAmount);
depositMsg = `Please deposit ${duffAmount} (Đ${dashAmount}) to ${addr}`;
if (satoshis) {
let dashAmount = toDash(satoshis);
depositMsg = `Please deposit ${satoshis} (Đ${dashAmount}) to ${addr}`;
}

let msgPad = Math.ceil((qrWidth - depositMsg.length) / 2);
Expand Down Expand Up @@ -2112,22 +2109,22 @@ function emptyStringOnErrEnoent(err) {
* @param {Number} duffs - ex: 00000000
*/
function toDash(duffs) {
return (duffs / DUFFS).toFixed(8);
return (duffs / SATOSHIS).toFixed(8);
}

/**
* @param {Number} duffs - ex: 00000000
*/
function toDASH(duffs) {
let dash = (duffs / DUFFS).toFixed(8);
let dash = (duffs / SATOSHIS).toFixed(8);
return `Đ` + dash.padStart(12, " ");
}

/**
* @param {String} dash - ex: 0.00000000
*/
function toDuff(dash) {
return Math.round(parseFloat(dash) * DUFFS);
return Math.round(parseFloat(dash) * SATOSHIS);
}

// Run
Expand Down
Loading