Compare commits
14 Commits
Author | SHA1 | Date | |
---|---|---|---|
7222c4e26b | |||
ba4e803eea | |||
ac1a4bc16c | |||
52508b7d4e | |||
4a40899653 | |||
80855a15a9 | |||
19de412fc5 | |||
1c6d5e9917 | |||
8b8307cc57 | |||
36b6504a38 | |||
60a4574c5a | |||
bf50ba4539 | |||
15a7052389 | |||
83fd89446d |
12
.vscode/launch.json
vendored
12
.vscode/launch.json
vendored
@@ -5,14 +5,16 @@
|
|||||||
"version": "0.2.0",
|
"version": "0.2.0",
|
||||||
"configurations": [
|
"configurations": [
|
||||||
{
|
{
|
||||||
"name": "Launch file",
|
"name": "Launch",
|
||||||
"type": "go",
|
"type": "go",
|
||||||
"request": "launch",
|
"request": "launch",
|
||||||
"mode": "debug",
|
"mode": "debug",
|
||||||
"console": "integratedTerminal",
|
"console": "integratedTerminal",
|
||||||
"program": "${file}"
|
"program": "${workspaceFolder}/moonmath.go",
|
||||||
}
|
"args": [
|
||||||
|
"--config-file",
|
||||||
|
"moonmath.toml"
|
||||||
|
]
|
||||||
|
}
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
3
.vscode/settings.json
vendored
3
.vscode/settings.json
vendored
@@ -1,5 +1,6 @@
|
|||||||
{
|
{
|
||||||
"cSpell.words": [
|
"cSpell.words": [
|
||||||
|
"alecthomas",
|
||||||
"bitcoinity",
|
"bitcoinity",
|
||||||
"Bitfinex",
|
"Bitfinex",
|
||||||
"Bitstamp",
|
"Bitstamp",
|
||||||
@@ -7,6 +8,8 @@
|
|||||||
"CDPR",
|
"CDPR",
|
||||||
"charmbracelet",
|
"charmbracelet",
|
||||||
"Coindesk",
|
"Coindesk",
|
||||||
|
"knadh",
|
||||||
|
"koanf",
|
||||||
"lipgloss",
|
"lipgloss",
|
||||||
"moonmath",
|
"moonmath",
|
||||||
"OHLC",
|
"OHLC",
|
||||||
|
14
README.md
14
README.md
@@ -1,6 +1,6 @@
|
|||||||
# moonmath
|
# moonmath
|
||||||
|
|
||||||
## Bullshit BTC Price Projections, Now in Your CLI!
|
## Bullshit Crypto Price Projections, Now in Your CLI!
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
@@ -12,6 +12,15 @@ program. It's written in Go using the [Bubble Tea][tea] library, and uses
|
|||||||
[tea]: https://github.com/charmbracelet/bubbletea
|
[tea]: https://github.com/charmbracelet/bubbletea
|
||||||
[coin]: https://www.coindesk.com
|
[coin]: https://www.coindesk.com
|
||||||
|
|
||||||
|
### Configuration
|
||||||
|
|
||||||
|
By default, the program will use Bitcoin along with various goals and bases
|
||||||
|
of comparison. With the `--asset` flag, another asset supported by Coindesk can
|
||||||
|
be chosen. The [builtin default config](./config/default.yaml) only has special
|
||||||
|
goals for Ethereum ("ETH"). With the `--config-file` flag, however, one can
|
||||||
|
specify a YAML file that overrides these defaults and adds goals for other
|
||||||
|
assets.
|
||||||
|
|
||||||
### "Theory"
|
### "Theory"
|
||||||
|
|
||||||
Given a pair of quotes taken at the start and end of some period,
|
Given a pair of quotes taken at the start and end of some period,
|
||||||
@@ -39,8 +48,7 @@ $$ x = {{log(p_f) - log(p_e)} \over log(r)} $$
|
|||||||
|
|
||||||
### Future Improvements
|
### Future Improvements
|
||||||
|
|
||||||
* Support other assets available from Coindesk.
|
* Add more default configurations for various assets.
|
||||||
* Configurable projection milestones.
|
|
||||||
* Allow projection by date, e.g. use the CDPR to calculate what the price
|
* Allow projection by date, e.g. use the CDPR to calculate what the price
|
||||||
would be on a particular date.
|
would be on a particular date.
|
||||||
* Log errors to a file.
|
* Log errors to a file.
|
||||||
|
@@ -5,7 +5,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"code.humancabbage.net/moonmath/bitcoinity"
|
"code.humancabbage.net/sam/moonmath/bitcoinity"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestUnmarshalGetTickerResponse(t *testing.T) {
|
func TestUnmarshalGetTickerResponse(t *testing.T) {
|
||||||
@@ -21,3 +21,24 @@ func TestUnmarshalGetTickerResponse(t *testing.T) {
|
|||||||
|
|
||||||
//go:embed get_ticker.json
|
//go:embed get_ticker.json
|
||||||
var getTickerJson []byte
|
var getTickerJson []byte
|
||||||
|
|
||||||
|
func TestUnmarshalWebhookMessages(t *testing.T) {
|
||||||
|
var msgs []bitcoinity.Message
|
||||||
|
err := json.Unmarshal(capturedJson, &msgs)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal webhook messages JSON: %v", err)
|
||||||
|
}
|
||||||
|
for _, msg := range msgs {
|
||||||
|
if msg.Event != "new_msg" {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
var payload bitcoinity.MarketPayload
|
||||||
|
err := json.Unmarshal(msg.Payload, &payload)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal market payload JSON: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed captured.json
|
||||||
|
var capturedJson []byte
|
||||||
|
1
coindesk/asset_tickers.json
Normal file
1
coindesk/asset_tickers.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"statusCode":200,"message":"OK","data":{"BTC":{"iso":"BTC","name":"Bitcoin","slug":"bitcoin","change":{"percent":-6.3,"value":-4187.634882541},"ohlc":{"o":66436.26558132548,"h":66436.26558132548,"l":61526.78744328402,"c":62248.630698784444},"circulatingSupply":19658987,"marketCap":1223745021675.2043,"ts":1710898259000,"src":"tb"}}}
|
326
coindesk/assets.go
Normal file
326
coindesk/assets.go
Normal file
@@ -0,0 +1,326 @@
|
|||||||
|
package coindesk
|
||||||
|
|
||||||
|
const (
|
||||||
|
BTC Asset = "BTC"
|
||||||
|
ETH Asset = "ETH"
|
||||||
|
XRP Asset = "XRP"
|
||||||
|
BCH Asset = "BCH"
|
||||||
|
EOS Asset = "EOS"
|
||||||
|
XLM Asset = "XLM"
|
||||||
|
LTC Asset = "LTC"
|
||||||
|
ADA Asset = "ADA"
|
||||||
|
XMR Asset = "XMR"
|
||||||
|
DASH Asset = "DASH"
|
||||||
|
IOTA Asset = "IOTA"
|
||||||
|
TRX Asset = "TRX"
|
||||||
|
NEO Asset = "NEO"
|
||||||
|
ETC Asset = "ETC"
|
||||||
|
XEM Asset = "XEM"
|
||||||
|
ZEC Asset = "ZEC"
|
||||||
|
BTG Asset = "BTG"
|
||||||
|
LSK Asset = "LSK"
|
||||||
|
QTUM Asset = "QTUM"
|
||||||
|
BSV Asset = "BSV"
|
||||||
|
DOGE Asset = "DOGE"
|
||||||
|
DCR Asset = "DCR"
|
||||||
|
USDT Asset = "USDT"
|
||||||
|
USDC Asset = "USDC"
|
||||||
|
LINK Asset = "LINK"
|
||||||
|
XTZ Asset = "XTZ"
|
||||||
|
ZRX Asset = "ZRX"
|
||||||
|
DAI Asset = "DAI"
|
||||||
|
BAT Asset = "BAT"
|
||||||
|
OXT Asset = "OXT"
|
||||||
|
ALGO Asset = "ALGO"
|
||||||
|
ATOM Asset = "ATOM"
|
||||||
|
KNC Asset = "KNC"
|
||||||
|
OMG Asset = "OMG"
|
||||||
|
ANT Asset = "ANT"
|
||||||
|
REP Asset = "REP"
|
||||||
|
BAND Asset = "BAND"
|
||||||
|
BTT Asset = "BTT"
|
||||||
|
MANA Asset = "MANA"
|
||||||
|
FET Asset = "FET"
|
||||||
|
ICX Asset = "ICX"
|
||||||
|
KAVA Asset = "KAVA"
|
||||||
|
LRC Asset = "LRC"
|
||||||
|
MKR Asset = "MKR"
|
||||||
|
MLN Asset = "MLN"
|
||||||
|
NANO Asset = "NANO"
|
||||||
|
NMR Asset = "NMR"
|
||||||
|
PAXG Asset = "PAXG"
|
||||||
|
USDP Asset = "USDP"
|
||||||
|
SC Asset = "SC"
|
||||||
|
STORJ Asset = "STORJ"
|
||||||
|
WAVES Asset = "WAVES"
|
||||||
|
FIL Asset = "FIL"
|
||||||
|
CVC Asset = "CVC"
|
||||||
|
DNT Asset = "DNT"
|
||||||
|
REN Asset = "REN"
|
||||||
|
BNT Asset = "BNT"
|
||||||
|
WBTC Asset = "WBTC"
|
||||||
|
GRT Asset = "GRT"
|
||||||
|
UNI Asset = "UNI"
|
||||||
|
DOT Asset = "DOT"
|
||||||
|
YFI Asset = "YFI"
|
||||||
|
AAVE Asset = "AAVE"
|
||||||
|
MATIC Asset = "MATIC"
|
||||||
|
AMP Asset = "AMP"
|
||||||
|
CELO Asset = "CELO"
|
||||||
|
COMP Asset = "COMP"
|
||||||
|
CRV Asset = "CRV"
|
||||||
|
RLC Asset = "RLC"
|
||||||
|
KSM Asset = "KSM"
|
||||||
|
NKN Asset = "NKN"
|
||||||
|
SHIB Asset = "SHIB"
|
||||||
|
SKL Asset = "SKL"
|
||||||
|
SNX Asset = "SNX"
|
||||||
|
LUNC Asset = "LUNC"
|
||||||
|
UMA Asset = "UMA"
|
||||||
|
ICP Asset = "ICP"
|
||||||
|
SOL Asset = "SOL"
|
||||||
|
AVAX Asset = "AVAX"
|
||||||
|
UST Asset = "UST"
|
||||||
|
ENJ Asset = "ENJ"
|
||||||
|
IOTX Asset = "IOTX"
|
||||||
|
AXS Asset = "AXS"
|
||||||
|
XYO Asset = "XYO"
|
||||||
|
SUSHI Asset = "SUSHI"
|
||||||
|
ANKR Asset = "ANKR"
|
||||||
|
CHZ Asset = "CHZ"
|
||||||
|
LPT Asset = "LPT"
|
||||||
|
COTI Asset = "COTI"
|
||||||
|
KEEP Asset = "KEEP"
|
||||||
|
SAND Asset = "SAND"
|
||||||
|
GALA Asset = "GALA"
|
||||||
|
APE Asset = "APE"
|
||||||
|
CRO Asset = "CRO"
|
||||||
|
ACHP Asset = "ACHP"
|
||||||
|
JASMY Asset = "JASMY"
|
||||||
|
REQ Asset = "REQ"
|
||||||
|
SLP Asset = "SLP"
|
||||||
|
NEAR Asset = "NEAR"
|
||||||
|
MBOX Asset = "MBOX"
|
||||||
|
POLIS Asset = "POLIS"
|
||||||
|
MOVR Asset = "MOVR"
|
||||||
|
POLS Asset = "POLS"
|
||||||
|
QUICK Asset = "QUICK"
|
||||||
|
MINA Asset = "MINA"
|
||||||
|
IMX Asset = "IMX"
|
||||||
|
XEC Asset = "XEC"
|
||||||
|
NEXO Asset = "NEXO"
|
||||||
|
RUNE Asset = "RUNE"
|
||||||
|
QNT Asset = "QNT"
|
||||||
|
VET Asset = "VET"
|
||||||
|
CAKE Asset = "CAKE"
|
||||||
|
BNB Asset = "BNB"
|
||||||
|
THETA Asset = "THETA"
|
||||||
|
HBAR Asset = "HBAR"
|
||||||
|
FTM Asset = "FTM"
|
||||||
|
RVN Asset = "RVN"
|
||||||
|
ZIL Asset = "ZIL"
|
||||||
|
DGB Asset = "DGB"
|
||||||
|
FTT Asset = "FTT"
|
||||||
|
ENS Asset = "ENS"
|
||||||
|
WRX Asset = "WRX"
|
||||||
|
WAXP Asset = "WAXP"
|
||||||
|
EGLD Asset = "EGLD"
|
||||||
|
BUSD Asset = "BUSD"
|
||||||
|
CEL Asset = "CEL"
|
||||||
|
OP Asset = "OP"
|
||||||
|
LUNA Asset = "LUNA"
|
||||||
|
RAY Asset = "RAY"
|
||||||
|
FLOW Asset = "FLOW"
|
||||||
|
AUDIO Asset = "AUDIO"
|
||||||
|
CKB Asset = "CKB"
|
||||||
|
VGX Asset = "VGX"
|
||||||
|
YGG Asset = "YGG"
|
||||||
|
CHR Asset = "CHR"
|
||||||
|
STMX Asset = "STMX"
|
||||||
|
SXP Asset = "SXP"
|
||||||
|
INJ Asset = "INJ"
|
||||||
|
JOE Asset = "JOE"
|
||||||
|
POLY Asset = "POLY"
|
||||||
|
STX Asset = "STX"
|
||||||
|
SFP Asset = "SFP"
|
||||||
|
FARM Asset = "FARM"
|
||||||
|
XVG Asset = "XVG"
|
||||||
|
CLV Asset = "CLV"
|
||||||
|
WOO Asset = "WOO"
|
||||||
|
GLMR Asset = "GLMR"
|
||||||
|
STEEM Asset = "STEEM"
|
||||||
|
RARE Asset = "RARE"
|
||||||
|
IDEX Asset = "IDEX"
|
||||||
|
SRM Asset = "SRM"
|
||||||
|
PYR Asset = "PYR"
|
||||||
|
MIR Asset = "MIR"
|
||||||
|
SYS Asset = "SYS"
|
||||||
|
ALPACA Asset = "ALPACA"
|
||||||
|
QSP Asset = "QSP"
|
||||||
|
SCRT Asset = "SCRT"
|
||||||
|
SUN Asset = "SUN"
|
||||||
|
APT Asset = "APT"
|
||||||
|
MASK Asset = "MASK"
|
||||||
|
DYDX Asset = "DYDX"
|
||||||
|
CVX Asset = "CVX"
|
||||||
|
GMT Asset = "GMT"
|
||||||
|
CTSI Asset = "CTSI"
|
||||||
|
METIS Asset = "METIS"
|
||||||
|
FORTH Asset = "FORTH"
|
||||||
|
RBN Asset = "RBN"
|
||||||
|
SAMO Asset = "SAMO"
|
||||||
|
SPELL Asset = "SPELL"
|
||||||
|
LDO Asset = "LDO"
|
||||||
|
ARB Asset = "ARB"
|
||||||
|
BLUR Asset = "BLUR"
|
||||||
|
GAS Asset = "GAS"
|
||||||
|
RACA Asset = "RACA"
|
||||||
|
BABYDOGE Asset = "BABYDOGE"
|
||||||
|
FLOKI Asset = "FLOKI"
|
||||||
|
HOT Asset = "HOT"
|
||||||
|
BFC Asset = "BFC"
|
||||||
|
KISHU Asset = "KISHU"
|
||||||
|
ELON Asset = "ELON"
|
||||||
|
SAITAMA Asset = "SAITAMA"
|
||||||
|
REEF Asset = "REEF"
|
||||||
|
CEEK Asset = "CEEK"
|
||||||
|
ATLAS Asset = "ATLAS"
|
||||||
|
LOOKS Asset = "LOOKS"
|
||||||
|
WIN Asset = "WIN"
|
||||||
|
ONE Asset = "ONE"
|
||||||
|
DENT Asset = "DENT"
|
||||||
|
GST Asset = "GST"
|
||||||
|
TWT Asset = "TWT"
|
||||||
|
HNT Asset = "HNT"
|
||||||
|
AGLD Asset = "AGLD"
|
||||||
|
BTRST Asset = "BTRST"
|
||||||
|
ETHW Asset = "ETHW"
|
||||||
|
ILV Asset = "ILV"
|
||||||
|
RARI Asset = "RARI"
|
||||||
|
STG Asset = "STG"
|
||||||
|
SYN Asset = "SYN"
|
||||||
|
TOKE Asset = "TOKE"
|
||||||
|
BLZ Asset = "BLZ"
|
||||||
|
FLR Asset = "FLR"
|
||||||
|
FIS Asset = "FIS"
|
||||||
|
GNS Asset = "GNS"
|
||||||
|
ID Asset = "ID"
|
||||||
|
PEPE Asset = "PEPE"
|
||||||
|
DIA Asset = "DIA"
|
||||||
|
TLM Asset = "TLM"
|
||||||
|
XCN Asset = "XCN"
|
||||||
|
BIT Asset = "BIT"
|
||||||
|
RPL Asset = "RPL"
|
||||||
|
RNDR Asset = "RNDR"
|
||||||
|
ONEINCH Asset = "1INCH"
|
||||||
|
BAL Asset = "BAL"
|
||||||
|
T Asset = "T"
|
||||||
|
GNO Asset = "GNO"
|
||||||
|
ASTR Asset = "ASTR"
|
||||||
|
GLM Asset = "GLM"
|
||||||
|
OCEAN Asset = "OCEAN"
|
||||||
|
BICO Asset = "BICO"
|
||||||
|
CELR Asset = "CELR"
|
||||||
|
LQTY Asset = "LQTY"
|
||||||
|
TRAC Asset = "TRAC"
|
||||||
|
ZEN Asset = "ZEN"
|
||||||
|
API3 Asset = "API3"
|
||||||
|
PLA Asset = "PLA"
|
||||||
|
AXL Asset = "AXL"
|
||||||
|
HFT Asset = "HFT"
|
||||||
|
MC Asset = "MC"
|
||||||
|
C98 Asset = "C98"
|
||||||
|
GAL Asset = "GAL"
|
||||||
|
GTC Asset = "GTC"
|
||||||
|
RAD Asset = "RAD"
|
||||||
|
POWR Asset = "POWR"
|
||||||
|
POND Asset = "POND"
|
||||||
|
ALICE Asset = "ALICE"
|
||||||
|
TRU Asset = "TRU"
|
||||||
|
OGN Asset = "OGN"
|
||||||
|
DAR Asset = "DAR"
|
||||||
|
BADGER Asset = "BADGER"
|
||||||
|
GHST Asset = "GHST"
|
||||||
|
LCX Asset = "LCX"
|
||||||
|
ARPA Asset = "ARPA"
|
||||||
|
MXC Asset = "MXC"
|
||||||
|
PERP Asset = "PERP"
|
||||||
|
LOKA Asset = "LOKA"
|
||||||
|
BOBA Asset = "BOBA"
|
||||||
|
BOND Asset = "BOND"
|
||||||
|
ALCX Asset = "ALCX"
|
||||||
|
KP3R Asset = "KP3R"
|
||||||
|
TON Asset = "TON"
|
||||||
|
AR Asset = "AR"
|
||||||
|
AVA Asset = "AVA"
|
||||||
|
BONE Asset = "BONE"
|
||||||
|
BONK Asset = "BONK"
|
||||||
|
CORE Asset = "CORE"
|
||||||
|
CSPR Asset = "CSPR"
|
||||||
|
DG Asset = "DG"
|
||||||
|
ERN Asset = "ERN"
|
||||||
|
FXS Asset = "FXS"
|
||||||
|
GMX Asset = "GMX"
|
||||||
|
GT Asset = "GT"
|
||||||
|
GUSD Asset = "GUSD"
|
||||||
|
HMT Asset = "HMT"
|
||||||
|
HT Asset = "HT"
|
||||||
|
KCS Asset = "KCS"
|
||||||
|
KLAY Asset = "KLAY"
|
||||||
|
LEO Asset = "LEO"
|
||||||
|
MPL Asset = "MPL"
|
||||||
|
OKB Asset = "OKB"
|
||||||
|
PIT Asset = "PIT"
|
||||||
|
OSMO Asset = "OSMO"
|
||||||
|
RLY Asset = "RLY"
|
||||||
|
SANTOS Asset = "SANTOS"
|
||||||
|
SUI Asset = "SUI"
|
||||||
|
SWEAT Asset = "SWEAT"
|
||||||
|
TUSD Asset = "TUSD"
|
||||||
|
TVK Asset = "TVK"
|
||||||
|
UNFI Asset = "UNFI"
|
||||||
|
USDD Asset = "USDD"
|
||||||
|
VLX Asset = "VLX"
|
||||||
|
WEMIX Asset = "WEMIX"
|
||||||
|
XDC Asset = "XDC"
|
||||||
|
XRD Asset = "XRD"
|
||||||
|
FB Asset = "FB"
|
||||||
|
BRISE Asset = "BRISE"
|
||||||
|
KAS Asset = "KAS"
|
||||||
|
XEN Asset = "XEN"
|
||||||
|
HAM Asset = "HAM"
|
||||||
|
TAMA Asset = "TAMA"
|
||||||
|
KDA Asset = "KDA"
|
||||||
|
CFX Asset = "CFX"
|
||||||
|
VRA Asset = "VRA"
|
||||||
|
BDX Asset = "BDX"
|
||||||
|
RDNT Asset = "RDNT"
|
||||||
|
WLD Asset = "WLD"
|
||||||
|
AGIX Asset = "AGIX"
|
||||||
|
PYUSD Asset = "PYUSD"
|
||||||
|
MOON Asset = "MOON"
|
||||||
|
SEI Asset = "SEI"
|
||||||
|
AKT Asset = "AKT"
|
||||||
|
MAGIC Asset = "MAGIC"
|
||||||
|
SNT Asset = "SNT"
|
||||||
|
ALPHA Asset = "ALPHA"
|
||||||
|
ALI Asset = "ALI"
|
||||||
|
CQT Asset = "CQT"
|
||||||
|
HIGH Asset = "HIGH"
|
||||||
|
AERGO Asset = "AERGO"
|
||||||
|
GODS Asset = "GODS"
|
||||||
|
ZBC Asset = "ZBC"
|
||||||
|
ACA Asset = "ACA"
|
||||||
|
MDT Asset = "MDT"
|
||||||
|
LIT Asset = "LIT"
|
||||||
|
QI Asset = "QI"
|
||||||
|
AURORA Asset = "AURORA"
|
||||||
|
TOMI Asset = "TOMI"
|
||||||
|
XCH Asset = "XCH"
|
||||||
|
MANTA Asset = "MANTA"
|
||||||
|
PYTH Asset = "PYTH"
|
||||||
|
STRK Asset = "STRK"
|
||||||
|
ETHFI Asset = "ETHFI"
|
||||||
|
TIA Asset = "TIA"
|
||||||
|
EETH Asset = "EETH"
|
||||||
|
)
|
@@ -1,29 +0,0 @@
|
|||||||
package coindesk_test
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"fmt"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"code.humancabbage.net/moonmath/coindesk"
|
|
||||||
)
|
|
||||||
|
|
||||||
func TestXxx(t *testing.T) {
|
|
||||||
now := time.Now()
|
|
||||||
then := now.Add(time.Duration(-24) * time.Hour)
|
|
||||||
|
|
||||||
values, err := coindesk.GetPriceValues(context.Background(), coindesk.BTC, then, now)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("test failure: %v", err)
|
|
||||||
}
|
|
||||||
_ = values
|
|
||||||
fmt.Println()
|
|
||||||
|
|
||||||
tickers, err := coindesk.GetAssetTickers(context.Background(), coindesk.BTC, coindesk.ETH)
|
|
||||||
if err != nil {
|
|
||||||
t.Errorf("test failure: %v", err)
|
|
||||||
}
|
|
||||||
_ = tickers
|
|
||||||
fmt.Println()
|
|
||||||
}
|
|
@@ -10,13 +10,6 @@ import (
|
|||||||
// Asset is a cryptocurrency, like Bitcoin, Ethereum, etc.
|
// Asset is a cryptocurrency, like Bitcoin, Ethereum, etc.
|
||||||
type Asset string
|
type Asset string
|
||||||
|
|
||||||
const (
|
|
||||||
// BTC is the Bitcoin asset.
|
|
||||||
BTC Asset = "BTC"
|
|
||||||
// ETH is the Ethereum asset.
|
|
||||||
ETH Asset = "ETH"
|
|
||||||
)
|
|
||||||
|
|
||||||
// Response represents the general top-level format of Coindesk API responses.
|
// Response represents the general top-level format of Coindesk API responses.
|
||||||
type Response[T any] struct {
|
type Response[T any] struct {
|
||||||
StatusCode int `json:"statusCode"`
|
StatusCode int `json:"statusCode"`
|
||||||
|
31
coindesk/model_test.go
Normal file
31
coindesk/model_test.go
Normal file
@@ -0,0 +1,31 @@
|
|||||||
|
package coindesk_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"encoding/json"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestUnmarshalPriceValues(t *testing.T) {
|
||||||
|
testUnmarshalResponse[coindesk.PriceValues](t, priceValuesJson)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnmarshalAssetTickers(t *testing.T) {
|
||||||
|
testUnmarshalResponse[coindesk.AssetTickers](t, assetTickersJson)
|
||||||
|
}
|
||||||
|
|
||||||
|
func testUnmarshalResponse[T any](t *testing.T, jsonBytes []byte) {
|
||||||
|
var resp coindesk.Response[T]
|
||||||
|
err := json.Unmarshal(jsonBytes, &resp)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("failed to unmarshal JSON: %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed price_values.json
|
||||||
|
var priceValuesJson []byte
|
||||||
|
|
||||||
|
//go:embed asset_tickers.json
|
||||||
|
var assetTickersJson []byte
|
1
coindesk/price_values.json
Normal file
1
coindesk/price_values.json
Normal file
@@ -0,0 +1 @@
|
|||||||
|
{"statusCode":200,"message":"OK","data":{"iso":"BTC","name":"Bitcoin","slug":"bitcoin","ingestionStart":"2014-11-03","interval":"15m","src":"tb","entries":[[1710548099000,69784.6599201257],[1710548999000,69907.6435505413],[1710549899000,69735.0244223474],[1710550799000,69608.3777481736],[1710551699000,69643.5872644923],[1710552599000,69389.4883714119],[1710553499000,69350.2996335944],[1710554399000,69241.2719800575],[1710555299000,69110.5930246779],[1710556199000,69046.5706710773],[1710557099000,69209.8770692447],[1710557999000,69069.0204183649],[1710558899000,69317.6327892521],[1710559799000,69175.2057532632],[1710560699000,69237.706076427],[1710561599000,69206.0448006493],[1710562499000,69077.8264056849],[1710563399000,69161.0784263752],[1710564299000,68903.1424009851],[1710565199000,68994.5234182921],[1710566099000,68754.9875193438],[1710566999000,68784.6200740683],[1710567899000,69002.1375650817],[1710568799000,68975.0878307215],[1710569699000,69045.2310776643],[1710570599000,69025.7239355934],[1710571499000,69068.3988090268],[1710572399000,69185.2444741912],[1710573299000,69246.5439171529],[1710574199000,69278.2574941136],[1710575099000,69407.4780576693],[1710575999000,69406.4527711537],[1710576899000,69296.4923727422],[1710577799000,69165.948416751],[1710578699000,69258.7912250956],[1710579599000,69017.1102984366],[1710580499000,69014.4480481396],[1710581399000,69099.3096696255],[1710582299000,69011.246072562],[1710583199000,68942.5343294262],[1710584099000,68697.8235476743],[1710584999000,68597.1119491188],[1710585899000,68532.2931864276],[1710586799000,68420.1712986066],[1710587699000,68354.8847177761],[1710588599000,67949.5219527423],[1710589499000,68327.2101380853],[1710590399000,68260.6576285653],[1710591299000,68192.7968537126],[1710592199000,68302.103987459],[1710593099000,68096.4362930929],[1710593999000,67906.8863092216],[1710594899000,67982.183818933],[1710595799000,68073.3862259585],[1710596699000,67891.8144823227],[1710597599000,67706.923652975],[1710598499000,67942.4174766702],[1710599399000,67848.3672161555],[1710600299000,67975.9404080798],[1710601199000,68177.0804610881],[1710602099000,67994.5903423281],[1710602999000,68024.8507494152],[1710603899000,68201.8594048759],[1710604799000,68358.4188016011],[1710605699000,68239.5960888028],[1710606599000,68224.3438496054],[1710607499000,68293.736596961],[1710608399000,68158.4057208716],[1710609299000,68114.4234280406],[1710610199000,67829.2127954109],[1710611099000,67630.8190396486],[1710611999000,67220.3085172518],[1710612899000,67043.4786652409],[1710613799000,66906.1713676385],[1710614699000,66871.2079622553],[1710615599000,67015.8311895788],[1710616499000,67458.766887969],[1710617399000,67493.9852796663],[1710618299000,67354.2332686561],[1710619199000,66916.4704143146],[1710620099000,66995.7196236024],[1710620999000,67046.4965828003],[1710621899000,67402.0503271179],[1710622799000,67087.3064846683],[1710623699000,66943.270112864],[1710624599000,66268.8656111216],[1710625499000,66290.9133774055],[1710626399000,66189.3483322972],[1710627299000,66073.8513767399],[1710628199000,66433.8329094007],[1710629099000,66549.3543050487],[1710629999000,66331.1108451565],[1710630899000,65376.9600942977],[1710631799000,65280.3933464042],[1710632699000,65598.1187015513],[1710633599000,65261.5180497119],[1710634499000,65780.8283123471]]}}
|
111
config/config.go
Normal file
111
config/config.go
Normal file
@@ -0,0 +1,111 @@
|
|||||||
|
package config
|
||||||
|
|
||||||
|
import (
|
||||||
|
_ "embed"
|
||||||
|
"slices"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||||
|
"code.humancabbage.net/sam/moonmath/moon"
|
||||||
|
"github.com/knadh/koanf/parsers/yaml"
|
||||||
|
"github.com/knadh/koanf/providers/file"
|
||||||
|
"github.com/knadh/koanf/providers/rawbytes"
|
||||||
|
"github.com/knadh/koanf/providers/structs"
|
||||||
|
"github.com/knadh/koanf/v2"
|
||||||
|
)
|
||||||
|
|
||||||
|
var k = koanf.New(".")
|
||||||
|
|
||||||
|
func Load(path string) (all All, err error) {
|
||||||
|
err = k.Load(structs.Provider(Default, "koanf"), nil)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if path != "" {
|
||||||
|
err = k.Load(file.Provider(path), yaml.Parser())
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
err = k.Unmarshal("", &all)
|
||||||
|
if err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
var Default All
|
||||||
|
|
||||||
|
type All struct {
|
||||||
|
Defaults Data `koanf:"defaults"`
|
||||||
|
Assets map[coindesk.Asset]Data `koanf:"assets"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type Data struct {
|
||||||
|
Asset coindesk.Asset `koanf:"asset"`
|
||||||
|
Goals []moon.Goal `koanf:"goals"`
|
||||||
|
ConstantBases []moon.ConstantBase `koanf:"constantBases"`
|
||||||
|
RelativeBases []moon.RelativeBase `koanf:"relativeBases"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func (all All) GetData(asset coindesk.Asset) (data Data, err error) {
|
||||||
|
data, ok := all.Assets[asset]
|
||||||
|
if !ok {
|
||||||
|
data = all.Defaults
|
||||||
|
}
|
||||||
|
if data.Asset == "" {
|
||||||
|
data.Asset = asset
|
||||||
|
}
|
||||||
|
if data.Goals == nil || len(data.Goals) == 0 {
|
||||||
|
data.Goals = all.Defaults.Goals
|
||||||
|
}
|
||||||
|
if data.ConstantBases == nil || len(data.ConstantBases) == 0 {
|
||||||
|
data.ConstantBases = all.Defaults.ConstantBases
|
||||||
|
}
|
||||||
|
if data.RelativeBases == nil || len(data.RelativeBases) == 0 {
|
||||||
|
data.RelativeBases = all.Defaults.RelativeBases
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetBases returns the concatenation of the constant and relative bases, sorted
|
||||||
|
// from most recent to least recent in time.
|
||||||
|
func GetBases(d *Data) (bases []moon.Base) {
|
||||||
|
for _, b := range d.ConstantBases {
|
||||||
|
bases = append(bases, b)
|
||||||
|
}
|
||||||
|
for _, b := range d.RelativeBases {
|
||||||
|
bases = append(bases, b)
|
||||||
|
}
|
||||||
|
now := time.Now()
|
||||||
|
slices.SortFunc(bases, func(a, b moon.Base) int {
|
||||||
|
aTime := a.From(now)
|
||||||
|
bTime := b.From(now)
|
||||||
|
aFirst := aTime.Before(bTime)
|
||||||
|
bFirst := bTime.Before(aTime)
|
||||||
|
switch {
|
||||||
|
case aFirst:
|
||||||
|
return 1
|
||||||
|
case bFirst:
|
||||||
|
return -1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
})
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
//go:embed default.yaml
|
||||||
|
var defaultYamlBytes []byte
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
var k = koanf.New(".")
|
||||||
|
err := k.Load(rawbytes.Provider(defaultYamlBytes), yaml.Parser())
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
err = k.Unmarshal("", &Default)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
}
|
10
config/config_test.go
Normal file
10
config/config_test.go
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package config_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestDefault(t *testing.T) {
|
||||||
|
// config.Default is parsed in config.init(), which panics on error.
|
||||||
|
// so just getting this far means that at least it parsed successfully.
|
||||||
|
}
|
46
config/default.yaml
Normal file
46
config/default.yaml
Normal file
@@ -0,0 +1,46 @@
|
|||||||
|
defaults:
|
||||||
|
goals:
|
||||||
|
- name: $100k
|
||||||
|
value: 100000
|
||||||
|
- name: $150k
|
||||||
|
value: 150000
|
||||||
|
- name: $200k
|
||||||
|
value: 200000
|
||||||
|
- name: $250k
|
||||||
|
value: 250000
|
||||||
|
- name: $300k
|
||||||
|
value: 300000
|
||||||
|
- name: $500k
|
||||||
|
value: 500000
|
||||||
|
- name: $1m
|
||||||
|
value: 1000000
|
||||||
|
relativeBases:
|
||||||
|
- name: Month
|
||||||
|
offset: -720h0m0s
|
||||||
|
- name: Quarter
|
||||||
|
offset: -2160h0m0s
|
||||||
|
- name: Half-Year
|
||||||
|
offset: -4368h0m0s
|
||||||
|
- name: Year
|
||||||
|
offset: -8760h0m0s
|
||||||
|
constantBases:
|
||||||
|
- name: 2020-
|
||||||
|
time: 2019-12-31T16:00:00-08:00
|
||||||
|
- name: 2017-
|
||||||
|
time: 2016-12-31T16:00:00-08:00
|
||||||
|
|
||||||
|
assets:
|
||||||
|
ETH:
|
||||||
|
goals:
|
||||||
|
- name: $5k
|
||||||
|
value: 5000
|
||||||
|
- name: $7.5k
|
||||||
|
value: 7500
|
||||||
|
- name: $10k
|
||||||
|
value: 10000
|
||||||
|
- name: $15k
|
||||||
|
value: 15000
|
||||||
|
- name: $20k
|
||||||
|
value: 20000
|
||||||
|
- name: $25k
|
||||||
|
value: 25000
|
16
go.mod
16
go.mod
@@ -1,12 +1,18 @@
|
|||||||
module code.humancabbage.net/moonmath
|
module code.humancabbage.net/sam/moonmath
|
||||||
|
|
||||||
go 1.22.1
|
go 1.22.1
|
||||||
|
|
||||||
require (
|
require (
|
||||||
|
github.com/alecthomas/kong v0.9.0
|
||||||
github.com/carlmjohnson/requests v0.23.5
|
github.com/carlmjohnson/requests v0.23.5
|
||||||
github.com/charmbracelet/bubbles v0.18.0
|
github.com/charmbracelet/bubbles v0.18.0
|
||||||
github.com/charmbracelet/bubbletea v0.25.0
|
github.com/charmbracelet/bubbletea v0.25.0
|
||||||
github.com/charmbracelet/lipgloss v0.10.0
|
github.com/charmbracelet/lipgloss v0.10.0
|
||||||
|
github.com/knadh/koanf/parsers/yaml v0.1.0
|
||||||
|
github.com/knadh/koanf/providers/file v0.1.0
|
||||||
|
github.com/knadh/koanf/providers/rawbytes v0.1.0
|
||||||
|
github.com/knadh/koanf/providers/structs v0.1.0
|
||||||
|
github.com/knadh/koanf/v2 v2.1.0
|
||||||
github.com/sourcegraph/conc v0.3.0
|
github.com/sourcegraph/conc v0.3.0
|
||||||
golang.org/x/net v0.22.0
|
golang.org/x/net v0.22.0
|
||||||
)
|
)
|
||||||
@@ -14,10 +20,17 @@ require (
|
|||||||
require (
|
require (
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
|
||||||
github.com/containerd/console v1.0.4 // indirect
|
github.com/containerd/console v1.0.4 // indirect
|
||||||
|
github.com/fatih/structs v1.1.0 // indirect
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0 // indirect
|
||||||
|
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 // indirect
|
||||||
|
github.com/knadh/koanf/maps v0.1.1 // indirect
|
||||||
|
github.com/kr/text v0.2.0 // indirect
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
|
||||||
github.com/mattn/go-isatty v0.0.20 // indirect
|
github.com/mattn/go-isatty v0.0.20 // indirect
|
||||||
github.com/mattn/go-localereader v0.0.1 // indirect
|
github.com/mattn/go-localereader v0.0.1 // indirect
|
||||||
github.com/mattn/go-runewidth v0.0.15 // indirect
|
github.com/mattn/go-runewidth v0.0.15 // indirect
|
||||||
|
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||||
|
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
|
||||||
github.com/muesli/cancelreader v0.2.2 // indirect
|
github.com/muesli/cancelreader v0.2.2 // indirect
|
||||||
github.com/muesli/reflow v0.3.0 // indirect
|
github.com/muesli/reflow v0.3.0 // indirect
|
||||||
@@ -29,4 +42,5 @@ require (
|
|||||||
golang.org/x/sys v0.18.0 // indirect
|
golang.org/x/sys v0.18.0 // indirect
|
||||||
golang.org/x/term v0.18.0 // indirect
|
golang.org/x/term v0.18.0 // indirect
|
||||||
golang.org/x/text v0.14.0 // indirect
|
golang.org/x/text v0.14.0 // indirect
|
||||||
|
gopkg.in/yaml.v3 v3.0.1 // indirect
|
||||||
)
|
)
|
||||||
|
40
go.sum
40
go.sum
@@ -1,3 +1,9 @@
|
|||||||
|
github.com/alecthomas/assert/v2 v2.6.0 h1:o3WJwILtexrEUk3cUVal3oiQY2tfgr/FHWiz/v2n4FU=
|
||||||
|
github.com/alecthomas/assert/v2 v2.6.0/go.mod h1:Bze95FyfUr7x34QZrjL+XP+0qgp/zg8yS+TtBj1WA3k=
|
||||||
|
github.com/alecthomas/kong v0.9.0 h1:G5diXxc85KvoV2f0ZRVuMsi45IrBgx9zDNGNj165aPA=
|
||||||
|
github.com/alecthomas/kong v0.9.0/go.mod h1:Y47y5gKfHp1hDc7CH7OeXgLIpp+Q2m1Ni0L5s3bI8Os=
|
||||||
|
github.com/alecthomas/repr v0.4.0 h1:GhI2A8MACjfegCPVq9f1FLvIBS+DrQ2KQBFZP1iFzXc=
|
||||||
|
github.com/alecthomas/repr v0.4.0/go.mod h1:Fr0507jx4eOXV7AlPV6AVZLYrLIuIeSOWtW57eE/O/4=
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1 h1:HwpRHbFMcZLEVr42D4p7XBqjyuxQH5SMiErDT4WkJ2k=
|
||||||
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
github.com/aymanbagabas/go-osc52/v2 v2.0.1/go.mod h1:uYgXzlJ7ZpABp8OJ+exZzJJhRNQ2ASbcXHWsFqH8hp8=
|
||||||
github.com/carlmjohnson/requests v0.23.5 h1:NPANcAofwwSuC6SIMwlgmHry2V3pLrSqRiSBKYbNHHA=
|
github.com/carlmjohnson/requests v0.23.5 h1:NPANcAofwwSuC6SIMwlgmHry2V3pLrSqRiSBKYbNHHA=
|
||||||
@@ -10,9 +16,34 @@ github.com/charmbracelet/lipgloss v0.10.0 h1:KWeXFSexGcfahHX+54URiZGkBFazf70JNMt
|
|||||||
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
|
github.com/charmbracelet/lipgloss v0.10.0/go.mod h1:Wig9DSfvANsxqkRsqj6x87irdy123SR4dOXlKa91ciE=
|
||||||
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
|
github.com/containerd/console v1.0.4 h1:F2g4+oChYvBTsASRTz8NP6iIAi97J3TtSAsLbIFn4ro=
|
||||||
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
|
github.com/containerd/console v1.0.4/go.mod h1:YynlIjWYF8myEu6sdkwKIvGQq+cOckRm6So2avqoYAk=
|
||||||
|
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
|
||||||
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
|
||||||
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
|
||||||
|
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
|
||||||
|
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0 h1:8JEhPFa5W2WU7YfeZzPNqzMP6Lwt7L2715Ggo0nosvA=
|
||||||
|
github.com/fsnotify/fsnotify v1.7.0/go.mod h1:40Bi/Hjc2AVfZrqy+aj+yEI+/bRxZnMJyTJwOpGvigM=
|
||||||
|
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1 h1:TQcrn6Wq+sKGkpyPvppOz99zsMBaUOKXq6HSv655U1c=
|
||||||
|
github.com/go-viper/mapstructure/v2 v2.0.0-alpha.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
|
||||||
|
github.com/hexops/gotextdiff v1.0.3 h1:gitA9+qJrrTCsiCl7+kh75nPqQt1cx4ZkudSTLoUqJM=
|
||||||
|
github.com/hexops/gotextdiff v1.0.3/go.mod h1:pSWU5MAI3yDq+fZBTazCSJysOMbxWL1BSow5/V2vxeg=
|
||||||
|
github.com/knadh/koanf/maps v0.1.1 h1:G5TjmUh2D7G2YWf5SQQqSiHRJEjaicvU0KpypqB3NIs=
|
||||||
|
github.com/knadh/koanf/maps v0.1.1/go.mod h1:npD/QZY3V6ghQDdcQzl1W4ICNVTkohC8E73eI2xW4yI=
|
||||||
|
github.com/knadh/koanf/parsers/yaml v0.1.0 h1:ZZ8/iGfRLvKSaMEECEBPM1HQslrZADk8fP1XFUxVI5w=
|
||||||
|
github.com/knadh/koanf/parsers/yaml v0.1.0/go.mod h1:cvbUDC7AL23pImuQP0oRw/hPuccrNBS2bps8asS0CwY=
|
||||||
|
github.com/knadh/koanf/providers/file v0.1.0 h1:fs6U7nrV58d3CFAFh8VTde8TM262ObYf3ODrc//Lp+c=
|
||||||
|
github.com/knadh/koanf/providers/file v0.1.0/go.mod h1:rjJ/nHQl64iYCtAW2QQnF0eSmDEX/YZ/eNFj5yR6BvA=
|
||||||
|
github.com/knadh/koanf/providers/rawbytes v0.1.0 h1:dpzgu2KO6uf6oCb4aP05KDmKmAmI51k5pe8RYKQ0qME=
|
||||||
|
github.com/knadh/koanf/providers/rawbytes v0.1.0/go.mod h1:mMTB1/IcJ/yE++A2iEZbY1MLygX7vttU+C+S/YmPu9c=
|
||||||
|
github.com/knadh/koanf/providers/structs v0.1.0 h1:wJRteCNn1qvLtE5h8KQBvLJovidSdntfdyIbbCzEyE0=
|
||||||
|
github.com/knadh/koanf/providers/structs v0.1.0/go.mod h1:sw2YZ3txUcqA3Z27gPlmmBzWn1h8Nt9O6EP/91MkcWE=
|
||||||
|
github.com/knadh/koanf/v2 v2.1.0 h1:eh4QmHHBuU8BybfIJ8mB8K8gsGCD/AUQTdwGq/GzId8=
|
||||||
|
github.com/knadh/koanf/v2 v2.1.0/go.mod h1:4mnTRbZCK+ALuBXHZMjDfG9y714L7TykVnZkXbMU3Es=
|
||||||
|
github.com/kr/pretty v0.3.0 h1:WgNl7dwNpEZ6jJ9k1snq4pZsg7DOEN8hP9Xw0Tsjwk0=
|
||||||
|
github.com/kr/pretty v0.3.0/go.mod h1:640gp4NfQd8pI5XOwp5fnNeVWj67G7CFk/SaSQn7NBk=
|
||||||
|
github.com/kr/text v0.2.0 h1:5Nx0Ya0ZqY2ygV366QzturHI13Jq95ApcVaJBhpS+AY=
|
||||||
|
github.com/kr/text v0.2.0/go.mod h1:eLer722TekiGuMkidMxC/pM04lWEeraHUUmBw8l2grE=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
|
||||||
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
|
||||||
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
|
||||||
@@ -22,6 +53,10 @@ github.com/mattn/go-localereader v0.0.1/go.mod h1:8fBrzywKY7BI3czFoHkuzRoWE9C+Ei
|
|||||||
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
github.com/mattn/go-runewidth v0.0.12/go.mod h1:RAqKPSqVFrSLVXbA8x7dzmKdmGzieGRCM46jaSJTDAk=
|
||||||
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
github.com/mattn/go-runewidth v0.0.15 h1:UNAjwbU9l54TA3KzvqLGxwWjHmMgBUVhBiTjelZgg3U=
|
||||||
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
github.com/mattn/go-runewidth v0.0.15/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
|
||||||
|
github.com/mitchellh/copystructure v1.2.0 h1:vpKXTN4ewci03Vljg/q9QvCGUDttBOGBIa15WveJJGw=
|
||||||
|
github.com/mitchellh/copystructure v1.2.0/go.mod h1:qLl+cE2AmVv+CoeAwDPye/v+N2HKCj9FbZEVFJRxO9s=
|
||||||
|
github.com/mitchellh/reflectwalk v1.0.2 h1:G2LzWKi524PWgd3mLHV8Y5k7s6XUvT0Gef6zxSIeXaQ=
|
||||||
|
github.com/mitchellh/reflectwalk v1.0.2/go.mod h1:mSTlrgnPZtwu0c4WaC2kGObEpuNDbx0jmZXqmk4esnw=
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 h1:ZK8zHtRHOkbHy6Mmr5D264iyp3TiX5OmNcI5cIARiQI=
|
||||||
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6/go.mod h1:CJlz5H+gyd6CUWT45Oy4q24RdLyn7Md9Vj2/ldJBSIo=
|
||||||
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
github.com/muesli/cancelreader v0.2.2 h1:3I4Kt4BQjOR54NavqnDogx/MIoWBFa0StPA8ELUXHmA=
|
||||||
@@ -36,6 +71,8 @@ github.com/rivo/uniseg v0.1.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJ
|
|||||||
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
|
||||||
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
github.com/rivo/uniseg v0.4.7 h1:WUdvkW8uEhrYfLC4ZzdpI2ztxP1I582+49Oc5Mq64VQ=
|
||||||
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
github.com/rivo/uniseg v0.4.7/go.mod h1:FN3SvrM+Zdj16jyLfmOkMNblXMcoc8DfTHruCPUcx88=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0 h1:73kH8U+JUqXU8lRuOHeVHaa/SZPifC7BkcraZVejAe8=
|
||||||
|
github.com/rogpeppe/go-internal v1.9.0/go.mod h1:WtVeX8xhTBvf0smdhujwtBcq4Qrzq/fJaraNFVN+nFs=
|
||||||
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
github.com/sourcegraph/conc v0.3.0 h1:OQTbbt6P72L20UqAkXXuLOj79LfEanQ+YQFNpLA9ySo=
|
||||||
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
github.com/sourcegraph/conc v0.3.0/go.mod h1:Sdozi7LEKbFPqYX2/J+iBAM6HpqSLTASQIKqDmF7Mt0=
|
||||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||||
@@ -58,5 +95,8 @@ golang.org/x/term v0.18.0 h1:FcHjZXDMxI8mM3nwhX9HlKop4C0YQvCVCdwYl2wOtE8=
|
|||||||
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
|
golang.org/x/term v0.18.0/go.mod h1:ILwASektA3OnRv7amZ1xhE/KTR+u50pbXfZ03+6Nx58=
|
||||||
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
|
||||||
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
|
||||||
|
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 h1:YR8cESwS4TdDjEe65xsg0ogRM/Nc3DYOhEAlW+xobZo=
|
||||||
|
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||||
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
|
||||||
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
|
||||||
|
117
moon/moon.go
117
moon/moon.go
@@ -2,31 +2,36 @@ package moon
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"fmt"
|
||||||
"math"
|
"math"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.humancabbage.net/moonmath/coindesk"
|
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||||
"github.com/sourcegraph/conc/pool"
|
"github.com/sourcegraph/conc/pool"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Math struct {
|
type Math struct {
|
||||||
|
Asset coindesk.Asset
|
||||||
CurrentPrice coindesk.Price
|
CurrentPrice coindesk.Price
|
||||||
Columns []Column
|
Columns []Column
|
||||||
Goals []float64
|
Goals []Goal
|
||||||
|
Labels []string
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewMath(goals []float64, bases []Base) (m Math) {
|
func NewMath(asset coindesk.Asset, goals []Goal, bases []Base) (m Math) {
|
||||||
if goals == nil {
|
if goals == nil || bases == nil {
|
||||||
goals = DefaultGoals
|
panic("goals and bases must be set")
|
||||||
}
|
|
||||||
if bases == nil {
|
|
||||||
bases = DefaultBases
|
|
||||||
}
|
}
|
||||||
|
m.Asset = asset
|
||||||
m.Goals = goals
|
m.Goals = goals
|
||||||
|
m.Labels = []string{"Starting", "CDPR"}
|
||||||
m.Columns = make([]Column, len(bases))
|
m.Columns = make([]Column, len(bases))
|
||||||
for i := range bases {
|
for i := range bases {
|
||||||
m.Columns[i].Base = bases[i]
|
m.Columns[i].Base = bases[i]
|
||||||
}
|
}
|
||||||
|
for i := range goals {
|
||||||
|
m.Labels = append(m.Labels, goals[i].Name)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,7 +48,7 @@ type Projection struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func ProjectDates(
|
func ProjectDates(
|
||||||
from time.Time, currentPrice float64, cdpr float64, goals []float64,
|
from time.Time, currentPrice float64, cdpr float64, goals []Goal,
|
||||||
) (p Projection) {
|
) (p Projection) {
|
||||||
if cdpr <= 0 {
|
if cdpr <= 0 {
|
||||||
return
|
return
|
||||||
@@ -51,7 +56,7 @@ func ProjectDates(
|
|||||||
logP := math.Log(currentPrice)
|
logP := math.Log(currentPrice)
|
||||||
logR := math.Log(cdpr)
|
logR := math.Log(cdpr)
|
||||||
for _, goal := range goals {
|
for _, goal := range goals {
|
||||||
daysToGo := (math.Log(goal) - logP) / logR
|
daysToGo := (math.Log(goal.Value) - logP) / logR
|
||||||
date := from.Add(time.Hour * 24 * time.Duration(daysToGo))
|
date := from.Add(time.Hour * 24 * time.Duration(daysToGo))
|
||||||
p.Dates = append(p.Dates, date)
|
p.Dates = append(p.Dates, date)
|
||||||
}
|
}
|
||||||
@@ -60,11 +65,11 @@ func ProjectDates(
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (m *Math) Refresh(ctx context.Context) (err error) {
|
func (m *Math) Refresh(ctx context.Context) (err error) {
|
||||||
resp, err := coindesk.GetAssetTickers(ctx, coindesk.BTC)
|
resp, err := coindesk.GetAssetTickers(ctx, m.Asset)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
m.CurrentPrice = resp.Data[coindesk.BTC].OHLC.Closing
|
m.CurrentPrice = resp.Data[m.Asset].OHLC.Closing
|
||||||
|
|
||||||
tasks := pool.New().WithErrors()
|
tasks := pool.New().WithErrors()
|
||||||
tasks.WithMaxGoroutines(len(m.Columns))
|
tasks.WithMaxGoroutines(len(m.Columns))
|
||||||
@@ -77,7 +82,7 @@ func (m *Math) Refresh(ctx context.Context) (err error) {
|
|||||||
c.StartingDate = c.Base.From(now)
|
c.StartingDate = c.Base.From(now)
|
||||||
nextDay := c.StartingDate.Add(time.Hour * 24)
|
nextDay := c.StartingDate.Add(time.Hour * 24)
|
||||||
resp, err := coindesk.GetPriceValues(ctx,
|
resp, err := coindesk.GetPriceValues(ctx,
|
||||||
coindesk.BTC, c.StartingDate, nextDay)
|
m.Asset, c.StartingDate, nextDay)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
@@ -86,7 +91,10 @@ func (m *Math) Refresh(ctx context.Context) (err error) {
|
|||||||
days := now.Sub(c.StartingDate).Hours() / 24
|
days := now.Sub(c.StartingDate).Hours() / 24
|
||||||
c.CDPR = CDPR(days, c.Gain)
|
c.CDPR = CDPR(days, c.Gain)
|
||||||
if c.CDPR > 1 {
|
if c.CDPR > 1 {
|
||||||
c.Projections = ProjectDates(now, float64(m.CurrentPrice), c.CDPR, m.Goals)
|
c.Projections = ProjectDates(
|
||||||
|
now, float64(m.CurrentPrice),
|
||||||
|
c.CDPR, m.Goals,
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
c.Projections.Dates = nil
|
c.Projections.Dates = nil
|
||||||
}
|
}
|
||||||
@@ -107,58 +115,83 @@ type Column struct {
|
|||||||
Projections Projection
|
Projections Projection
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultGoals = []float64{
|
func (c *Column) Column() (entries []string) {
|
||||||
100000,
|
entries = append(entries, fmt.Sprintf("$%.2f", c.StartingPrice))
|
||||||
150000,
|
entries = append(entries, fmt.Sprintf("%.2f%%", (c.CDPR-1)*100))
|
||||||
200000,
|
never := c.CDPR <= 1
|
||||||
250000,
|
for i := range c.Projections.Dates {
|
||||||
300000,
|
var cell string
|
||||||
500000,
|
if never {
|
||||||
1000000,
|
cell = "NEVER!!!!!"
|
||||||
|
} else {
|
||||||
|
cell = c.
|
||||||
|
Projections.
|
||||||
|
Dates[i].
|
||||||
|
Format("2006-01-02")
|
||||||
|
}
|
||||||
|
entries = append(entries, cell)
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
var DefaultBases = []Base{
|
var DefaultGoals = []Goal{
|
||||||
RelativeBase{"Month", time.Duration(-30) * time.Hour * 24},
|
{"$100k", 100000},
|
||||||
RelativeBase{"Quarter", time.Duration(-90) * time.Hour * 24},
|
{"$150k", 150000},
|
||||||
RelativeBase{"Half-Year", time.Duration(-182) * time.Hour * 24},
|
{"$200k", 200000},
|
||||||
RelativeBase{"Year", time.Duration(-365) * time.Hour * 24},
|
{"$250k", 250000},
|
||||||
ConstantBase{"2020-", time.Unix(1577836800, 0)},
|
{"$300k", 300000},
|
||||||
ConstantBase{"2019-", time.Unix(1546300800, 0)},
|
{"$500k", 500000},
|
||||||
ConstantBase{"2018-", time.Unix(1514764800, 0)},
|
{"$1m", 1000000},
|
||||||
ConstantBase{"2017-", time.Unix(1483228800, 0)},
|
}
|
||||||
|
|
||||||
|
var DefaultConstantBases = []ConstantBase{
|
||||||
|
{"2020-", time.Unix(1577836800, 0)},
|
||||||
|
{"2017-", time.Unix(1483228800, 0)},
|
||||||
|
}
|
||||||
|
|
||||||
|
var DefaultRelativeBases = []RelativeBase{
|
||||||
|
{"Month", time.Duration(-30) * time.Hour * 24},
|
||||||
|
{"Quarter", time.Duration(-90) * time.Hour * 24},
|
||||||
|
{"Half-Year", time.Duration(-182) * time.Hour * 24},
|
||||||
|
{"Year", time.Duration(-365) * time.Hour * 24},
|
||||||
|
}
|
||||||
|
|
||||||
|
type Goal struct {
|
||||||
|
Name string `koanf:"name"`
|
||||||
|
Value float64 `koanf:"value"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// Base is a temporal point of comparison used for price projection.
|
// Base is a temporal point of comparison used for price projection.
|
||||||
type Base interface {
|
type Base interface {
|
||||||
From(now time.Time) time.Time
|
From(now time.Time) time.Time
|
||||||
Name() string
|
Label() string
|
||||||
}
|
}
|
||||||
|
|
||||||
// ConstantBase is a base that is a constant time, e.g. 2020-01-01.
|
// ConstantBase is a base that is a constant time, e.g. 2020-01-01.
|
||||||
type ConstantBase struct {
|
type ConstantBase struct {
|
||||||
name string
|
Name string `koanf:"name"`
|
||||||
time time.Time
|
Time time.Time `koanf:"time"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cb ConstantBase) From(_ time.Time) time.Time {
|
func (cb ConstantBase) From(_ time.Time) time.Time {
|
||||||
return cb.time
|
return cb.Time
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cb ConstantBase) Name() string {
|
func (cb ConstantBase) Label() string {
|
||||||
return cb.name
|
return cb.Name
|
||||||
}
|
}
|
||||||
|
|
||||||
// RelativeBase is a base that is relative, e.g. "90 days ago."
|
// RelativeBase is a base that is relative, e.g. "90 days ago."
|
||||||
type RelativeBase struct {
|
type RelativeBase struct {
|
||||||
name string
|
Name string `koanf:"name"`
|
||||||
offset time.Duration
|
Offset time.Duration `koanf:"offset"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rb RelativeBase) From(now time.Time) time.Time {
|
func (rb RelativeBase) From(now time.Time) time.Time {
|
||||||
then := now.Add(time.Duration(rb.offset))
|
then := now.Add(time.Duration(rb.Offset))
|
||||||
return then
|
return then
|
||||||
}
|
}
|
||||||
|
|
||||||
func (rb RelativeBase) Name() string {
|
func (rb RelativeBase) Label() string {
|
||||||
return rb.name
|
return rb.Name
|
||||||
}
|
}
|
||||||
|
@@ -4,20 +4,13 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"code.humancabbage.net/moonmath/moon"
|
"code.humancabbage.net/sam/moonmath/moon"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestCDPR(t *testing.T) {
|
func TestCDPR(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestProjection(t *testing.T) {
|
func TestProjection(t *testing.T) {
|
||||||
p := moon.ProjectDates(time.Now(), 68900, 1.0055, []float64{
|
p := moon.ProjectDates(time.Now(), 68900, 1.0055, moon.DefaultGoals)
|
||||||
100000,
|
|
||||||
150000,
|
|
||||||
200000,
|
|
||||||
250000,
|
|
||||||
300000,
|
|
||||||
350000,
|
|
||||||
})
|
|
||||||
_ = p
|
_ = p
|
||||||
}
|
}
|
||||||
|
159
moonmath.go
159
moonmath.go
@@ -1,147 +1,42 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
"os"
|
||||||
"time"
|
"strings"
|
||||||
|
|
||||||
"code.humancabbage.net/moonmath/moon"
|
"code.humancabbage.net/sam/moonmath/coindesk"
|
||||||
"github.com/charmbracelet/bubbles/table"
|
"code.humancabbage.net/sam/moonmath/config"
|
||||||
tea "github.com/charmbracelet/bubbletea"
|
"code.humancabbage.net/sam/moonmath/tui"
|
||||||
"github.com/charmbracelet/lipgloss"
|
"github.com/alecthomas/kong"
|
||||||
)
|
)
|
||||||
|
|
||||||
var baseStyle = lipgloss.NewStyle().
|
var CLI struct {
|
||||||
BorderStyle(lipgloss.NormalBorder()).
|
Asset string `short:"a" default:"BTC" help:"Asset to project."`
|
||||||
BorderForeground(lipgloss.Color("240"))
|
ConfigFile string `short:"c" help:"Path to YAML configuration file."`
|
||||||
|
|
||||||
type model struct {
|
|
||||||
math moon.Math
|
|
||||||
|
|
||||||
prices table.Model
|
|
||||||
projections table.Model
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m model) Init() tea.Cmd {
|
|
||||||
return func() tea.Msg {
|
|
||||||
_ = m.math.Refresh(context.TODO())
|
|
||||||
return m.math
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func initialModel() model {
|
|
||||||
math := moon.NewMath(nil, nil)
|
|
||||||
|
|
||||||
tableStyle := table.DefaultStyles()
|
|
||||||
tableStyle.Selected = lipgloss.NewStyle()
|
|
||||||
prices := table.New(
|
|
||||||
table.WithColumns([]table.Column{
|
|
||||||
{Title: "Price", Width: 9},
|
|
||||||
}),
|
|
||||||
table.WithHeight(1),
|
|
||||||
table.WithStyles(tableStyle),
|
|
||||||
)
|
|
||||||
|
|
||||||
projectionCols := []table.Column{
|
|
||||||
{Title: "Labels", Width: 8},
|
|
||||||
}
|
|
||||||
for i := range math.Columns {
|
|
||||||
projectionCols = append(projectionCols, table.Column{
|
|
||||||
Title: math.Columns[i].Base.Name(),
|
|
||||||
Width: 10,
|
|
||||||
})
|
|
||||||
}
|
|
||||||
projectionRows := make([]table.Row, len(math.Goals)+2)
|
|
||||||
for i := range projectionRows {
|
|
||||||
projectionRows[i] = make(table.Row, len(projectionCols))
|
|
||||||
}
|
|
||||||
projectionRows[0][0] = "Starting"
|
|
||||||
projectionRows[1][0] = "CDPR"
|
|
||||||
for i := range math.Goals {
|
|
||||||
projectionRows[i+2][0] = fmt.Sprintf("$%.0f", math.Goals[i])
|
|
||||||
}
|
|
||||||
projections := table.New(
|
|
||||||
table.WithColumns(projectionCols),
|
|
||||||
table.WithRows(projectionRows),
|
|
||||||
table.WithHeight(len(math.Goals)+2),
|
|
||||||
table.WithStyles(tableStyle),
|
|
||||||
)
|
|
||||||
|
|
||||||
return model{
|
|
||||||
math: math,
|
|
||||||
prices: prices,
|
|
||||||
projections: projections,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
|
||||||
switch msg := msg.(type) {
|
|
||||||
case moon.Math:
|
|
||||||
m.math = msg
|
|
||||||
refillPrice(&m)
|
|
||||||
refillProjections(&m)
|
|
||||||
return m, tea.Tick(time.Second*30, func(t time.Time) tea.Msg {
|
|
||||||
_ = m.math.Refresh(context.TODO())
|
|
||||||
return m.math
|
|
||||||
})
|
|
||||||
case tea.KeyMsg:
|
|
||||||
switch msg.String() {
|
|
||||||
case "ctrl+c", "q", "esc":
|
|
||||||
return m, tea.Quit
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return m, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func refillPrice(m *model) {
|
|
||||||
rows := []table.Row{
|
|
||||||
[]string{fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
|
|
||||||
}
|
|
||||||
m.prices.SetRows(rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
func refillProjections(m *model) {
|
|
||||||
rows := m.projections.Rows()
|
|
||||||
|
|
||||||
for col := range m.math.Columns {
|
|
||||||
_ = col
|
|
||||||
never := false
|
|
||||||
if m.math.Columns[col].CDPR <= 1 {
|
|
||||||
never = true
|
|
||||||
}
|
|
||||||
|
|
||||||
rows[0][col+1] = fmt.Sprintf("$%.2f", m.math.Columns[col].StartingPrice)
|
|
||||||
rows[1][col+1] = fmt.Sprintf("%.2f%%", (m.math.Columns[col].CDPR-1)*100)
|
|
||||||
for row := 0; row < len(m.math.Goals); row++ {
|
|
||||||
var cell string
|
|
||||||
if never {
|
|
||||||
cell = "NEVER!!!!!"
|
|
||||||
} else {
|
|
||||||
cell = m.math.Columns[col].
|
|
||||||
Projections.Dates[row].
|
|
||||||
Format("2006-01-02")
|
|
||||||
}
|
|
||||||
rows[row+2][col+1] = cell
|
|
||||||
}
|
|
||||||
}
|
|
||||||
m.projections.SetRows(rows)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (m model) View() string {
|
|
||||||
var s string
|
|
||||||
s += lipgloss.JoinHorizontal(
|
|
||||||
lipgloss.Top,
|
|
||||||
baseStyle.Render(m.prices.View()),
|
|
||||||
baseStyle.Render(m.projections.View()),
|
|
||||||
)
|
|
||||||
return s + "\n"
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
p := tea.NewProgram(initialModel())
|
ctx := kong.Parse(&CLI)
|
||||||
|
if ctx.Error != nil {
|
||||||
|
fail(ctx.Error)
|
||||||
|
}
|
||||||
|
allCfg, err := config.Load(CLI.ConfigFile)
|
||||||
|
if err != nil {
|
||||||
|
fail(err)
|
||||||
|
}
|
||||||
|
CLI.Asset = strings.ToUpper(CLI.Asset)
|
||||||
|
cfg, err := allCfg.GetData(coindesk.Asset(CLI.Asset))
|
||||||
|
if err != nil {
|
||||||
|
fail(err)
|
||||||
|
}
|
||||||
|
p := tui.New(cfg)
|
||||||
if _, err := p.Run(); err != nil {
|
if _, err := p.Run(); err != nil {
|
||||||
|
fail(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func fail(err error) {
|
||||||
fmt.Printf("program error: %v\n", err)
|
fmt.Printf("program error: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
BIN
screenshot.png
BIN
screenshot.png
Binary file not shown.
Before Width: | Height: | Size: 51 KiB After Width: | Height: | Size: 38 KiB |
202
tui/tui.go
Normal file
202
tui/tui.go
Normal file
@@ -0,0 +1,202 @@
|
|||||||
|
package tui
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"code.humancabbage.net/sam/moonmath/config"
|
||||||
|
"code.humancabbage.net/sam/moonmath/moon"
|
||||||
|
"github.com/charmbracelet/bubbles/spinner"
|
||||||
|
"github.com/charmbracelet/bubbles/table"
|
||||||
|
tea "github.com/charmbracelet/bubbletea"
|
||||||
|
"github.com/charmbracelet/lipgloss"
|
||||||
|
)
|
||||||
|
|
||||||
|
func New(cfg config.Data) (p *tea.Program) {
|
||||||
|
p = tea.NewProgram(
|
||||||
|
newModel(cfg),
|
||||||
|
tea.WithAltScreen(),
|
||||||
|
tea.WithFPS(30),
|
||||||
|
)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
type Model struct {
|
||||||
|
math moon.Math
|
||||||
|
|
||||||
|
refreshing bool
|
||||||
|
indicator spinner.Model
|
||||||
|
|
||||||
|
properties table.Model
|
||||||
|
projections table.Model
|
||||||
|
}
|
||||||
|
|
||||||
|
func newModel(cfg config.Data) (m Model) {
|
||||||
|
m.math = moon.NewMath(
|
||||||
|
cfg.Asset,
|
||||||
|
cfg.Goals,
|
||||||
|
config.GetBases(&cfg),
|
||||||
|
)
|
||||||
|
|
||||||
|
tableStyle := table.DefaultStyles()
|
||||||
|
tableStyle.Selected = tableStyle.Cell.Copy().
|
||||||
|
Padding(0)
|
||||||
|
tableStyle.Header = tableStyle.Header.
|
||||||
|
Bold(true).
|
||||||
|
Foreground(lipgloss.Color("214")).
|
||||||
|
Border(baseStyle.GetBorderStyle(), false, false, true, false)
|
||||||
|
|
||||||
|
// properties table
|
||||||
|
|
||||||
|
m.properties = table.New(
|
||||||
|
table.WithColumns([]table.Column{
|
||||||
|
{Title: "Property", Width: 9},
|
||||||
|
{Title: "Value", Width: 9},
|
||||||
|
}),
|
||||||
|
table.WithHeight(2),
|
||||||
|
table.WithStyles(tableStyle),
|
||||||
|
)
|
||||||
|
|
||||||
|
// projections table
|
||||||
|
|
||||||
|
labelsWidth := 0
|
||||||
|
for _, l := range m.math.Labels {
|
||||||
|
if len(l) > labelsWidth {
|
||||||
|
labelsWidth = len(l)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
projectionCols := []table.Column{
|
||||||
|
{Title: "Labels", Width: labelsWidth},
|
||||||
|
}
|
||||||
|
for _, c := range m.math.Columns {
|
||||||
|
projectionCols = append(projectionCols,
|
||||||
|
table.Column{
|
||||||
|
Title: c.Base.Label(),
|
||||||
|
Width: 10,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
m.projections = table.New(
|
||||||
|
table.WithColumns(projectionCols),
|
||||||
|
table.WithHeight(len(m.math.Labels)),
|
||||||
|
table.WithStyles(tableStyle),
|
||||||
|
)
|
||||||
|
|
||||||
|
// indicator spinner
|
||||||
|
|
||||||
|
m.indicator = spinner.New()
|
||||||
|
m.indicator.Spinner = spinner.Points
|
||||||
|
m.indicator.Style = lipgloss.NewStyle().
|
||||||
|
Foreground(lipgloss.Color("69"))
|
||||||
|
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) Init() tea.Cmd {
|
||||||
|
return tea.Batch(
|
||||||
|
m.indicator.Tick,
|
||||||
|
func() tea.Msg {
|
||||||
|
return refresh{}
|
||||||
|
},
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
|
||||||
|
switch msg := msg.(type) {
|
||||||
|
case refresh:
|
||||||
|
m.refreshing = true
|
||||||
|
return m, func() tea.Msg {
|
||||||
|
// TODO: log errors
|
||||||
|
_ = m.math.Refresh(context.TODO())
|
||||||
|
return m.math
|
||||||
|
}
|
||||||
|
case moon.Math:
|
||||||
|
m.math = msg
|
||||||
|
refillProperties(&m)
|
||||||
|
refillProjections(&m)
|
||||||
|
return m, tea.Batch(
|
||||||
|
// schedule the next refresh
|
||||||
|
tea.Tick(time.Second*30,
|
||||||
|
func(t time.Time) tea.Msg {
|
||||||
|
return refresh{}
|
||||||
|
}),
|
||||||
|
// wait a bit to stop the indicator, so that it's more obvious
|
||||||
|
// even when the refresh completes quickly.
|
||||||
|
tea.Tick(time.Millisecond*500,
|
||||||
|
func(t time.Time) tea.Msg {
|
||||||
|
return stopIndicator{}
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
case stopIndicator:
|
||||||
|
m.refreshing = false
|
||||||
|
return m, nil
|
||||||
|
case spinner.TickMsg:
|
||||||
|
var cmd tea.Cmd
|
||||||
|
m.indicator, cmd = m.indicator.Update(msg)
|
||||||
|
return m, cmd
|
||||||
|
case tea.KeyMsg:
|
||||||
|
switch msg.String() {
|
||||||
|
case "ctrl+c", "q", "esc":
|
||||||
|
return m, tea.Quit
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return m, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
type refresh struct{}
|
||||||
|
type stopIndicator struct{}
|
||||||
|
|
||||||
|
func refillProperties(m *Model) {
|
||||||
|
rows := []table.Row{
|
||||||
|
{"Asset", string(m.math.Asset)},
|
||||||
|
{"Price", fmt.Sprintf("$%0.2f", m.math.CurrentPrice)},
|
||||||
|
}
|
||||||
|
m.properties.SetRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
func refillProjections(m *Model) {
|
||||||
|
rows := []table.Row{m.math.Labels}
|
||||||
|
for i := range m.math.Columns {
|
||||||
|
rows = append(rows, m.math.Columns[i].Column())
|
||||||
|
}
|
||||||
|
rows = transpose(rows)
|
||||||
|
m.projections.SetRows(rows)
|
||||||
|
}
|
||||||
|
|
||||||
|
func transpose(slice []table.Row) []table.Row {
|
||||||
|
xl := len(slice[0])
|
||||||
|
yl := len(slice)
|
||||||
|
result := make([]table.Row, xl)
|
||||||
|
for i := range result {
|
||||||
|
result[i] = make(table.Row, yl)
|
||||||
|
}
|
||||||
|
for i := 0; i < xl; i++ {
|
||||||
|
for j := 0; j < yl; j++ {
|
||||||
|
result[i][j] = slice[j][i]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
}
|
||||||
|
|
||||||
|
func (m Model) View() string {
|
||||||
|
var s string
|
||||||
|
indicator := ""
|
||||||
|
if m.refreshing {
|
||||||
|
indicator = m.indicator.View()
|
||||||
|
}
|
||||||
|
right := lipgloss.JoinVertical(
|
||||||
|
lipgloss.Center,
|
||||||
|
baseStyle.Render(m.properties.View()),
|
||||||
|
indicator,
|
||||||
|
)
|
||||||
|
s += lipgloss.JoinHorizontal(
|
||||||
|
lipgloss.Center,
|
||||||
|
right,
|
||||||
|
baseStyle.Render(m.projections.View()),
|
||||||
|
)
|
||||||
|
return s + "\n"
|
||||||
|
}
|
||||||
|
|
||||||
|
var baseStyle = lipgloss.NewStyle().
|
||||||
|
BorderStyle(lipgloss.RoundedBorder()).
|
||||||
|
BorderForeground(lipgloss.Color("240"))
|
Reference in New Issue
Block a user