Skip to content

Commit

Permalink
fix nominators for stacking
Browse files Browse the repository at this point in the history
  • Loading branch information
mr-tron committed Jun 15, 2023
1 parent e63ee0c commit 7f1c9f4
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 22 deletions.
5 changes: 5 additions & 0 deletions api/openapi.json
Original file line number Diff line number Diff line change
Expand Up @@ -2094,6 +2094,11 @@
],
"type": "string"
},
"liquid_jetton_master": {
"description": "for liquid staking master account of jetton",
"example": "0:4a91d32d0289bda9813ae00ff7640e6c38fdce76e4583dd6afc463b70c7d767c",
"type": "string"
},
"max_nominators": {
"description": "maximum number of nominators",
"example": 100,
Expand Down
4 changes: 4 additions & 0 deletions api/openapi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3422,6 +3422,10 @@ components:
type: integer
example: 100
description: maximum number of nominators
liquid_jetton_master:
type: string
example: "0:4a91d32d0289bda9813ae00ff7640e6c38fdce76e4583dd6afc463b70c7d767c"
description: "for liquid staking master account of jetton"
PoolImplementation:
type: object
required:
Expand Down
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ require (
github.com/sourcegraph/conc v0.3.0
github.com/stretchr/testify v1.8.1
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269
github.com/tonkeeper/tongo v1.1.2-0.20230613142834-d288ff73f9f6
github.com/tonkeeper/tongo v1.1.2-0.20230614170918-21867e935297
go.opentelemetry.io/otel v1.11.1
go.opentelemetry.io/otel/metric v0.33.0
go.opentelemetry.io/otel/trace v1.11.1
Expand Down Expand Up @@ -61,6 +61,7 @@ require (
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
golang.org/x/exp v0.0.0-20230116083435-1de6713980de // indirect
golang.org/x/mod v0.8.0 // indirect
golang.org/x/net v0.7.0 // indirect
golang.org/x/sync v0.1.0 // indirect
Expand Down
6 changes: 6 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,8 @@ github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269 h1
github.com/tonkeeper/scam_backoffice_rules v0.0.0-20230413094040-a66ab71fb269/go.mod h1:VGp8QednbWkKHcpQVlWyO0XSqAA0cR6d9wEdrDmHbbA=
github.com/tonkeeper/tongo v1.1.2-0.20230613142834-d288ff73f9f6 h1:+j84fDe+Gol48eBFcYI5IqKiKjLVfZSalpzWRGf3rus=
github.com/tonkeeper/tongo v1.1.2-0.20230613142834-d288ff73f9f6/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
github.com/tonkeeper/tongo v1.1.2-0.20230614170918-21867e935297 h1:dH9HmZUn/l+93SeDrLHxzEqod+OdX8313gp/NR+WECE=
github.com/tonkeeper/tongo v1.1.2-0.20230614170918-21867e935297/go.mod h1:LdOBjpUz6vLp1EdX3E0XLNks9YI5XMSqaQahfOMrBEY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
Expand Down Expand Up @@ -316,8 +318,12 @@ golang.org/x/exp v0.0.0-20191227195350-da58074b4299/go.mod h1:2RIsYlXP63K8oxa1u0
golang.org/x/exp v0.0.0-20200119233911-0405dc783f0a/go.mod h1:2RIsYlXP63K8oxa1u096TMicItID8zy7Y6sNkU49FU4=
golang.org/x/exp v0.0.0-20200207192155-f17229e696bd/go.mod h1:J/WKrq2StrnmMY6+EHIKF9dgMWnmCNThgcyBT1FY9mM=
golang.org/x/exp v0.0.0-20200224162631-6cc2880d07d6/go.mod h1:3jZMyOhIsHpP37uCMkUooju7aAi5cS1Q23tOzKc+0MU=
golang.org/x/exp v0.0.0-20221006183845-316c7553db56 h1:BrYbdKcCNjLyrN6aKqXy4hPw9qGI8IATkj4EWv9Q+kQ=
golang.org/x/exp v0.0.0-20221006183845-316c7553db56/go.mod h1:cyybsKvd6eL0RnXn6p/Grxp8F5bW7iYuBgsNCOHpMYE=
golang.org/x/exp v0.0.0-20230116083435-1de6713980de h1:DBWn//IJw30uYCgERoxCg84hWtA97F4wMiKOIh00Uf0=
golang.org/x/exp v0.0.0-20230116083435-1de6713980de/go.mod h1:CxIveKay+FTh1D0yPZemJVgC/95VzuuOLq5Qi4xnoYc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1 h1:k/i9J1pBpvlfR+9QsetwPyERsqu1GIbi967PQMq3Ivc=
golang.org/x/exp v0.0.0-20230522175609-2e198f4a06a1/go.mod h1:V1LtkGg67GoY2N1AnLN78QLrzxkLyJw7RJb1gzOOz9w=
golang.org/x/image v0.0.0-20190227222117-0694c2d4d067/go.mod h1:kZ7UVZpmo3dzQBMxlp+ypCbDeSB+sBbTgSJuh5dn5js=
golang.org/x/image v0.0.0-20190802002840-cff245a6509b/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
Expand Down
6 changes: 3 additions & 3 deletions pkg/addressbook/addressbook.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func NewAddressBook(logger *zap.Logger, addressPath, jettonPath, collectionPath
collections[a] = i
}
}
go getGGWhitelist(collections)
go getGGWhitelist(collections, logger)
for _, v := range getPools(logger) {
a, err := tongo.ParseAccountID(v.Address)
if err != nil {
Expand Down Expand Up @@ -238,12 +238,12 @@ func downloadJson[T any](url string) ([]T, error) {
return data, nil
}

func getGGWhitelist(collections map[tongo.AccountID]KnownCollection) {
func getGGWhitelist(collections map[tongo.AccountID]KnownCollection, logger *zap.Logger) {
client := graphql.NewClient("https://api.getgems.io/graphql", nil)
for {
addresses, err := _getGGWhitelist(client)
if err != nil {
fmt.Printf("get nft collection whitelist: %v\n", err)
logger.Info(fmt.Sprintf("get nft collection whitelist: %v", err))
time.Sleep(time.Minute * 3)
continue
}
Expand Down
9 changes: 5 additions & 4 deletions pkg/api/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,14 @@ type storage interface {
// GetStorageProviders returns a list of storage contracts deployed to the blockchain.
GetStorageProviders(ctx context.Context) ([]core.StorageProvider, error)

GetParticipatingInWhalesPools(ctx context.Context, id tongo.AccountID) ([]core.WhalesNominator, error)
GetWhalesPoolMemberInfo(ctx context.Context, pool, member tongo.AccountID) (core.WhalesNominator, error)
GetParticipatingInWhalesPools(ctx context.Context, id tongo.AccountID) ([]core.Nominator, error)
GetWhalesPoolMemberInfo(ctx context.Context, pool, member tongo.AccountID) (core.Nominator, error)
GetWhalesPoolInfo(ctx context.Context, id tongo.AccountID) (abi.GetParams_WhalesNominatorResult, abi.GetStakingStatusResult, int, error)
GetTFPools(ctx context.Context) ([]core.TFPool, error)
GetParticipatingInTfPools(ctx context.Context, member tongo.AccountID) ([]core.Nominator, error)
GetTFPools(ctx context.Context, onlyVerified bool) ([]core.TFPool, error)
GetTFPool(ctx context.Context, pool tongo.AccountID) (core.TFPool, error)
GetLiquidPool(ctx context.Context, pool tongo.AccountID) (core.LiquidPool, error)
GetLiquidPools(ctx context.Context) ([]core.LiquidPool, error)
GetLiquidPools(ctx context.Context, onlyVerified bool) ([]core.LiquidPool, error)

GetNFTs(ctx context.Context, accounts []tongo.AccountID) ([]core.NftItem, error)
SearchNFTs(ctx context.Context,
Expand Down
20 changes: 18 additions & 2 deletions pkg/api/staking_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ func (h Handler) StakingPoolInfo(ctx context.Context, params oas.StakingPoolInfo
func (h Handler) StakingPools(ctx context.Context, params oas.StakingPoolsParams) (r oas.StakingPoolsRes, _ error) {
var result oas.StakingPoolsOK

tfPools, err := h.storage.GetTFPools(ctx)
tfPools, err := h.storage.GetTFPools(ctx, !params.IncludeUnverified.Value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -107,7 +107,7 @@ func (h Handler) StakingPools(ctx context.Context, params oas.StakingPoolsParams
result.Pools = append(result.Pools, pool)
}

liquidPools, err := h.storage.GetLiquidPools(ctx)
liquidPools, err := h.storage.GetLiquidPools(ctx, !params.IncludeUnverified.Value)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -154,6 +154,13 @@ func (h Handler) PoolsByNominators(ctx context.Context, params oas.PoolsByNomina
}
return &oas.InternalError{Error: err.Error()}, nil
}
tfPools, err := h.storage.GetParticipatingInTfPools(ctx, accountID)
if err != nil {
if errors.Is(err, core.ErrEntityNotFound) {
return &oas.NotFound{Error: err.Error()}, nil
}
return &oas.InternalError{Error: err.Error()}, nil
}
var result oas.AccountStaking
for _, w := range whalesPools {
if _, ok := references.WhalesPools[w.Pool]; !ok {
Expand All @@ -167,5 +174,14 @@ func (h Handler) PoolsByNominators(ctx context.Context, params oas.PoolsByNomina
ReadyWithdraw: w.MemberWithdraw,
})
}
for _, w := range tfPools {
result.Pools = append(result.Pools, oas.AccountStakingInfo{
Pool: w.Pool.ToRaw(),
Amount: w.MemberBalance,
PendingDeposit: w.MemberPendingDeposit,
PendingWithdraw: w.MemberPendingWithdraw,
ReadyWithdraw: w.MemberWithdraw,
})
}
return &result, nil
}
2 changes: 1 addition & 1 deletion pkg/core/staking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"github.com/tonkeeper/tongo"
)

type WhalesNominator struct {
type Nominator struct {
Pool tongo.AccountID
Member tongo.AccountID
MemberBalance int64
Expand Down
51 changes: 42 additions & 9 deletions pkg/litestorage/stacking.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import (
"bytes"
"context"
"fmt"
"github.com/tonkeeper/tongo/tlb"
"math/big"

"github.com/prometheus/client_golang/prometheus"

Expand All @@ -13,23 +15,23 @@ import (
"github.com/tonkeeper/tongo/abi"
)

func (s *LiteStorage) GetWhalesPoolMemberInfo(ctx context.Context, pool, member tongo.AccountID) (core.WhalesNominator, error) {
func (s *LiteStorage) GetWhalesPoolMemberInfo(ctx context.Context, pool, member tongo.AccountID) (core.Nominator, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
storageTimeHistogramVec.WithLabelValues("get_whales_pool_member_info").Observe(v)
}))
defer timer.ObserveDuration()
_, value, err := abi.GetMember(ctx, s.client, pool, member.ToMsgAddress())
if err != nil {
return core.WhalesNominator{}, err
return core.Nominator{}, err
}
m, ok := value.(abi.GetMember_WhalesNominatorResult)
if !ok {
return core.WhalesNominator{}, fmt.Errorf("invalid result")
return core.Nominator{}, fmt.Errorf("invalid result")
}
if m.MemberBalance+m.MemberWithdraw+m.MemberPendingWithdraw+m.MemberPendingDeposit == 0 {
return core.WhalesNominator{}, fmt.Errorf("not pool member")
return core.Nominator{}, fmt.Errorf("not pool member")
}
return core.WhalesNominator{
return core.Nominator{
Pool: pool,
Member: member,
MemberBalance: m.MemberBalance,
Expand All @@ -39,12 +41,12 @@ func (s *LiteStorage) GetWhalesPoolMemberInfo(ctx context.Context, pool, member
}, nil
}

func (s *LiteStorage) GetParticipatingInWhalesPools(ctx context.Context, member tongo.AccountID) ([]core.WhalesNominator, error) {
func (s *LiteStorage) GetParticipatingInWhalesPools(ctx context.Context, member tongo.AccountID) ([]core.Nominator, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
storageTimeHistogramVec.WithLabelValues("get_participating_in_whales_pool").Observe(v)
}))
defer timer.ObserveDuration()
var result []core.WhalesNominator
var result []core.Nominator
for k := range references.WhalesPools {
info, err := s.GetWhalesPoolMemberInfo(ctx, k, member)
if err != nil {
Expand All @@ -55,6 +57,37 @@ func (s *LiteStorage) GetParticipatingInWhalesPools(ctx context.Context, member
return result, nil
}

func (s *LiteStorage) GetParticipatingInTfPools(ctx context.Context, member tongo.AccountID) ([]core.Nominator, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
storageTimeHistogramVec.WithLabelValues("get_participating_in_tf_pools").Observe(v)
}))
defer timer.ObserveDuration()
var result []core.Nominator
fmt.Println(len(s.knownAccounts["tf_pools"]))
for _, a := range s.knownAccounts["tf_pools"] {
var i big.Int
i.SetBytes(member.Address[:])
_, p, err := abi.GetNominatorData(ctx, s.client, a, tlb.Int257(i))
if err != nil {
continue
}
if data, ok := p.(abi.GetNominatorDataResult); ok {
nominator := core.Nominator{
Pool: a,
Member: member,
MemberPendingDeposit: int64(data.PendingDepositAmount),
MemberBalance: int64(data.Amount),
}
if data.WithdrawFound {
nominator.MemberPendingWithdraw = nominator.MemberBalance
nominator.MemberBalance = 0
}
result = append(result, nominator)
}
}
return result, nil
}

func (s *LiteStorage) GetWhalesPoolInfo(ctx context.Context, id tongo.AccountID) (abi.GetParams_WhalesNominatorResult, abi.GetStakingStatusResult, int, error) {
timer := prometheus.NewTimer(prometheus.ObserverFunc(func(v float64) {
storageTimeHistogramVec.WithLabelValues("get_whales_pool_info").Observe(v)
Expand Down Expand Up @@ -120,7 +153,7 @@ func (s *LiteStorage) GetTFPool(ctx context.Context, pool tongo.AccountID) (core
VerifiedSources: bytes.Equal(hash, references.TFPoolCodeHash[:]),
}, nil
}
func (s *LiteStorage) GetTFPools(ctx context.Context) ([]core.TFPool, error) {
func (s *LiteStorage) GetTFPools(ctx context.Context, onlyVerified bool) ([]core.TFPool, error) {
var result []core.TFPool
for _, a := range s.knownAccounts["tf_pools"] {
p, err := s.GetTFPool(ctx, a)
Expand Down Expand Up @@ -153,6 +186,6 @@ func (s *LiteStorage) GetLiquidPool(ctx context.Context, pool tongo.AccountID) (
}, err
}

func (s *LiteStorage) GetLiquidPools(ctx context.Context) ([]core.LiquidPool, error) {
func (s *LiteStorage) GetLiquidPools(ctx context.Context, onlyVerified bool) ([]core.LiquidPool, error) {
return nil, nil
}
19 changes: 18 additions & 1 deletion pkg/oas/oas_json_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions pkg/oas/oas_schemas_gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/references/tf.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ var TFPoolCodeHash = tongo.MustParseHash("9A3EC14BC098F6B44064C305222CAEA2800F17
const LiquidImplementationsName = "Liquid staking"
const LiquidImplementationsUrl = "https://ton.org"

var TFLiquidPoolCodeHash = tongo.MustParseHash("e7a160ae9b4e86a406a3767e575c3d215c88181e9e10af1f1af4de62685cabbf")
var TFLiquidPoolCodeHash = tongo.MustParseHash("e8eeae986d782a96eef432c45edd1f0a84b943416ccd8caa42118c0dc7b1d34a")

0 comments on commit 7f1c9f4

Please sign in to comment.