Skip to content

Commit

Permalink
Merge pull request #61 from sean118/master
Browse files Browse the repository at this point in the history
Added AssetAddressesAll function, added APIQueryParams to AssetAddresses, added PreProd and Preview URLs
  • Loading branch information
HannesKimara authored Sep 22, 2022
2 parents d09ce13 + 399f58f commit 777ce58
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 5 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,5 @@

# Editor Files
.vscode/

.DS_Store
59 changes: 56 additions & 3 deletions api_assets.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,11 @@ type AssetResult struct {
Err error
}

type AssetAddressesAll struct {
Res []AssetAddress
Err error
}

// Assets returns a paginated list of assets.
func (c *apiClient) Assets(ctx context.Context, query APIQueryParams) (a []Asset, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s", c.server, resourceAssets))
Expand Down Expand Up @@ -240,17 +245,21 @@ func (c *apiClient) AssetTransactions(ctx context.Context, asset string) (trs []
return trs, nil
}

// AssetAddresses returns list of a addresses containing a specific asset.
func (c *apiClient) AssetAddresses(ctx context.Context, asset string) (addrs []AssetAddress, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceAssets, asset, resourceAssetHistory))
func (c *apiClient) AssetAddresses(ctx context.Context, asset string, query APIQueryParams) (addrs []AssetAddress, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceAssets, asset, resourceAddresses))
if err != nil {
return
}

req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}

v := req.URL.Query()
v = formatParams(v, query)
req.URL.RawQuery = v.Encode()

res, err := c.handleRequest(req)
if err != nil {
return
Expand All @@ -263,6 +272,50 @@ func (c *apiClient) AssetAddresses(ctx context.Context, asset string) (addrs []A
return addrs, nil
}

// AssetAddresses returns list of a addresses containing a specific asset.
func (c *apiClient) AssetAddressesAll(ctx context.Context, asset string) <-chan AssetAddressesAll {
ch := make(chan AssetAddressesAll, c.routines)
jobs := make(chan methodOptions, c.routines)
quit := make(chan bool, 1)

wg := sync.WaitGroup{}

for i := 0; i < c.routines; i++ {
wg.Add(1)
go func(jobs chan methodOptions, ch chan AssetAddressesAll, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
ad, err := c.AssetAddresses(j.ctx, asset, j.query)
if len(ad) != j.query.Count || err != nil {
select {
case quit <- true:
default:
}
}
res := AssetAddressesAll{Res: ad, Err: err}
ch <- res
}

}(jobs, ch, &wg)
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
select {
case <-quit:
fetchScripts = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
}

close(jobs)
wg.Wait()
}()
return ch
}

// AssetsByPolicy returns list of assets minted under a specific policy.
func (c *apiClient) AssetsByPolicy(ctx context.Context, policyId string) (a []Asset, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s", c.server, resourcePolicyAssets, policyId))
Expand Down
2 changes: 1 addition & 1 deletion api_assets_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestResourceAssetddressesIntegration(t *testing.T) {
asset := "3a9241cd79895e3a8d65261b40077d4437ce71e9d7c8c6c00e3f658e4669727374636f696e"
api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{})

got, err := api.AssetAddresses(context.TODO(), asset)
got, err := api.AssetAddresses(context.TODO(), asset, blockfrost.APIQueryParams{})
if err != nil {
t.Fatal(err)
}
Expand Down
3 changes: 2 additions & 1 deletion client.go
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ type APIClient interface {
AssetsAll(ctx context.Context) <-chan AssetResult
AssetHistory(ctx context.Context, asset string) ([]AssetHistory, error)
AssetTransactions(ctx context.Context, asset string) ([]AssetTransaction, error)
AssetAddresses(ctx context.Context, asset string) ([]AssetAddress, error)
AssetAddresses(ctx context.Context, asset string, query APIQueryParams) ([]AssetAddress, error)
AssetAddressesAll(ctx context.Context, asset string) <-chan AssetAddressesAll
AssetsByPolicy(ctx context.Context, policyId string) ([]Asset, error)
Genesis(ctx context.Context) (GenesisBlock, error)
MetadataTxLabels(ctx context.Context, query APIQueryParams) ([]MetadataTxLabel, error)
Expand Down
2 changes: 2 additions & 0 deletions types.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import "fmt"
const (
CardanoMainNet = "https://cardano-mainnet.blockfrost.io/api/v0"
CardanoTestNet = "https://cardano-testnet.blockfrost.io/api/v0"
CardanoPreProd = "https://cardano-preprod.blockfrost.io/api/v0"
CardanoPreview = "https://cardano-preview.blockfrost.io/api/v0"
IPFSNet = "https://ipfs.blockfrost.io/api/v0"
)

Expand Down

0 comments on commit 777ce58

Please sign in to comment.