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

the great msat migration #216

Open
wants to merge 5 commits 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
2 changes: 1 addition & 1 deletion client/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mkdir -p $DEST $DEST/lib $DEST/fonts $DEST/swatch
# Static assets
cp -r www/* $DEST/
cp -r fonts/node_modules/typeface-* $DEST/fonts/
cp -r node_modules/bootswatch/dist/!(darkly|litera|minty|sketchy|journal|pulse) $DEST/swatch/
cp -r node_modules/bootswatch/dist/!(litera|minty|sketchy|journal|pulse) $DEST/swatch/
cp -r swatch/*/ $DEST/swatch/
find $DEST/swatch -type f ! -name '*.min.css' -delete
find $DEST/fonts -type f -name "*.md" -delete
Expand Down
2 changes: 1 addition & 1 deletion client/src/intent.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ module.exports = ({ DOM, route, conf$, scan$, urihandler$, offerInv$ }) => {
// New invoice/offer action
, newInv$ = submit('[do=new-invoice]').map(r => ({
label: nanoid()
, msatoshi: r.msatoshi || 'any'
, amount_msat: r.amount_msat || 'any'
, description: r.description || '⚡'
, reusable_offer: !!r['reusable-offer'] }))
, invUseOffer$ = on('[do=new-invoice] [name=reusable-offer]', 'input')
Expand Down
2 changes: 1 addition & 1 deletion client/src/load-theme.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// quickly load the theme and set its primary theme-color, without requiring the whole app to load.
// post-load theme changes are handled by the app itself.

const theme = localStorage.conf && JSON.parse(localStorage.conf).theme || 'dark'
const theme = localStorage.conf && JSON.parse(localStorage.conf).theme || 'darkly'

document.write('<link rel="stylesheet" href="swatch/'+theme+'/bootstrap.min.css">')

Expand Down
32 changes: 16 additions & 16 deletions client/src/model.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ const msatbtc = big(100000000000) // msat in 1 btc
const
sumChans = chans =>
chans.filter(c => c.chan.state === 'CHANNELD_NORMAL')
.reduce((T, c) => T + c.chan.msatoshi_to_us, 0)
.reduce((T, c) => T + c.chan.to_us_msat, 0)

, sumOuts = outs =>
outs.filter(o => o.status === 'confirmed')
.reduce((T, o) => T + o.value*1000, 0)
.reduce((T, o) => T + o.amount_msat, 0)

, fmtAlert = (s, unitf) => s.replace(/@\{\{(\d+)\}\}/g, (_, msat) => unitf(msat))

, idx = xs => x => xs.indexOf(x)
, idn = x => x

const
themes = 'cerulean cosmo cyborg dark flatly lumen lux materia sandstone simplex slate solar spacelab superhero united yeti'.split(' ')
themes = 'cerulean cosmo cyborg darkly flatly lumen lux materia sandstone simplex slate solar spacelab superhero united yeti'.split(' ')
, units = 'sat bits milli BTC USD'.split(' ')
, unitprec = { sat: 3, bits: 5, milli: 8, BTC: 11, USD: 6 }
, unitrate = { sat: 0.001, bits: 0.00001, milli: 0.00000001, BTC: 0.00000000001 }
Expand All @@ -37,9 +37,9 @@ module.exports = ({ dismiss$, togExp$, togTheme$, togUnit$, page$, goHome$, goRe

// Config options
conf = (name, def, list) => savedConf$.first().map(c => c[name] || def).map(list ? idx(list) : idn)
, expert$ = conf('expert', false) .concat(togExp$) .scan(x => !x)
, theme$ = conf('theme', 'dark', themes).concat(togTheme$).scan(n => (n+1) % themes.length).map(n => themes[n])
, unit$ = conf('unit', 'bits', units).concat(togUnit$) .scan(n => (n+1) % units.length) .map(n => units[n])
, expert$ = conf('expert', true) .concat(togExp$) .scan(x => !x)
, theme$ = conf('theme', 'darkly', themes).concat(togTheme$).scan(n => (n+1) % themes.length).map(n => themes[n])
, unit$ = conf('unit', 'sat', units).concat(togUnit$) .scan(n => (n+1) % units.length) .map(n => units[n])
, conf$ = combine({ expert$, theme$, unit$ })

// Currency & unit conversion handling
Expand Down Expand Up @@ -68,8 +68,8 @@ module.exports = ({ dismiss$, togExp$, togTheme$, togUnit$, page$, goHome$, goRe
, alert$ = O.merge(
error$.map(err => [ 'danger', ''+err ])
, incoming$.map(i => [ 'success', `Received payment of @{{${recvAmt(i)}}}` ])
, paySent$.map(p => [ 'success', `Sent payment of @{{${p.msatoshi}}}` ])
, funded$.map(c => [ 'success', `Opening channel for @{{${c.chan.msatoshi_total}}}, awaiting on-chain confirmation` ])
, paySent$.map(p => [ 'success', `Sent payment of @{{${p.amount_msat}}}` ])
, funded$.map(c => [ 'success', `Opening channel for @{{${c.chan.total_msat}}}, awaiting on-chain confirmation` ])
, closed$.map(c => [ 'success', `Channel ${c.chan.short_channel_id || c.chan.channel_id} is closing` ])
, dismiss$.mapTo(null)
)
Expand All @@ -88,7 +88,7 @@ module.exports = ({ dismiss$, togExp$, togTheme$, togUnit$, page$, goHome$, goRe
// Periodically re-sync channel balance from "listpeers",
// continuously patch with known channel opening/closing
, channels$ = O.merge(
peers$.map(peers => _ => getChannels(peers))
peers$.map(channels => _ => getChannels(channels))
, funded$.map(chan => S => [ ...S, chan ])
, closed$.map(chan => S => [ ...S.filter(c => c.chan.channel_id != chan.chan.channel_id), chan ])
).startWith(null).scan((S, mod) => mod(S))
Expand All @@ -99,8 +99,8 @@ module.exports = ({ dismiss$, togExp$, togTheme$, togUnit$, page$, goHome$, goRe
// continuously patch with known incoming & outgoing payments
, cbalance$ = O.merge(
channels$.map(chans => _ => sumChans(chans))
, incoming$.map(inv => N => N + inv.msatoshi_received)
, paySent$.map(pay => N => N - pay.msatoshi_sent)
, incoming$.map(inv => N => N + inv.amount_received_msat)
, paySent$.map(pay => N => N - pay.amount_sent_msat)
).startWith(null).scan((N, mod) => mod(N)).distinctUntilChanged()

// Periodically re-sync from listpays, continuously patch with known outgoing
Expand Down Expand Up @@ -128,7 +128,7 @@ module.exports = ({ dismiss$, togExp$, togTheme$, togUnit$, page$, goHome$, goRe
// Chronologically sorted feed of incoming and outgoing payments
, feed$ = O.combineLatest(freshInvs$, freshPays$, (invoices, payments) => [
...invoices.map(i => [ 'in', i.paid_at, recvAmt(i), i ])
, ...payments.map(p => [ 'out', p.created_at, p.msatoshi, p ])
, ...payments.map(p => [ 'out', p.created_at, p.amount_msat, p ])
].sort((a, b) => b[1] - a[1]))

// Collapsed payment/invoice on home feed list
Expand All @@ -145,7 +145,7 @@ module.exports = ({ dismiss$, togExp$, togTheme$, togUnit$, page$, goHome$, goRe
, amtMsat$ = amtVal$.withLatestFrom(rate$, (amt, rate) => amt && rate && big(amt).div(rate).toFixed(0) || '')
.merge(page$.mapTo(null)).startWith(null)
, amtData$ = combine({
msatoshi: amtMsat$
amount_msat: amtMsat$
, amount: unit$.withLatestFrom(amtMsat$, rate$, (unit, msat, rate) => formatAmt(msat, rate, unitprec[unit], false))
.merge(goRecv$.merge(offer$).merge(payreq$).mapTo(''))
, unit: unit$
Expand Down Expand Up @@ -205,8 +205,8 @@ const markFailed = (payments, payment_hash) => payments.map(pay =>
const pendingPayStub = inv => ({
status: 'pending'
, created_at: Date.now()/1000|0
, msatoshi: inv.custom_msat || inv.msatoshi
, msatoshi_sent: 0
, amount_msat: inv.custom_msat || inv.amount_msat
, amount_sent_msat: 0
, destination: inv.payee || inv.node_id || inv.destination
, ...only(inv, 'payment_hash', 'description', 'offer_id', 'vendor', 'quantity', 'payer_note' )
})
Expand All @@ -229,4 +229,4 @@ const unitFormatter = (unit, msatusd) => (msat, as_alt_unit=false, non_breaking=
// Check if experimental offers support is enabled
// Always considered off in c-lightning <=v0.10.0 because it used an incompatible spec.
const checkOffersEnabled = conf =>
!!(conf['experimental-offers'] && !/^0\.(9\.|10\.0)/.test(conf['# version']))
!!(conf['experimental-offers'] && !/^0\.(9\.|10\.0)/.test(conf['# version']))
10 changes: 5 additions & 5 deletions client/src/rpc.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ exports.parseRes = ({ HTTP, SSE }) => {

// Periodic updates
, info$: reply('getinfo').map(r => r.body)
, peers$: reply('listpeers').map(r => r.body.peers)
, peers$: reply('listpeerchannels').map(r => r.body.channels)
, payments$: reply('_listpays').map(r => r.body.pays)
, invoices$: reply('_listinvoices').map(r => r.body.invoices)
, funds$: reply('listfunds').map(r => r.body)
Expand Down Expand Up @@ -69,14 +69,14 @@ exports.makeReq = ({ viewPay$, confPay$, offerPay$, offerRecv$, newInv$, goLogs$
viewPay$.map(paystr => [ '_decodecheck', [ paystr ], { paystr } ])
, confPay$.map(pay => [ '_pay', [ pay.paystr, pay.custom_msat ], { pay, bg: true } ])
, newInv$.map(inv => !inv.reusable_offer
? [ 'invoice', [ inv.msatoshi, inv.label, inv.description, INVOICE_TTL ], inv ]
: [ 'offer', [ inv.msatoshi, inv.description, null, inv.label ], inv ])
? [ 'invoice', [ inv.amount_msat, inv.label, inv.description, INVOICE_TTL ], inv ]
: [ 'offer', [ inv.amount_msat, inv.description, null, inv.label ], inv ])
, offerPay$.map(pay => [ '_fetchinvoice', [ pay.paystr, pay.custom_msat, pay.quantity, pay.payer_note ] ])
, offerRecv$.map(recv => [ 'sendinvoice', [ recv.paystr, recv.label ] ])

, goLogs$.mapTo( [ 'getlog' ] )

, updChan$.mapTo( [ 'listpeers' ] )
, updChan$.mapTo( [ 'listpeerchannels' ] )
, openChan$.map(d => [ '_connectfund', [ d.nodeuri, d.channel_capacity_sat, d.feerate ] ])
, closeChan$.map(d => [ '_close', [ d.peerid, d.chanid ] ])

Expand All @@ -90,7 +90,7 @@ exports.makeReq = ({ viewPay$, confPay$, offerPay$, offerRecv$, newInv$, goLogs$
, timer(60000).mapTo( [ '_listpays', [], { bg: true } ])
, timer(60000).mapTo( [ 'getinfo', [], { bg: true } ])
, timer(60000).merge(goChan$).throttleTime(2000)
.mapTo( [ 'listpeers', [], { bg: true } ])
.mapTo( [ 'listpeerchannels', [], { bg: true } ])
, timer(60000).merge(goNewChan$).merge(goDeposit$).throttleTime(2000)
.mapTo( [ 'listfunds', [], { bg: true } ])

Expand Down
16 changes: 7 additions & 9 deletions client/src/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ export const parseUri = uri => {
// returns the expected invoice amount when its <0.5% different from the actual amount paid,
// or the actual amount paid otherwise. this is done to make the UX less confusing when the
// sender uses overpayment randomization (/~https://github.com/ElementsProject/lightning/issues/1089)
export const recvAmt = ({ msatoshi: expected, msatoshi_received: actual }) =>
export const recvAmt = ({ amount_msat: expected, amount_received_msat: actual }) =>
(expected && (actual-expected)/expected<0.005) ? expected : actual

// Parse "listpeers" to get all channels as a list of (peer,channel) tuples
export const getChannels = peers => [].concat(...peers.map(peer => peer.channels.map(chan => ({ peer, chan }))))
// Parse "listpeerchannels" to get all channels as a list of (peer,channel) tuples
export const getChannels = channels => [].concat(...channels.map(chan => ({ chan })))

// TODO: remove this function and refactor the code that was using it...
// this function is not needed anymore, as a quickfix, it is now Identity
// Parse the `sat`-suffixed amount string fields into numbers
export const parsePayment = p => ({
...p
, msatoshi: p.msatoshi != null ? p.msatoshi : p.amount_msat ? +p.amount_msat.slice(0, -4) : null
, msatoshi_sent: p.amount_sent_msat ? +p.amount_sent_msat.slice(0, -4) : null
})
export const parsePayment = p => p

export const combine = obj => {
const keys = Object.keys(obj).map(k => k.replace(/\$$/, ''))
Expand Down Expand Up @@ -70,4 +68,4 @@ export const parseRpcCmd = str => {
export const only = (obj, ...keys) => keys.reduce((R, k) => {
if (obj[k] != null) R[k] = obj[k]
return R
}, {})
}, {})
36 changes: 18 additions & 18 deletions client/src/views/channels.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const blockInterval = process.env.BLOCK_INTERVAL || 600

const stateGroups = {
active: [ 'CHANNELD_NORMAL' ]
, opening: [ 'CHANNELD_AWAITING_LOCKIN', 'OPENINGD' ]
, opening: [ 'CHANNELD_AWAITING_LOCKIN', 'OPENINGD', 'DUALOPEND_AWAITING_LOCKIN' ]
, closing: [ 'CLOSINGD_COMPLETE', 'CLOSINGD_SIGEXCHANGE', 'CHANNELD_SHUTTING_DOWN', 'AWAITING_UNILATERAL' ]
, closed: [ 'FUNDING_SPEND_SEEN', 'ONCHAIN' ]
}
Expand All @@ -14,12 +14,12 @@ const stateGroups = {
const getGroup = state => Object.keys(stateGroups).find(group => stateGroups[group].includes(state))

// Sort by status first, then by amount
const chanSorter = (a, b) => (chanSorting(b) - chanSorting(a)) || (b.chan.msatoshi_total - a.chan.msatoshi_total)
const chanSorter = (a, b) => (chanSorting(b) - chanSorting(a)) || (b.chan.total_msat - a.chan.total_msat)

const chanSorting = ({ peer, chan }) =>
peer.connected && chan.state == 'CHANNELD_NORMAL' ? 6
: peer.connected && stateGroups.opening.includes(chan.state) ? 5
: !peer.connected && chan.state == 'CHANNELD_NORMAL' ? 4
const chanSorting = ({ chan }) =>
chan.peer_connected && chan.state == 'CHANNELD_NORMAL' ? 6
: chan.peer_connected && stateGroups.opening.includes(chan.state) ? 5
: !chan.peer_connected && chan.state == 'CHANNELD_NORMAL' ? 4
: stateGroups.closing.includes(chan.state) ? 3
: stateGroups.opening.includes(chan.state) ? 2
: stateGroups.closed.includes(chan.state) ? 1
Expand Down Expand Up @@ -77,19 +77,19 @@ export const newChannel = ({ amtData, fundMaxChan, obalance, unitf, conf: { unit
])
}

const channelRenderer = ({ chanActive, unitf, expert, blockheight }) => ({ chan, peer }) => {
const channelRenderer = ({ chanActive, unitf, expert, blockheight }) => ({ chan }) => {

const bar = (label, color, msatoshi, amtText=unitf(msatoshi)) =>
const bar = (label, color, amount_msat, amtText=unitf(amount_msat)) =>
div(`.progress-bar.bg-${color}`, {
attrs: { role: 'progressbar', title: `${label}: ${amtText}` }
, style: { width: `${msatoshi / chan.msatoshi_total * 100}%` }
}, msatoshi/chan.msatoshi_total > 0.05 ? amtText : '')
, style: { width: `${amount_msat / chan.total_msat * 100}%` }
}, amount_msat/chan.total_msat > 0.05 ? amtText : '')

const stateGroup = getGroup(chan.state)
, stateLabel = !peer.connected && stateGroup == 'active' ? 'offline' : stateGroup
, stateLabel = !chan.peer_connected && stateGroup == 'active' ? 'offline' : stateGroup
, isClosed = [ 'closing', 'closed' ].includes(stateGroup)
, ours = chan.msatoshi_to_us
, theirs = chan.msatoshi_total - ours
, ours = chan.to_us_msat
, theirs = chan.total_msat - ours
// the channel reserve fields appear to be sometimes (incorrectly?) missing,
// defaulting them to 0 isn't quite right but should work for now
, ourReserve = chan.our_channel_reserve_satoshis*1000 || 0
Expand All @@ -103,11 +103,11 @@ const channelRenderer = ({ chanActive, unitf, expert, blockheight }) => ({ chan,

const visible = chanActive == chan.channel_id
, classes = { active: visible, 'list-group-item-action': !visible
, [`c-${stateGroup}`]: true, 'p-online': peer.connected, 'p-offline': !peer.connected }
, [`c-${stateGroup}`]: true, 'p-online': chan.peer_connected, 'p-offline': !chan.peer_connected }

return li('.list-group-item', { class: classes, dataset: { chanToggle: chan.channel_id } }, [
header('.d-flex.justify-content-between.mb-2', [
span('.capacity', unitf(chan.msatoshi_total))
span('.capacity', unitf(chan.total_msat))
, span('.state', stateLabel)
])

Expand All @@ -133,14 +133,14 @@ const channelRenderer = ({ chanActive, unitf, expert, blockheight }) => ({ chan,
, isClosed || expert ? li([ strong('Theirs:'), ' ', unitf(theirs) ]) : ''

, channelAge ? li([ strong('Age:'), ' ', `${channelAge} blocks (${channelAgeFuz})` ]) : ''
, li([ strong('Peer:'), ' ', small('.break-all', peer.id), ' ', em(`(${peer.connected ? 'connected' : 'disconnected'})`) ])
, li([ strong('Peer:'), ' ', small('.break-all', chan.peer_id), ' ', em(`(${chan.peer_connected ? 'connected' : 'disconnected'})`) ])
, expert ? li([ strong('Funding TXID:'), ' ', small('.break-all', chan.funding_txid) ]) : ''
, expert ? li('.status-text', chan.status.join('\n')) : ''

, !isClosed ? li('.text-center'
, button('.btn.btn-link.btn-sm', { dataset: { closeChannel: chan.channel_id, closeChannelPeer: peer.id } }, 'Close channel')) : ''
, button('.btn.btn-link.btn-sm', { dataset: { closeChannel: chan.channel_id, closeChannelPeer: chan.peer_id } }, 'Close channel')) : ''

, expert ? li(yaml({ peer: omitKey('channels', peer), ...omitKey('status', chan) })) : ''
, expert ? li(yaml({ chan: omitKey('status', chan) })) : ''
])
])
}
6 changes: 3 additions & 3 deletions client/src/views/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ const itemRenderer = ({ feedActive, unitf, expert }) => ([ type, ts, msat, obj ]
status == 'pending' ? pendingStatus(obj) : ''
, li([ strong(tsLabel), ' ', tsStr ])
, status == 'failed' && msat ? li([ strong('Amount:'), ' ', unitf(msat) ]) : ''
, type == 'in' && obj.msatoshi_received > obj.msatoshi ? li([ strong('Overpayment:'), ' ', unitf(obj.msatoshi_received-obj.msatoshi) ]) : ''
, type == 'out' && status == 'complete' && obj.msatoshi ? li([ strong('Fee:'), ' ', feesText(obj, unitf) ]) : ''
, type == 'in' && obj.amount_received_msat > obj.amount_msat ? li([ strong('Overpayment:'), ' ', unitf(obj.amount_received_msat-obj.amount_msat) ]) : ''
, type == 'out' && status == 'complete' && obj.amount_msat ? li([ strong('Fee:'), ' ', feesText(obj, unitf) ]) : ''
, obj.vendor ? li([ strong('Issuer:'), ' ', span('.break-word', obj.vendor) ]) : ''
, showDesc(obj) ? li([ strong('Description:'), ' ', span('.break-word', obj.description) ]) : ''
, obj.quantity ? li([ strong('Quantity:'), ' ', span('.break-word', obj.quantity) ]) : ''
Expand All @@ -72,7 +72,7 @@ const itemRenderer = ({ feedActive, unitf, expert }) => ([ type, ts, msat, obj ]
])
}

const feesText = ({ msatoshi: quoted, msatoshi_sent: sent }, unitf) =>
const feesText = ({ amount_msat: quoted, amount_sent_msat: sent }, unitf) =>
`${unitf(sent-quoted)} (${((sent-quoted)/quoted*100).toFixed(2)}%)`

const pendingStatus = ({ attempts, number_of_parts }) => {
Expand Down
Loading