Skip to content

Commit

Permalink
update readme with examples
Browse files Browse the repository at this point in the history
  • Loading branch information
davidzhangg committed Mar 27, 2024
1 parent 25c076b commit 1e4dfee
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
60 changes: 60 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,66 @@ The `XykService` refers to the [Xy=k API endpoints](https://www.covalenthq.com/d
- `GetEcosystemChartData()`: Get a 7d and 30d time-series chart of DEX activity. Includes volume and swap count.
- `GetHealthData()`: Ping the health of xy=k endpoints to get the synced block height per chain.

## Additional Helper Functions
### calculate_pretty_balance
The `CalculatePrettyBalance` function is designed to take up to 4 inputs: the `Balance` field obtained from the `TokenBalances` endpoint and the `ContractDecimals`. The function also includes two optional fields, `roundOff` and `precision`, to allow developers to round the unscaled balance to a certain decimal precision. The primary purpose of this function is to convert the scaled token balance (the balance parameter) into its unscaled, human-readable form. The scaled balance needs to be divided by 10^(contractDecimals) to remove the scaling factor.

```go
package main

import (
"fmt"
"github.com/covalenthq/covalent-api-sdk-go/covalentclient"
"github.com/covalenthq/covalent-api-sdk-go/chains"
"github.com/covalenthq/covalent-api-sdk-go/valuefmt"
)

func main() {
var Client = covalentclient.CovalentClient("API_KEY")
resp, err := Client.BalanceService.GetTokenBalancesForWalletAddress(chains.EthMainnet, "demo.eth")
if err != nil {
fmt.Printf("error: %s", err)
} else {
if len(resp.Data.Items) != 0 {
// get first Balance
prettybalance := valuefmt.CalculatePrettyBalance(*resp.Data.Items[0].Balance, *resp.Data.Items[0].ContractDecimals, true, 0)
fmt.Println(prettybalance)
}
}
}
```
### prettify_currency
The `PrettifyCurrency` function refines the presentation of a monetary value, accepting a numerical amount and a fiat currency code as parameters (with USD as the default currency). It simplifies currency formatting for developers, ensuring visually polished representations of financial information in user interfaces for an enhanced user experience.

```go
package main

import (
"fmt"
"github.com/covalenthq/covalent-api-sdk-go/chains"
"github.com/covalenthq/covalent-api-sdk-go/covalentclient"
"github.com/covalenthq/covalent-api-sdk-go/quotes"
"github.com/covalenthq/covalent-api-sdk-go/valuefmt"
)

func main() {
var Client = covalentclient.CovalentClient("API_KEY")
resp, err := Client.BalanceService.GetTokenBalancesForWalletAddress(chains.EthMainnet, "demo.eth")
if err != nil {
fmt.Printf("error: %s", err)
} else {
if len(resp.Data.Items) != 0 {
// get first Balance
opts := valuefmt.NewCurrencyOptions();
opts.Currency = quotes.CAD
opts.Decimals = 5
prettyCurrency := valuefmt.PrettifyCurrency(*resp.Data.Items[0].QuoteRate, opts)
fmt.Println(prettyCurrency)
}
}
}
```

## Built-in SDK Features
### Explaining Pagination Mechanism Within the SDK

Expand Down
3 changes: 3 additions & 0 deletions valuefmt/calculate_pretty_balance.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package valuefmt
import (
"fmt"
"math/big"
"github.com/covalenthq/covalent-api-sdk-go/utils"
)

func CalculatePrettyBalance(value interface{}, decimals int, roundOff bool, precision int) string {
Expand All @@ -12,6 +13,8 @@ func CalculatePrettyBalance(value interface{}, decimals int, roundOff bool, prec
bigDecimalValue = new(big.Float).SetFloat64(v)
case int:
bigDecimalValue = new(big.Float).SetInt64(int64(v))
case utils.BigInt:
bigDecimalValue = new(big.Float).SetInt(v.Int)
default:
return "-1"
}
Expand Down

0 comments on commit 1e4dfee

Please sign in to comment.