mirror of
https://github.com/seigler/govobject-proposal
synced 2025-07-27 06:46:10 +00:00
refactor start and end epoch calculation
This commit is contained in:
parent
c28f342ec3
commit
861cdede2f
1 changed files with 189 additions and 106 deletions
295
index.html
295
index.html
|
@ -10,24 +10,40 @@
|
||||||
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
|
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/jquery-ui.min.js"></script>
|
||||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js" integrity="sha384-Tc5IQib027qvyjSMfHjOMaLkfuWVxZxUPnCJA7l2mCWNIpG9mGCD8wGNIcPD7Txa" crossorigin="anonymous"></script>
|
||||||
<script src="bitcore-lib-dash.js"></script>
|
<script src="bitcore-lib-dash.js"></script>
|
||||||
|
<script src="date.js"></script>
|
||||||
|
|
||||||
<script src="https://dev-test.dash.org:3001/socket.io/socket.io.js"></script>
|
<script src="https://dev-test.dash.org:3001/socket.io/socket.io.js"></script>
|
||||||
<script type="text/javascript">
|
<script type="text/javascript">
|
||||||
|
|
||||||
var Bitcore = require('bitcore-lib-dash');
|
var Bitcore = require('bitcore-lib-dash');
|
||||||
|
|
||||||
var apiserversocket = 'https://dev-test.dash.org:3001/';
|
var apiserversocket = 'https://dev-test.dash.org:3001/';
|
||||||
|
|
||||||
//number of days to have as buffer for proposal start date before jumping to next month
|
var bufferdays = 3; //number of days to have as buffer for proposal start date before jumping to next month
|
||||||
var bufferdays = 3;
|
|
||||||
var blockheight;
|
var blockheight;
|
||||||
|
|
||||||
var progresstxt = "As of yet your transaction has 0 confirmations. We'll wait for 6 confirmations until you can submit your proposal. Waiting for new blocks to confirm...";
|
var progresstxt = "As of yet your transaction has 0 confirmations. We'll wait for 6 confirmations until you can submit your proposal. Waiting for new blocks to confirm...";
|
||||||
|
|
||||||
var gov = new Bitcore.GovObject.Proposal();
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
$(document).ready(function() {
|
$(document).ready(function() {
|
||||||
|
|
||||||
|
var gov = new Bitcore.GovObject.Proposal();
|
||||||
|
|
||||||
|
gov.network = 'testnet';
|
||||||
|
|
||||||
|
var cycle = new PaymentCycle(gov);
|
||||||
|
|
||||||
|
|
||||||
$("#time").val(Math.floor((new Date).getTime() / 1000));
|
$("#time").val(Math.floor((new Date).getTime() / 1000));
|
||||||
|
|
||||||
prepareTime();
|
cycle.updateDropdowns(); // update dropdown menus based on network and current time
|
||||||
|
|
||||||
|
// prepareTime(gov);
|
||||||
|
|
||||||
|
|
||||||
$("#progresstxt").text(progresstxt);
|
$("#progresstxt").text(progresstxt);
|
||||||
|
|
||||||
|
@ -78,26 +94,17 @@
|
||||||
|
|
||||||
var ProposalGenerator = function(gov) {
|
var ProposalGenerator = function(gov) {
|
||||||
this._mode = 'proposal';
|
this._mode = 'proposal';
|
||||||
|
if(!gov.network) gov.network = 'livenet';
|
||||||
|
|
||||||
this.gov = gov;
|
this.gov = gov;
|
||||||
this.gov.network = 'livenet';
|
|
||||||
|
|
||||||
// proposal basic fields
|
// proposal basic fields
|
||||||
this.gov.name = $('#name').val();
|
this.gov.name = $('#name').val();
|
||||||
this.gov.url = $('#url').val();
|
this.gov.url = $('#url').val();
|
||||||
this.gov.payment_address = $('#payment_address').val();
|
this.gov.payment_address = $('#payment_address').val();
|
||||||
this.gov.payment_amount = parseFloat($('#payment_amount').val());
|
this.gov.payment_amount = parseFloat($('#payment_amount').val());
|
||||||
|
this.gov.start_epoch = $('#start_epoch').val();
|
||||||
// format dates for govobject serialization
|
this.gov.end_epoch = $('#end_epoch').val();
|
||||||
var start_epoch = $('#start_epoch');
|
|
||||||
var startdate = formattedDateForSuperblockHeight(start_epoch.val());
|
|
||||||
|
|
||||||
console.log('startdate: ' + startdate);
|
|
||||||
this.gov.start_epoch = (new Date(startdate) / 1000) || null;
|
|
||||||
|
|
||||||
var enddate = formattedDateForSuperblockHeight(start_epoch.val(),$('#end_epoch').val());
|
|
||||||
console.log('enddate: ' + enddate);
|
|
||||||
|
|
||||||
this.gov.end_epoch = (new Date(enddate) / 1000) || null;
|
|
||||||
|
|
||||||
// hidden elements
|
// hidden elements
|
||||||
this.gov.type = parseInt($('#type').val());
|
this.gov.type = parseInt($('#type').val());
|
||||||
|
@ -431,93 +438,6 @@
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
prepareTime = function() {
|
|
||||||
$.getJSON(apiserversocket + "insight-api-dash/status?q=getinfo", function( data ) {
|
|
||||||
var timefield = $("#time");
|
|
||||||
timefield.val(Math.floor((new Date).getTime() / 1000));
|
|
||||||
console.log('time: ' + timefield.val());
|
|
||||||
blockheight = data.info.blocks;
|
|
||||||
console.log('network: ' + gov.network);
|
|
||||||
var budgetPaymentCycleBlocks = getBudgetPaymentCycleBlocks();
|
|
||||||
var nextsuperblock = Math.round(blockheight/budgetPaymentCycleBlocks) * budgetPaymentCycleBlocks;
|
|
||||||
var selectblock = nextsuperblock;
|
|
||||||
|
|
||||||
console.log('budgetPaymentCycleBlocks: ' + budgetPaymentCycleBlocks);
|
|
||||||
console.log('blockheight: ' + blockheight);
|
|
||||||
console.log('next superblock ' + nextsuperblock);
|
|
||||||
|
|
||||||
var optionsStart = [];
|
|
||||||
var optionsEnd = [];
|
|
||||||
var numOfSuperBlockDates = 12;
|
|
||||||
|
|
||||||
// Clear the options first
|
|
||||||
var start_epoch = $("#start_epoch");
|
|
||||||
var end_epoch = $("#end_epoch");
|
|
||||||
start_epoch.find("option").each(function(index, option) {
|
|
||||||
$(option).remove();
|
|
||||||
});
|
|
||||||
end_epoch.find("option").each(function(index, option) {
|
|
||||||
$(option).remove();
|
|
||||||
});
|
|
||||||
|
|
||||||
var blocksleft = nextsuperblock - blockheight;
|
|
||||||
for (i = 0; i < numOfSuperBlockDates; i ++)
|
|
||||||
{
|
|
||||||
var timeleft;
|
|
||||||
var blockdiff = nextsuperblock - blockheight;
|
|
||||||
var nextsuperblocktimestamp = $.now() + blockdiff * (2.6 * 60 * 1000);
|
|
||||||
var d = $.datepicker.formatDate('yy-mm-dd', new Date(nextsuperblocktimestamp));
|
|
||||||
|
|
||||||
if (i == 0) {
|
|
||||||
var daysleft = Math.round( blocksleft/554 * 10) / 10; // day rounded to one decimal
|
|
||||||
if (daysleft >= 2) {
|
|
||||||
timeleft = daysleft + ' days'
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
timeleft = daysleft + ' day'
|
|
||||||
}
|
|
||||||
optionsStart.push("<option value='" + nextsuperblock + "'>block " + nextsuperblock + " in " + timeleft + " (" + d + ")</option>");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
if (i == 1) {
|
|
||||||
timeleft = '1 month';
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
timeleft = i + ' months';
|
|
||||||
}
|
|
||||||
optionsStart.push("<option value='" + nextsuperblock + "'>block " + nextsuperblock + " in " + timeleft + " (" + d + ")</option>");
|
|
||||||
if (i == numOfSuperBlockDates - 1) {
|
|
||||||
timeleft = numOfSuperBlockDates + ' month';
|
|
||||||
nextsuperblock += budgetPaymentCycleBlocks;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
nextsuperblock += budgetPaymentCycleBlocks;
|
|
||||||
}
|
|
||||||
|
|
||||||
start_epoch.append(optionsStart.join(""));
|
|
||||||
|
|
||||||
//populate budget cycle dropdown
|
|
||||||
for (i = 1; i < 100; i ++)
|
|
||||||
{
|
|
||||||
if (i == 1) {
|
|
||||||
optionsEnd.push("<option value='" + i + "'>one-time payment</option>");
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
optionsEnd.push("<option value='" + i + "'>payment once a month for " + i + " months</option>");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
end_epoch.append(optionsEnd.join(""));
|
|
||||||
|
|
||||||
//if next superblock is less than n (bufferdays) days (60*24*bufferdays/2.6) away, select next month's superblock by default so proposal has more time to be voted upon
|
|
||||||
if (selectblock - blockheight < (60*24*bufferdays/2.6)) {
|
|
||||||
selectblock += budgetPaymentCycleBlocks;
|
|
||||||
start_epoch.val(selectblock);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
function setFormEditable(edit) {
|
function setFormEditable(edit) {
|
||||||
$('.createProposal input').each(function() {
|
$('.createProposal input').each(function() {
|
||||||
$(this).attr("disabled", edit);
|
$(this).attr("disabled", edit);
|
||||||
|
@ -537,6 +457,170 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/***
|
||||||
|
* Payment Cycle Generator
|
||||||
|
*
|
||||||
|
* @param gov
|
||||||
|
* @constructor
|
||||||
|
*/
|
||||||
|
function PaymentCycle(gov) {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
this.network = gov.network;
|
||||||
|
this.paymentCycle = 16616; // mainnet
|
||||||
|
this.budgetCycles = 24;
|
||||||
|
|
||||||
|
if (this.network == 'testnet') this.paymentCycle = 24;
|
||||||
|
if (this.network == 'testnet') this.budgetCycles = 96;
|
||||||
|
|
||||||
|
this.blockHeight = null;
|
||||||
|
|
||||||
|
this.Messages = {
|
||||||
|
paymentCycle: {
|
||||||
|
months: "Months",
|
||||||
|
month: "Month",
|
||||||
|
days: "Days",
|
||||||
|
day: "Day",
|
||||||
|
hours: "Hours",
|
||||||
|
hour: "Hour",
|
||||||
|
minutes: "Minutes",
|
||||||
|
minute: "Minute",
|
||||||
|
seconds: "Seconds",
|
||||||
|
second: "Second"
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
this.getInfo(function(err,res) {
|
||||||
|
console.log("current blockheight: " + res.info.blocks);
|
||||||
|
|
||||||
|
self.blockHeight = res.info.blocks;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
PaymentCycle.prototype.getNextSuperblock = function(block) {
|
||||||
|
return (Math.floor((block/this.paymentCycle)) * this.paymentCycle + this.paymentCycle);
|
||||||
|
};
|
||||||
|
|
||||||
|
PaymentCycle.prototype.getBlockTimestamp = function(block) {
|
||||||
|
var blocks = block - this.blockHeight;
|
||||||
|
var now = Math.floor(Date.now());
|
||||||
|
|
||||||
|
return (now + (blocks * (155 * 1000))); // 155 seconds per block x 1000 = ms per block
|
||||||
|
};
|
||||||
|
|
||||||
|
PaymentCycle.prototype.getTimeDifference = function(opts, start, end) {
|
||||||
|
|
||||||
|
var precision = opts.precision;
|
||||||
|
|
||||||
|
var millisec = end - start;
|
||||||
|
|
||||||
|
var seconds = (millisec / 1000).toFixed(precision);
|
||||||
|
|
||||||
|
var minutes = (millisec / (1000 * 60)).toFixed(precision);
|
||||||
|
|
||||||
|
var hours = (millisec / (1000 * 60 * 60)).toFixed(precision);
|
||||||
|
|
||||||
|
var days = (millisec / (1000 * 60 * 60 * 24)).toFixed(precision);
|
||||||
|
|
||||||
|
var months = (millisec / (1000 * 60 * 60 * 24 * 30)).toFixed(precision);
|
||||||
|
|
||||||
|
if (seconds < 60) {
|
||||||
|
if (seconds <= 1) return seconds + " " + this.Messages.paymentCycle.second; // singular
|
||||||
|
return seconds + " " + this.Messages.paymentCycle.seconds;
|
||||||
|
} else if (minutes < 60) {
|
||||||
|
if (minutes <= 1) return minutes + " " + this.Messages.paymentCycle.minute; // singular
|
||||||
|
return minutes + " " + this.Messages.paymentCycle.minutes;
|
||||||
|
} else if (hours < 24) {
|
||||||
|
if (hours <= 1) return hours + " " + this.Messages.paymentCycle.hour; // singular
|
||||||
|
return hours + " " + this.Messages.paymentCycle.hours;
|
||||||
|
} else if (days < 30) {
|
||||||
|
if (days <= 1) return days + " " + this.Messages.paymentCycle.day; // singular
|
||||||
|
return days + " " + this.Messages.paymentCycle.days;
|
||||||
|
} else {
|
||||||
|
if (months <= 1) return months + " " + this.Messages.paymentCycle.month; // singular
|
||||||
|
return months + " " + this.Messages.paymentCycle.months;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
PaymentCycle.prototype.updateDropdowns = function() {
|
||||||
|
var self = this;
|
||||||
|
|
||||||
|
var blockHeight = this.blockHeight;
|
||||||
|
|
||||||
|
var startDate = [];
|
||||||
|
var endDate = [];
|
||||||
|
|
||||||
|
for (i = 0; i < this.budgetCycles + 1; i++) {
|
||||||
|
|
||||||
|
var superblock = this.getNextSuperblock(blockHeight);
|
||||||
|
var timestamp = this.getBlockTimestamp(superblock);
|
||||||
|
|
||||||
|
var label = new Date(timestamp).toLocaleDateString();
|
||||||
|
if (this.network == 'testnet') label = new Date(timestamp).toLocaleString();
|
||||||
|
|
||||||
|
var superblockDate = {
|
||||||
|
superblock: superblock,
|
||||||
|
timestamp: timestamp,
|
||||||
|
label: label
|
||||||
|
};
|
||||||
|
startDate.push(superblockDate);
|
||||||
|
endDate.push(superblockDate);
|
||||||
|
|
||||||
|
blockHeight = superblock;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
endDate.shift(); // remove first element of endDate
|
||||||
|
startDate.pop(); // remove last element of startDate to keep length even
|
||||||
|
|
||||||
|
var now = Math.floor(Date.now());
|
||||||
|
|
||||||
|
var opts = {
|
||||||
|
precision: 2
|
||||||
|
}; // 2 unit of precision for eta formatting
|
||||||
|
|
||||||
|
// calculate the amount of time between start and stop, show: e.g. 5 Months or 5 Hours
|
||||||
|
|
||||||
|
var start_epoch = $("#start_epoch");
|
||||||
|
start_epoch.find('option').remove();
|
||||||
|
$.each(startDate, function(index) {
|
||||||
|
|
||||||
|
var eta = self.getTimeDifference(opts, now, this.timestamp);
|
||||||
|
var time = this.timestamp - now;
|
||||||
|
var option = $("<option />").val((Math.floor(this.timestamp / 1000))).text(this.label).attr('data-index', index).attr('data-time', time).attr('data-eta', eta).attr('data-block', this.superblock);
|
||||||
|
start_epoch.append(option);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
opts.precision = null; // 0 units of precision for eta formatting
|
||||||
|
|
||||||
|
var end_epoch = $("#end_epoch");
|
||||||
|
end_epoch.find('option').remove();
|
||||||
|
$.each(endDate, function(index) {
|
||||||
|
|
||||||
|
var eta = self.getTimeDifference(opts, startDate[0].timestamp, this.timestamp);
|
||||||
|
var time = this.timestamp - startDate[0].timestamp;
|
||||||
|
|
||||||
|
var option = $("<option />").val((Math.floor(this.timestamp / 1000))).text(eta + " (" + this.label + ")").attr('data-index', index).attr('data-time', time).attr('data-eta', eta).attr('data-block', this.superblock);
|
||||||
|
end_epoch.append(option);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
PaymentCycle.prototype.getInfo = function(cb) {
|
||||||
|
$.getJSON(apiserversocket + "insight-api-dash/status?q=getinfo", function( data ) {
|
||||||
|
cb(null, data);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
var copyToClipboard = function(id) {
|
var copyToClipboard = function(id) {
|
||||||
document.getElementById(id).select();
|
document.getElementById(id).select();
|
||||||
document.execCommand('copy');
|
document.execCommand('copy');
|
||||||
|
@ -548,8 +632,7 @@
|
||||||
return 16616;
|
return 16616;
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
//for testing purposes
|
return 50; // ten times per day
|
||||||
return 50; //ten times per day
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue