diff --git a/cmd/koios-rest/cmd-network.go b/cmd/koios-rest/cmd-network.go index 99240da..4a916df 100644 --- a/cmd/koios-rest/cmd-network.go +++ b/cmd/koios-rest/cmd-network.go @@ -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 + }, }, }...) } diff --git a/network.go b/network.go index 5fbfde6..49f8397 100644 --- a/network.go +++ b/network.go @@ -18,6 +18,7 @@ package koios import ( "context" "encoding/json" + "net/url" ) // GetTip returns the tip info about the latest block seen by chain. @@ -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 { @@ -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 +} diff --git a/types.go b/types.go index 0c15b0c..6fb300e 100644 --- a/types.go +++ b/types.go @@ -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 { @@ -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"` + } +)