govobject-proposal/index.html
2016-11-22 09:18:32 -04:00

623 lines
28 KiB
HTML

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Dash Governance Tools</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.0/themes/smoothness/jquery-ui.css">
<script src="https://code.jquery.com/jquery-3.1.0.js" integrity="sha256-slogkvB1K3VOkzAI8QITxV3VzpOnkeNVsKvtkYLMjfk=" crossorigin="anonymous"></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="bitcore-lib-dash.js"></script>
<script src="https://dev-test.dash.org:3001/socket.io/socket.io.js"></script>
<script type="text/javascript">
var Bitcore = require('bitcore-lib-dash');
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;
var blockheight;
var gov = new Bitcore.GovObject.Proposal();
$(document).ready(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 3 days (60*24*3/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);
nextsuperblocktimestamp = nextsuperblocktimestamp + budgetPaymentCycleBlocks * (2.6 * 60 * 1000);
}
});
$('#prepareProposal').click(function() {
copyToClipboard($(this).attr('id'));
});
$('#submitProposal').click(function() {
copyToClipboard($(this).attr('id'));
});
$('.createProposal input').focus(function() {
$(this).removeClass('validationError');
});
$('#btnPrepare').click(function() {
var proposal = new ProposalGenerator(gov);
var validProposal = proposal.validate();
if (validProposal) {
proposal.walletCommands();
$('#feeTxid').on('input', function() {
if ($(this).val().length > 0) {
var txidfield = $('#feeTxid');
console.log('value entered: ' + txidfield.val());
var submitCommand = "gobject submit " + $('#parentHash').val() + " " + $('#revision').val() + " " + $('#time').val() + " " + proposal.gov.serialize() + " " + $(this).val();
$('textarea#submitProposal').val(submitCommand);
txidfield.change(function() {
// Check input( $( this ).val() ) for validity here
console.log('there is something wrong with your transaction ID: ' + $('#feeTxid').val() + ' Please copy and paste it here .');
txidfield.val('')
});
//some checks if feeTxid seems valid before we check the api
if (txidfield.val().length == 64) {
if (isAlphaNumeric(txidfield.val())) {
txidfield.unbind( "change" );
console.log('feeTxid seems good: ' + txidfield.val());
console.log("wait while we check the api!");
// first check if transactionid is already in api
$.getJSON(apiserversocket + 'insight-api-dash/tx/' + txidfield.val(), function(data) {
txidfield.attr("disabled", true);
var txid = data.tx;
var confirmations = data.confirmations;
console.log('Transaction has ' + confirmations + ' confirmation(s)');
if (confirmations != 'undefined' && $.isNumeric(confirmations)) {
var socket = io(apiserversocket);
if (confirmations == 0) {
// we have to count the blocks and wait for 6 confirmations
console.log("we have to count the blocks and wait for 6 confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
var blockhash = data;
// let's check if transaction is really in this block or not
if (confirmations == 0) {
$.getJSON(apiserversocket + 'insight-api-dash/txs/?block=' + blockhash, function(data) {
var txs = data.txs;
var found;
var numOfTxs = txs.length;
for (var i = 0; i < numOfTxs; i++) {
console.log('txs' + i + ': ' + txs[i].txid);
if (txs[i].txid == txidfield.val()) {
console.log('found tx!');
found = true;
}
}
if (found) {
console.log('all good. Count up confirmations.');
confirmations = confirmations + 1;
}
else {
console.log('txid not in new block');
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('block hash not found in api!');
} else {
console.log('There seems to be a problem with the api connection. Maybe endpoint resyncing?');
}
});
}
else {
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
}
if (confirmations >= 6) {
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
});
}
else if (confirmations > 0 && confirmations <= 5) {
// we have to count the blocks and wait for outstanding confirmations
console.log("we have to count the blocks and wait for outstanding confirmations");
eventToListenTo = 'block';
room = 'inv';
socket.on('connect', function() {
// Join the room.
socket.emit('subscribe', room);
console.log("listening for '" + eventToListenTo + "' in '" + room + "'");
});
socket.on(eventToListenTo, function(data) {
console.log("New block received: " + data + " time: " + data.time);
var blockhash = data;
// for the time being just count up the confirmations without confirming everytime if the transaction is still inside the previous blocks
confirmations = confirmations + 1;
if (confirmations >= 6) {
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
});
}
else {
// already reached 6 or more confirmations, so we can proceed
alert("already reached 6 or more confirmations, so we can proceed");
$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
}
}
else {
console.log('Something went terribly wrong. Faulty api data?');
txidfield.attr("disabled", false);
}
}).fail(function(jqXHR) {
if (jqXHR.status == 400) {
// there seems to be a problem with your feeTxid because txid is not found in api
console.log('problem with feeTxid! Ask for new input!');
alert("Check again and please enter your correct TxID!");
txidfield.attr("disabled", false);
} else {
txidfield.attr("disabled", false);
console.log('There seems to be a problem with the api connection');
}
});
}
else {
alert("there is something wrong with your transaction ID. It must be alphanumeric!")
}
}
} else {
$('textarea#submitProposal').val('');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
}
});
$('#btnEdit').click(function() {
proposal.createProposal();
});
$('#btnNew').click(function() {
proposal.resetProposal();
});
}
});
});
var ProposalGenerator = function(gov) {
this._mode = 'proposal';
this.gov = gov;
this.gov.network = 'livenet';
// proposal basic fields
this.gov.name = $('#name').val();
this.gov.url = $('#url').val();
this.gov.payment_address = $('#payment_address').val();
this.gov.payment_amount = parseFloat($('#payment_amount').val());
// format dates for gobject serialization
var start_epoch = $('#start_epoch');
var startdate = formattedDateForSuperblockHeight(start_epoch.val());
console.log('startdate: ' + startdate);
this.gov.start_epoch = (new Date(startdate)) || null;
var enddate = formattedDateForSuperblockHeight(start_epoch.val(),$('#end_epoch').val());
console.log('enddate: ' + enddate);
this.gov.end_epoch = (new Date(enddate)) || null;
// hidden elements
this.gov.type = parseInt($('#type').val());
};
ProposalGenerator.prototype.validate = function() {
try {
var gov = this.gov.serialize();
}
catch (e) {
switch(e.message) {
case 'Invalid Timespan':
console.log("Error: invalid timespan");
$('#start_epoch, #end_epoch').addClass('validationError');
break;
case 'Invalid Start Date':
console.log("Error: invalid start date");
$('#start_epoch').addClass('validationError');
break;
case 'Invalid End Date':
console.log("Error: invalid end date");
$('#end_epoch').addClass('validationError');
break;
case 'Invalid Address':
console.log("Error: invalid address");
$('#payment_address').addClass('validationError');
break;
case 'Invalid Payment Amount':
console.log("Error: invalid payment amount");
$('#payment_amount').addClass('validationError');
break;
case 'Invalid URL':
console.log("Error: invalid url");
$('#url').addClass('validationError');
break;
case 'Invalid Name':
console.log("error: invalid name");
$('#name').addClass('validationError');
break;
default:
console.log(e);
break;
}
return false;
}
return true;
};
ProposalGenerator.prototype.walletCommands = function() {
var gov = this.gov;
var propCommand = "gobject prepare "+$('#parentHash').val() + " " + $('#revision').val() +" " + $('#time').val() +" " + gov.serialize();
$("textarea#prepareProposal").val(propCommand);
if(this._mode == 'proposal') {
setFormEditable(true);
$('.walletCommands#walletCommandsHeader').removeClass('hidden');
$('.walletCommands#walletCommandsPrepare').removeClass('hidden');
$('.walletCommands#walletCommandsTx').removeClass('hidden');
//$('.walletCommands#walletCommandsSubmit').removeClass('hidden');
this._mode = 'command';
}
};
ProposalGenerator.prototype.createProposal = function() {
$('#feeTxid').val("");
$('#submitProposal').val("");
if(this._mode == 'command') {
setFormEditable(false);
$('.walletCommands#walletCommandsHeader').addClass('hidden');
$('.walletCommands#walletCommandsPrepare').addClass('hidden');
$('.walletCommands#walletCommandsTx').addClass('hidden');
$('.walletCommands#walletCommandsSubmit').addClass('hidden');
this._mode = 'proposal';
}
};
ProposalGenerator.prototype.resetProposal = function() {
$('.createProposal input').each(function() {
$(this).val('');
});
setFormEditable(true);
};
function setFormEditable(edit) {
$('.createProposal input').each(function() {
$(this).attr("disabled", edit);
});
$('.createProposal select').each(function() {
$(this).attr("disabled", edit);
});
if (edit === true) {
$('#btnPrepare').addClass('hidden');
$('#btnEdit').removeClass('hidden');
$('#btnNew').removeClass('hidden');
} else {
$('#btnPrepare').removeClass('hidden');
$('#btnEdit').addClass('hidden');
$('#btnNew').addClass('hidden');
}
}
var copyToClipboard = function(id) {
document.getElementById(id).select();
document.execCommand('copy');
};
getBudgetPaymentCycleBlocks = function() {
// Amount of blocks in a months period of time (using 2.6 minutes per) = (60*24*30)/2.6
if (gov.network == 'livenet') {
return 16616;
}
else {
//for testing purposes
return 50; //ten times per day
}
};
formattedDateForSuperblockHeight = function(startheight, months) {
months = months || 0;
var endheight;
var blockdiff;
if (months !== 0) {
endheight = (parseInt(startheight) + (getBudgetPaymentCycleBlocks()*months));
blockdiff = endheight - blockheight;
}
else {
blockdiff = startheight - blockheight;
}
var superblocktimestamp = $.now() + blockdiff * (2.6 * 60 * 1000);
return $.datepicker.formatDate('yy-mm-dd', new Date(superblocktimestamp));
};
function isAlphaNumeric(str) {
var code, i, len;
for (i = 0, len = str.length; i < len; i++) {
code = str.charCodeAt(i);
if (!(code > 47 && code < 58) && // numeric (0-9)
!(code > 64 && code < 91) && // upper alpha (A-Z)
!(code > 96 && code < 123)) { // lower alpha (a-z)
return false;
}
}
return true;
};
</script>
<style type="text/css">
textarea { background-color:#fafafa !important; }
#header { margin-top:2em; }
.createProposal { }
#createProposalHeader { margin-top:1em; }
#createProposalForm { margin-top:1em; }
.walletCommands { }
#walletCommandsHeader { margin-top:2em; }
#walletCommandsPrepare { margin-top:2em; }
#walletCommandsTx { margin-top:2em; }
#walletCommandsSubmit { margin-top:2em; }
.validationError { background-color:#ffe6e6; }
</style>
</head>
<body>
<div class="col-xs-10 col-xs-offset-1">
<div class="row" id="header">
<div class="col-xs-12">
<h1>Dash Budget Proposal Generator</h1>
Generate budget proposal commands you can copy/paste into your Dash wallet to prepare a budget proposal and submit it to the network.
</div>
</div>
<div class="row createProposal" id="createProposalHeader">
<div class="col-xs-12">
<h2>Create a Proposal</h2>
Enter details for your proposal and click 'Create Proposal'. This will generate a command you can run in your local wallet to prepare the proposal at a cost of 0.33 Dash
</div>
</div>
<div class="row createProposal" id="createProposalForm">
<div class="col-xs-6">
<div class="form-group">
<label for="name">Proposal Name:</label>
<input type="text" class="form-control" id="name" value="" placeholder="proposal-name">
</div>
<div class="form-group">
<label for="url">Proposal Description URL:</label>
<input type="text" class="form-control" id="url" value="" placeholder="https://www.dashcentral.org/p/proposal-name">
</div>
<div class="form-group">
<label for="start_epoch">Proposal Start Date:</label>
<select name="start_epoch" class="form-control" id="start_epoch"></select>
</div>
<div type="submit" class="btn btn-primary" id="btnPrepare">Create Proposal</div>
<div type="submit" class="btn btn-primary hidden" id="btnEdit">Edit Proposal</div>
<div type="submit" class="btn btn-primary hidden" id="btnNew">New Proposal</div>
<div class="form-group" style="display:none;">
<label for="type">type:</label>
<input type="text" class="form-control" id="type" value="1" placeholder="1">
</div>
<div class="form-group" style="display:none;">
<label for="parentHash">parent-hash:</label>
<input type="text" class="form-control" id="parentHash" value="0" placeholder="0">
</div>
<div class="form-group" style="display:none;">
<label for="revision">revision:</label>
<input type="text" class="form-control" id="revision" value="1" placeholder="1">
</div>
<div class="form-group" style="display:none;">
<label for="time">Creation Time:</label>
<input type="text" class="form-control" id="time" value="" placeholder="">
</div>
</div>
<div class="col-xs-6">
<div class="form-group">
<label for="payment_address">Payment Address:</label>
<input type="text" class="form-control" id="payment_address" value="" placeholder="">
</div>
<div class="form-group">
<label for="payment_amount">Monthly Payment Amount in Dash:</label>
<input type="text" class="form-control" id="payment_amount" value="" placeholder="0">
</div>
<div class="form-group">
<label for="end_epoch">Choose payment cycle:</label>
<select name="end_epoch" class="form-control" id="end_epoch"></select>
</div>
</div>
</div>
<div class="row walletCommands hidden" id="walletCommandsHeader">
<div class="col-xs-12">
<h2>Wallet Commands</h2>
</div>
</div>
<div class="row walletCommands hidden" id="walletCommandsPrepare">
<div class="col-xs-12">
<div class="form-group">
<label for="prepareProposal">Prepare-Proposal Command:</label>
<div>Paste this proposal command into your Dash wallet console to spend a Fee transaction of 0.33 DASH.</div>
<textarea readonly class="form-control" id="prepareProposal" rows="4" placeholder=""></textarea>
</div>
</div>
</div>
<div class="row walletCommands hidden" id="walletCommandsTx">
<div class="col-xs-12">
<div class="form-group">
<label for="feeTxid">Fee Transaction ID:</label>
<div>Paste the transaction ID returned by the wallet below to generate the final submit command.</div>
<input type="text" class="form-control" id="feeTxid" value="" placeholder="<fee-txid>">
</div>
</div>
</div>
<div class="row walletCommands hidden" id="walletCommandsSubmit">
<div class="col-xs-12">
<div class="form-group">
<label for="submitProposal">Submit Proposal Command:</label>
<div>Paste the Fee TX id to generate the proposal submit command. This is the final step and can be completed after 6 confirmations on the fee tx.</div>
<textarea readonly class="form-control" id="submitProposal" rows="4" placeholder=""></textarea>
</div>
</div>
</div>
</div>
</body>
</html>