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

Commit

Permalink
implement asset-list
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 7, 2022
1 parent 3fc030d commit 0b278d9
Show file tree
Hide file tree
Showing 9 changed files with 113 additions and 23 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,8 @@ issues and bug reports are welcome to: https://github.com/howijd/decimal/issues.
| `/account_addresses` | [`*.GetAccountAddresses(...) *AccountAddressesResponse`](https://pkg.go.dev/github.com/howijd/koios-rest-go-client#Client.GetAccountAddresses) | `account-addresses` | [![](https://img.shields.io/badge/API-doc-%2349cc90)](https://api.koios.rest/#get-/account_addresses) |
| `/account_assets` | [`*.GetAccountAssets(...) *AccountAssetsResponse`](https://pkg.go.dev/github.com/howijd/koios-rest-go-client#Client.GetAccountAssets) | `account-assets` | [![](https://img.shields.io/badge/API-doc-%2349cc90)](https://api.koios.rest/#get-/account_assets) |
| `/account_history` | [`*.GetAccountHistory(...) *AccountHistoryResponse`](https://pkg.go.dev/github.com/howijd/koios-rest-go-client#Client.GetAccountHistory) | `account-history` | [![](https://img.shields.io/badge/API-doc-%2349cc90)](https://api.koios.rest/#get-/account_history) |
| **ASSET** | | | |
| `/asset_list` | [`*.GetAssetList(...) *AssetListResponse`](https://pkg.go.dev/github.com/howijd/koios-rest-go-client#Client.GetAssetList) | `asset-list` | [![](https://img.shields.io/badge/API-doc-%2349cc90)](https://api.koios.rest/#get-/asset_list) |
| **POOL** | | | |
| **SCRIPT** | | | |

Expand Down
2 changes: 1 addition & 1 deletion account.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ type (
Name string `json:"asset_name"`

// PolicyID Asset Policy ID (hex).
PolicyID AssetPolicy `json:"asset_policy"`
PolicyID PolicyID `json:"asset_policy"`

// Quantity of assets
Quantity Lovelace `json:"quantity"`
Expand Down
2 changes: 1 addition & 1 deletion address.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ type (
NameHEX string `json:"asset_name_hex"`

// Asset Policy ID (hex).
PolicyID string `json:"asset_policy_hex"`
PolicyID PolicyID `json:"asset_policy_hex"`

// Quantity of asset accoiated to the given address.
Quantity Lovelace `json:"quantity"`
Expand Down
82 changes: 82 additions & 0 deletions asset.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
// Copyright 2022 The Howijd.Network Authors
// SPDX-License-Identifier: Apache-2.0
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at:
//
// http://www.apache.org/licenses/LICENSE-2.0
// or LICENSE file in repository root.
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package koios

import (
"context"
"encoding/json"
"net/http"
)

type (
// Asset info.
Asset struct {
// Asset Name (hex).
Name string `json:"asset_name"`

// Asset Policy ID (hex).
PolicyID PolicyID `json:"policy_id"`

// Quantity
// Input: asset balance on the selected input transaction.
// Output: sum of assets for output UTxO.
// Mint: sum of minted assets (negative on burn).
Quantity Lovelace `json:"quantity"`
}

// AssetListItem used to represent response from /asset_list`.
AssetListItem struct {
PolicyID PolicyID `json:"policy_id"`
AssetNames struct {
HEX []string `json:"hex"`
ASCII []string `json:"ascii"`
} `json:"asset_names"`
}

// AssetListResponse represents response from `/asset_list` endpoint.
AssetListResponse struct {
Response
Data []AssetListItem `json:"response"`
}
)

// GetTip returns the list of all native assets (paginated).
func (c *Client) GetAssetList(ctx context.Context) (res *AssetListResponse, err error) {
res = &AssetListResponse{}
rsp, err := c.request(ctx, &res.Response, "GET", nil, "/asset_list", nil, nil)
if err != nil {
res.applyError(nil, err)
return
}

body, err := readResponseBody(rsp)
if err != nil {
res.applyError(body, err)
return
}

if err = json.Unmarshal(body, &res.Data); err != nil {
res.applyError(body, err)
return
}

if rsp.StatusCode != http.StatusOK {
res.applyError(body, err)
return
}
return res, nil
}
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (c *Client) applyReqHeaders(req *http.Request, headers http.Header) {
func (c *Client) requestWithStats(req *http.Request, res *Response) (*http.Response, error) {
res.Stats = &RequestStats{}
var dns, tlshs, connect time.Time

trace := &httptrace.ClientTrace{
DNSStart: func(dsi httptrace.DNSStartInfo) {
dns = time.Now().UTC()
Expand Down
5 changes: 5 additions & 0 deletions cmd/koios-rest/cmd-asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ func addAssetCommands(app *cli.App, api *koios.Client) {
Name: "asset-list",
Category: "ASSET",
Usage: "Get the list of all native assets (paginated).",
Action: func(ctx *cli.Context) error {
res, err := api.GetAssetList(callctx)
output(ctx, res, err)
return nil
},
},
{
Name: "asset-address-list",
Expand Down
1 change: 1 addition & 0 deletions cmd/koios-rest/cmd-general.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func addGeneralCommands(app *cli.App, api *koios.Client) {
handleErr(err)
defer res.Body.Close()
body, err := ioutil.ReadAll(res.Body)

printResponseBody(ctx, body)
return nil
},
Expand Down
18 changes: 16 additions & 2 deletions cmd/koios-rest/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,19 @@ func handleErr(err error) {

func printResponseBody(ctx *cli.Context, body []byte) {
if ctx.Bool("ugly") {
fmt.Println(string(body))
if ctx.Bool("no-color") {
fmt.Println(string(body))
return
}
fmt.Println(string(pretty.Color(body, pretty.TerminalStyle)))
return
}
fmt.Println(string(pretty.Pretty(body)))
pr := pretty.Pretty(body)
if ctx.Bool("no-color") {
fmt.Println(string(pr))
return
}
fmt.Println(string(pretty.Color(pr, pretty.TerminalStyle)))
}

type printable interface {
Expand Down Expand Up @@ -157,5 +166,10 @@ func globalFlags() []cli.Flag {
Usage: "Enable request stats.",
Value: false,
},
&cli.BoolFlag{
Name: "no-color",
Usage: "Disable coloring output json.",
Value: false,
},
}
}
23 changes: 4 additions & 19 deletions koios.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,9 +107,6 @@ type (
// AssetName defines type for _asset_name.
AssetName string

// AssetPolicy defines type for _asset_policy.
AssetPolicy string

// BlockHash defines type for _block_hash.
BlockHash string

Expand All @@ -122,9 +119,12 @@ type (
// EpochNo defines type for _epoch_no.
EpochNo uint64

// PoolID defines (hex).
// PoolID.
PoolID string

// PolicyID.
PolicyID string

// ScriptHash defines type for _script_hash.
ScriptHash string

Expand All @@ -146,21 +146,6 @@ type (
decimal.Decimal
}

// Asset info.
Asset struct {
// Asset Name (hex).
Name string `json:"asset_name"`

// Asset Policy ID (hex).
PolicyID PoolID `json:"policy_id"`

// Quantity
// Input: asset balance on the selected input transaction.
// Output: sum of assets for output UTxO.
// Mint: sum of minted assets (negative on burn).
Quantity Lovelace `json:"quantity"`
}

// PaymentAddr info.
PaymentAddr struct {
// Bech32 is Cardano payment/base address (bech32 encoded)
Expand Down

0 comments on commit 0b278d9

Please sign in to comment.