Skip to content

Commit

Permalink
Add accounts and addresses endpoints
Browse files Browse the repository at this point in the history
  • Loading branch information
Salvionied committed Sep 19, 2023
1 parent 070d879 commit 7944a78
Show file tree
Hide file tree
Showing 8 changed files with 548 additions and 1 deletion.
119 changes: 119 additions & 0 deletions client/accounts.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package client

import (
"encoding/json"
"fmt"

"github.com/maestro-org/go-sdk/models"
"github.com/maestro-org/go-sdk/utils"
)

func (c *Client) AccountAddresses(stakeAddr string, params *utils.Parameters) (*models.AccountAddresses, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/accounts/%s/addresses%s", stakeAddr, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var accountAddresses models.AccountAddresses
err = json.NewDecoder(resp.Body).Decode(&accountAddresses)
if err != nil {
return nil, err
}
return &accountAddresses, nil
}

func (c *Client) AccountAssets(stakeAddr string, params *utils.Parameters) (*models.AccountAssets, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/accounts/%s/assets%s", stakeAddr, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var accountAssets models.AccountAssets
err = json.NewDecoder(resp.Body).Decode(&accountAssets)
if err != nil {
return nil, err
}
return &accountAssets, nil
}

func (c *Client) StakeAccountHistory(stakeAddr string, params *utils.Parameters) (*models.StakeAccountHistory, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/accounts/%s/history%s", stakeAddr, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var stakeAccountHistory models.StakeAccountHistory
err = json.NewDecoder(resp.Body).Decode(&stakeAccountHistory)
if err != nil {
return nil, err
}
return &stakeAccountHistory, nil
}

func (c *Client) StakeAccountInformation(stakeAddr string) (*models.StakeAccountInformation, error) {
url := fmt.Sprintf("/accounts/%s", stakeAddr)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var stakeAccountInformation models.StakeAccountInformation
err = json.NewDecoder(resp.Body).Decode(&stakeAccountInformation)
if err != nil {
return nil, err
}
return &stakeAccountInformation, nil
}

func (c *Client) StakeAccountRewards(stakeAddr string, params *utils.Parameters) (*models.StakeAccountRewards, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/accounts/%s/rewards%s", stakeAddr, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var stakeAccountRewards models.StakeAccountRewards
err = json.NewDecoder(resp.Body).Decode(&stakeAccountRewards)
if err != nil {
return nil, err
}
return &stakeAccountRewards, nil
}

func (c *Client) StakeAccountUpdates(stakeAddr string, params *utils.Parameters) (*models.StakeAccountUpdates, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/accounts/%s/updates%s", stakeAddr, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var stakeAccountUpdates models.StakeAccountUpdates
err = json.NewDecoder(resp.Body).Decode(&stakeAccountUpdates)
if err != nil {
return nil, err
}
return &stakeAccountUpdates, nil
}
158 changes: 158 additions & 0 deletions client/addresses.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
package client

import (
"encoding/json"
"fmt"

"github.com/maestro-org/go-sdk/models"
"github.com/maestro-org/go-sdk/utils"
)

func (c *Client) DecodeAddress(address string) (*models.DecodedAddress, error) {
url := fmt.Sprintf("/addresses/%s/decode/", address)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var decodedAddress models.DecodedAddress
err = json.NewDecoder(resp.Body).Decode(&decodedAddress)
if err != nil {
return nil, err
}
return &decodedAddress, nil
}

func (c *Client) AddressTransactionCount(address string) (*models.AddressTransactionCount, error) {
url := fmt.Sprintf("/addresses/%s/transactions/count", address)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var addressTransactionCount models.AddressTransactionCount
err = json.NewDecoder(resp.Body).Decode(&addressTransactionCount)
if err != nil {
return nil, err
}
return &addressTransactionCount, nil
}

func (c *Client) AddressTransactions(address string, params *utils.Parameters) (*models.AddressTransactions, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/addresses/%s/transactions%s", address, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var addressTransactions models.AddressTransactions
err = json.NewDecoder(resp.Body).Decode(&addressTransactions)
if err != nil {
return nil, err
}
return &addressTransactions, nil
}

func (c *Client) PaymentCredentialTransactions(paymentCredential string, params *utils.Parameters) (*models.AddressTransactions, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/addresses/cred/%s/transactions%s", paymentCredential, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var addressTransactions models.AddressTransactions
err = json.NewDecoder(resp.Body).Decode(&addressTransactions)
if err != nil {
return nil, err
}
return &addressTransactions, nil
}

func (c *Client) UtxoReferencesAtAddress(address string, params *utils.Parameters) (*models.UtxoReferencesAtAddress, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/addresses/%s/utxo_refs%s", address, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var utxoReferencesAtAddress models.UtxoReferencesAtAddress
err = json.NewDecoder(resp.Body).Decode(&utxoReferencesAtAddress)
if err != nil {
return nil, err
}
return &utxoReferencesAtAddress, nil
}

func (c *Client) UtxosAtAddress(address string, params *utils.Parameters) (*models.UtxosAtAddress, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/addresses/%s/utxos%s", address, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var utxosAtAddress models.UtxosAtAddress
err = json.NewDecoder(resp.Body).Decode(&utxosAtAddress)
if err != nil {
return nil, err
}
return &utxosAtAddress, nil
}

func (c *Client) UtxosAtAddresses(addresses []string, params *utils.Parameters) (*models.UtxosAtAddress, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/addresses/utxos%s", formattedParams)
body := struct {
Addresses []string `json:"addresses"`
}{
Addresses: addresses,
}
resp, err := c.post(url, body)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var utxosAtAddress models.UtxosAtAddress
err = json.NewDecoder(resp.Body).Decode(&utxosAtAddress)
if err != nil {
return nil, err
}
return &utxosAtAddress, nil
}

func (c *Client) UtxosByPaymentCredential(paymentCredential string, params *utils.Parameters) (*models.UtxosAtAddress, error) {
formattedParams := ""
if params != nil {
formattedParams = params.Format()
}
url := fmt.Sprintf("/addresses/cred/%s/utxos%s", paymentCredential, formattedParams)
resp, err := c.get(url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
var utxosAtAddress models.UtxosAtAddress
err = json.NewDecoder(resp.Body).Decode(&utxosAtAddress)
if err != nil {
return nil, err
}
return &utxosAtAddress, nil
}
26 changes: 26 additions & 0 deletions client/client.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package client

import (
"bytes"
"encoding/json"
"errors"
"fmt"
Expand Down Expand Up @@ -75,3 +76,28 @@ func (c *Client) sendRequest(req *http.Request, responseBody *string) error {

return nil
}

func (c *Client) get(url string) (*http.Response, error) {
req, err := http.NewRequest("GET", c.baseUrl+url, nil)
req.Header.Set("Accept", "application/json")
req.Header.Add("api-key", c.apiKey)
if err != nil {
return nil, err
}
return c.HTTPClient.Do(req)
}

func (c *Client) post(url string, body interface{}) (*http.Response, error) {
jsonBody, err := json.Marshal(body)
if err != nil {
return nil, err
}
req, err := http.NewRequest("POST", c.baseUrl+url, bytes.NewReader(jsonBody))
req.Header.Set("Accept", "application/json")
req.Header.Add("api-key", c.apiKey)
if err != nil {
return nil, err
}
req.Header.Set("Content-Type", "application/json")
return c.HTTPClient.Do(req)
}
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module github.com/maestro-org/go-sdk

go 1.21.0
go 1.20
Loading

0 comments on commit 7944a78

Please sign in to comment.