Skip to content

Commit

Permalink
fix: update GetAssetInfo to return list pased on provided args
Browse files Browse the repository at this point in the history
BREAKING CHANGE: requires argument of assets with asset policy id and
asset name set

Signed-off-by: Marko Kungla <[email protected]>
  • Loading branch information
mkungla committed Mar 29, 2024
1 parent d41fcda commit 79519bd
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 31 deletions.
70 changes: 39 additions & 31 deletions asset.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"context"
"encoding/json"
"fmt"
"io"

"github.com/shopspring/decimal"
)
Expand Down Expand Up @@ -94,14 +95,24 @@ type (

// AssetInfo info about the asset.
AssetInfo struct {
// Asset Policy ID (hex).
PolicyID PolicyID `json:"policy_id,omitempty"`
// Asset Name (hex).
AssetName AssetName `json:"asset_name"`

// Asset Name (ASCII)
AssetNameASCII string `json:"asset_name_ascii,omitempty"`

// The CIP14 fingerprint of the asset
Fingerprint AssetFingerprint `json:"fingerprint"`
// MintingTxHash mint tx
MintingTxHash TxHash `json:"minting_tx_hash,omitempty"`
// TotalSupply of Asset
TotalSupply decimal.Decimal `json:"total_supply"`
// MintCnt count of mint transactions
MintCnt int `json:"mint_cnt,omitempty"`
// BurnCnt count of burn transactions
BurnCnt int `json:"burn_cnt,omitempty"`
// CreationTime of Asset
CreationTime Timestamp `json:"creation_time,omitempty"`

// MintingTxMetadata minting Tx JSON payload if it can be decoded as JSON
// MintingTxMetadata *TxInfoMetadata `json:"minting_tx_metadata"`
Expand All @@ -110,23 +121,7 @@ type (
// Asset metadata registered on the Cardano Token Registry
TokenRegistryMetadata *TokenRegistryMetadata `json:"token_registry_metadata,omitempty"`

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

// TotalSupply of Asset
TotalSupply decimal.Decimal `json:"total_supply"`

// CreationTime of Asset
CreationTime Timestamp `json:"creation_time,omitempty"`

// MintCnt count of mint transactions
MintCnt int `json:"mint_cnt,omitempty"`

// BurnCnt count of burn transactions
BurnCnt int `json:"burn_cnt,omitempty"`

// MintingTxHash mint tx
MintingTxHash TxHash `json:"minting_tx_hash,omitempty"`
CIP68Metadata *json.RawMessage `json:"cip68_metadata,omitempty"`
}

// AssetListResponse represents response from `/asset_list` endpoint.
Expand All @@ -149,7 +144,7 @@ type (

// AssetInfoResponse represents response from `/asset_info` endpoint.
AssetInfoResponse struct {
Data *AssetInfo `json:"data"`
Data []AssetInfo `json:"data"`
Response
}

Expand Down Expand Up @@ -256,28 +251,41 @@ func (c *Client) GetAssetAddresses(
// first minting & token registry metadata.
func (c *Client) GetAssetInfo(
ctx context.Context,
policy PolicyID,
name AssetName,
assets []Asset,
opts *RequestOptions,
) (res *AssetInfoResponse, err error) {
res = &AssetInfoResponse{}

if opts == nil {
opts = c.NewRequestOptions()
}
opts.QuerySet("_asset_policy", policy.String())
opts.QuerySet("_asset_name", name.String())

rsp, err := c.request(ctx, &res.Response, "GET", "/asset_info", nil, opts)
if err != nil {
return
if len(assets) == 0 {
return nil, fmt.Errorf("%w: atleast one asset must be provided", ErrAsset)
}
info := []AssetInfo{}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &info)

if len(info) == 1 {
res.Data = &info[0]
var payload = struct {
Assets [][]string `json:"_asset_list"`
}{}

for _, asset := range assets {
if asset.PolicyID == "" || asset.AssetName == "" {
return nil, fmt.Errorf("%w: policy_id and asset_name must be provided", ErrAsset)
}
payload.Assets = append(payload.Assets, []string{asset.PolicyID.String(), asset.AssetName.String()})
}

rpipe, w := io.Pipe()
go func() {
_ = json.NewEncoder(w).Encode(payload)
defer w.Close()
}()

rsp, err := c.request(ctx, &res.Response, "POST", "/asset_info", rpipe, opts)
if err != nil {
return
}
err = ReadAndUnmarshalResponse(rsp, &res.Response, &res.Data)
return
}

Expand Down
1 change: 1 addition & 0 deletions koios.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ var (
ErrUnexpectedResponseField = errors.New("unexpected response field")
ErrUTxOInputAlreadyUsed = errors.New("UTxO already used")
ErrNoData = errors.New("no data")
ErrAsset = errors.New("asset error")

// ZeroLovelace is alias decimal.Zero.
ZeroLovelace = decimal.Zero.Copy() //nolint: gochecknoglobals
Expand Down

0 comments on commit 79519bd

Please sign in to comment.