stacker.news/lib/format.js
Keyan cc289089cf
not-custodial zap beta (#1178)
* not-custodial zap scaffolding

* invoice forward state machine

* small refinements to state machine

* make wrap invoice work

* get state machine working end to end

* untested logic layout for paidAction invoice wraps

* perform pessimisitic actions before outgoing payment

* working end to end

* remove unneeded params from wallets/server/createInvoice

* fix cltv relative/absolute confusion + cancelling forwards

* small refinements

* add p2p wrap info to paidAction docs

* fallback to SN invoice when wrap fails

* fix paidAction retry description

* consistent naming scheme for state machine

* refinements

* have sn pay bounded outbound fee

* remove debug logging

* reenable lnc permissions checks

* don't p2p zap on item forward splits

* make createInvoice params json encodeable

* direct -> p2p badge on notifications

* allow no tls in dev for core lightning

* fix autowithdraw to create invoice with msats

* fix autowithdraw msats/sats inconsitency

* label p2p zaps properly in satistics

* add fees to autowithdrawal notifications

* add RETRYING as terminal paid action state

* Update api/paidAction/README.md

Co-authored-by: ekzyis <ek@stacker.news>

* Update api/paidAction/README.md

Co-authored-by: ekzyis <ek@stacker.news>

* Update api/lnd/index.js

Co-authored-by: ekzyis <ek@stacker.news>

* ek suggestions

* add bugetable to nwc card

* get paranoid with numbers

* better finalize retries and better max timeout height

* refine forward failure transitions

* more accurate satistics p2p status

* make sure paidaction cancel in state machine only

* dont drop bolt11s unless status is not null

* only allow PENDING_HELD to transition to FORWARDING

* add mermaid state machine diagrams to paid action doc

* fix cancel transition name

* cleanup readme

* move forwarding outside of transition

* refine testServerConnect and make sure ensureB64 transforms

* remove unused params from testServerConnect

---------

Co-authored-by: ekzyis <ek@stacker.news>
Co-authored-by: k00b <k00b@stacker.news>
2024-08-13 09:48:30 -05:00

110 lines
3.0 KiB
JavaScript

export const abbrNum = n => {
if (n < 1e4) return n
if (n >= 1e4 && n < 1e6) return +(n / 1e3).toFixed(1) + 'k'
if (n >= 1e6 && n < 1e9) return +(n / 1e6).toFixed(1) + 'm'
if (n >= 1e9 && n < 1e12) return +(n / 1e9).toFixed(1) + 'b'
if (n >= 1e12) return +(n / 1e12).toFixed(1) + 't'
}
export function suffix (n) {
const j = n % 10
const k = n % 100
if (j === 1 && k !== 11) {
return n + 'st'
}
if (j === 2 && k !== 12) {
return n + 'nd'
}
if (j === 3 && k !== 13) {
return n + 'rd'
}
return n + 'th'
}
/**
* Take a number that represents a count
* and return a formatted label e.g. 0 sats, 1 sat, 2 sats
*
* @param n The number of sats
* @param opts Options
* @param opts.abbreviate Whether to abbreviate the number
* @param opts.unitSingular The singular unit label
* @param opts.unitPlural The plural unit label
* @param opts.format Format the number with `Intl.NumberFormat`. Can only be used if `abbreviate` is false
*/
export const numWithUnits = (n, {
abbreviate = true,
unitSingular = 'sat',
unitPlural = 'sats',
format
} = {}) => {
if (isNaN(n)) {
return `${n} ${unitPlural}`
}
return `${abbreviate ? abbrNum(n) : !abbreviate || format === true ? new Intl.NumberFormat().format(n) : n} ${n === 1 ? unitSingular : unitPlural}`
}
export const fixedDecimal = (n, f) => {
return Number.parseFloat(n).toFixed(f)
}
export const msatsToSats = msats => {
if (msats === null || msats === undefined) {
return null
}
return Number(BigInt(msats) / 1000n)
}
export const satsToMsats = sats => {
if (sats === null || sats === undefined) {
return null
}
return BigInt(sats) * 1000n
}
export const msatsToSatsDecimal = msats => {
if (msats === null || msats === undefined) {
return null
}
return fixedDecimal(Number(msats) / 1000.0, 3)
}
export const hexToB64 = hexstring => {
return btoa(hexstring.match(/\w{2}/g).map(function (a) {
return String.fromCharCode(parseInt(a, 16))
}).join(''))
}
// some base64 encoders get fancy and remove padding
export const ensureB64Padding = str => {
return str + Array((4 - str.length % 4) % 4 + 1).join('=')
}
export const B64_REGEX = /^(?:[A-Za-z0-9+/]{4})*(?:[A-Za-z0-9+/]{2}==|[A-Za-z0-9+/]{3}=)?$/
export const B64_URL_REGEX = /^(?:[A-Za-z0-9_-]{4})*(?:[A-Za-z0-9_-]{2}[.=]{2}|[A-Za-z0-9_-]{3}[.=])?$/
export const HEX_REGEX = /^[0-9a-fA-F]+$/
export const ensureB64 = hexOrB64Url => {
if (HEX_REGEX.test(hexOrB64Url)) {
hexOrB64Url = hexToB64(hexOrB64Url)
}
hexOrB64Url = ensureB64Padding(hexOrB64Url)
// some folks use url-safe base64
if (B64_URL_REGEX.test(hexOrB64Url)) {
// Convert from URL-safe base64 to regular base64
hexOrB64Url = hexOrB64Url.replace(/-/g, '+').replace(/_/g, '/').replace(/\./g, '=')
switch (hexOrB64Url.length % 4) {
case 2: hexOrB64Url += '=='; break
case 3: hexOrB64Url += '='; break
}
}
if (B64_REGEX.test(hexOrB64Url)) {
return hexOrB64Url
}
throw new Error('not a valid hex or base64 url or base64 encoded string')
}