github.com/mavryk-network/mvgo@v1.19.9/rpc/delegates.go (about) 1 // Copyright (c) 2020-2024 Blockwatch Data Inc. 2 // Author: alex@blockwatch.cc 3 4 package rpc 5 6 import ( 7 "context" 8 "fmt" 9 "strconv" 10 11 "github.com/mavryk-network/mvgo/mavryk" 12 ) 13 14 // Delegate holds information about an active delegate 15 type Delegate struct { 16 // extra info 17 Delegate mavryk.Address `json:"-"` 18 Height int64 `json:"-"` 19 Block string `json:"-"` 20 21 // tezos data 22 Deactivated bool `json:"deactivated"` 23 Balance int64 `json:"balance,string"` 24 DelegatedContracts []mavryk.Address `json:"delegated_contracts"` 25 FrozenBalance int64 `json:"frozen_balance,string"` 26 FrozenBalanceByCycle []CycleBalance `json:"frozen_balance_by_cycle"` 27 GracePeriod int64 `json:"grace_period"` 28 StakingBalance int64 `json:"staking_balance,string"` 29 DelegatedBalance int64 `json:"delegated_balance,string"` 30 VotingPower Int64orString `json:"voting_power"` 31 32 // v012+ 33 FullBalance int64 `json:"full_balance,string"` 34 FrozenDeposits int64 `json:"frozen_deposits,string"` 35 CurrentFrozenDeposits int64 `json:"current_frozen_deposits,string"` 36 FrozenDepositsLimit int64 `json:"frozen_deposits_limit,string"` 37 38 // v015+ 39 ActiveConsensusKey mavryk.Address `json:"active_consensus_key"` 40 PendingConsensusKeys []CycleKey `json:"pending_consensus_keys"` 41 42 // v019+ 43 MinDelegated struct { 44 Amount int64 `json:"amount,string"` 45 Level LevelInfo `json:"level"` 46 } `json:"min_delegated_in_current_cycle"` 47 PendingDenunciations bool `json:"pending_denunciations"` 48 TotalDelegatedStake int64 `json:"total_delegated_stakem,string"` 49 StakingDenominator int64 `json:"staking_denominator,string"` 50 } 51 52 type CycleKey struct { 53 Cycle int64 `json:"cycle"` 54 Pkh mavryk.Address `json:"pkh"` 55 } 56 57 type CycleBalance struct { 58 Cycle int64 `json:"cycle"` 59 Deposit int64 `json:"deposit,string"` 60 Fees int64 `json:"fees,string"` 61 Rewards int64 `json:"rewards,string"` 62 } 63 64 // DelegateList contains a list of delegates 65 type DelegateList []mavryk.Address 66 67 // ListActiveDelegates returns information about all active delegates at a block. 68 func (c *Client) ListActiveDelegates(ctx context.Context, id BlockID) (DelegateList, error) { 69 p, err := c.GetParams(ctx, id) 70 if err != nil { 71 return nil, err 72 } 73 selector := "active=true" 74 if p.Version >= 13 { 75 selector += "&with_minimal_stake=true" 76 } 77 delegates := make(DelegateList, 0) 78 u := fmt.Sprintf("chains/main/blocks/%s/context/delegates?%s", id, selector) 79 if err := c.Get(ctx, u, &delegates); err != nil { 80 return nil, err 81 } 82 return delegates, nil 83 } 84 85 // GetDelegate returns information about a delegate at a specific height. 86 func (c *Client) GetDelegate(ctx context.Context, addr mavryk.Address, id BlockID) (*Delegate, error) { 87 delegate := &Delegate{ 88 Delegate: addr, 89 Height: id.Int64(), 90 Block: id.String(), 91 } 92 u := fmt.Sprintf("chains/main/blocks/%s/context/delegates/%s", id, addr) 93 if err := c.Get(ctx, u, &delegate); err != nil { 94 return nil, err 95 } 96 return delegate, nil 97 } 98 99 // GetDelegateBalance returns a delegate's balance 100 func (c *Client) GetDelegateBalance(ctx context.Context, addr mavryk.Address, id BlockID) (int64, error) { 101 u := fmt.Sprintf("chains/main/blocks/%s/context/delegates/%s/balance", id, addr) 102 var bal string 103 err := c.Get(ctx, u, &bal) 104 if err != nil { 105 return 0, err 106 } 107 return strconv.ParseInt(bal, 10, 64) 108 } 109 110 // GetDelegateKey returns a delegate's current consensus key 111 func (c *Client) GetDelegateKey(ctx context.Context, addr mavryk.Address, id BlockID) (mavryk.Key, error) { 112 u := fmt.Sprintf("chains/main/blocks/%s/context/delegates/%s/consensus_key", id, addr) 113 type ActiveConsensusKey struct { 114 Active struct { 115 Pk mavryk.Key `json:"pk"` 116 } `json:"active"` 117 } 118 var key ActiveConsensusKey 119 err := c.Get(ctx, u, &key) 120 return key.Active.Pk, err 121 }