Skip to content

Commit 5c6f1a9

Browse files
committed
rebase main
2 parents 5a79ceb + 78e1a91 commit 5c6f1a9

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

cmd/zetatool/chains/sui.go

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package chains
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"strconv"
7+
8+
"github.com/block-vision/sui-go-sdk/models"
9+
"github.com/block-vision/sui-go-sdk/sui"
10+
)
11+
12+
const (
13+
// nanoSUIPerSUI is the number of MIST in 1 SUI (10^9)
14+
nanoSUIPerSUI = 1_000_000_000
15+
)
16+
17+
// GetSuiBalance fetches the SUI balance for a given address
18+
// Returns the balance in MIST (1 SUI = 10^9 MIST)
19+
func GetSuiBalance(ctx context.Context, rpcURL string, address string) (uint64, error) {
20+
client := sui.NewSuiClient(rpcURL)
21+
22+
var (
23+
totalBalance uint64
24+
cursor = any(nil)
25+
)
26+
27+
// Query all SUI coins owned by the address
28+
for {
29+
resp, err := client.SuiXGetCoins(ctx, models.SuiXGetCoinsRequest{
30+
Owner: address,
31+
CoinType: "0x2::sui::SUI",
32+
Cursor: cursor,
33+
})
34+
if err != nil {
35+
return 0, err
36+
}
37+
38+
// Sum up balances from all coins
39+
for _, coin := range resp.Data {
40+
balance, err := strconv.ParseUint(coin.Balance, 10, 64)
41+
if err != nil {
42+
return 0, err
43+
}
44+
totalBalance += balance
45+
}
46+
47+
if !resp.HasNextPage {
48+
break
49+
}
50+
cursor = resp.NextCursor
51+
}
52+
53+
return totalBalance, nil
54+
}
55+
56+
// FormatSuiBalance converts MIST to SUI with 9 decimal places
57+
func FormatSuiBalance(mist uint64) string {
58+
suiVal := float64(mist) / nanoSUIPerSUI
59+
return fmt.Sprintf("%.9f", suiVal)
60+
}

cmd/zetatool/chains/ton.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package chains
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/tonkeeper/tongo/ton"
8+
9+
tonrpc "github.com/zeta-chain/node/zetaclient/chains/ton/rpc"
10+
)
11+
12+
const (
13+
// nanoTONPerTON is the number of nanoTON in 1 TON (10^9)
14+
nanoTONPerTON = 1_000_000_000
15+
)
16+
17+
// GetTONGatewayBalance fetches the TON balance of the gateway contract
18+
// Returns the balance in nanoTON (1 TON = 10^9 nanoTON)
19+
func GetTONGatewayBalance(ctx context.Context, rpcURL string, gatewayAddress string) (uint64, error) {
20+
accountID, err := ton.ParseAccountID(gatewayAddress)
21+
if err != nil {
22+
return 0, fmt.Errorf("failed to parse gateway address: %w", err)
23+
}
24+
25+
// Create TON RPC client (chainID 0 is fine for balance queries)
26+
client := tonrpc.New(rpcURL, 0)
27+
28+
account, err := client.GetAccountState(ctx, accountID)
29+
if err != nil {
30+
return 0, fmt.Errorf("failed to get account state: %w", err)
31+
}
32+
33+
return account.Balance, nil
34+
}
35+
36+
// FormatTONBalance converts nanoTON to TON with 9 decimal places
37+
func FormatTONBalance(nanoTON uint64) string {
38+
tonVal := float64(nanoTON) / nanoTONPerTON
39+
return fmt.Sprintf("%.9f", tonVal)
40+
}

0 commit comments

Comments
 (0)