Skip to content

Commit 4bbb1a3

Browse files
authored
chore: update GetBalance Kaspa (#1363)
1 parent 4cccf4d commit 4bbb1a3

File tree

1 file changed

+24
-54
lines changed

1 file changed

+24
-54
lines changed

data_layer/kaspa/kaspa.go

Lines changed: 24 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package kaspa
22

33
import (
4-
"bytes"
54
"encoding/json"
65
"fmt"
76
"io"
@@ -17,9 +16,11 @@ import (
1716
)
1817

1918
const (
20-
ConfigFileName = "kaspa.toml"
21-
MnemonicEntropySize = 256
22-
requiredKAS = 1
19+
ConfigFileName = "kaspa.toml"
20+
MnemonicEntropySize = 256
21+
requiredKAS = 1
22+
defaultKaspaTestnetGRPC = "185.69.54.99:16210"
23+
defaultKaspaMainnetGRPC = "91.84.65.9:16210"
2324
)
2425

2526
type Kaspa struct {
@@ -49,10 +50,12 @@ func NewKaspa(root string) *Kaspa {
4950
if err != nil {
5051
if rollerData.HubData.Environment == "mainnet" {
5152
daNetwork = string(consts.KaspaMainnet)
52-
kaspaConfig.Network = "mainnet"
53+
kaspaConfig.Network = "kaspa-mainnet"
54+
kaspaConfig.GrpcAddress = defaultKaspaMainnetGRPC
5355
} else {
5456
daNetwork = string(consts.KaspaTestnet)
55-
kaspaConfig.Network = "testnet"
57+
kaspaConfig.Network = "kaspa-testnet-10"
58+
kaspaConfig.GrpcAddress = defaultKaspaTestnetGRPC
5659
}
5760

5861
daData, exists := consts.DaNetworks[daNetwork]
@@ -72,8 +75,6 @@ func NewKaspa(root string) *Kaspa {
7275
kaspaConfig.GrpcAddress, _ = pterm.DefaultInteractiveTextInput.WithDefaultText(
7376
"> Enter your gRPC endpoint",
7477
).Show()
75-
} else {
76-
kaspaConfig.GrpcAddress = "localhost:16210"
7778
}
7879

7980
kaspaConfig.Address, _ = pterm.DefaultInteractiveTextInput.WithDefaultText(
@@ -199,65 +200,34 @@ func (k *Kaspa) GetAppID() uint32 {
199200
return 0
200201
}
201202

202-
type GetUtxosParams struct {
203+
type KaspaBalanceResponse struct {
203204
Address string `json:"address"`
204-
}
205-
206-
type JsonRpcRequest struct {
207-
Jsonrpc string `json:"jsonrpc"`
208-
Method string `json:"method"`
209-
Params GetUtxosParams `json:"params"`
210-
ID int `json:"id"`
211-
}
212-
213-
type UTXO struct {
214-
Amount uint64 `json:"amount"`
215-
}
216-
217-
type JsonRpcResponse struct {
218-
Result struct {
219-
Entries []UTXO `json:"entries"`
220-
} `json:"result"`
221-
Error interface{} `json:"error"`
205+
Balance uint64 `json:"balance"` // Đơn vị: sompi
222206
}
223207

224208
func (k *Kaspa) getBalance() (uint64, error) {
225-
reqBody := JsonRpcRequest{
226-
Jsonrpc: "2.0",
227-
Method: "getUtxosByAddress",
228-
Params: GetUtxosParams{Address: k.Address},
229-
ID: 1,
230-
}
209+
url := fmt.Sprintf("%s/addresses/%s/balance", k.ApiUrl, k.Address)
231210

232-
body, err := json.Marshal(reqBody)
211+
resp, err := http.Get(url)
233212
if err != nil {
234-
return 0, err
235-
}
236-
237-
resp, err := http.Post(k.ApiUrl, "application/json", bytes.NewBuffer(body))
238-
if err != nil {
239-
return 0, err
213+
return 0, fmt.Errorf("failed to call Kaspa API: %w", err)
240214
}
241215
defer resp.Body.Close()
242216

243-
respData, err := io.ReadAll(resp.Body)
244-
if err != nil {
245-
return 0, err
246-
}
247-
248-
var rpcResp JsonRpcResponse
249-
if err := json.Unmarshal(respData, &rpcResp); err != nil {
250-
return 0, err
217+
if resp.StatusCode != 200 {
218+
return 0, fmt.Errorf("Kaspa API returned status: %s", resp.Status)
251219
}
252220

253-
if rpcResp.Error != nil {
254-
return 0, fmt.Errorf("RPC error: %v", rpcResp.Error)
221+
body, err := io.ReadAll(resp.Body)
222+
if err != nil {
223+
return 0, fmt.Errorf("failed to read response body: %w", err)
255224
}
256225

257-
var total uint64 = 0
258-
for _, utxo := range rpcResp.Result.Entries {
259-
total += utxo.Amount
226+
var data KaspaBalanceResponse
227+
err = json.Unmarshal(body, &data)
228+
if err != nil {
229+
return 0, fmt.Errorf("failed to parse JSON: %w", err)
260230
}
261231

262-
return total, nil
232+
return data.Balance, nil
263233
}

0 commit comments

Comments
 (0)