Skip to content

Commit

Permalink
add bitcoin balance endpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
asim committed Jul 15, 2022
1 parent a0ebf2b commit 364bbb4
Show file tree
Hide file tree
Showing 5 changed files with 227 additions and 16 deletions.
12 changes: 12 additions & 0 deletions bitcoin/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,17 @@
"price": 34682.74
}
}
],
"balance": [
{
"title": "Get a bitcoin address balance",
"description": "Returns the balance of a bitcoin address",
"request": {
"address": "1MDUoxL1bGvMxhuoDYx6i11ePytECAk9QK"
},
"response": {
"balance": 4134
}
}
]
}
34 changes: 34 additions & 0 deletions bitcoin/handler/bitcoin.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,40 @@ func New() *Bitcoin {
}
}

func (b *Bitcoin) Balance(ctx context.Context, req *pb.BalanceRequest, rsp *pb.BalanceResponse) error {
if len(req.Address) == 0 {
return errors.BadRequest("bitcoin.balance", "missing address")
}

uri := fmt.Sprintf("https://blockchain.info/balance?active=%s", req.Address)

resp, err := http.Get(uri)
if err != nil {
logger.Errorf("Failed to get balance: %v\n", err)
return errors.InternalServerError("bitcoin.balance", "failed to get price")
}
defer resp.Body.Close()

buf, _ := ioutil.ReadAll(resp.Body)

if resp.StatusCode != 200 {
logger.Errorf("Failed to get price (non 200): %d %v\n", resp.StatusCode, string(buf))
return errors.InternalServerError("bitcoin.balance", "failed to get price")
}

var respBody map[string]interface{}

if err := json.Unmarshal(buf, &respBody); err != nil {
logger.Errorf("Failed to unmarshal balance: %v\n", err)
return errors.InternalServerError("bitcoin.balance", "failed to get price")
}

info := respBody[req.Address].(map[string]interface{})
rsp.Balance = int64(info["final_balance"].(float64))

return nil
}

func (b *Bitcoin) Price(ctx context.Context, req *pb.PriceRequest, rsp *pb.PriceResponse) error {
if len(req.Symbol) <= 0 {
req.Symbol = "BTCUSD"
Expand Down
168 changes: 152 additions & 16 deletions bitcoin/proto/bitcoin.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

17 changes: 17 additions & 0 deletions bitcoin/proto/bitcoin.pb.micro.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions bitcoin/proto/bitcoin.proto
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ option go_package = "./proto;bitcoin";

service Bitcoin {
rpc Price(PriceRequest) returns (PriceResponse) {}
rpc Balance(BalanceRequest) returns (BalanceResponse) {}
}

// Get the price of bitcoin
Expand All @@ -20,3 +21,14 @@ message PriceResponse {
// The price of bitcoin
double price = 2;
}

// Get the BTC balance of an address
message BalanceRequest {
// address to lookup
string address = 1;
}

message BalanceResponse {
// total BTC as satoshis
int64 balance = 1;
}

0 comments on commit 364bbb4

Please sign in to comment.