Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDK update #70

Merged
merged 16 commits into from
Dec 18, 2023
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ name: Test

on:
push:
branches:
branches:
- master
- feat**
- test**
- docs**
- ref**
pull_request:
branches: [ master ]
branches: [master]

jobs:
test:
Expand All @@ -18,12 +18,12 @@ jobs:
BLOCKFROST_PROJECT_ID: ${{ secrets.BLOCKFROST_PROJECT_ID }}
BLOCKFROST_IPFS_PROJECT_ID: ${{ secrets.BLOCKFROST_IPFS_PROJECT_ID }}
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v4

- name: Set up Go
uses: actions/setup-go@v2
with:
go-version: 1.17
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: 1.21.4

- name: Test
run: go clean -testcache && go test -v
- name: Test
run: go clean -testcache && go test -v
52 changes: 26 additions & 26 deletions api_account.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type Account struct {
Active bool `json:"active"`

// Epoch of the most recent action - registration or deregistration
ActiveEpoch int64 `json:"active_epoch"`
ActiveEpoch *int64 `json:"active_epoch"`

// Balance of the account in Lovelaces
ControlledAmount string `json:"controlled_amount"`
Expand All @@ -52,7 +52,7 @@ type Account struct {
WithdrawableAmount string `json:"withdrawable_amount"`

// Bech32 pool ID that owns the account
PoolID string `json:"pool_id"`
PoolID *string `json:"pool_id"`
}

// AccountRewardsHist return Account reward history
Expand Down Expand Up @@ -267,11 +267,11 @@ func (c *apiClient) AccountRewardsHistoryAll(ctx context.Context, stakeAddress s
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -339,11 +339,11 @@ func (c *apiClient) AccountHistoryAll(ctx context.Context, address string) <-cha
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -411,11 +411,11 @@ func (c *apiClient) AccountDelegationHistoryAll(ctx context.Context, stakeAddres
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -483,11 +483,11 @@ func (c *apiClient) AccountRegistrationHistoryAll(ctx context.Context, stakeAddr
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -555,11 +555,11 @@ func (c *apiClient) AccountWithdrawalHistoryAll(ctx context.Context, stakeAddres
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -627,11 +627,11 @@ func (c *apiClient) AccountMIRHistoryAll(ctx context.Context, stakeAddress strin
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -698,11 +698,11 @@ func (c *apiClient) AccountAssociatedAddressesAll(ctx context.Context, stakeAddr
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -770,11 +770,11 @@ func (c *apiClient) AccountAssociatedAssetsAll(ctx context.Context, stakeAddress
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down
90 changes: 82 additions & 8 deletions api_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type Address struct {
Amount []AddressAmount `json:"amount"`

// Stake address that controls the key
StakeAddress string `json:"stake_address"`
StakeAddress *string `json:"stake_address"`

// Address era.
// Enum: "byron" "shelley"
Expand Down Expand Up @@ -62,6 +62,7 @@ type AddressTransactions struct {
}

type AddressUTXO struct {
Address string `json:"address"`
// Transaction hash of the UTXO
TxHash string `json:"tx_hash"`

Expand All @@ -73,7 +74,10 @@ type AddressUTXO struct {
Block string `json:"block"`

// The hash of the transaction output datum
DataHash string `json:"data_hash"`
DataHash *string `json:"data_hash"`

InlineDatum *string `json:"inline_datum"`
ReferenceScriptHash *string `json:"reference_script_hash"`
}

type AddressTxResult struct {
Expand Down Expand Up @@ -162,11 +166,11 @@ func (c *apiClient) AddressTransactionsAll(ctx context.Context, address string)
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down Expand Up @@ -253,11 +257,81 @@ func (c *apiClient) AddressUTXOsAll(ctx context.Context, address string) <-chan
}
go func() {
defer close(ch)
fetchScripts := true
for i := 1; fetchScripts; i++ {
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
}

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

func (c *apiClient) AddressUTXOsAsset(ctx context.Context, address, asset string, query APIQueryParams) (utxos []AddressUTXO, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s/%s", c.server, resourceAddresses, address, resourceUTXOs, asset))
if err != nil {
return
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, requestUrl.String(), nil)
if err != nil {
return
}
v := req.URL.Query()
query.From = ""
query.To = ""
v = formatParams(v, query)
req.URL.RawQuery = v.Encode()

res, err := c.handleRequest(req)
if err != nil {
return
}
defer res.Body.Close()

if err = json.NewDecoder(res.Body).Decode(&utxos); err != nil {
return
}
return utxos, nil
}

func (c *apiClient) AddressUTXOsAssetAll(ctx context.Context, address, asset string) <-chan AddressUTXOResult {
ch := make(chan AddressUTXOResult, 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 AddressUTXOResult, wg *sync.WaitGroup) {
defer wg.Done()
for j := range jobs {
autxo, err := c.AddressUTXOsAsset(j.ctx, address, asset, j.query)
if len(autxo) != j.query.Count || err != nil {
select {
case quit <- true:
default:
}
}
res := AddressUTXOResult{Res: autxo, Err: err}
ch <- res
}

}(jobs, ch, &wg)
}
go func() {
defer close(ch)
fetchNextPage := true
for i := 1; fetchNextPage; i++ {
select {
case <-quit:
fetchScripts = false
fetchNextPage = false
default:
jobs <- methodOptions{ctx: ctx, query: APIQueryParams{Count: 100, Page: i}}
}
Expand Down
25 changes: 24 additions & 1 deletion api_addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ var (
const testdata = "testdata"

func TestAddressUnMarshall(t *testing.T) {
stakeAddress := "stake1ux3u6x5cs388djqz6awnyuvez2f6n8jzjhqq59s4yxhm8jskeh0t9"

want := blockfrost.Address{
Address: "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz",
Amount: []blockfrost.AddressAmount{{Unit: "lovelace", Quantity: "0"}},
StakeAddress: "stake1ux3u6x5cs388djqz6awnyuvez2f6n8jzjhqq59s4yxhm8jskeh0t9",
StakeAddress: &stakeAddress,
Type: "shelley",
Script: false,
}
Expand Down Expand Up @@ -152,3 +154,24 @@ func TestAddressUTXOs(t *testing.T) {
var want []blockfrost.AddressUTXO
testIntUtil(t, fp, &got, &want)
}

func TestAddressUTXOsAsset(t *testing.T) {
addr := "addr1q8zsjx7vxkl4esfejafhxthyew8c54c9ch95gkv3nz37sxrc9ty742qncmffaesxqarvqjmxmy36d9aht2duhmhvekgq3jd3w2"
asset := "d436d9f6b754582f798fe33f4bed12133d47493f78b944b9cc55fd1853756d6d69744c6f64676534393539"

api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{})

got, err := api.AddressUTXOsAsset(
context.TODO(),
addr,
asset,
blockfrost.APIQueryParams{},
)
if err != nil {
t.Fatal(err)
}

fp := filepath.Join(testdata, strings.ToLower(strings.TrimLeft(t.Name(), "Test"))+".golden")
var want []blockfrost.AddressUTXO
testIntUtil(t, fp, &got, &want)
}
Loading