Skip to content

Commit

Permalink
Add new methods getFiatRates & getUserExchangeRewards (#179)
Browse files Browse the repository at this point in the history
* add new methods getFiatRates & getUserExchangeRewards

* remove unused

* update text in docs

* remove usdToDaiRate

* remove unused methods, update docs

* update docs

* improve date in milliseconds
  • Loading branch information
dfkadyr authored Oct 3, 2024
1 parent 04f2c8e commit 5d48fc9
Show file tree
Hide file tree
Showing 19 changed files with 275 additions and 338 deletions.
156 changes: 67 additions & 89 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,28 +89,28 @@ const sdk = new StakeWiseSDK({
## Quick Links

##### Request table:
| **Vault** | **osToken** | **RewardSplitter** |
|---------------------------------------------------------------|--------------------------------------------------------------|-------------------------------------------------------------------|
| [vault.getStakerActions](#sdkvaultgetstakeractions) | [osToken.getBurnAmount](#sdkostokengetburnamount) | [rewardSplitter.getClaimAmount](#sdkrewardsplittergetclaimamount) |
| [vault.getSnapshots](#sdkvaultgetsnapshots) | [osToken.getHealthFactor](#sdkostokengethealthfactor) | |
| [vault.getExitQueuePositions](#sdkvaultgetexitqueuepositions) | [osToken.getAPY](#sdkostokengetapy) | |
| [vault.getValidators](#sdkvaultgetvalidators) | [osToken.getPosition](#sdkostokengetposition) | |
| [vault.getVault](#sdkvaultgetvault) | [osToken.getMaxMint](#sdkostokengetmaxmint) | |
| [vault.getMaxWithdraw](#sdkvaultgetmaxwithdraw) | [osToken.getSharesFromAssets](#sdkostokengetsharesfromassets) | |
| [vault.getHarvestParams](#sdkvaultgetharvestparams) | [osToken.getAssetsFromShares](#sdkostokengetassetsfromshares) | |
| [vault.getStakeBalance](#sdkvaultgetstakebalance) | [osToken.getRate](#sdkostokengetrate) | |
| [vault.getScorePercentiles](#sdkvaultgetscorepercentiles) | [osToken.getConfig](#sdkostokengetconfig) | |
| [vault.getUserRewards](#sdkvaultgetuserrewards) | | |
| [vault.getWhitelist](#sdkvaultgetwhitelist) | | |
| [vault.getBlocklist](#sdkvaultgetblocklist) | | |
| [vault.getRewardSplitters](#sdkvaultgetrewardsplitters) | | |
| [vault.getVaultStats](#sdkvaultgetvaultstats) | | |
| [vault.getUserStats](#sdkvaultgetuserstats) | | |
| **Vault** | **osToken** | **RewardSplitter** |
|-----------------------------------------------------------------|--------------------------------------------------------------|-------------------------------------------------------------------|
| [vault.getStakerActions](#sdkvaultgetstakeractions) | [osToken.getBurnAmount](#sdkostokengetburnamount) | [rewardSplitter.getClaimAmount](#sdkrewardsplittergetclaimamount) |
| [vault.getExitQueuePositions](#sdkvaultgetexitqueuepositions) | [osToken.getAPY](#sdkostokengetapy) | |
| [vault.getValidators](#sdkvaultgetvalidators) | [osToken.getPosition](#sdkostokengetposition) | |
| [vault.getVault](#sdkvaultgetvault) | [osToken.getMaxMint](#sdkostokengetmaxmint) | |
| [vault.getMaxWithdraw](#sdkvaultgetmaxwithdraw) | [osToken.getSharesFromAssets](#sdkostokengetsharesfromassets) | |
| [vault.getHarvestParams](#sdkvaultgetharvestparams) | [osToken.getAssetsFromShares](#sdkostokengetassetsfromshares) | |
| [vault.getStakeBalance](#sdkvaultgetstakebalance) | [osToken.getRate](#sdkostokengetrate) | |
| [vault.getScorePercentiles](#sdkvaultgetscorepercentiles) | [osToken.getConfig](#sdkostokengetconfig) | |
| [vault.getUserRewards](#sdkvaultgetuserrewards) | [osToken.getHealthFactor](#sdkostokengethealthfactor) | |
| [vault.getWhitelist](#sdkvaultgetwhitelist) | | |
| [vault.getBlocklist](#sdkvaultgetblocklist) | | |
| [vault.getRewardSplitters](#sdkvaultgetrewardsplitters) | | |
| [vault.getVaultStats](#sdkvaultgetvaultstats) | | |
| [vault.getUserStats](#sdkvaultgetuserstats) | | |

| **Utils** |
|-----------------------------------------------------|
| [utils.getSwiseUsdPrice](#sdkutilsgetswiseusdprice) |
| [utils.getTransactions](#sdkutilsgettransactions) |
| [utils.getFiatRates](#sdkutilsgetfiatrates) |


All of these methods (except synchronous getHealthFactor) return a promise that can be
Expand Down Expand Up @@ -207,49 +207,6 @@ await sdk.vault.getStakerActions({
})
```
---
### `sdk.vault.getSnapshots`

#### Description:

Deprecated, use `sdk.vault.getVaultStats` instead.

TVL and APY snapshots for the vault. With the help of this data it is possible to build a chart.

#### Arguments:

| Name | Type | Type | Description |
|--------------|----------|-----------------|---------|
| vaultAddress | `string` | **Yes** | - |
| dateFrom | `number` | **Yes** | Time to start |

#### Returns:

```ts
type Snapshot = {
APY: number
TVL: string
}

type Output = {
days: Record<number, Snapshot>
first: Snapshot
}
```
| Name | Description |
|------|-------------|
| `days` | The result of the query on your parameters, is returned as an object where the keys are timestamps |
| `first` | We always send the very first saved snapshot regardless of the request parameters, this helps for rendering the chart |
#### Example:
```ts
await sdk.vault.getSnapshots({
vaultAddress: '0x...',
dateFrom: 1695730032793,
})
```
---
### `sdk.vault.getScorePercentiles`

#### Description:
Expand Down Expand Up @@ -285,48 +242,46 @@ await sdk.vault.getScorePercentiles()
#### Description:
Deprecated, use `sdk.vault.getUserStats` instead.
Daily rewards for the user who has made a deposit in the vault. With the help of this data it is possible to build a chart.
Daily rewards for the user who has made a deposit in the vault.
#### Arguments:
| Name | Type | Type | Description |
|------|----------|-------------|---|
| vaultAddress | `string` | **Yes** | - |
| userAddress | `string` | **Yes** | - |
| dateFrom | `number` | **Yes** | Time to start |
| dateTo | `number` | **No** | Time to end |
| fillGaps | `boolean` | **No** | Fill in the empty days with zeros |
| Name | Type | Required | Description |
|------|----------|----------|---|
| dateFrom | `number` | **Yes** | Time to start in milliseconds |
| dateTo | `number` | **Yes** | Time to end in milliseconds |
| userAddress | `string` | **Yes** | The user address |
| vaultAddress | `string` | **Yes** | The address of the vault |
#### Returns:
```ts
type UserReward = {
date: number
sumRewards: string
dailyRewards: string
dailyRewardsEur: string
dailyRewardsGbp: string
dailyRewardsUsd: string
}

type Output = {
days: Record<number, UserReward>
date: number
dailyRewards: number
dailyRewardsEur: number
dailyRewardsGbp: number
dailyRewardsUsd: number
}
```
| Name | Description |
|------|-------------|
| `days` | The result of the query on your parameters, is returned as an object where the keys are timestamps |
| Name | Description |
|------|---------------------------|
| `date` | Сurrent rate date |
| `dailyRewards` | Daily reward asset in ETH |
| `dailyRewardsEur` | Daily reward asset in EUR |
| `dailyRewardsGbp` | Daily reward asset in GBP |
| `dailyRewardsUsd` | Daily reward asset in USD |
#### Example:
```ts
await sdk.vault.getUserRewards({
userAddress: '0x...',
vaultAddress: '0x...',
dateFrom: 1695730032793,
dateTo: 1727827200000,
dateFrom: 1721606400000,
})
```
---
Expand Down Expand Up @@ -866,12 +821,12 @@ type Output = {
}
```
| Name | Description |
|------|---------------------------------------------------------------------------------------------------------|
| `time` | Date and time for each data point |
| `apy` | Current APY based on time, rewards and balance. The information is taken from allocatorStats_collection |
| `rewards` | Number of assets earned by the user in current vault during the interval in ETH |
| `balance` | Total assets by the user in current vault at the moment of time in ETH |
| Name | Description |
|------|--------------------------------------------------------------------------------------------------------|
| `time` | Date and time for each data point |
| `apy` | Current APY based on time, rewards and balance. |
| `rewards` | Number of assets earned by the user in current vault during the interval in ETH |
| `balance` | Total assets by the user in current vault at the moment of time in ETH |
#### Example:
Expand All @@ -883,6 +838,7 @@ await sdk.vault.getUserStats({
})
```
---
## API-osToken
### `sdk.osToken.getBurnAmount`
Expand Down Expand Up @@ -1370,6 +1326,28 @@ type Output = Array<{
await sdk.utils.getTransactions({ hash: '0x...' })
```
---
### `sdk.utils.getFiatRates`
#### Description:
Returns the USD, EUR, GBP exchange rates for the current asset
#### Returns:
```ts
type Output = {
assetsUsdRate: number
usdToEurRate: number
usdToGbpRate: number
}
```
#### Example:
```ts
await sdk.utils.getFiatRates()
```
---
## Transactions
Transactions work through the provider you sent when creating an instance of our SDK class. Or, if you are a custodian, you can get the transaction data and sign it yourself. Each transaction also gives you the opportunity to get an approximate gas for sending it. For custodians, it is more reliable to calculate the gas yourself. Each transaction has encode and estimateGas methods for your convenience
Expand Down
31 changes: 27 additions & 4 deletions changelog/next-release.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,38 @@ type RemovedOutput = {
---
---
### 10. Deprecated method `sdk.vault.getSnapshots`
### 10. Removed method `sdk.vault.getSnapshots`
### Use `sdk.vault.getVaultStats` instead
---
### 11. Update method `sdk.vault.getUserRewards`
#### Update output format:
```ts
type Output = {
date: number
dailyRewards: number
dailyRewardsEur: number
dailyRewardsGbp: number
dailyRewardsUsd: number
}[]
```
#### New arguments:
| Name | Type | Type | Description |
|------|----------|-------------|---|
| dateFrom | `number` | **Yes** | Time to start in milliseconds |
| dateTo | `number` | **Yes** | Time to end in milliseconds |
| userAddress | `string` | **Yes** | The user address |
| vaultAddress | `string` | **Yes** | The address of the vault |
---
### 11. Deprecated method `sdk.vault.getUserRewards`
### Use `sdk.vault.getUserStats` instead
### 12. Added method getFiatRates
### `sdk.utils.getFiatRates`
---
3 changes: 0 additions & 3 deletions src/graphql/backend/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ export type { SnapshotsQueryPayload, SnapshotsQueryVariables } from './snapshots
export { fetchValidatorsQuery } from './validatorsQuery.graphql'
export type { ValidatorsQueryPayload, ValidatorsQueryVariables } from './validatorsQuery.graphql'

export { fetchUserRewardsQuery } from './userRewardsQuery.graphql'
export type { UserRewardsQueryPayload, UserRewardsQueryVariables } from './userRewardsQuery.graphql'

export { fetchScorePercentilesQuery } from './scorePercentilesQuery.graphql'
export type { ScorePercentilesQueryPayload, ScorePercentilesQueryVariables } from './scorePercentilesQuery.graphql'

Expand Down
10 changes: 0 additions & 10 deletions src/graphql/backend/vault/userRewardsQuery.graphql

This file was deleted.

7 changes: 7 additions & 0 deletions src/graphql/subgraph/stats/fiatRatesQuery.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
query FiatRates {
networks {
assetsUsdRate
usdToEurRate
usdToGbpRate
}
}
3 changes: 3 additions & 0 deletions src/graphql/subgraph/stats/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
export { fetchStatsQuery } from './statsQuery.graphql'
export type { StatsQueryPayload, StatsQueryVariables } from './statsQuery.graphql'

export { fetchFiatRatesQuery } from './fiatRatesQuery.graphql'
export type { FiatRatesQueryPayload, FiatRatesQueryVariables } from './fiatRatesQuery.graphql'
3 changes: 3 additions & 0 deletions src/graphql/subgraph/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ export type { UserStatsQueryPayload, UserStatsQueryVariables } from './userStats
export { fetchVaultStatsQuery } from './vaultStatsQuery.graphql'
export type { VaultStatsQueryPayload, VaultStatsQueryVariables } from './vaultStatsQuery.graphql'

export { fetchUserRewardsQuery } from './userRewardsQuery.graphql'
export type { UserRewardsQueryPayload, UserRewardsQueryVariables } from './userRewardsQuery.graphql'

export { fetchHarvestParamsQuery } from './harvestParamsQuery.graphql'
export type { HarvestParamsQueryPayload, HarvestParamsQueryVariables } from './harvestParamsQuery.graphql'

Expand Down
18 changes: 18 additions & 0 deletions src/graphql/subgraph/vault/userRewardsQuery.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
query UserRewards($user: Bytes!, $vaultAddress: String!, $dateFrom: Timestamp!, $dateTo: Timestamp!) {
exchangeRate: exchangeRateStats_collection(
interval: day
where: { timestamp_gte: $dateFrom, timestamp_lte: $dateTo }
) {
timestamp
assetsUsdRate
usdToEurRate
usdToGbpRate
}
allocator: allocatorStats_collection(
interval: day
where: { timestamp_gte: $dateFrom, timestamp_lte: $dateTo, allocator_: { address: $user, vault: $vaultAddress } }
) {
timestamp
earnedAssets
}
}
42 changes: 42 additions & 0 deletions src/methods/utils/getFiatRates.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import graphql from '../../graphql'
import { apiUrls, configs, Network } from '../../utils'


type GetFiatRatesInput = {
options: StakeWise.Options
}

const getMainnetGbpRate = () => {
return graphql.subgraph.stats.fetchFiatRatesQuery({
url: configs[Network.Mainnet].api.subgraph,
modifyResult: (data): number => {
const usdInGbp = Number(data.networks[0].usdToGbpRate)

return usdInGbp
},
})
}

const getFiatRates = (values: GetFiatRatesInput) => {
const { options } = values

return graphql.subgraph.stats.fetchFiatRatesQuery({
url: apiUrls.getSubgraphqlUrl(options),
modifyResult: async (data) => {
const usd = Number(data.networks[0].assetsUsdRate)
const usdInEur = Number(data.networks[0].usdToEurRate)

const isGnosis = [ Network.Gnosis, Network.Chiado ].includes(options.network)
const usdInGbp = isGnosis ? await getMainnetGbpRate() : Number(data.networks[0].usdToGbpRate)

return {
assetsUsdRate: usd,
usdToEurRate: usdInEur,
usdToGbpRate: usdInGbp,
}
},
})
}


export default getFiatRates
1 change: 1 addition & 0 deletions src/methods/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export type { BaseInput } from './types'
export { default as getFiatRates } from './getFiatRates'
export { default as getTransactions } from './getTransactions'
export { default as getSwiseUsdPrice } from './getSwiseUsdPrice'
export { default as getStakewiseStats } from './getStakewiseStats'
Expand Down
Loading

0 comments on commit 5d48fc9

Please sign in to comment.