|
| 1 | +package client |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "net/http" |
| 6 | + |
| 7 | + webapi_mana "github.com/iotaledger/goshimmer/plugins/webapi/mana" |
| 8 | +) |
| 9 | + |
| 10 | +const ( |
| 11 | + routeGetMana = "mana" |
| 12 | + routeGetAllMana = "mana/all" |
| 13 | + routeGetManaPercentile = "mana/percentile" |
| 14 | + routeGetOnlineAccessMana = "mana/access/online" |
| 15 | + routeGetOnlineConsensusMana = "mana/consensus/online" |
| 16 | + routeGetNHighestAccessMana = "mana/access/nhighest" |
| 17 | + routeGetNHighestConsensusMana = "mana/consensus/nhighest" |
| 18 | + routePending = "mana/pending" |
| 19 | + routePastConsensusVector = "mana/consensus/past" |
| 20 | + routePastConsensusEventLogs = "mana/consensus/logs" |
| 21 | +) |
| 22 | + |
| 23 | +// GetOwnMana returns the access and consensus mana of the node this api client is communicating with. |
| 24 | +func (api *GoShimmerAPI) GetOwnMana() (*webapi_mana.GetManaResponse, error) { |
| 25 | + res := &webapi_mana.GetManaResponse{} |
| 26 | + if err := api.do(http.MethodGet, routeGetMana, |
| 27 | + &webapi_mana.GetManaRequest{NodeID: ""}, res); err != nil { |
| 28 | + return nil, err |
| 29 | + } |
| 30 | + return res, nil |
| 31 | +} |
| 32 | + |
| 33 | +// GetManaFullNodeID returns the access and consensus mana of the node specified in the argument. |
| 34 | +// Note, that for the node to understand which nodeID we are referring to, short node ID is not sufficient. |
| 35 | +func (api *GoShimmerAPI) GetManaFullNodeID(fullNodeID string) (*webapi_mana.GetManaResponse, error) { |
| 36 | + res := &webapi_mana.GetManaResponse{} |
| 37 | + if err := api.do(http.MethodGet, routeGetMana, |
| 38 | + &webapi_mana.GetManaRequest{NodeID: fullNodeID}, res); err != nil { |
| 39 | + return nil, err |
| 40 | + } |
| 41 | + return res, nil |
| 42 | +} |
| 43 | + |
| 44 | +// GetMana returns the access and consensus mana a node has based on its shortNodeID. |
| 45 | +func (api *GoShimmerAPI) GetMana(shortNodeID string) (*webapi_mana.GetManaResponse, error) { |
| 46 | + // ask the node about the full mana map and filter out based on shortID |
| 47 | + allManaRes := &webapi_mana.GetAllManaResponse{} |
| 48 | + if err := api.do(http.MethodGet, routeGetAllMana, |
| 49 | + nil, allManaRes); err != nil { |
| 50 | + return nil, err |
| 51 | + } |
| 52 | + res := &webapi_mana.GetManaResponse{ShortNodeID: shortNodeID} |
| 53 | + // look for node's mana values in the map |
| 54 | + for _, nodeStr := range allManaRes.Access { |
| 55 | + if nodeStr.ShortNodeID == shortNodeID { |
| 56 | + res.Access = nodeStr.Mana |
| 57 | + break |
| 58 | + } |
| 59 | + } |
| 60 | + for _, nodeStr := range allManaRes.Consensus { |
| 61 | + if nodeStr.ShortNodeID == shortNodeID { |
| 62 | + res.Consensus = nodeStr.Mana |
| 63 | + break |
| 64 | + } |
| 65 | + } |
| 66 | + return res, nil |
| 67 | +} |
| 68 | + |
| 69 | +// GetAllMana returns the mana perception of the node in the network. |
| 70 | +func (api *GoShimmerAPI) GetAllMana() (*webapi_mana.GetAllManaResponse, error) { |
| 71 | + res := &webapi_mana.GetAllManaResponse{} |
| 72 | + if err := api.do(http.MethodGet, routeGetAllMana, |
| 73 | + nil, res); err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + return res, nil |
| 77 | +} |
| 78 | + |
| 79 | +// GetManaPercentile returns the mana percentile for access and consensus mana of a node. |
| 80 | +func (api *GoShimmerAPI) GetManaPercentile(fullNodeID string) (*webapi_mana.GetPercentileResponse, error) { |
| 81 | + res := &webapi_mana.GetPercentileResponse{} |
| 82 | + if err := api.do(http.MethodGet, routeGetManaPercentile, |
| 83 | + &webapi_mana.GetPercentileRequest{NodeID: fullNodeID}, res); err != nil { |
| 84 | + return nil, err |
| 85 | + } |
| 86 | + return res, nil |
| 87 | +} |
| 88 | + |
| 89 | +// GetOnlineAccessMana returns the sorted list of online access mana of nodes. |
| 90 | +func (api *GoShimmerAPI) GetOnlineAccessMana() (*webapi_mana.GetOnlineResponse, error) { |
| 91 | + res := &webapi_mana.GetOnlineResponse{} |
| 92 | + if err := api.do(http.MethodGet, routeGetOnlineAccessMana, |
| 93 | + nil, res); err != nil { |
| 94 | + return nil, err |
| 95 | + } |
| 96 | + return res, nil |
| 97 | +} |
| 98 | + |
| 99 | +// GetOnlineConsensusMana returns the sorted list of online consensus mana of nodes. |
| 100 | +func (api *GoShimmerAPI) GetOnlineConsensusMana() (*webapi_mana.GetOnlineResponse, error) { |
| 101 | + res := &webapi_mana.GetOnlineResponse{} |
| 102 | + if err := api.do(http.MethodGet, routeGetOnlineConsensusMana, |
| 103 | + nil, res); err != nil { |
| 104 | + return nil, err |
| 105 | + } |
| 106 | + return res, nil |
| 107 | +} |
| 108 | + |
| 109 | +// GetNHighestAccessMana returns the N highest access mana holders in the network, sorted in descending order. |
| 110 | +func (api *GoShimmerAPI) GetNHighestAccessMana(n int) (*webapi_mana.GetNHighestResponse, error) { |
| 111 | + res := &webapi_mana.GetNHighestResponse{} |
| 112 | + if err := api.do(http.MethodGet, func() string { |
| 113 | + return fmt.Sprintf("%s?number=%d", routeGetNHighestAccessMana, n) |
| 114 | + }(), nil, res); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + return res, nil |
| 118 | +} |
| 119 | + |
| 120 | +// GetNHighestConsensusMana returns the N highest consensus mana holders in the network, sorted in descending order. |
| 121 | +func (api *GoShimmerAPI) GetNHighestConsensusMana(n int) (*webapi_mana.GetNHighestResponse, error) { |
| 122 | + res := &webapi_mana.GetNHighestResponse{} |
| 123 | + if err := api.do(http.MethodGet, func() string { |
| 124 | + return fmt.Sprintf("%s?number=%d", routeGetNHighestConsensusMana, n) |
| 125 | + }(), nil, res); err != nil { |
| 126 | + return nil, err |
| 127 | + } |
| 128 | + return res, nil |
| 129 | +} |
| 130 | + |
| 131 | +// GetPending returns the mana (bm2) that will be pledged by spending the output specified. |
| 132 | +func (api *GoShimmerAPI) GetPending(outputID string) (*webapi_mana.PendingResponse, error) { |
| 133 | + res := &webapi_mana.PendingResponse{} |
| 134 | + if err := api.do(http.MethodGet, routePending, |
| 135 | + &webapi_mana.PendingRequest{OutputID: outputID}, res); err != nil { |
| 136 | + return nil, err |
| 137 | + } |
| 138 | + return res, nil |
| 139 | +} |
| 140 | + |
| 141 | +// GetPastConsensusManaVector returns the consensus base mana vector of a time in the past. |
| 142 | +func (api *GoShimmerAPI) GetPastConsensusManaVector(t int64) (*webapi_mana.PastConsensusManaVectorResponse, error) { |
| 143 | + res := &webapi_mana.PastConsensusManaVectorResponse{} |
| 144 | + if err := api.do(http.MethodGet, routePastConsensusVector, |
| 145 | + &webapi_mana.PastConsensusManaVectorRequest{Timestamp: t}, res); err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + return res, nil |
| 149 | +} |
| 150 | + |
| 151 | +// GetPastConsensusVectorMetadata returns the consensus base mana vector metadata of a time in the past. |
| 152 | +func (api *GoShimmerAPI) GetPastConsensusVectorMetadata() (*webapi_mana.PastConsensusVectorMetadataResponse, error) { |
| 153 | + res := &webapi_mana.PastConsensusVectorMetadataResponse{} |
| 154 | + if err := api.do(http.MethodGet, routePastConsensusVector, nil, res); err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + return res, nil |
| 158 | +} |
| 159 | + |
| 160 | +// GetConsensusEventLogs returns the consensus event logs or the nodeIDs specified. |
| 161 | +func (api *GoShimmerAPI) GetConsensusEventLogs(nodeIDs []string) (*webapi_mana.GetEventLogsResponse, error) { |
| 162 | + res := &webapi_mana.GetEventLogsResponse{} |
| 163 | + if err := api.do(http.MethodGet, routePastConsensusEventLogs, |
| 164 | + &webapi_mana.GetEventLogsRequest{NodeIDs: nodeIDs}, res); err != nil { |
| 165 | + return nil, err |
| 166 | + } |
| 167 | + return res, nil |
| 168 | +} |
0 commit comments