Skip to content

Commit

Permalink
fix: get-balance response parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
maharifu committed Sep 2, 2024
1 parent ae9a700 commit 3fe1231
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,15 @@ type BalanceResponse struct {
Balance CoreCoin `json:"balance"`
}

func getBalance(address string) (amount int64) {
func getBalance(address, denom string) (amount int64) {
cmd := exec.Command(
palomad,
"--node", rpcUrl,
"q", "bank", "balances",
"q", "bank", "balance",
"--output", "json",
"--chain-id", chainID,
address,
denom,
)
res, err := cmd.CombinedOutput()

Expand All @@ -103,6 +104,11 @@ func getBalance(address string) (amount int64) {
if err != nil {
panic(err)
}

if balance.Amount == "" {
return 0
}

amount, err = strconv.ParseInt(balance.Amount, 10, 64)
if err != nil {
panic(err)
Expand Down Expand Up @@ -154,7 +160,7 @@ func (requestLog *RequestLog) dripCoin(denom string) error {
func checkAndUpdateLimit(db *leveldb.DB, account []byte, denom string) error {
address, _ := bech32.ConvertAndEncode("paloma", account)

if getBalance(address) >= amountTable[denom]*2 {
if getBalance(address, denom) >= amountTable[denom]*2 {
return errors.New("amount limit exceeded")
}

Expand Down Expand Up @@ -202,6 +208,7 @@ func createGetCoinsHandler(db *leveldb.DB) http.HandlerFunc {
return func(w http.ResponseWriter, request *http.Request) {
defer func() {
if err := recover(); err != nil {
fmt.Printf("req error: %v\n", err)
http.Error(w, err.(error).Error(), 400)
}
}()
Expand Down

0 comments on commit 3fe1231

Please sign in to comment.