Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

init sumcoin #10

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
55 changes: 55 additions & 0 deletions currencies/sum.coffee
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
class SUM
constructor: ->
@ws = null
@socketUrl = "wss://insight.sumcore.org/socket.io/?EIO=3&transport=websocket"
@txApi = "https://insight.sumcore.org/api/tx/"
@blockApi = "https://insight.sumcore.org/api/block/"
@txFees = [0.000224, 0.0005]
@txFeeTimestamp = 0
@txFeeInterval = 3000 # how often to query for a fee
@donationAddress = "SX75kbV2to7LCMj2rc9Bb8agpZxAg6rzoL"

start: (txCb, blockCb) ->
@stop() if @ws
@ws = new WebSocket @socketUrl

@ws.onclose = =>
setTimeout (=> @start txCb, blockCb), 1000

@ws.onopen = =>
@ws.send '2probe'
@ws.send '5'
@ws.send '420["subscribe","sync"]'
@ws.send '421["subscribe","inv"]'
@ws.send '422["subscribe","sync"]'
@ws.send '424["subscribe","sync"]'
@ws.send '425["subscribe","inv"]'
@ping = setInterval (=> @ws.send '2'), 25*1000

@ws.onmessage = ({data}) =>
data = data.match /^\d+(\[.+?)$/
if data
[type, payload] = JSON.parse(data[1])
if type is 'tx'
# fetch fees every now and then
if new Date().getTime() - @txFeeInterval > @txFeeTimestamp
$.get @txApi + payload.txid, ({fees}) =>
if fees
@txFees.shift()
@txFees.push(fees)
@txFeeTimestamp = new Date().getTime()

txCb? {
amount: payload.valueOut
fee: Math.random() * Math.abs(@txFees[0] - @txFees[1]) + Math.min.apply(0, @txFees)
link: 'https://insight.sumcore.org/tx/' + payload.txid
donation: !!payload.vout.find (vout) => Object.keys(vout)[0] is @donationAddress
}
else
$.get @blockApi + payload, ({tx}) =>
blockCb? count: if tx then tx.length else 0

stop: ->
@ws.close()
clearInterval @ping
@ws = null
83 changes: 83 additions & 0 deletions currencies/sum.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions currencies/sum.js.map

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added img/sum-qr.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/sum.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added img/sum2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
<script type="text/javascript" src="currencies/btc.js"></script>
<script type="text/javascript" src="currencies/eth.js"></script>
<script type="text/javascript" src="currencies/ltc.js"></script>
<script type="text/javascript" src="currencies/sum.js"></script>
<script type="text/javascript" src="currencies/nano.js"></script>
<script type="text/javascript" src="currencies/xrp.js"></script>
<script type="text/javascript" src="engine.js"></script>
Expand Down Expand Up @@ -87,6 +88,14 @@
<a class="donate">Donate</a>
</div>
</div>
<div class="sum">
<div class="text">
<span class="stats"></span>
<a href="https://sumcoin.org" target="_blank" class="label">Sumcoin</a>
<span class="price"></span>
<a class="donate">Donate</a>
</div>
</div>
<div class="ltc">
<div class="text">
<span class="stats"></span>
Expand Down
30 changes: 29 additions & 1 deletion main.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@ currencyFormat =
minimumFractionDigits: 0
maximumFractionDigits:0

sumcurrency =
sum: new SUM()
prices = {}
stats = {}

sumcurrencyFormat =
style: 'currency'
currency: 'USD'
minimumFractionDigits: 0
maximumFractionDigits:0

# render TX
showTx = (engine, currency, tx) ->
value = tx.amount * (prices[currency] or 1)
Expand Down Expand Up @@ -48,8 +59,25 @@ updatePrices = (currencies) ->
if data
$(".marketcap").text JSON.parse(data)[0].total_mcap.toLocaleString(undefined, currencyFormat)

setTimeout updatePrices.bind(null, currencies), 10*1000

# get current Sumcoin price
updatePrices = (sumcurrency) ->
sumcurrencyAPI = 'https://rates.slicewallet.org/api/rates'
$.get code + price.join(',').toUpperCase(), (data) ->
if data
for sumcurrency, price of data
sumcurrency = sumcurrency.toLowerCase()
prices[currency] = Math.round(1/price*100)/100
$(".#{sumcurrency} .price").text prices[currency].toLocaleString(undefined, { style: 'currency', sumcurrency: 'USD' })

# get sumcoin marketcap
summarketcapAPI = 'https://sumcoinindex.com/rates/marketcap.json'
$.get marketcapAPI, (data) ->
if data
$("marketcap_USD").text JSON.parse(data)[0].total_mcap.toLocaleString(undefined, currencyFormat)

setTimeout updatePrices.bind(null, currencies), 10*1000
setTimeout updateSumPrice.bind(null, sumcurrency), 10*1000

# update stats for a currency, called whenever there is a new TX
# to do that, keep a log of the last 60 seconds of tx
Expand Down
1 change: 1 addition & 0 deletions main.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ a.label {

div.marketcap {
position: absolute;
bottom: 88px;
bottom: 100px;
left: 50%;
text-align: center;
color: #a4d2fd;
Expand Down