diff --git a/bin/gobject-prepare.js b/bin/gobject-prepare.js index c9db3f7..bcb5225 100755 --- a/bin/gobject-prepare.js +++ b/bin/gobject-prepare.js @@ -20,15 +20,22 @@ async function main() { console.info(""); console.info("USAGE"); console.info( - " dashgov draft-proposal [start period] [num periods] <./collateral-key.wif>", + " dashgov draft-proposal [start period] [num periods] <./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(""); + /** @type {"mainnet"|"testnet"} */ + let network = "mainnet"; + let isTestnet = takeFlag(process.argv, ["--testnet"]); + if (isTestnet) { + 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); @@ -95,7 +102,8 @@ async function main() { let totalCycleCount = numPeriods - 1; let endPeriod = startPeriod + totalCycleCount; - let cycleCount = Math.max(3, endPeriod); + let cycleCount = endPeriod; + let displayCycleCount = Math.max(3, endPeriod); let snapshot = { ms: blockMs, block: blockHeight, @@ -107,7 +115,7 @@ async function main() { secondsPerBlock, ); let estimates = DashGov.estimateProposalCycles( - cycleCount, + displayCycleCount, snapshot, secondsPerBlock, ); @@ -268,7 +276,7 @@ async function main() { * @param {Number} i */ getPrivateKey: async function (txInput, i) { - return DashKeys.wifToPrivKey(collateralWif, { version: "testnet" }); + return DashKeys.wifToPrivKey(collateralWif, { version: network }); }, /** @@ -307,7 +315,7 @@ async function main() { // dash-cli -testnet getaddressutxos '{"addresses":["yT6GS8qPrhsiiLHEaTWPYJMwfPPVt2SSFC"]}' let collateralAddr = await DashKeys.wifToAddr(collateralWif, { - version: "testnet", + version: network, }); console.log(""); @@ -317,7 +325,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], @@ -406,31 +414,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; } @@ -449,6 +458,28 @@ async function main() { } } +/** + * Find, remove, and return the first matching flag from the arguments list + * @param {Array} argv + * @param {Array} flags + */ +function takeFlag(argv, flags) { + let flagValue = null; + + for (let flag of flags) { + let index = argv.indexOf(flag); + if (index === -1) { + continue; + } + + flagValue = argv[index]; + void argv.splice(index, 1); + break; + } + + return flagValue; +} + main() .then(function () { process.exit(0); diff --git a/scripts/estimate.js b/scripts/estimate.js index 6283056..a3cb42f 100755 --- a/scripts/estimate.js +++ b/scripts/estimate.js @@ -75,7 +75,7 @@ function main() { let blockTime = process.argv[4]; let blockMs = 0; let secondsPerBlock = 0; - let proposalLeadtime = 0; + // let proposalLeadtime = 0; if (!cycleCount) { console.error(`ERROR`); @@ -102,7 +102,7 @@ function main() { block: blockHeight, }; if (!secondsPerBlock) { - secondsPerBlock = DashGov.measureSecondsPerBlock(snapshot); + secondsPerBlock = DashGov.estimateSecondsPerBlock(snapshot); } let estimates = DashGov.estimateProposalCycles( cycleCount, diff --git a/tests/calc-nth-gov-cycle.js b/tests/calc-nth-gov-cycle.js index 4e9fcd7..dff71ba 100644 --- a/tests/calc-nth-gov-cycle.js +++ b/tests/calc-nth-gov-cycle.js @@ -12,6 +12,10 @@ let tests = [ [ -1, { + endIso: "2024-06-28T15:00:00.000Z", + endMs: 1719586800000, + startIso: "2024-05-29T13:00:00.000Z", + startMs: 1716987600000, voteHeight: 2091954, voteIso: "2024-06-21T13:44:00.273Z", voteMs: 1718977440273, @@ -27,6 +31,10 @@ let tests = [ [ 0, { + endIso: "2024-07-28T22:00:00.000Z", + endMs: 1722204000000, + startIso: "2024-06-28T21:00:00.000Z", + startMs: 1719608400000, voteHeight: 2108570, voteIso: "2024-07-21T21:03:37.273Z", voteMs: 1721595817273, @@ -42,6 +50,10 @@ let tests = [ [ 1, { + endIso: "2024-08-28T06:00:00.000Z", + endMs: 1724824800000, + startIso: "2024-07-29T04:00:00.000Z", + startMs: 1722225600000, voteHeight: 2125186, voteIso: "2024-08-21T04:23:14.273Z", voteMs: 1724214194273,