2024-08-26 14:53:07 +02:00
|
|
|
package server
|
2024-08-26 11:10:16 +02:00
|
|
|
|
2024-08-26 12:32:12 +02:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2024-08-26 11:10:16 +02:00
|
|
|
|
2024-08-26 12:32:12 +02:00
|
|
|
"git.grassecon.net/urdt/ussd/config"
|
|
|
|
"git.grassecon.net/urdt/ussd/internal/models"
|
|
|
|
)
|
2024-08-26 11:10:16 +02:00
|
|
|
|
2024-08-27 09:15:11 +02:00
|
|
|
// CheckBalance retrieves the balance for a given public key from the custodial balance API endpoint.
|
|
|
|
// Parameters:
|
|
|
|
// - publicKey: The public key associated with the account whose balance needs to be checked.
|
2024-08-26 12:32:12 +02:00
|
|
|
func CheckBalance(publicKey string) (string, error) {
|
|
|
|
|
|
|
|
resp, err := http.Get(config.BalanceURL + publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return "0.0", err
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
body, err := io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
|
|
|
return "0.0", err
|
|
|
|
}
|
|
|
|
|
|
|
|
var balanceResp models.BalanceResponse
|
|
|
|
err = json.Unmarshal(body, &balanceResp)
|
|
|
|
if err != nil {
|
|
|
|
return "0.0", err
|
|
|
|
}
|
|
|
|
|
|
|
|
balance := balanceResp.Result.Balance
|
|
|
|
return balance, nil
|
|
|
|
}
|