Skip to content

Commit

Permalink
ref: separate args and config from business logic
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Oct 24, 2024
1 parent 7ff94ff commit 76b7b54
Showing 1 changed file with 73 additions and 27 deletions.
100 changes: 73 additions & 27 deletions bin/gobject-prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,37 @@ import * as Secp256k1 from "@dashincubator/secp256k1";

import Fs from "node:fs/promises";

async function main() {
/**
* @typedef ProposalData
* @prop {"mainnet"|"testnet"} network - The network type, either mainnet or testnet.
* @prop {String} rpcBasicAuth - Basic authentication credentials for RPC (e.g., `api:null`).
* @prop {String} rpcBaseUrl - Base URL for the RPC server, adjusted for the network.
* @prop {String} rpcExplorer - URL for the RPC explorer.
* @prop {Number} startPeriod - The start period for the proposal.
* @prop {Number} numPeriods - Number of periods for which the proposal applies.
* @prop {Number} dashAmount - The amount of Dash for the proposal.
* @prop {String} proposalUrl - URL of the proposal.
* @prop {String} proposalName - Name of the proposal.
* @prop {String} paymentAddr - Payment address for the proposal.
* @prop {String} burnWif - Wallet Import Format (WIF) key used for the burn address.
*/

/**
* @param {ProposalData} opts
*/
async function prepAndSubmit({
network,
rpcBasicAuth,
rpcBaseUrl,
rpcExplorer,
startPeriod,
numPeriods,
dashAmount,
proposalUrl,
proposalName,
paymentAddr,
burnWif,
}) {
/* jshint maxcomplexity: 100 */
/* jshint maxstatements: 1000 */

Expand All @@ -24,32 +54,6 @@ async function main() {
);
console.info("");

/** @type {"mainnet"|"testnet"} */
let network = "mainnet";
let rpcBasicAuth = `api:null`;
let rpcBaseUrl = `https://${rpcBasicAuth}@rpc.digitalcash.dev/`;
let rpcExplorer = "https://rpc.digitalcash.dev/";

let isTestnet = takeFlag(process.argv, ["--testnet"]);
if (isTestnet) {
rpcBaseUrl = `https://${rpcBasicAuth}@trpc.digitalcash.dev/`;
rpcExplorer = "https://trpc.digitalcash.dev/";
network = "testnet";
}

let startPeriod = parseInt(process.argv[2] || "1", 10);
let numPeriods = parseInt(process.argv[3] || "1", 10);
let dashAmount = parseInt(process.argv[4] || "1", 10);
let proposalUrl = process.argv[5] || "";
let proposalName = process.argv[6] || "";
let paymentAddr = process.argv[7] || "";
let burnWifPath = process.argv[8] || "";
let burnWif = "";
if (burnWifPath) {
burnWif = await Fs.readFile(burnWifPath, "utf8");
burnWif = burnWif.trim();
}

/**
* @param {String} method
* @param {...any} params
Expand Down Expand Up @@ -428,6 +432,48 @@ async function main() {
}
}

async function main() {
/** @type {"mainnet"|"testnet"} */
let network = "mainnet";
let rpcBasicAuth = `api:null`;
let rpcBaseUrl = `https://${rpcBasicAuth}@rpc.digitalcash.dev/`;
let rpcExplorer = "https://rpc.digitalcash.dev/";

let isTestnet = takeFlag(process.argv, ["--testnet"]);
if (isTestnet) {
rpcBaseUrl = `https://${rpcBasicAuth}@trpc.digitalcash.dev/`;
rpcExplorer = "https://trpc.digitalcash.dev/";
network = "testnet";
}

let startPeriod = parseInt(process.argv[2] || "1", 10);
let numPeriods = parseInt(process.argv[3] || "1", 10);
let dashAmount = parseInt(process.argv[4] || "1", 10);
let proposalUrl = process.argv[5] || "";
let proposalName = process.argv[6] || "";
let paymentAddr = process.argv[7] || "";
let burnWifPath = process.argv[8] || "";
let burnWif = "";
if (burnWifPath) {
burnWif = await Fs.readFile(burnWifPath, "utf8");
burnWif = burnWif.trim();
}

await prepAndSubmit({
network,
rpcBasicAuth,
rpcBaseUrl,
rpcExplorer,
startPeriod,
numPeriods,
dashAmount,
proposalUrl,
proposalName,
paymentAddr,
burnWif,
});
}

/**
* Find, remove, and return the first matching flag from the arguments list
* @param {Array<String>} argv
Expand Down

0 comments on commit 76b7b54

Please sign in to comment.