Skip to content
This repository has been archived by the owner on Mar 18, 2022. It is now read-only.

Commit

Permalink
implement /totals endpoint and cmd
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Kungla <marko.kungla@gmail.com>
  • Loading branch information
mkungla committed Feb 4, 2022
1 parent 67f91c9 commit 74eac37
Show file tree
Hide file tree
Showing 3 changed files with 110 additions and 1 deletion.
17 changes: 17 additions & 0 deletions cmd/koios-rest/cmd-network.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,23 @@ func addNetworkCommands(app *cli.App, api *koios.Client) {
Name: "totals",
Category: "NETWORK",
Usage: "Get the circulating utxo, treasury, rewards, supply and reserves in lovelace for specified epoch, all epochs if empty.",
Flags: []cli.Flag{
&cli.UintFlag{
Name: "epoch-no",
Usage: "Epoch Number to fetch details for",
},
},
Action: func(ctx *cli.Context) error {
var epochNo *koios.EpochNo
if ctx.Uint("epoch-no") > 0 {
v := koios.EpochNo(ctx.String("epoch-no"))
epochNo = &v
}

res, err := api.GetTotals(context.Background(), epochNo)
output(ctx, res, err)
return nil
},
},
}...)
}
28 changes: 27 additions & 1 deletion network.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package koios
import (
"context"
"encoding/json"
"net/url"
)

// GetTip returns the tip info about the latest block seen by chain.
Expand All @@ -39,7 +40,7 @@ func (c *Client) GetTip(ctx context.Context) (*TipResponse, error) {
return res, nil
}

// Get the Genesis parameters used to start specific era on chain.
// GetGenesis returns the Genesis parameters used to start specific era on chain.
func (c *Client) GetGenesis(ctx context.Context) (*GenesisResponse, error) {
rsp, err := c.GET(ctx, "/genesis")
if err != nil {
Expand All @@ -57,3 +58,28 @@ func (c *Client) GetGenesis(ctx context.Context) (*GenesisResponse, error) {

return res, nil
}

// GetTotals returns the circulating utxo, treasury, rewards, supply and reserves in
// lovelace for specified epoch, all epochs if empty.
func (c *Client) GetTotals(ctx context.Context, epochNo *EpochNo) (*TotalsResponse, error) {
params := url.Values{}
if epochNo != nil {
params.Set("_epoch_no", string(*epochNo))
}

rsp, err := c.GET(ctx, "/totals", params)
if err != nil {
return nil, err
}
res := &TotalsResponse{}
res.setStatus(rsp)
body, err := readResponseBody(rsp)
if err != nil {
return nil, err
}
if err := json.Unmarshal(body, &res.Totals); err != nil {
return nil, err
}

return res, nil
}
66 changes: 66 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,41 @@
// limitations under the License.
package koios

type (
// Address defines model for _address.
Address string

// AnyAddress defines model for _any_address.
AnyAddress string

// AssetName defines model for _asset_name.
AssetName string

// AssetPolicy defines model for _asset_policy.
AssetPolicy string

// BlockHash defines model for _block_hash.
BlockHash string

// EarnedEpochNo defines model for _earned_epoch_no.
EarnedEpochNo string

// EpochNo defines model for _epoch_no.
EpochNo string

// PoolBech32 defines model for _pool_bech32.
PoolBech32 string

// PoolBech32Optional defines model for _pool_bech32_optional.
PoolBech32Optional string

// ScriptHash defines model for _script_hash.
ScriptHash string

// StakeAddress defines model for _stake_address.
StakeAddress string
)

type (
// Tip defines model for tip.
Tip []struct {
Expand Down Expand Up @@ -100,3 +135,34 @@ type (
Genesis Genesis `json:"response"`
}
)

type (
// Totals defines model for totals.
Totals []struct {

// Circulating UTxOs for given epoch (in lovelaces).
Circulation *string `json:"circulation,omitempty"`

// Epoch number.
EpochNo *int `json:"epoch_no,omitempty"`

// Total Reserves yet to be unlocked on chain.
Reserves *string `json:"reserves,omitempty"`

// Rewards accumulated as of given epoch (in lovelaces).
Reward *string `json:"reward,omitempty"`

// Total Active Supply (sum of treasury funds, rewards,
// UTxOs, deposits and fees) for given epoch (in lovelaces).
Supply *string `json:"supply,omitempty"`

// Funds in treasury for given epoch (in lovelaces).
Treasury *string `json:"treasury,omitempty"`
}

// GenesisResponse response of /genesis.
TotalsResponse struct {
Response
Totals Totals `json:"response"`
}
)

0 comments on commit 74eac37

Please sign in to comment.