-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathservice.go
59 lines (48 loc) · 1.58 KB
/
service.go
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
59
package balance
import (
"github.com/MinterTeam/minter-explorer-api/v2/helpers"
"github.com/MinterTeam/minter-explorer-api/v2/pool"
"github.com/MinterTeam/minter-explorer-api/v2/services"
"github.com/MinterTeam/minter-explorer-extender/v2/models"
"github.com/MinterTeam/minter-go-node/formula"
"math/big"
)
type Service struct {
baseCoin string
poolService *pool.Service
swapService *services.SwapService
}
func NewService(baseCoin string, poolService *pool.Service, swapService *services.SwapService) *Service {
return &Service{baseCoin, poolService, swapService}
}
func (s *Service) GetTotalBalance(address *models.Address) *big.Int {
sum := big.NewInt(0)
for _, balance := range address.Balances {
sum = sum.Add(sum, s.swapService.EstimateInBip(balance.Coin, helpers.StringToBigInt(balance.Value)))
}
return sum
}
func (s *Service) GetStakeBalance(stakes []models.Stake) *big.Int {
sum := big.NewInt(0)
for _, stake := range stakes {
if stake.IsKicked || stake.Coin.Crr == 0 {
continue
}
// just add base coin to sum
if stake.CoinID == 0 {
sum = sum.Add(sum, helpers.StringToBigInt(stake.Value))
continue
}
// calculate the sale return value for custom coin
sum = sum.Add(sum, formula.CalculateSaleReturn(
helpers.StringToBigInt(stake.Coin.Volume),
helpers.StringToBigInt(stake.Coin.Reserve),
uint(stake.Coin.Crr),
helpers.StringToBigInt(stake.Value),
))
}
return sum
}
func (s *Service) GetTotalBalanceInUSD(sumInBasecoin *big.Int) *big.Float {
return new(big.Float).Mul(new(big.Float).SetInt(sumInBasecoin), s.poolService.GetCoinPrice(0))
}