info = { type = "app", title = "Halving", author = "BRRRO", license = "MIT", desc = "Countdown to the halving", category = "btc", api = 43, } txt = { own = "Own mempool server", own_h = "e.g. http://umbrel.local:3006 - empty = mempool.space", nodata = "NO DATA", to = "UNTIL HALVING", reward = "NEW REWARD", day = { "DAY", "DAYS", "DAYS" }, 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 DÁT", to = "DO HALVINGU", reward = "NOVÁ ODMENA", day = { "DEŇ", "DNI", "DNÍ" }, 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", to = "DO HALVINGU", reward = "NOVÁ ODMĚNA", day = { "DEN", "DNY", "DNÍ" }, loading = "NACITAM", nowifi = "BEZ WIFI", console = "VIZ KONZOLI" }, })[lang] or {}) do txt[k] = v end settings = { { key = "node", type = "server", label = txt.own, hint = txt.own_h }, } sources = { height = { url = "https://mempool.space", limit = 60, own = "node", as = "text", demo = "912345" }, } function on_settings() net.fetch("height", "/api/blocks/tip/height", 300) end permissions = { "images" } local W, H = gfx.W, gfx.H local px, rect, blend, blit_rgb, blend_mask = gfx.px, gfx.rect, gfx.blend, gfx.blit_rgb, gfx.blend_mask local text, text_center, text_width, text_ink = gfx.text, gfx.text_center, gfx.text_width, gfx.text_ink local floor, sin, cos, sqrt, atan, pi = math.floor, math.sin, math.cos, math.sqrt, math.atan, math.pi local hash32, umod = math.hash32, math.umod local fmt = string.format local EPOCH = 210000 local GOLD = { 255, 196, 64 } local WHT = { 255, 250, 232 } local F_BIG, F_MID = { font = "9x15b" }, { font = "6x10", cell = true, shadow = { 40, 18, 10 } } local F_LBL = { font = "6x10", shadow = { 40, 18, 10 } } local F_SUB = { font = "4x6", shadow = { 40, 18, 10 } } local F_BIGS = { font = "8x13b", shadow = { 40, 18, 10 } } local DAYS_Y = 2 local CX, CY = 31.5, 29 local COIN_X = 22 local COIN_R = 10 local RING_R0, RING_R1 = 12.5, 14.5 local TIME_Y = 48 local SUB_Y = 58 local function mix(a, b, t) return floor(a[1] + (b[1] - a[1]) * t), floor(a[2] + (b[2] - a[2]) * t), floor(a[3] + (b[3] - a[3]) * t) end local BG, COIN local BTC = { "00101000", "11111100", "01100110", "01100110", "01111100", "01100011", "01100011", "11111110", "00101000" } local PLANET_CY, PLANET_R = 150, 104.3 local function rim_y(x) local dx = x + 0.5 - (CX + 0.5) return PLANET_CY - sqrt(PLANET_R * PLANET_R - dx * dx) end local function build_space() rect(0, 0, W, H, 6, 6, 22) for i = 0, 39 do local h = hash32(i * 0x9E3779B1 + 5) local x, y = umod(h, W), umod(h >> 8, 46) local v = 60 + umod(h >> 16, 90) if y < rim_y(x) - 2 then px(x, y, v, v, floor(v * 1.1)) end end for x = 0, W - 1 do local top = floor(rim_y(x)) local warm = math.max(0, 1 - math.abs(x - CX) / 22) for k = 1, 4 do blend(x, top - k, floor(60 + 195 * warm), floor(140 - 10 * warm), floor(230 - 170 * warm), 0.5 * (1 - k / 5)) end px(x, top, floor(120 + 135 * warm), floor(200 - 20 * warm), floor(255 - 150 * warm)) rect(x, top + 1, 1, 2, 30, 64, 120) rect(x, top + 3, 1, H - top - 3, 12, 24, 54) end end local function build_bg() build_space() BG = gfx.snapshot(0, 0, W, H) rect(COIN_X, CY - COIN_R, 2 * COIN_R, 2 * COIN_R + 1, 0, 0, 0) for dy = -COIN_R, COIN_R do for x = COIN_X, COIN_X + 2 * COIN_R - 1 do local dx = x - CX local d = sqrt(dx * dx + dy * dy) if d <= COIN_R + 0.3 then px(x, CY + dy, mix({ 255, 214, 110 }, { 240, 120, 30 }, d / COIN_R)) end end end for r, row in ipairs(BTC) do for c = 1, 8 do if row:sub(c, c) == "1" then px(COIN_X + COIN_R - 4 + c - 1, CY - 5 + r, 120, 52, 12) end end end COIN = gfx.snapshot(COIN_X, CY - COIN_R, 2 * COIN_R, 2 * COIN_R + 1) end local RING = {} for dy = -15, 15 do for x = 16, 47 do local dx = x - CX local d = sqrt(dx * dx + dy * dy) if d >= RING_R0 and d < RING_R1 then local a = atan(dx, -dy) / (2 * pi) if a < 0 then a = a + 1 end RING[#RING + 1] = { x, CY + dy, a } end end end table.sort(RING, function(p, q) return p[3] < q[3] end) local CRACK = {} do local zig = { 31, 32, 33, 32, 31, 30, 31, 32, 31, 30, 31, 32, 33, 32, 31, 32, 31, 32, 31, 30, 31 } for k = 0, 2 * COIN_R do CRACK[k + 1] = { zig[k + 1], CY - COIN_R + k } end end local PX0, PW = 18, 28 local CMASK, HMASK do local c, h = {}, {} for y = 0, PW - 1 do for x = 0, PW - 1 do local dx, dy = PX0 + x - CX, y - 14 local d = sqrt(dx * dx + dy * dy) c[y * PW + x + 1] = d <= COIN_R + 0.3 and 255 or 0 h[y * PW + x + 1] = (d > COIN_R + 0.3 and d < COIN_R + 3.3) and floor(255 * (1 - (d - COIN_R) / 3.3)) or 0 end end CMASK, HMASK = string.char(table.unpack(c)), string.char(table.unpack(h)) end local SPARK_PX, SPARK_EVERY, PULSE_S = 12, 4, 0.6 local function draw_coin(frac, glow, split) if split then gfx.clip(0, 0, 29, H); blit_rgb(COIN, COIN_X - 3, CY - COIN_R, 2 * COIN_R, 2 * COIN_R + 1, 0, 0, 0) gfx.clip(35, 0, W, H); blit_rgb(COIN, COIN_X + 3, CY - COIN_R, 2 * COIN_R, 2 * COIN_R + 1, 0, 0, 0) gfx.clip() return end blit_rgb(COIN, COIN_X, CY - COIN_R, 2 * COIN_R, 2 * COIN_R + 1, 0, 0, 0) local n = floor(frac * #CRACK + 0.5) for i = 1, n do px(CRACK[i][1], CRACK[i][2], 70, 26, 6) end if n < 2 then return end local tt, run = math.fmod(frame / gfx.FPS, SPARK_EVERY), n / SPARK_PX local head = floor(tt * SPARK_PX) + 1 for j = 0, 3 do local i = head - j if i >= 1 and i <= n then blend(CRACK[i][1], CRACK[i][2], 255, 244, 200, j == 0 and 1 or 0.7 - j * 0.2) end end local d = tt - run if d >= 0 and d < PULSE_S then local env = 1 - d / PULSE_S blend_mask(HMASK, 255, 170, 50, PX0, CY - 14, PW, PW, PW, PW, env * 0.9) blend_mask(CMASK, 255, 240, 190, PX0, CY - 14, PW, PW, PW, PW, env * 0.25) local e = CRACK[n] blend(e[1], e[2], 255, 250, 220, env) end if glow > 0 and n > 0 then local p = CRACK[n] blend(p[1], p[2], 255, 250, 220, glow) blend(p[1] - 1, p[2], 255, 230, 160, glow * 0.6); blend(p[1] + 1, p[2], 255, 230, 160, glow * 0.6) end end local function draw_ring(frac, glow) local n = floor(frac * #RING + 0.5) for i = 1, #RING do local p = RING[i] if i <= n then px(p[1], p[2], GOLD[1], GOLD[2], GOLD[3]) else blend(p[1], p[2], 30, 10, 20, 0.55) end end if glow > 0 then for i = n - 6, n do local p = RING[i] if p then blend(p[1], p[2], 255, 250, 220, glow) end end end end local function win_at(len) local t = data.time return t.unix // len, t.unix % len + t.ms / 1000 end local function draw_stars() local t = data.time for i = 0, 9 do local wn, s = (t.unix + i * 3) // 9, (t.unix + i * 3) % 9 + t.ms / 1000 local h = hash32(wn * 0x85EBCA6B + i * 7919) local x, y = umod(h, W), umod(h >> 8, 44) local dx, dy = x - CX, y - CY local a = sin(s / 9 * pi) if a > 0.15 and dx * dx + dy * dy > 16 * 16 then blend(x, y, 255, 236, 200, a * 0.85) end end end local function draw_comet() local w, s = win_at(80) local h = hash32(w * 0x27D4EB2F + 13) if umod(h, 2) == 1 then return end local t0 = umod(h >> 4, 50) local e = s - t0 if e < 0 or e > 9 then return end local dir = umod(h >> 10, 2) == 0 and 1 or -1 local y0 = 14 + umod(h >> 12, 6) local x = dir > 0 and (-10 + e * 9) or (W + 10 - e * 9) local y = y0 + e * 1.2 for k = 0, 9 do local tx, ty = floor(x - dir * k * 0.9 + 0.5), floor(y - k * 0.12 + 0.5) if tx >= 0 and tx < W then blend(tx, ty, 170, 220, 255, 0.9 * (1 - k / 10)) end end local hx, hy = floor(x + 0.5), floor(y + 0.5) if hx >= 0 and hx < W then px(hx, hy, 240, 250, 255) end end local last, seen, ev_at = nil, nil, -99 local data_at, prev_now = nil, -1 local function height() local h = web.height and tonumber(web.height) if h and h > 0 then last = floor(h) end return last end local DARK = { 40, 18, 10 } local FONTS = { F_MID, F_LBL, F_SUB, F_BIGS, F_BIG } local was_img = nil local function text_back(img) if img == was_img then return end was_img = img for _, o in ipairs(FONTS) do o.shadow, o.outline = (not img) and DARK or nil, img and DARK or nil end F_BIG.shadow = nil end local FW_N, FW_T, FW_UP, FW_BURST = 4, 2.0, 0.6, 1.2 local FW_V, FW_G, FW_RAYS = 11, 8, 16 local FW_PAL = { { 255, 80, 80 }, { 140, 255, 150 }, { 150, 235, 255 }, { 255, 214, 80 }, { 255, 120, 240 }, { 247, 147, 26 } } local FW_DIR = {} for k = 0, FW_RAYS - 1 do FW_DIR[k + 1] = { cos(k * 2 * pi / FW_RAYS), sin(k * 2 * pi / FW_RAYS) } end local FW_TOP, FW_BOT = 15, TIME_Y - 2 local function draw_fireworks(t) gfx.clip(0, FW_TOP, W, FW_BOT - FW_TOP) for i = 0, FW_N - 1 do local tt = t + i * FW_T / FW_N local c, tau = floor(tt / FW_T), math.fmod(tt, FW_T) local key = c * 7 + i * 131 local x0, yb = 6 + umod(hash32(key + 1), W - 12), FW_TOP + 5 + umod(hash32(key + 2), 10) local col = FW_PAL[umod(hash32(key + 3), #FW_PAL) + 1] if tau < FW_UP then local q = 1 - tau / FW_UP local y = floor(yb + (FW_BOT - yb) * q * q) blend(x0, y, 255, 240, 200, 1) blend(x0, y + 1, 255, 180, 90, 0.5) elseif tau < FW_UP + FW_BURST then local s = tau - FW_UP local a = math.min(1, 2 * (1 - s / FW_BURST)) for _, d in ipairs(FW_DIR) do for j = 0, 2 do local sj = s - j * 0.07 if sj > 0 then blend(floor(x0 + d[1] * FW_V * sj + 0.5), floor(yb + d[2] * FW_V * sj + FW_G * sj * sj + 0.5), col[1], col[2], col[3], a * (1 - j * 0.35)) end end end if s < 0.12 then blend(x0, yb, 255, 255, 255, 1) end end end gfx.clip() end local function plural(n) if n == 1 then return txt.day[1] end if n >= 2 and n <= 4 then return txt.day[2] end return txt.day[3] end local function reward(epoch) local s = fmt("%.8f", 50 / (1 << epoch)):gsub("0+$", ""):gsub("%.$", "") if lang == "sk" or lang == "cs" then s = s:gsub("%.", ",") end return s .. " BTC" 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() local img = gfx.bg() if not img then if not BG then build_bg() end blit_rgb(BG, 0, 0, W, H) end text_back(img) local t = data.time if t and t.ok and not img then draw_stars(); draw_comet() end local src = data.src.height if not src.ok then draw_coin(0, 0); gate(src, TIME_Y); return end local h = height() if not h then draw_coin(0, 0); text_center(TIME_Y, txt.nodata, 200, 190, 180, F_SUB) return end local now = frame / gfx.FPS if now < prev_now then seen, ev_at, data_at = nil, -99, nil end prev_now = now if seen and h > seen then ev_at = now end seen = h data_at = data_at or now local glow = now - ev_at < 1.5 and (1 - (now - ev_at) / 1.5) or 0 local epoch, inside = h // EPOCH, h % EPOCH local blocks_left = EPOCH - inside local frac = inside / EPOCH local k = data.arrive and (now - data_at) / 1.4 or 1 if k < 1 then frac = frac * (1 - (1 - k) ^ 3) end if epoch > 0 and inside < 144 then draw_fireworks(now) draw_ring(1, 0) draw_coin(1, 0, true) text_center(DAYS_Y + 1, "HALVING", GOLD[1], GOLD[2], GOLD[3], F_BIGS) text_center(TIME_Y, reward(epoch), WHT[1], WHT[2], WHT[3], F_LBL) text_center(SUB_Y, txt.reward, GOLD[1], GOLD[2], GOLD[3], F_SUB) return end draw_ring(frac, glow) draw_coin(frac, glow) local seconds = blocks_left * 600 - floor(src.age) if seconds < 0 then seconds = 0 end local days, rem = seconds // 86400, seconds % 86400 local hh = rem // 3600; rem = rem % 3600 local mm, ss = rem // 60, rem % 60 local ds, lbl = fmt("%d", days), plural(days) local dw, lw = text_width(ds, F_BIG), text_width(lbl, F_LBL) local x = (W - (dw + 3 + lw)) // 2 text(x, DAYS_Y, ds, GOLD[1], GOLD[2], GOLD[3], F_BIG) local _, bot = text_ink(ds, F_BIG) local _, lbot = text_ink(lbl, F_LBL) text(x + dw + 3, DAYS_Y + bot - lbot, lbl, WHT[1], WHT[2], WHT[3], F_LBL) local cd = fmt("%02d:%02d:%02d", hh, mm, ss) text((W - text_width(cd, F_MID)) // 2, TIME_Y, cd, WHT[1], WHT[2], WHT[3], F_MID) text_center(SUB_Y, txt.to, GOLD[1], GOLD[2], GOLD[3], F_SUB) end