Skip to content

Commit 92cec32

Browse files
committed
feat: make network optional
1 parent 57d3c51 commit 92cec32

File tree

1 file changed

+60
-30
lines changed

1 file changed

+60
-30
lines changed

bin/gobject-prepare.js

Lines changed: 60 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,22 @@ async function main() {
2020
console.info("");
2121
console.info("USAGE");
2222
console.info(
23-
" dashgov draft-proposal [start period] [num periods] <DASH-per-period> <proposal-url> <name> <payment-addr> <./collateral-key.wif>",
23+
" dashgov draft-proposal [start period] [num periods] <DASH-per-period> <proposal-url> <name> <payment-addr> <./collateral-key.wif> [network]",
2424
);
2525
console.info("");
2626
console.info("EXAMPLE");
2727
console.info(
28-
" dashgov draft-proposal '1' '3' '100' 'https://example.com/example-proposal' example-proposal yT6GS8qPrhsiiLHEaTWPYJMwfPPVt2SSFC ./private-key.wif",
28+
" dashgov draft-proposal '1' '3' '100' 'https://example.com/example-proposal' example-proposal yT6GS8qPrhsiiLHEaTWPYJMwfPPVt2SSFC ./private-key.wif testnet",
2929
);
3030
console.info("");
3131

32+
/** @type {"mainnet"|"testnet"} */
33+
let network = "mainnet";
34+
let isTestnet = takeFlag(process.argv, ["--testnet"]);
35+
if (isTestnet) {
36+
network = "testnet";
37+
}
38+
3239
let startPeriod = parseInt(process.argv[2] || "1", 10);
3340
let numPeriods = parseInt(process.argv[3] || "1", 10);
3441
let dashAmount = parseInt(process.argv[4] || "1", 10);
@@ -269,7 +276,7 @@ async function main() {
269276
* @param {Number} i
270277
*/
271278
getPrivateKey: async function (txInput, i) {
272-
return DashKeys.wifToPrivKey(collateralWif, { version: "testnet" });
279+
return DashKeys.wifToPrivKey(collateralWif, { version: network });
273280
},
274281

275282
/**
@@ -308,7 +315,7 @@ async function main() {
308315

309316
// dash-cli -testnet getaddressutxos '{"addresses":["yT6GS8qPrhsiiLHEaTWPYJMwfPPVt2SSFC"]}'
310317
let collateralAddr = await DashKeys.wifToAddr(collateralWif, {
311-
version: "testnet",
318+
version: network,
312319
});
313320

314321
console.log("");
@@ -318,7 +325,7 @@ async function main() {
318325
// we can set txid to short circuit for testing
319326
let txid = "";
320327
// ./bin/gobject-prepare.js 1 3 100 https://example.com/proposal-00 proposal-00 yPPy7Z5RQj46SnFtuFXyT6DFAygxESPR7K ./yjZxu7SJAwgSm1JtWybuQRYQDx34z8P2Z7.wif
321-
// txid = "";
328+
// txid = "10d9862feb6eac6cf6fa653589e39b60a0ed640bae4140c51c35401ffe019479";
322329
if (!txid) {
323330
let utxosResult = await rpc.getaddressutxos({
324331
addresses: [collateralAddr],
@@ -407,31 +414,32 @@ async function main() {
407414
// }
408415

409416
async function submit() {
410-
let gobjResult = await rpc
411-
.request("/", {
412-
method: "gobject",
413-
params: [
414-
"submit",
415-
gobj.hashParent.toString(), // '0' must be a string for some reason
416-
gobj.revision.toString(),
417-
gobj.time.toString(),
418-
gobj.dataHex,
419-
txid,
420-
],
421-
})
422-
.catch(
423-
/** @param {Error} err */ function (err) {
424-
const E_INVALID_COLLATERAL = -32603;
425-
// @ts-ignore - code exists
426-
let code = err.code;
427-
if (code === E_INVALID_COLLATERAL) {
428-
// wait for collateral to become valid
429-
console.error(code, err.message);
430-
return null;
431-
}
432-
throw err;
433-
},
434-
);
417+
let req = {
418+
method: "gobject",
419+
params: [
420+
"submit",
421+
gobj.hashParent.toString(), // '0' must be a string for some reason
422+
gobj.revision.toString(),
423+
gobj.time.toString(),
424+
gobj.dataHex,
425+
txid,
426+
],
427+
};
428+
let args = req.params.join(" ");
429+
console.log(`${req.method} ${args}`);
430+
let gobjResult = await rpc.request("/", req).catch(
431+
/** @param {Error} err */ function (err) {
432+
const E_INVALID_COLLATERAL = -32603;
433+
// @ts-ignore - code exists
434+
let code = err.code;
435+
if (code === E_INVALID_COLLATERAL) {
436+
// wait for collateral to become valid
437+
console.error(code, err.message);
438+
return null;
439+
}
440+
throw err;
441+
},
442+
);
435443

436444
return gobjResult;
437445
}
@@ -450,6 +458,28 @@ async function main() {
450458
}
451459
}
452460

461+
/**
462+
* Find, remove, and return the first matching flag from the arguments list
463+
* @param {Array<String>} argv
464+
* @param {Array<String>} flags
465+
*/
466+
function takeFlag(argv, flags) {
467+
let flagValue = null;
468+
469+
for (let flag of flags) {
470+
let index = argv.indexOf(flag);
471+
if (index === -1) {
472+
continue;
473+
}
474+
475+
flagValue = argv[index];
476+
void argv.splice(index, 1);
477+
break;
478+
}
479+
480+
return flagValue;
481+
}
482+
453483
main()
454484
.then(function () {
455485
process.exit(0);

0 commit comments

Comments
 (0)