info = { type = "app", title = "Mempool", author = "BRRRO", license = "MIT", desc = "Transaction queue and fees", category = "btc", api = 43, } local W, H = gfx.W, gfx.H local FPS = gfx.FPS local px = gfx.px local blend = gfx.blend local rect = gfx.rect local blend_rect = gfx.blend_rect local text, text_center, text_width, text_ink = gfx.text, gfx.text_center, gfx.text_width, gfx.text_ink local F_SMALL = { font = "3x5" } local F_BIG = { font = "9x15b" } local floor, fmod, abs, sin, log = math.floor, math.fmod, math.abs, math.sin, math.log local hash32, umod, min = math.hash32, math.umod, math.min local fmt = string.format txt = { own = "Own mempool server", own_h = "e.g. http://umbrel.local:3006 - empty = mempool.space", nodata = "NO DATA", net = "Network", net_h = "only mempool.space", eta = "ETA labels", eta_h = "10M/20M/30M under the blocks", sweep = "Bar highlight", sweep_o = { "none", "slow", "fast" }, loading = "LOADING", nowifi = "NO WIFI", offline = "OFFLINE", console = "SEE CONSOLE" } for k, v in pairs(({ sk = { own = "Vlastný mempool server", own_h = "napr. http://umbrel.local:3006 - prázdne = mempool.space", nodata = "BEZ DAT", net = "Sieť", net_h = "len mempool.space", eta = "Odhad času", eta_h = "10M/20M/30M pod blokmi", sweep = "Odlesk lišty", sweep_o = { "žiadny", "pomalý", "rýchly" }, loading = "NACITAVAM", nowifi = "BEZ WIFI", console = "POZRI KONZOLU" }, cs = { own = "Vlastní mempool server", own_h = "např. http://umbrel.local:3006 - prázdné = mempool.space", nodata = "BEZ DAT", net = "Síť", net_h = "jen mempool.space", eta = "Odhad času", eta_h = "10M/20M/30M pod bloky", sweep = "Odlesk lišty", sweep_o = { "žádný", "pomalý", "rychlý" }, loading = "NACITAM", nowifi = "BEZ WIFI", console = "VIZ KONZOLI" }, })[lang] or {}) do txt[k] = v end settings = { { key = "net", type = "int", default = "main", label = txt.net, hint = txt.net_h, opts = { keys = { "main", "testnet3", "testnet4", "signet" }, "mainnet", "testnet3", "testnet4", "signet" } }, { key = "eta", type = "bool", default = true, label = txt.eta, hint = txt.eta_h }, { key = "sweep", type = "int", default = 1, label = txt.sweep, opts = txt.sweep_o }, { key = "node", type = "server", label = txt.own, hint = txt.own_h }, } local LANE_Y = 6 local BLOCK_Y, BLOCK_W, BLOCK_H, BLOCK_GAP, BLOCK_N = 10, 20, 20, 2, 3 local BLOCK_X = (W - (BLOCK_N * BLOCK_W + (BLOCK_N - 1) * BLOCK_GAP)) // 2 local O_FEE = { font = "7x14b", w = BLOCK_W, align = "center" } local O_FEE_S = { w = BLOCK_W, align = "center", fit = "3x5" } local function mid_y(o) local top, bot = text_ink("0", o) return BLOCK_Y + (BLOCK_H - (bot - top + 1)) // 2 - top end local FEE_BY, FEE_SY = mid_y(O_FEE), mid_y(O_FEE_S) local O_ETA = { font = "3x5", w = BLOCK_W, align = "center", pad = 0 } local ETA_Y = 31 local FEE_Y = 38 local DEPTH_Y, DEPTH_H, DEPTH_SEG, DEPTH_MAX = 51, 6, 6, 8 local DEPTH_X = (W - (DEPTH_MAX * DEPTH_SEG + (DEPTH_MAX - 1) * BLOCK_GAP)) // 2 local TX_Y = 58 local N_TX = 24 local BOLT_W = 3 local function mix(ar, ag, ab, br, bg, bb, t) return floor(ar + (br - ar) * t), floor(ag + (bg - ag) * t), floor(ab + (bb - ab) * t) end local FEE_A = { 1.0, 3.0, 7.0, 15.0, 40.0, 100.0 } local FEE_C = { { 70, 215, 140 }, { 170, 225, 70 }, { 250, 210, 60 }, { 250, 150, 45 }, { 240, 95, 70 }, { 205, 115, 255 } } local function fee_col(f) if f <= FEE_A[1] then local c = FEE_C[1]; return c[1], c[2], c[3] end for i = 2, 6 do if f < FEE_A[i] then local t = log(f / FEE_A[i - 1]) / log(FEE_A[i] / FEE_A[i - 1]) local a, b = FEE_C[i - 1], FEE_C[i] return mix(a[1], a[2], a[3], b[1], b[2], b[3], t) end end local c = FEE_C[6] return c[1], c[2], c[3] end local function draw_bg() rect(0, 0, W, H, 12, 14, 30) for y = BLOCK_Y - 2, BLOCK_Y + BLOCK_H + 1 do local k = 1.0 - abs((y - (BLOCK_Y + BLOCK_H * 0.5)) / (BLOCK_H * 0.75)) if k > 0 then blend_rect(0, y, W, 1, 40, 60, 110, 0.22 * k) end end end local TX_WIN, TX_P_MIN, VS_FULL = 2.5, 0.12, 100 local function rnd(i, k, salt) return umod(hash32(i * 0x9E3779B1 + k * 0x85EBCA6B + salt), 1024) / 1024 end local function day_s() local t = data.time return t.unix % 86400 + t.ms / 1000 end local function draw_incoming(t, vs) local p = TX_P_MIN + (1 - TX_P_MIN) * min(1, log(1 + vs) / log(1 + VS_FULL)) for i = 0, N_TX - 1 do local tt = t + i * TX_WIN / N_TX local k = floor(tt / TX_WIN) if rnd(i, k, 4) < p then local sp = 33 + 40 * rnd(i, k, 1) local go = rnd(i, k, 2) * (TX_WIN - (W + 8) / sp) local x = W + 4 - floor((tt - k * TX_WIN - go) * sp) local y = LANE_Y + floor(rnd(i, k, 3) * 3) if x >= 0 and x < W then local fade = x < 8 and x / 8.0 or 1.0 blend(x, y, 215, 240, 255, fade) blend(x + 1, y, 130, 180, 240, 0.75 * fade) blend(x + 2, y, 130, 180, 240, 0.35 * fade) end end end end local ETA = { "10M", "20M", "30M" } local function draw_blocks(dt, t, nb) local pulse = 0.5 + 0.5 * sin(fmod(t * 5.4, 6.28318)) for i = 0, BLOCK_N - 1 do local x = BLOCK_X + (BLOCK_N - 1 - i) * (BLOCK_W + BLOCK_GAP) local empty = i >= nb local br, bg, bb if empty then br, bg, bb = 26, 29, 40 else br, bg, bb = fee_col(dt.fee[i + 1]) end local r, g, b = mix(br, bg, bb, 0, 0, 0, 0.22) if not empty and i == 0 then r, g, b = mix(r, g, b, 255, 255, 255, 0.15 * pulse) end rect(x, BLOCK_Y, BLOCK_W, BLOCK_H, r, g, b) if not empty then local v = dt.fee[i + 1] local s = fmt(v < 10.0 and "%.1f" or "%.0f", v) if v < 100 then text(x, FEE_BY, s, 255, 255, 255, O_FEE) else text(x, FEE_SY, s, 255, 255, 255, O_FEE_S) end if cfg.eta then text(x, ETA_Y, ETA[i + 1], 110, 125, 150, O_ETA) end end end end local BOLT = { "..#", ".##", ".#.", "##.", "###", ".##", ".#.", "#..", "#.." } local HASH = string.byte("#") local function draw_bolt(x, y, r, g, b) for row = 1, 9 do local s = BOLT[row] for i = 1, BOLT_W do if s:byte(i) == HASH then px(x + i - 1, y + row - 1, r, g, b) end end end end local function draw_fee(dt) local s = fmt("%d", dt.fastest) local r, g, b = fee_col(dt.fastest) local numW = text_width(s, F_BIG) local unitW = text_width("SAT/VB", F_SMALL) local x = (W - (BOLT_W + 3 + numW + 3 + unitW)) // 2 draw_bolt(x, FEE_Y + 1, r, g, b) x = x + BOLT_W + 3 text(x, FEE_Y, s, r, g, b, F_BIG) x = x + numW + 3 local _, ink_bot = text_ink(s, F_BIG) text(x, FEE_Y + ink_bot - 4, "SAT/VB", 170, 190, 215, F_SMALL) end local SWEEP_SPEED = { 0, 3.0, 7.2 } local function draw_depth(dt, t, nb) local sp = SWEEP_SPEED[(cfg.sweep or 1) + 1] or 0 local sweep = fmod(t * sp, DEPTH_MAX + 4.0) for i = 0, DEPTH_MAX - 1 do local x = DEPTH_X + (DEPTH_MAX - 1 - i) * (DEPTH_SEG + BLOCK_GAP) local full = i < nb local r, g, b if full then r, g, b = fee_col(dt.fee[i + 1]) else r, g, b = 26, 29, 40 end rect(x, DEPTH_Y, DEPTH_SEG, DEPTH_H, r, g, b) if full and sp > 0 then local k = 1.0 - abs(sweep - i) if k > 0 then blend_rect(x, DEPTH_Y, DEPTH_SEG, DEPTH_H, 255, 255, 255, 0.35 * k) end end end end local function draw_foot(dt) local kb = dt.vsize * 1000.0 local v = dt.vsize >= 1.0 and fmt("%.1fMB", dt.vsize) or fmt(kb < 10.0 and "%.1fKB" or "%.0fKB", kb) local n = dt.tx >= 1000 and fmt("%.1fK TX", dt.tx / 1000.0) or fmt("%d TX", dt.tx) text_center(TX_Y, v .. " " .. n, 140, 160, 190, F_SMALL) end local NET_PATH = { main = "", testnet3 = "/testnet", testnet4 = "/testnet4", signet = "/signet" } sources = { blocks = { url = "https://mempool.space", limit = 60, own = "node", fields = { "[].medianFee", "[].nTx", "[].blockVSize" }, demo = [=[[{"medianFee":8.4,"nTx":3072,"blockVSize":966666.67},{"medianFee":6.2,"nTx":3068,"blockVSize":966666.67},{"medianFee":4.1,"nTx":3068,"blockVSize":966666.67}, {"medianFee":3.0,"nTx":3068,"blockVSize":966666.67},{"medianFee":2.4,"nTx":3068,"blockVSize":966666.67},{"medianFee":1.8,"nTx":3068,"blockVSize":966666.67}]]=] }, fees = { url = "https://mempool.space", limit = 60, own = "node", fields = { "fastestFee" }, demo = [[{"fastestFee":9,"halfHourFee":6}]] }, } local test_net function on_settings() local n = cfg.node == "" and cfg.net or "main" test_net = n ~= "main" and string.upper(n) or nil local p = NET_PATH[n] net.fetch("blocks", p .. "/api/v1/fees/mempool-blocks", 120) net.fetch("fees", p .. "/api/v1/fees/recommended", 120) end local dt = { fee = {} } local function derive() local b = web.blocks if not b then dt.blocks = nil; return end local n, tx, vs = 0, 0, 0.0 for i = 1, 8 do local e = b[i] if not e then break end n = n + 1 dt.fee[i] = e.medianFee or 0 tx = tx + (e.nTx or 0) vs = vs + (e.blockVSize or 0) end dt.blocks = n dt.tx = tx dt.vsize = vs / 1000000.0 dt.fastest = web.fees and web.fees.fastestFee end local F_GATE = { font = "4x6" } local function gate(s, y, r, g, b) if s.ok then return true end r, g, b = r or 255, g or 250, b or 232 text_center(y, data.net.mode ~= "online" and txt.offline or not data.net.up and txt.nowifi or s.code ~= 0 and "ERR " .. s.code or txt.loading, r, g, b) if s.code ~= 0 then text_center(y + 10, txt.console, r, g, b, F_GATE) end return false end function render() derive() local t = frame / FPS draw_bg() if test_net then text_center(0, test_net, 235, 175, 80, F_SMALL) else text_center(0, "MEMPOOL", 150, 170, 200, F_SMALL) end if not gate(data.src.blocks, 30) then return end if not dt.blocks then text_center(28, txt.nodata, 120, 120, 130, F_SMALL) return end local nb = dt.blocks draw_incoming(day_s(), dt.vsize) draw_blocks(dt, t, nb) if dt.fastest then draw_fee(dt) end draw_depth(dt, t, nb) draw_foot(dt) end