|
| 1 | +package ticker |
| 2 | + |
| 3 | +import ( |
| 4 | + "crypto/hmac" |
| 5 | + "crypto/sha256" |
| 6 | + "encoding/hex" |
| 7 | + "encoding/json" |
| 8 | + "fmt" |
| 9 | + "io/ioutil" |
| 10 | + "net/http" |
| 11 | + "strings" |
| 12 | + "sync" |
| 13 | + "time" |
| 14 | +) |
| 15 | + |
| 16 | +const ( |
| 17 | + btcavgFiatEndpoint = "https://apiv2.bitcoinaverage.com/indices/global/ticker/all?crypto=BTC" |
| 18 | + btcavgCryptoEndpoint = "https://apiv2.bitcoinaverage.com/indices/crypto/ticker/all" |
| 19 | +) |
| 20 | + |
| 21 | +type btcavgFetcher struct { |
| 22 | + pubkey string |
| 23 | + privkey string |
| 24 | +} |
| 25 | + |
| 26 | +func NewBTCAVGFetcher(pubkey string, privkey string) fetchFn { |
| 27 | + return func() (exchangeRates, error) { |
| 28 | + output := exchangeRates{} |
| 29 | + errCh := make(chan error, 2) |
| 30 | + |
| 31 | + // Request both endpoints and save their responses |
| 32 | + wg := sync.WaitGroup{} |
| 33 | + wg.Add(2) |
| 34 | + go func() { |
| 35 | + defer wg.Done() |
| 36 | + rates, err := fetchBTCAVGResource(btcavgFiatEndpoint, pubkey, privkey) |
| 37 | + if err != nil { |
| 38 | + errCh <- err |
| 39 | + return |
| 40 | + } |
| 41 | + |
| 42 | + formatBTCAVGFiatOutput(output, rates) |
| 43 | + }() |
| 44 | + |
| 45 | + go func() { |
| 46 | + defer wg.Done() |
| 47 | + rates, err := fetchBTCAVGResource(btcavgCryptoEndpoint, pubkey, privkey) |
| 48 | + if err != nil { |
| 49 | + errCh <- err |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + err = formatBTCAVGCryptoOutput(output, rates) |
| 54 | + if err != nil { |
| 55 | + errCh <- err |
| 56 | + return |
| 57 | + } |
| 58 | + }() |
| 59 | + wg.Wait() |
| 60 | + close(errCh) |
| 61 | + |
| 62 | + for err := range errCh { |
| 63 | + return nil, err |
| 64 | + } |
| 65 | + |
| 66 | + return output, nil |
| 67 | + } |
| 68 | +} |
| 69 | + |
| 70 | +// fetchBTCAVGResource gets the response for a given BitcoinAverage endpoint |
| 71 | +func fetchBTCAVGResource(url string, pubkey string, privkey string) (exchangeRates, error) { |
| 72 | + // Create signed request |
| 73 | + req, err := http.NewRequest("GET", url, nil) |
| 74 | + if err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + req.Header.Add("X-signature", createBTCAVGSignature(pubkey, privkey)) |
| 78 | + |
| 79 | + // Send the requests |
| 80 | + resp, err := httpClient.Do(req) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + defer resp.Body.Close() |
| 85 | + |
| 86 | + // Read the response body |
| 87 | + body, err := ioutil.ReadAll(resp.Body) |
| 88 | + if err != nil { |
| 89 | + return nil, err |
| 90 | + } |
| 91 | + |
| 92 | + // Return an error if no success, otherwise deserialize and return our body |
| 93 | + if resp.StatusCode != 200 { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + |
| 97 | + rates := make(exchangeRates) |
| 98 | + err = json.Unmarshal(body, &rates) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + |
| 103 | + return rates, nil |
| 104 | +} |
| 105 | + |
| 106 | +// formatBTCAVGFiatOutput formats BTC->fiat pairs |
| 107 | +func formatBTCAVGFiatOutput(outgoing exchangeRates, incoming exchangeRates) { |
| 108 | + for k, v := range incoming { |
| 109 | + if strings.HasPrefix(k, "BTC") { |
| 110 | + outgoing[strings.TrimPrefix(k, "BTC")] = v |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +// formatBTCAVGCryptoOutput formats BTC->crypto pairs |
| 116 | +func formatBTCAVGCryptoOutput(outgoing exchangeRates, incoming exchangeRates) error { |
| 117 | + for symbol, entry := range incoming { |
| 118 | + trimmedSymbol := strings.TrimSuffix(symbol, "BTC") |
| 119 | + if symbol == trimmedSymbol { |
| 120 | + continue |
| 121 | + } |
| 122 | + symbol := CanonicalizeSymbol(trimmedSymbol) |
| 123 | + |
| 124 | + if !IsCorrectIDForSymbol(symbol, entry.ID) { |
| 125 | + continue |
| 126 | + } |
| 127 | + |
| 128 | + if entry.Ask == "" || entry.Bid == "" || entry.Last == "" { |
| 129 | + continue |
| 130 | + } |
| 131 | + |
| 132 | + ask, err := invertAndFormatPrice(entry.Ask) |
| 133 | + if err != nil { |
| 134 | + return err |
| 135 | + } |
| 136 | + bid, err := invertAndFormatPrice(entry.Bid) |
| 137 | + if err != nil { |
| 138 | + return err |
| 139 | + } |
| 140 | + last, err := invertAndFormatPrice(entry.Last) |
| 141 | + if err != nil { |
| 142 | + return err |
| 143 | + } |
| 144 | + |
| 145 | + outgoing[symbol] = exchangeRate{Ask: ask, Bid: bid, Last: last} |
| 146 | + } |
| 147 | + return nil |
| 148 | +} |
| 149 | + |
| 150 | +func createBTCAVGSignature(pubkey string, privkey string) string { |
| 151 | + // Build payload |
| 152 | + payload := fmt.Sprintf("%d.%s", time.Now().Unix(), pubkey) |
| 153 | + |
| 154 | + // Generate the HMAC-sha256 signature |
| 155 | + mac := hmac.New(sha256.New, []byte(privkey)) |
| 156 | + mac.Write([]byte(payload)) |
| 157 | + signature := hex.EncodeToString(mac.Sum(nil)) |
| 158 | + |
| 159 | + // Return the final payload |
| 160 | + return fmt.Sprintf("%s.%s", payload, signature) |
| 161 | +} |
0 commit comments