+/**
+ * @param {Object} opts
+ * @param {any} opts.dashApi - TODO
+ * @param {String} opts.defaultAddr
+ * @param {Boolean} opts.forceGenerate
+ * @param {String} opts.insightBaseUrl
+ * @param {any} opts.insightApi
+ * @param {Boolean} opts.noReserve
+ * @param {Array<String>} args
+ */
+async function stakeDash(
+ {
+ dashApi,
+ defaultAddr,
+ forceGenerate,
+ insightApi,
+ insightBaseUrl,
+ noReserve,
+ },
+ args,
+) {
+ let err = await Fs.access(args[0]).catch(Object);
+ let addr;
+ if (!err) {
+ addr = await importKey(null, [args[0]]);
+ } else if (forceGenerate) {
+ addr = await generateKey({ defaultKey: defaultAddr }, []);
+ } else {
+ addr = await initKeystore({ defaultAddr });
+ }
+
+ if (!addr) {
+ let [_addr] = await mustGetAddr({ defaultAddr }, args);
+ addr = _addr;
+ }
+
+ let extra = feeEstimate;
+ console.info("Checking CrowdNode account... ");
+ await CrowdNode.init({
+ baseUrl: "https://app.crowdnode.io",
+ insightBaseUrl,
+ });
+ let hotwallet = CrowdNode.main.hotwallet;
+ let state = await getCrowdNodeStatus({ addr, hotwallet });
+
+ if (!state.status?.accept) {
+ if (!state.status?.signup) {
+ let signUpDeposit = signupOnly + feeEstimate;
+ console.info(
+ ` ${TODO} SignUpForApi deposit is ${signupOnly} (+ tx fee)`,
+ );
+ extra += signUpDeposit;
+ } else {
+ console.info(` ${DONE} SignUpForApi complete`);
+ }
+ let acceptDeposit = acceptOnly + feeEstimate;
+ console.info(` ${TODO} AcceptTerms deposit is ${acceptOnly} (+ tx fee)`);
+ extra += acceptDeposit;
+ }
+
+ let desiredAmountDash = args.shift() || "0.5";
+ let effectiveDuff = toDuff(desiredAmountDash);
+ effectiveDuff += extra;
+
+ let balanceInfo = await dashApi.getInstantBalance(addr);
+ effectiveDuff -= balanceInfo.balanceSat;
+
+ if (effectiveDuff > 0) {
+ effectiveDuff = roundDuff(effectiveDuff, 3);
+ let effectiveDash = toDash(effectiveDuff);
+ await plainLoadAddr({
+ addr,
+ effectiveDash,
+ effectiveDuff,
+ insightBaseUrl,
+ });
+ }
+
+ if (!state.status?.accept) {
+ if (!state.status?.signup) {
+ await sendSignup({ dashApi, defaultAddr: addr, insightBaseUrl }, []);
+ }
+ await acceptTerms({ dashApi, defaultAddr: addr, insightBaseUrl }, []);
+ }
+
+ await depositDash(
+ { dashApi, defaultAddr: addr, insightBaseUrl, noReserve },
+ args,
+ );
+}
+
+/**
+ * @param {Object} opts
+ * @param {String} opts.defaultAddr
+ */
+async function initKeystore({ defaultAddr }) {
+ // if we have no keys, make one
+ let wifnames = await listManagedKeynames();
+ if (!wifnames.length) {
+ return await generateKey({ defaultKey: defaultAddr }, []);
+ }
+ // if we have no passphrase, ask about it
+ await initPassphrase();
+ return defaultAddr || wifnames[0];
+}
+