Skip to content

Commit

Permalink
Merge pull request #84 from blockfrost/chore/address-extended
Browse files Browse the repository at this point in the history
chore: add AddressExtended (/address/:addr/extended)
  • Loading branch information
slowbackspace authored Jan 6, 2025
2 parents ca0c5e2 + 4fb3ae7 commit 75bbb41
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 1 deletion.
47 changes: 47 additions & 0 deletions api_addresses.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,21 @@ const (
resourceTotal = "total"
resourceTransactions = "transactions"
resourceUTXOs = "utxos"
resourceExtended = "extended"
)

type AddressAmount struct {
Unit string `json:"unit"`
Quantity string `json:"quantity"`
}

type AddressAmountExtended struct {
Unit string `json:"unit"`
Quantity string `json:"quantity"`
Decimals *int `json:"decimals"`
HasNftOnchainMetadata bool `json:"has_nft_onchain_metadata"`
}

type Address struct {
// Bech32 encoded addresses
Address string `json:"address"`
Expand All @@ -47,6 +55,22 @@ type AddressDetails struct {
TxCount int `json:"tx_count"`
}

type AddressExtended struct {
// Bech32 encoded addresses
Address string `json:"address"`
Amount []AddressAmountExtended `json:"amount"`

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

// Address era.
// Enum: "byron" "shelley"
Type string `json:"type"`

// True if this is a script address
Script bool `json:"script"`
}

type AddressTransactions struct {
// Hash of the transaction
TxHash string `json:"tx_hash"`
Expand Down Expand Up @@ -342,3 +366,26 @@ func (c *apiClient) AddressUTXOsAssetAll(ctx context.Context, address, asset str
}()
return ch
}

func (c *apiClient) AddressExtended(ctx context.Context, address string) (addrExtended AddressExtended, err error) {
requestUrl, err := url.Parse(fmt.Sprintf("%s/%s/%s/%s", c.server, resourceAddresses, address, resourceExtended))
if err != nil {
return
}

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

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

if err = json.NewDecoder(res.Body).Decode(&addrExtended); err != nil {
return
}
return addrExtended, nil
}
43 changes: 43 additions & 0 deletions api_addresses_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,35 @@ func TestAddressDetailsUnMarshall(t *testing.T) {
testStructGotWant(t, fp, &got, &want)
}

func TestAddressExtendedUnMarshall(t *testing.T) {
StakeAddress := "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7"
Decimals := 6

want := blockfrost.AddressExtended{
Address: "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz",
Amount: []blockfrost.AddressAmountExtended{
{
Unit: "lovelace",
Quantity: "42000000",
Decimals: &Decimals,
HasNftOnchainMetadata: false},
{
Unit: "b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a76e7574636f696e",
Quantity: "12",
Decimals: nil,
HasNftOnchainMetadata: true,
},
},
StakeAddress: &StakeAddress,
Type: "shelley",
Script: false,
}

fp := filepath.Join(testdata, "json", "address", "address_extended.json")
got := blockfrost.AddressDetails{}
testStructGotWant(t, fp, &got, &want)
}

func WriteGoldenFile(t *testing.T, path string, bytes []byte) {
t.Helper()
err := os.MkdirAll(filepath.Dir(path), 0777)
Expand Down Expand Up @@ -117,6 +146,20 @@ func TestResourceAddressDetails(t *testing.T) {
testIntUtil(t, fp, &got, &want)
}

func TestResourceAddressExtended(t *testing.T) {
addr := "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz"
api := blockfrost.NewAPIClient(blockfrost.APIClientOptions{})

got, err := api.AddressExtended(context.TODO(), addr)
if err != nil {
t.Fatal(err)
}
fp := filepath.Join(testdata, strings.ToLower(strings.TrimLeft(t.Name(), "Test"))+".golden")
want := blockfrost.AddressExtended{}

testIntUtil(t, fp, &got, &want)
}

func TestResourceAddressTransactions(t *testing.T) {
addr := "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz"

Expand Down
1 change: 1 addition & 0 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ type APIClient interface {
EpochParameters(ctx context.Context, epochNumber int) (EpochParameters, error)
Address(ctx context.Context, address string) (Address, error)
AddressDetails(ctx context.Context, address string) (AddressDetails, error)
AddressExtended(ctx context.Context, address string) (AddressExtended, error)
AddressTransactions(ctx context.Context, address string, query APIQueryParams) ([]AddressTransactions, error)
AddressTransactionsAll(ctx context.Context, address string) <-chan AddressTxResult
AddressUTXOs(ctx context.Context, address string, query APIQueryParams) ([]AddressUTXO, error)
Expand Down
20 changes: 20 additions & 0 deletions testdata/json/address/address_extended.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"address": "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz",
"amount": [
{
"unit": "lovelace",
"quantity": "42000000",
"decimals": 6,
"has_nft_onchain_metadata": false
},
{
"unit": "b0d07d45fe9514f80213f4020e5a61241458be626841cde717cb38a76e7574636f696e",
"quantity": "12",
"decimals": null,
"has_nft_onchain_metadata": true
}
],
"stake_address": "stake1ux3g2c9dx2nhhehyrezyxpkstartcqmu9hk63qgfkccw5rqttygt7",
"type": "shelley",
"script": false
}
14 changes: 14 additions & 0 deletions testdata/resourceaddressextended.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"address": "addr1qxqs59lphg8g6qndelq8xwqn60ag3aeyfcp33c2kdp46a09re5df3pzwwmyq946axfcejy5n4x0y99wqpgtp2gd0k09qsgy6pz",
"amount": [
{
"unit": "lovelace",
"quantity": "0",
"decimals": 6,
"has_nft_onchain_metadata": false
}
],
"stake_address": "stake1ux3u6x5cs388djqz6awnyuvez2f6n8jzjhqq59s4yxhm8jskeh0t9",
"type": "shelley",
"script": false
}
2 changes: 1 addition & 1 deletion testdata/resourceinfointegration.golden
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"url": "https://blockfrost.io/",
"version": "1.7.0"
"version": "3.0.1"
}

0 comments on commit 75bbb41

Please sign in to comment.