Skip to content

Commit

Permalink
Merge pull request #11 from palomachain/fix/get-balance
Browse files Browse the repository at this point in the history
fix: get-balance response parsing
  • Loading branch information
maharifu authored Sep 2, 2024
2 parents ae9a700 + 3fe1231 commit 19ea62e
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 19ea62e

Please sign in to comment.