|
| 1 | +// Copyright 2025 Shift Crypto AG |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package blockbook |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + "encoding/json" |
| 20 | + "fmt" |
| 21 | + "io" |
| 22 | + "math/big" |
| 23 | + "net/http" |
| 24 | + "net/url" |
| 25 | + "path" |
| 26 | + |
| 27 | + "github.com/BitBoxSwiss/bitbox-wallet-app/backend/accounts" |
| 28 | + "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth/erc20" |
| 29 | + "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth/rpcclient" |
| 30 | + ethtypes "github.com/BitBoxSwiss/bitbox-wallet-app/backend/coins/eth/types" |
| 31 | + "github.com/BitBoxSwiss/bitbox-wallet-app/util/errp" |
| 32 | + "github.com/ethereum/go-ethereum" |
| 33 | + "github.com/ethereum/go-ethereum/common" |
| 34 | + "github.com/ethereum/go-ethereum/core/types" |
| 35 | + "golang.org/x/time/rate" |
| 36 | +) |
| 37 | + |
| 38 | +// callsPerSec is the number of blockbook requests allowed |
| 39 | +// per second. |
| 40 | +// TODO bznein determine a better value for this. |
| 41 | +const callsPerSec = 3.8 |
| 42 | + |
| 43 | +// Blockbook is a rate-limited blockbook api client. |
| 44 | +type Blockbook struct { |
| 45 | + url string |
| 46 | + httpClient *http.Client |
| 47 | + limiter *rate.Limiter |
| 48 | +} |
| 49 | + |
| 50 | +// NewBlockbook creates a new instance of EtherScan. |
| 51 | +func NewBlockbook(chainId string, httpClient *http.Client) *Blockbook { |
| 52 | + // TODO chainID is not used here, do we have blockbook running for SEPETH as well? |
| 53 | + return &Blockbook{ |
| 54 | + url: "https://bb1.shiftcrypto.io/api/", |
| 55 | + httpClient: httpClient, |
| 56 | + limiter: rate.NewLimiter(rate.Limit(callsPerSec), 1), |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func (blockbook *Blockbook) call(ctx context.Context, handler string, params url.Values, result interface{}) error { |
| 61 | + if err := blockbook.limiter.Wait(ctx); err != nil { |
| 62 | + return errp.WithStack(err) |
| 63 | + } |
| 64 | + |
| 65 | + reqUrl := blockbook.url + handler + "?" + params.Encode() |
| 66 | + |
| 67 | + response, err := blockbook.httpClient.Get(reqUrl) |
| 68 | + if err != nil { |
| 69 | + return errp.WithStack(err) |
| 70 | + } |
| 71 | + defer func() { _ = response.Body.Close() }() |
| 72 | + if response.StatusCode != http.StatusOK { |
| 73 | + return errp.Newf("expected 200 OK, got %d", response.StatusCode) |
| 74 | + } |
| 75 | + body, err := io.ReadAll(response.Body) |
| 76 | + if err != nil { |
| 77 | + return errp.WithStack(err) |
| 78 | + } |
| 79 | + if err := json.Unmarshal(body, result); err != nil { |
| 80 | + return errp.Newf("unexpected response from blockbook: %s", string(body)) |
| 81 | + } |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +func (blockbook *Blockbook) address(ctx context.Context, account common.Address, result interface{}) error { |
| 87 | + params := url.Values{} |
| 88 | + address := account.Hex() |
| 89 | + |
| 90 | + if err := blockbook.call(ctx, path.Join("address", address), params, result); err != nil { |
| 91 | + return errp.WithStack(err) |
| 92 | + } |
| 93 | + |
| 94 | + return nil |
| 95 | +} |
| 96 | + |
| 97 | +// Balance implements rpc.Interface. |
| 98 | +func (blockbook *Blockbook) Balance(ctx context.Context, account common.Address) (*big.Int, error) { |
| 99 | + result := struct { |
| 100 | + Balance string `json:"balance"` |
| 101 | + }{} |
| 102 | + |
| 103 | + if err := blockbook.address(ctx, account, &result); err != nil { |
| 104 | + return nil, errp.WithStack(err) |
| 105 | + } |
| 106 | + |
| 107 | + balance, ok := new(big.Int).SetString(result.Balance, 10) |
| 108 | + if !ok { |
| 109 | + return nil, errp.Newf("could not parse balance %q", result.Balance) |
| 110 | + } |
| 111 | + return balance, nil |
| 112 | + |
| 113 | +} |
| 114 | + |
| 115 | +// BlockNumber implements rpc.Interface. |
| 116 | +func (blockbook *Blockbook) BlockNumber(ctx context.Context) (*big.Int, error) { |
| 117 | + return nil, fmt.Errorf("Not yet implemented") |
| 118 | +} |
| 119 | + |
| 120 | +// PendingNonceAt implements rpc.Interface. |
| 121 | +func (blockbook *Blockbook) PendingNonceAt(ctx context.Context, account common.Address) (uint64, error) { |
| 122 | + return 0, fmt.Errorf("Not yet implemented") |
| 123 | +} |
| 124 | + |
| 125 | +// Transactions implement TransactionSource. |
| 126 | +func (blockbook *Blockbook) Transactions(blockTipHeight *big.Int, address common.Address, endBlock *big.Int, erc20Token *erc20.Token) ([]*accounts.TransactionData, error) { |
| 127 | + return nil, fmt.Errorf("Not yet implemented") |
| 128 | +} |
| 129 | + |
| 130 | +// SendTransaction implements rpc.Interface. |
| 131 | +func (blockbook *Blockbook) SendTransaction(ctx context.Context, tx *types.Transaction) error { |
| 132 | + return fmt.Errorf("Not yet implemented") |
| 133 | +} |
| 134 | + |
| 135 | +// ERC20Balance implements rpc.Interface. |
| 136 | +func (blockbook *Blockbook) ERC20Balance(account common.Address, erc20Token *erc20.Token) (*big.Int, error) { |
| 137 | + return nil, fmt.Errorf("Not yet implemented") |
| 138 | +} |
| 139 | + |
| 140 | +// EstimateGas implements rpc.Interface. |
| 141 | +func (blockbook *Blockbook) EstimateGas(ctx context.Context, call ethereum.CallMsg) (gas uint64, err error) { |
| 142 | + return 0, fmt.Errorf("Not yet implemented") |
| 143 | +} |
| 144 | + |
| 145 | +// FeeTargets implements rpc.Interface. |
| 146 | +func (blockbook *Blockbook) FeeTargets(ctx context.Context) ([]*ethtypes.FeeTarget, error) { |
| 147 | + return nil, fmt.Errorf("Not yet implemented") |
| 148 | +} |
| 149 | + |
| 150 | +// SuggestGasPrice implements rpc.Interface. |
| 151 | +func (blockbook *Blockbook) SuggestGasPrice(ctx context.Context) (*big.Int, error) { |
| 152 | + return nil, fmt.Errorf("Not yet implemented") |
| 153 | +} |
| 154 | + |
| 155 | +// TransactionByHash implements rpc.Interface. |
| 156 | +func (blockbook *Blockbook) TransactionByHash(ctx context.Context, hash common.Hash) (tx *types.Transaction, isPending bool, err error) { |
| 157 | + return nil, false, fmt.Errorf("Not yet implemented") |
| 158 | +} |
| 159 | + |
| 160 | +// TransactionReceiptWithBlockNumber implements rpcclient.Interface. |
| 161 | +func (blockbook *Blockbook) TransactionReceiptWithBlockNumber(ctx context.Context, hash common.Hash) (*rpcclient.RPCTransactionReceipt, error) { |
| 162 | + return nil, fmt.Errorf("Not yet implemented") |
| 163 | +} |
0 commit comments