-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathget-balance-of.go
More file actions
58 lines (48 loc) · 1.86 KB
/
Copy pathget-balance-of.go
File metadata and controls
58 lines (48 loc) · 1.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package usecases
import (
"errors"
"github.com/0xfbravo/brla/abi"
"github.com/0xfbravo/brla/model"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/params"
"go.uber.org/zap"
)
// GetBalanceOf retrieves the balance of a wallet directly from the blockchain
func (u *Impl) GetBalanceOf(options *model.BalanceOfOptions) (*model.BalanceOf, error) {
u.log.Info("Retrieving balanceOf(wallet)", zap.Any("options", options))
tokenAddresses, err := u.GetContractAddress(options.Chain)
if err != nil {
u.log.Error("Failed to get contract address", zap.Error(err))
return nil, err
}
client, err := ethclient.Dial(options.RpcUrl)
if err != nil {
u.log.Error("Failed to connect to the Ethereum client", zap.Error(err))
return nil, errors.New("failed to connect to the Ethereum client")
}
contractAddress := common.HexToAddress(tokenAddresses.BRLATokenAddress)
contract, err := brla.NewBrla(contractAddress, client)
if err != nil {
u.log.Error("Failed to get contract", zap.Error(err))
return nil, err
}
balance, err := contract.BalanceOf(nil, options.Wallet)
if err != nil {
u.log.Error("Failed to get balance", zap.Error(err), zap.Any("options", options))
return nil, err
}
// Convert balance to cents
newWei := new(big.Int).Set(balance)
newWei.Mul(newWei, new(big.Int).SetInt64(100))
// Convert balance to ether
ether := new(big.Float).SetInt(newWei) // Convert big.Int (wei) to big.Float
ether.Quo(ether, big.NewFloat(params.Ether)) // Divide by 10^18 (Ether constant)
etherInt, _ := ether.Int64() // Convert big.Float to big.Int
u.log.Info("Wallet balance retrieved successfully", zap.Any("balance", balance), zap.Any("ether", etherInt), zap.Any("options", options))
return &model.BalanceOf{
Balance: etherInt,
BalanceWei: balance.String(),
}, nil
}