|
| 1 | +// SPDX-License-Identifier: Apache-2.0 |
| 2 | + |
| 3 | +//go:build tinygo || wasip1 |
| 4 | + |
| 5 | +package main |
| 6 | + |
| 7 | +import ( |
| 8 | + "github.com/simonovic86/igor/sdk/igor" |
| 9 | +) |
| 10 | + |
| 11 | +// stateSize is the fixed checkpoint size in bytes: |
| 12 | +// TickCount(8) + BirthNano(8) + LastNano(8) + |
| 13 | +// BTCLatest(8) + BTCHigh(8) + BTCLow(8) + |
| 14 | +// ETHLatest(8) + ETHHigh(8) + ETHLow(8) + |
| 15 | +// ObservationCount(4) + ErrorCount(4) |
| 16 | +const stateSize = 80 |
| 17 | + |
| 18 | +const apiURL = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,ethereum&vs_currencies=usd" |
| 19 | + |
| 20 | +// fetchInterval is how many ticks between API calls (CoinGecko free tier: ~30 req/min). |
| 21 | +const fetchInterval = 10 |
| 22 | + |
| 23 | +// PriceWatcher fetches BTC and ETH prices from CoinGecko on every tick, |
| 24 | +// tracks all-time high/low, and logs price updates. All state survives |
| 25 | +// checkpoint/resume across machines — proving portable, immortal agents |
| 26 | +// that accumulate real-world knowledge over time. |
| 27 | +type PriceWatcher struct { |
| 28 | + TickCount uint64 |
| 29 | + BirthNano int64 |
| 30 | + LastNano int64 |
| 31 | + BTCLatest uint64 // price in millicents (USD × 100) |
| 32 | + BTCHigh uint64 |
| 33 | + BTCLow uint64 |
| 34 | + ETHLatest uint64 |
| 35 | + ETHHigh uint64 |
| 36 | + ETHLow uint64 |
| 37 | + ObservationCount uint32 |
| 38 | + ErrorCount uint32 |
| 39 | +} |
| 40 | + |
| 41 | +func (p *PriceWatcher) Init() {} |
| 42 | + |
| 43 | +func (p *PriceWatcher) Tick() bool { |
| 44 | + p.TickCount++ |
| 45 | + |
| 46 | + now := igor.ClockNow() |
| 47 | + if p.BirthNano == 0 { |
| 48 | + p.BirthNano = now |
| 49 | + } |
| 50 | + p.LastNano = now |
| 51 | + |
| 52 | + ageSec := (p.LastNano - p.BirthNano) / 1_000_000_000 |
| 53 | + |
| 54 | + // Fetch prices on tick 1 and every fetchInterval ticks after that. |
| 55 | + if p.TickCount == 1 || p.TickCount%fetchInterval == 0 { |
| 56 | + status, body, err := igor.HTTPGet(apiURL) |
| 57 | + if err != nil { |
| 58 | + p.ErrorCount++ |
| 59 | + igor.Logf("[pricewatcher] tick=%d ERROR: http failed: %s (errors=%d)", |
| 60 | + p.TickCount, err.Error(), p.ErrorCount) |
| 61 | + return false |
| 62 | + } |
| 63 | + if status < 200 || status >= 300 { |
| 64 | + p.ErrorCount++ |
| 65 | + igor.Logf("[pricewatcher] tick=%d ERROR: http status %d (errors=%d)", |
| 66 | + p.TickCount, status, p.ErrorCount) |
| 67 | + return false |
| 68 | + } |
| 69 | + |
| 70 | + btc := extractPrice(string(body), "bitcoin") |
| 71 | + eth := extractPrice(string(body), "ethereum") |
| 72 | + |
| 73 | + if btc == 0 && eth == 0 { |
| 74 | + p.ErrorCount++ |
| 75 | + igor.Logf("[pricewatcher] tick=%d ERROR: could not parse prices (errors=%d)", |
| 76 | + p.TickCount, p.ErrorCount) |
| 77 | + return false |
| 78 | + } |
| 79 | + |
| 80 | + p.ObservationCount++ |
| 81 | + |
| 82 | + if btc > 0 { |
| 83 | + p.BTCLatest = btc |
| 84 | + if p.BTCHigh == 0 || btc > p.BTCHigh { |
| 85 | + p.BTCHigh = btc |
| 86 | + } |
| 87 | + if p.BTCLow == 0 || btc < p.BTCLow { |
| 88 | + p.BTCLow = btc |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + if eth > 0 { |
| 93 | + p.ETHLatest = eth |
| 94 | + if p.ETHHigh == 0 || eth > p.ETHHigh { |
| 95 | + p.ETHHigh = eth |
| 96 | + } |
| 97 | + if p.ETHLow == 0 || eth < p.ETHLow { |
| 98 | + p.ETHLow = eth |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + igor.Logf("[pricewatcher] tick=%d age=%ds obs=%d FETCH | BTC=$%d.%02d (high=$%d.%02d low=$%d.%02d) | ETH=$%d.%02d (high=$%d.%02d low=$%d.%02d)", |
| 103 | + p.TickCount, ageSec, p.ObservationCount, |
| 104 | + p.BTCLatest/100, p.BTCLatest%100, p.BTCHigh/100, p.BTCHigh%100, p.BTCLow/100, p.BTCLow%100, |
| 105 | + p.ETHLatest/100, p.ETHLatest%100, p.ETHHigh/100, p.ETHHigh%100, p.ETHLow/100, p.ETHLow%100) |
| 106 | + } else if p.TickCount%5 == 0 { |
| 107 | + // Log a heartbeat every 5 ticks with cached prices. |
| 108 | + igor.Logf("[pricewatcher] tick=%d age=%ds obs=%d | BTC=$%d.%02d | ETH=$%d.%02d", |
| 109 | + p.TickCount, ageSec, p.ObservationCount, |
| 110 | + p.BTCLatest/100, p.BTCLatest%100, |
| 111 | + p.ETHLatest/100, p.ETHLatest%100) |
| 112 | + } |
| 113 | + |
| 114 | + if p.TickCount%10 == 0 { |
| 115 | + igor.Logf("[pricewatcher] MILESTONE: %d observations across %ds — this agent remembers everything", |
| 116 | + p.ObservationCount, ageSec) |
| 117 | + } |
| 118 | + |
| 119 | + return false |
| 120 | +} |
| 121 | + |
| 122 | +func (p *PriceWatcher) Marshal() []byte { |
| 123 | + return igor.NewEncoder(stateSize). |
| 124 | + Uint64(p.TickCount). |
| 125 | + Int64(p.BirthNano). |
| 126 | + Int64(p.LastNano). |
| 127 | + Uint64(p.BTCLatest). |
| 128 | + Uint64(p.BTCHigh). |
| 129 | + Uint64(p.BTCLow). |
| 130 | + Uint64(p.ETHLatest). |
| 131 | + Uint64(p.ETHHigh). |
| 132 | + Uint64(p.ETHLow). |
| 133 | + Uint32(p.ObservationCount). |
| 134 | + Uint32(p.ErrorCount). |
| 135 | + Finish() |
| 136 | +} |
| 137 | + |
| 138 | +func (p *PriceWatcher) Unmarshal(data []byte) { |
| 139 | + d := igor.NewDecoder(data) |
| 140 | + p.TickCount = d.Uint64() |
| 141 | + p.BirthNano = d.Int64() |
| 142 | + p.LastNano = d.Int64() |
| 143 | + p.BTCLatest = d.Uint64() |
| 144 | + p.BTCHigh = d.Uint64() |
| 145 | + p.BTCLow = d.Uint64() |
| 146 | + p.ETHLatest = d.Uint64() |
| 147 | + p.ETHHigh = d.Uint64() |
| 148 | + p.ETHLow = d.Uint64() |
| 149 | + p.ObservationCount = d.Uint32() |
| 150 | + p.ErrorCount = d.Uint32() |
| 151 | + if err := d.Err(); err != nil { |
| 152 | + panic("unmarshal checkpoint: " + err.Error()) |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +// extractPrice parses a CoinGecko simple/price JSON response to find |
| 157 | +// the USD price for a given coin. Returns price in cents (USD × 100). |
| 158 | +// Uses simple string scanning — no encoding/json needed (TinyGo-safe). |
| 159 | +// |
| 160 | +// Expected format: {"bitcoin":{"usd":83456.78},"ethereum":{"usd":1923.45}} |
| 161 | +func extractPrice(body, coin string) uint64 { |
| 162 | + // Find "bitcoin":{"usd": or "ethereum":{"usd": |
| 163 | + key := `"` + coin + `":{"usd":` |
| 164 | + idx := indexOf(body, key) |
| 165 | + if idx < 0 { |
| 166 | + return 0 |
| 167 | + } |
| 168 | + |
| 169 | + // Skip past the key to the number. |
| 170 | + start := idx + len(key) |
| 171 | + if start >= len(body) { |
| 172 | + return 0 |
| 173 | + } |
| 174 | + |
| 175 | + // Parse the number (integer part and optional decimal). |
| 176 | + var whole uint64 |
| 177 | + var frac uint64 |
| 178 | + var fracDigits int |
| 179 | + inFrac := false |
| 180 | + i := start |
| 181 | + |
| 182 | + // Skip whitespace. |
| 183 | + for i < len(body) && body[i] == ' ' { |
| 184 | + i++ |
| 185 | + } |
| 186 | + |
| 187 | + for i < len(body) { |
| 188 | + c := body[i] |
| 189 | + if c >= '0' && c <= '9' { |
| 190 | + if inFrac { |
| 191 | + if fracDigits < 2 { |
| 192 | + frac = frac*10 + uint64(c-'0') |
| 193 | + fracDigits++ |
| 194 | + } |
| 195 | + // Skip additional decimal digits. |
| 196 | + } else { |
| 197 | + whole = whole*10 + uint64(c-'0') |
| 198 | + } |
| 199 | + } else if c == '.' && !inFrac { |
| 200 | + inFrac = true |
| 201 | + } else { |
| 202 | + break |
| 203 | + } |
| 204 | + i++ |
| 205 | + } |
| 206 | + |
| 207 | + // Pad fractional part to 2 digits. |
| 208 | + for fracDigits < 2 { |
| 209 | + frac *= 10 |
| 210 | + fracDigits++ |
| 211 | + } |
| 212 | + |
| 213 | + return whole*100 + frac |
| 214 | +} |
| 215 | + |
| 216 | +// indexOf returns the index of the first occurrence of needle in s, or -1. |
| 217 | +func indexOf(s, needle string) int { |
| 218 | + if len(needle) > len(s) { |
| 219 | + return -1 |
| 220 | + } |
| 221 | + for i := 0; i <= len(s)-len(needle); i++ { |
| 222 | + match := true |
| 223 | + for j := 0; j < len(needle); j++ { |
| 224 | + if s[i+j] != needle[j] { |
| 225 | + match = false |
| 226 | + break |
| 227 | + } |
| 228 | + } |
| 229 | + if match { |
| 230 | + return i |
| 231 | + } |
| 232 | + } |
| 233 | + return -1 |
| 234 | +} |
| 235 | + |
| 236 | +func init() { igor.Run(&PriceWatcher{}) } |
| 237 | +func main() {} |
0 commit comments