Skip to content

Commit

Permalink
feat: make network optional
Browse files Browse the repository at this point in the history
  • Loading branch information
coolaj86 committed Aug 10, 2024
1 parent 35fc77e commit 466d396
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions bin/gobject-prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,12 @@ async function main() {
console.info("");
console.info("USAGE");
console.info(
" dashgov draft-proposal [start period] [num periods] <DASH-per-period> <proposal-url> <name> <payment-addr> <./collateral-key.wif>",
" dashgov draft-proposal [start period] [num periods] <DASH-per-period> <proposal-url> <name> <payment-addr> <./collateral-key.wif> [network]",
);
console.info("");
console.info("EXAMPLE");
console.info(
" dashgov draft-proposal '1' '3' '100' 'https://example.com/example-proposal' example-proposal yT6GS8qPrhsiiLHEaTWPYJMwfPPVt2SSFC ./private-key.wif",
" dashgov draft-proposal '1' '3' '100' 'https://example.com/example-proposal' example-proposal yT6GS8qPrhsiiLHEaTWPYJMwfPPVt2SSFC ./private-key.wif testnet",
);
console.info("");

Expand All @@ -41,6 +41,9 @@ async function main() {
collateralWif = await Fs.readFile(collateralWifPath, "utf8");
collateralWif = collateralWif.trim();
}
/** @type {"mainnet"|"testnet"} */
//@ts-ignore - TODO
let network = process.argv[9] || "mainnet";

let rpcConfig = {
protocol: process.env.DASHD_RPC_PROTOCOL || "",
Expand Down Expand Up @@ -268,7 +271,7 @@ async function main() {
* @param {Number} i
*/
getPrivateKey: async function (txInput, i) {
return DashKeys.wifToPrivKey(collateralWif, { version: "testnet" });
return DashKeys.wifToPrivKey(collateralWif, { version: network });
},

/**
Expand Down Expand Up @@ -307,7 +310,7 @@ async function main() {

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

console.log("");
Expand All @@ -317,7 +320,7 @@ async function main() {
// we can set txid to short circuit for testing
let txid = "";
// ./bin/gobject-prepare.js 1 3 100 https://example.com/proposal-00 proposal-00 yPPy7Z5RQj46SnFtuFXyT6DFAygxESPR7K ./yjZxu7SJAwgSm1JtWybuQRYQDx34z8P2Z7.wif
// txid = "";
// txid = "10d9862feb6eac6cf6fa653589e39b60a0ed640bae4140c51c35401ffe019479";
if (!txid) {
let utxosResult = await rpc.getaddressutxos({
addresses: [collateralAddr],
Expand Down Expand Up @@ -406,31 +409,32 @@ async function main() {
// }

async function submit() {
let gobjResult = await rpc
.request("/", {
method: "gobject",
params: [
"submit",
gobj.hashParent.toString(), // '0' must be a string for some reason
gobj.revision.toString(),
gobj.time.toString(),
gobj.dataHex,
txid,
],
})
.catch(
/** @param {Error} err */ function (err) {
const E_INVALID_COLLATERAL = -32603;
// @ts-ignore - code exists
let code = err.code;
if (code === E_INVALID_COLLATERAL) {
// wait for collateral to become valid
console.error(code, err.message);
return null;
}
throw err;
},
);
let req = {
method: "gobject",
params: [
"submit",
gobj.hashParent.toString(), // '0' must be a string for some reason
gobj.revision.toString(),
gobj.time.toString(),
gobj.dataHex,
txid,
],
};
let args = req.params.join(" ");
console.log(`${req.method} ${args}`);
let gobjResult = await rpc.request("/", req).catch(
/** @param {Error} err */ function (err) {
const E_INVALID_COLLATERAL = -32603;
// @ts-ignore - code exists
let code = err.code;
if (code === E_INVALID_COLLATERAL) {
// wait for collateral to become valid
console.error(code, err.message);
return null;
}
throw err;
},
);

return gobjResult;
}
Expand Down

0 comments on commit 466d396

Please sign in to comment.