|
| 1 | +# DashGov.js |
| 2 | + |
| 3 | +Utility functions for Dash governance on the blockchain. |
| 4 | + |
| 5 | +## Prosposal |
| 6 | + |
| 7 | +- proposal close (previous end epoch) |
| 8 | +- vote close |
| 9 | +- superblock (payment) |
| 10 | +- proposal open (start epoch) |
| 11 | +- vote open |
| 12 | +- proposal close (upcoming end epoch) |
| 13 | + |
| 14 | +```js |
| 15 | +let current = { |
| 16 | + height: 2114623, |
| 17 | + ms: new Date("2024-08-01 22:01:00").valueOf(), |
| 18 | +}; |
| 19 | +let cycles = 3; |
| 20 | +let estimates = DashGov.estimateFutureBlocks(current, cycles); |
| 21 | + |
| 22 | +let proposalObj = { |
| 23 | + start_epoch: current.proposalOpen[0].seconds, |
| 24 | + end_epoch: current.proposalClose[2].seconds, |
| 25 | + name: "test-proposal-4", |
| 26 | + payment_address: "yM7M4YJFv58hgea9FUxLtjKBpKXCsjxWNX", |
| 27 | + payment_amount: 100, |
| 28 | + type: 1, |
| 29 | + url: "https://www.dashcentral.org/p/test-proposal-4", |
| 30 | +}; |
| 31 | + |
| 32 | +let proposalJson = JSON.stringify(proposal); |
| 33 | +let encoder = new TextEncoder(); |
| 34 | +let proposalBytes = encoder.encode(proposalJson); |
| 35 | + |
| 36 | +// JSON *should* be serialized canonically with lexicographically-sorted keys, |
| 37 | +// HOWEVER, a hex string (not bytes) is used to guarantee reproducible output. |
| 38 | +let proposalHex = Gobj.utils.bytesToHex(proposalBytes); |
| 39 | + |
| 40 | +let now = Date.now(); |
| 41 | +let secs = now / 1000; |
| 42 | +secs = Math.round(secs); |
| 43 | + |
| 44 | +// Note: the full object is shown for completeness, however, most |
| 45 | +// gobject args were either deprecated or never implemented |
| 46 | +let gobj = { |
| 47 | + // hashParent: null, |
| 48 | + // revision: 1, // MUST be one |
| 49 | + time: secs, |
| 50 | + hexJson: proposalHex, |
| 51 | + // signature: null, |
| 52 | +}; |
| 53 | + |
| 54 | +gobj = DashGov.normalize(gobj); |
| 55 | + |
| 56 | +let gobjBytes = DashGov.serializeForCollateralTx(gobj); |
| 57 | +let gobjOpReturn = await DashGov.doubleSha256Hash(gobjBytes); |
| 58 | + |
| 59 | +let keyUtils = { |
| 60 | + getPrivateKey: function (info) { |
| 61 | + // lookup by info.address or similar |
| 62 | + let privKeyBytes = doStuff(); |
| 63 | + return privKeyBytes; |
| 64 | + }, |
| 65 | +}; |
| 66 | +let dashTx = DashTx.create(keyUtils); |
| 67 | +let txraw = await dashTx.createMemo({ bytes: gobjOpReturnBytes }); |
| 68 | + |
| 69 | +let result = await RPC.request({ |
| 70 | + method: "sendrawtransaction", |
| 71 | + params: [txraw.hex], |
| 72 | +}); |
| 73 | + |
| 74 | +// poll for txid to appear in recent blocks |
| 75 | +let txid = result.txid; |
| 76 | + |
| 77 | +let result = await RPC.request({ |
| 78 | + method: "gobject", |
| 79 | + params: [ |
| 80 | + "submit", |
| 81 | + gobj.hashParent, |
| 82 | + gobj.revision, |
| 83 | + gobj.time, |
| 84 | + gobj.hexJson, |
| 85 | + txid, |
| 86 | + ], |
| 87 | +}); |
| 88 | +``` |
| 89 | + |
| 90 | +# Notes |
| 91 | + |
| 92 | +```text |
| 93 | +A block is generated for a self-correcting target average of 155 seconds. |
| 94 | +(actually 157.64 seconds over the 7 year average) |
| 95 | +
|
| 96 | +Superblock is every 16616 blocks (rounded up from 30 days). |
| 97 | +Superblock is when payment occurs. |
| 98 | +
|
| 99 | +Voting ends 1662 blocks before the superblock (rounded up from 3 days). |
| 100 | + // ~(60*24*3)/2.6 |
| 101 | +
|
| 102 | +Votes after the block deadline are discarded for that superblock, but will |
| 103 | +be counted for the next, if the proposal is still active. |
| 104 | +``` |
0 commit comments