Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
95 changes: 63 additions & 32 deletions bin/gobject-prepare.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,22 @@ 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("");

/** @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);
Expand Down Expand Up @@ -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,
Expand All @@ -107,7 +115,7 @@ async function main() {
secondsPerBlock,
);
let estimates = DashGov.estimateProposalCycles(
cycleCount,
displayCycleCount,
snapshot,
secondsPerBlock,
);
Expand Down Expand Up @@ -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 });
},

/**
Expand Down Expand Up @@ -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("");
Expand All @@ -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],
Expand Down Expand Up @@ -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;
}
Expand All @@ -449,6 +458,28 @@ async function main() {
}
}

/**
* Find, remove, and return the first matching flag from the arguments list
* @param {Array<String>} argv
* @param {Array<String>} 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);
Expand Down
4 changes: 2 additions & 2 deletions scripts/estimate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
Expand All @@ -102,7 +102,7 @@ function main() {
block: blockHeight,
};
if (!secondsPerBlock) {
secondsPerBlock = DashGov.measureSecondsPerBlock(snapshot);
secondsPerBlock = DashGov.estimateSecondsPerBlock(snapshot);
}
let estimates = DashGov.estimateProposalCycles(
cycleCount,
Expand Down
12 changes: 12 additions & 0 deletions tests/calc-nth-gov-cycle.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
Expand Down
Loading