Created
December 10, 2017 04:43
-
-
Save raypulver/5f7fd42c1d8f10920d99dfd5719d6ccf to your computer and use it in GitHub Desktop.
token sale ES5 UI scripting serverless loading data from infura or backup
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function () { | |
'use strict'; | |
var RPC = 'https://mainnet.infura.io/aR7WPNCrZhhnYRnn8yRT'; | |
RPC = 'http://159.203.127.98:9176'; | |
var saleAddress = '0x0d845706DdC11f181303a80828219c714ceb3687'; | |
var dvipAddress = '0xadc46ff5434910bd17b24ffb429e585223287d7f'; | |
var beneficiaryAddress = '0x532cc8b8AA6d61Da69c17E7C6197B0e43862a468'; | |
var auraPerDvip = '50000'; | |
var totalSaleDvip = '1600'; | |
var pollInterval = 10000; | |
var figureDecimals = 2; | |
var lodash = _; | |
var bindKey = lodash.bindKey; | |
var promisify = Promise.promisify; | |
var noop = lodash.noop; | |
var BN = BigNumber; | |
BN.config({ | |
DECIMAL_PLACES: 100, | |
ERRORS: false | |
}); | |
var RPC = 'https://mainnet.infura.io/aR7WPNCrZhhnYRnn8yRT'; | |
RPC = 'http://159.203.127.98:9176'; | |
var web3 = new Web3(new Web3.providers.HttpProvider(RPC)); | |
var call = promisify(bindKey(web3.eth, 'call')); | |
var encodeFunctionCall = bindKey(web3.eth.abi, 'encodeFunctionCall'); | |
var getBlockNumber = promisify(bindKey(web3.eth, 'getBlockNumber')); | |
var getBalance = promisify(bindKey(web3.eth, 'getBalance')); | |
var map = Promise.map; | |
var mapSeries = Promise.mapSeries; | |
var blockSubscribers = []; | |
function subscribeToBlock(fn) { | |
blockSubscribers.push(fn); | |
return function () { | |
blockSubscribers = blockSubscribers.filter(function (v) { | |
return v !== fn; | |
}); | |
}; | |
} | |
function timeout(n) { | |
return new Promise(function (resolve, reject) { | |
setTimeout(resolve, n); | |
}); | |
} | |
subscribeToBlock(function () { | |
call({ | |
to: saleAddress, | |
data: encodeFunctionCall({ | |
name: 'price', | |
inputs: [] | |
}, []) | |
}).then(function (price) { | |
var pricePerDvip = new BN(price).div(new BN(10).pow(10)).toPrecision(); | |
var dvipPerEth = new BN(1).div(pricePerDvip).toPrecision(); | |
var auraPerEth = new BN(dvipPerEth).mul(auraPerDvip).toPrecision(); | |
var pricePerDvipRounded = new BN(pricePerDvip).mul(new BN(10).pow(figureDecimals)).floor().div(new BN(10).pow(figureDecimals)).toPrecision(); | |
var dvipPerEthRounded = new BN(dvipPerEth).mul(new BN(10).pow(figureDecimals)).floor().div(new BN(10).pow(figureDecimals)).toPrecision(); | |
var auraPerEthRounded = new BN(auraPerEth).mul(new BN(10).pow(figureDecimals)).floor().div(new BN(10).pow(figureDecimals)).toPrecision(); | |
// do something with rounded values | |
$('#price-per-dvip').html(pricePerDvipRounded + " ETH per DVIP"); | |
$('#dvip-per-eth').html(dvipPerEthRounded); | |
$('#aura-per-eth').html(auraPerEthRounded); | |
console.log(pricePerDvipRounded); | |
}); | |
}); | |
subscribeToBlock(function () { | |
call({ | |
to: dvipAddress, | |
data: encodeFunctionCall({ | |
name: 'balanceOf', | |
inputs: [{ | |
type: 'address', | |
name: 'owner' | |
}] | |
}, [ saleAddress ]) | |
}).then(function (amount) { | |
var amountString = new BN(new BN(totalSaleDvip).mul(new BN(10).pow(8))).minus(new BN(amount)).div(new BN(10).pow(8 - figureDecimals)).floor().div(new BN(10).pow(figureDecimals)).toPrecision(); | |
$('.figures').children().eq(0).text(amountString + ' DVIP Sold'); | |
$('.figures').children().eq(1).text(new BN(totalSaleDvip).minus(amountString).toPrecision() + ' DVIP Left'); | |
var percentComplete = new BN(amountString).div(totalSaleDvip).mul(10000).floor().div(100).toPrecision(); | |
var percentCompleteAdjusted = new BN(percentComplete).mul(0.99-0.21).add('21').toPrecision(); | |
var percentCompleteAdjustedProgress = new BN(percentComplete).mul(1-0.20).add('20').toPrecision(); | |
var auraGivenString = new BN(auraPerDvip).mul(amountString).toPrecision(); | |
$('.figures').children().eq(2).text(auraGivenString + ' AURA Given'); | |
$('.progress-bar--002').eq(0).css({ | |
width: percentCompleteAdjusted + '%' | |
}); | |
$('#progress-percent').text(percentCompleteAdjustedProgress + '%'); | |
// After the green bar has grown enough move the text into the bar | |
if (percentCompleteAdjusted > 33) { | |
$('.progress-bar--002').removeClass('pivot'); | |
} | |
}); | |
}); | |
subscribeToBlock(function () { | |
getBalance(beneficiaryAddress).then(function (balance) { | |
var eth = new BN(balance).div(new BN(10).pow(18 - figureDecimals)).floor().div(new BN(10).pow(figureDecimals)).toPrecision(); | |
$('.figures').children().eq(3).text(eth + ' ETH Raised'); | |
}); | |
}); | |
(function startPollingBlocks() { | |
var block = 0; | |
(function pollOne() { | |
function next() { | |
return timeout(pollInterval).then(pollOne); | |
} | |
return getBlockNumber().then((number) => { | |
if (number !== block) { | |
block = number; | |
blockSubscribers.forEach(function (v) { | |
v(number); | |
}); | |
} | |
}).catch(noop).then(next); | |
})(); | |
})(); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment