Skip to content

Commit 5d48fc9

Browse files
authored
Add new methods getFiatRates & getUserExchangeRewards (#179)
* add new methods getFiatRates & getUserExchangeRewards * remove unused * update text in docs * remove usdToDaiRate * remove unused methods, update docs * update docs * improve date in milliseconds
1 parent 04f2c8e commit 5d48fc9

19 files changed

+275
-338
lines changed

README.md

+67-89
Original file line numberDiff line numberDiff line change
@@ -89,28 +89,28 @@ const sdk = new StakeWiseSDK({
8989
## Quick Links
9090

9191
##### Request table:
92-
| **Vault** | **osToken** | **RewardSplitter** |
93-
|---------------------------------------------------------------|--------------------------------------------------------------|-------------------------------------------------------------------|
94-
| [vault.getStakerActions](#sdkvaultgetstakeractions) | [osToken.getBurnAmount](#sdkostokengetburnamount) | [rewardSplitter.getClaimAmount](#sdkrewardsplittergetclaimamount) |
95-
| [vault.getSnapshots](#sdkvaultgetsnapshots) | [osToken.getHealthFactor](#sdkostokengethealthfactor) | |
96-
| [vault.getExitQueuePositions](#sdkvaultgetexitqueuepositions) | [osToken.getAPY](#sdkostokengetapy) | |
97-
| [vault.getValidators](#sdkvaultgetvalidators) | [osToken.getPosition](#sdkostokengetposition) | |
98-
| [vault.getVault](#sdkvaultgetvault) | [osToken.getMaxMint](#sdkostokengetmaxmint) | |
99-
| [vault.getMaxWithdraw](#sdkvaultgetmaxwithdraw) | [osToken.getSharesFromAssets](#sdkostokengetsharesfromassets) | |
100-
| [vault.getHarvestParams](#sdkvaultgetharvestparams) | [osToken.getAssetsFromShares](#sdkostokengetassetsfromshares) | |
101-
| [vault.getStakeBalance](#sdkvaultgetstakebalance) | [osToken.getRate](#sdkostokengetrate) | |
102-
| [vault.getScorePercentiles](#sdkvaultgetscorepercentiles) | [osToken.getConfig](#sdkostokengetconfig) | |
103-
| [vault.getUserRewards](#sdkvaultgetuserrewards) | | |
104-
| [vault.getWhitelist](#sdkvaultgetwhitelist) | | |
105-
| [vault.getBlocklist](#sdkvaultgetblocklist) | | |
106-
| [vault.getRewardSplitters](#sdkvaultgetrewardsplitters) | | |
107-
| [vault.getVaultStats](#sdkvaultgetvaultstats) | | |
108-
| [vault.getUserStats](#sdkvaultgetuserstats) | | |
92+
| **Vault** | **osToken** | **RewardSplitter** |
93+
|-----------------------------------------------------------------|--------------------------------------------------------------|-------------------------------------------------------------------|
94+
| [vault.getStakerActions](#sdkvaultgetstakeractions) | [osToken.getBurnAmount](#sdkostokengetburnamount) | [rewardSplitter.getClaimAmount](#sdkrewardsplittergetclaimamount) |
95+
| [vault.getExitQueuePositions](#sdkvaultgetexitqueuepositions) | [osToken.getAPY](#sdkostokengetapy) | |
96+
| [vault.getValidators](#sdkvaultgetvalidators) | [osToken.getPosition](#sdkostokengetposition) | |
97+
| [vault.getVault](#sdkvaultgetvault) | [osToken.getMaxMint](#sdkostokengetmaxmint) | |
98+
| [vault.getMaxWithdraw](#sdkvaultgetmaxwithdraw) | [osToken.getSharesFromAssets](#sdkostokengetsharesfromassets) | |
99+
| [vault.getHarvestParams](#sdkvaultgetharvestparams) | [osToken.getAssetsFromShares](#sdkostokengetassetsfromshares) | |
100+
| [vault.getStakeBalance](#sdkvaultgetstakebalance) | [osToken.getRate](#sdkostokengetrate) | |
101+
| [vault.getScorePercentiles](#sdkvaultgetscorepercentiles) | [osToken.getConfig](#sdkostokengetconfig) | |
102+
| [vault.getUserRewards](#sdkvaultgetuserrewards) | [osToken.getHealthFactor](#sdkostokengethealthfactor) | |
103+
| [vault.getWhitelist](#sdkvaultgetwhitelist) | | |
104+
| [vault.getBlocklist](#sdkvaultgetblocklist) | | |
105+
| [vault.getRewardSplitters](#sdkvaultgetrewardsplitters) | | |
106+
| [vault.getVaultStats](#sdkvaultgetvaultstats) | | |
107+
| [vault.getUserStats](#sdkvaultgetuserstats) | | |
109108

110109
| **Utils** |
111110
|-----------------------------------------------------|
112111
| [utils.getSwiseUsdPrice](#sdkutilsgetswiseusdprice) |
113112
| [utils.getTransactions](#sdkutilsgettransactions) |
113+
| [utils.getFiatRates](#sdkutilsgetfiatrates) |
114114

115115

116116
All of these methods (except synchronous getHealthFactor) return a promise that can be
@@ -207,49 +207,6 @@ await sdk.vault.getStakerActions({
207207
})
208208
```
209209
---
210-
### `sdk.vault.getSnapshots`
211-
212-
#### Description:
213-
214-
Deprecated, use `sdk.vault.getVaultStats` instead.
215-
216-
TVL and APY snapshots for the vault. With the help of this data it is possible to build a chart.
217-
218-
#### Arguments:
219-
220-
| Name | Type | Type | Description |
221-
|--------------|----------|-----------------|---------|
222-
| vaultAddress | `string` | **Yes** | - |
223-
| dateFrom | `number` | **Yes** | Time to start |
224-
225-
#### Returns:
226-
227-
```ts
228-
type Snapshot = {
229-
APY: number
230-
TVL: string
231-
}
232-
233-
type Output = {
234-
days: Record<number, Snapshot>
235-
first: Snapshot
236-
}
237-
```
238-
239-
| Name | Description |
240-
|------|-------------|
241-
| `days` | The result of the query on your parameters, is returned as an object where the keys are timestamps |
242-
| `first` | We always send the very first saved snapshot regardless of the request parameters, this helps for rendering the chart |
243-
244-
#### Example:
245-
246-
```ts
247-
await sdk.vault.getSnapshots({
248-
vaultAddress: '0x...',
249-
dateFrom: 1695730032793,
250-
})
251-
```
252-
---
253210
### `sdk.vault.getScorePercentiles`
254211

255212
#### Description:
@@ -285,48 +242,46 @@ await sdk.vault.getScorePercentiles()
285242
286243
#### Description:
287244
288-
Deprecated, use `sdk.vault.getUserStats` instead.
289245
290-
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.
246+
Daily rewards for the user who has made a deposit in the vault.
291247
292248
#### Arguments:
293249
294-
| Name | Type | Type | Description |
295-
|------|----------|-------------|---|
296-
| vaultAddress | `string` | **Yes** | - |
297-
| userAddress | `string` | **Yes** | - |
298-
| dateFrom | `number` | **Yes** | Time to start |
299-
| dateTo | `number` | **No** | Time to end |
300-
| fillGaps | `boolean` | **No** | Fill in the empty days with zeros |
250+
| Name | Type | Required | Description |
251+
|------|----------|----------|---|
252+
| dateFrom | `number` | **Yes** | Time to start in milliseconds |
253+
| dateTo | `number` | **Yes** | Time to end in milliseconds |
254+
| userAddress | `string` | **Yes** | The user address |
255+
| vaultAddress | `string` | **Yes** | The address of the vault |
301256
302257
#### Returns:
303258
304259
```ts
305-
type UserReward = {
306-
date: number
307-
sumRewards: string
308-
dailyRewards: string
309-
dailyRewardsEur: string
310-
dailyRewardsGbp: string
311-
dailyRewardsUsd: string
312-
}
313-
314260
type Output = {
315-
days: Record<number, UserReward>
261+
date: number
262+
dailyRewards: number
263+
dailyRewardsEur: number
264+
dailyRewardsGbp: number
265+
dailyRewardsUsd: number
316266
}
317267
```
318268
319-
| Name | Description |
320-
|------|-------------|
321-
| `days` | The result of the query on your parameters, is returned as an object where the keys are timestamps |
269+
| Name | Description |
270+
|------|---------------------------|
271+
| `date` | Сurrent rate date |
272+
| `dailyRewards` | Daily reward asset in ETH |
273+
| `dailyRewardsEur` | Daily reward asset in EUR |
274+
| `dailyRewardsGbp` | Daily reward asset in GBP |
275+
| `dailyRewardsUsd` | Daily reward asset in USD |
322276
323277
#### Example:
324278
325279
```ts
326280
await sdk.vault.getUserRewards({
327281
userAddress: '0x...',
328282
vaultAddress: '0x...',
329-
dateFrom: 1695730032793,
283+
dateTo: 1727827200000,
284+
dateFrom: 1721606400000,
330285
})
331286
```
332287
---
@@ -866,12 +821,12 @@ type Output = {
866821
}
867822
```
868823
869-
| Name | Description |
870-
|------|---------------------------------------------------------------------------------------------------------|
871-
| `time` | Date and time for each data point |
872-
| `apy` | Current APY based on time, rewards and balance. The information is taken from allocatorStats_collection |
873-
| `rewards` | Number of assets earned by the user in current vault during the interval in ETH |
874-
| `balance` | Total assets by the user in current vault at the moment of time in ETH |
824+
| Name | Description |
825+
|------|--------------------------------------------------------------------------------------------------------|
826+
| `time` | Date and time for each data point |
827+
| `apy` | Current APY based on time, rewards and balance. |
828+
| `rewards` | Number of assets earned by the user in current vault during the interval in ETH |
829+
| `balance` | Total assets by the user in current vault at the moment of time in ETH |
875830
876831
#### Example:
877832
@@ -883,6 +838,7 @@ await sdk.vault.getUserStats({
883838
})
884839
```
885840
---
841+
886842
## API-osToken
887843
888844
### `sdk.osToken.getBurnAmount`
@@ -1370,6 +1326,28 @@ type Output = Array<{
13701326
await sdk.utils.getTransactions({ hash: '0x...' })
13711327
```
13721328
---
1329+
### `sdk.utils.getFiatRates`
1330+
1331+
#### Description:
1332+
1333+
Returns the USD, EUR, GBP exchange rates for the current asset
1334+
1335+
#### Returns:
1336+
1337+
```ts
1338+
type Output = {
1339+
assetsUsdRate: number
1340+
usdToEurRate: number
1341+
usdToGbpRate: number
1342+
}
1343+
```
1344+
1345+
#### Example:
1346+
1347+
```ts
1348+
await sdk.utils.getFiatRates()
1349+
```
1350+
---
13731351
## Transactions
13741352
13751353
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

changelog/next-release.md

+27-4
Original file line numberDiff line numberDiff line change
@@ -86,15 +86,38 @@ type RemovedOutput = {
8686
8787
---
8888
89-
---
9089
91-
### 10. Deprecated method `sdk.vault.getSnapshots`
90+
### 10. Removed method `sdk.vault.getSnapshots`
9291
### Use `sdk.vault.getVaultStats` instead
9392
9493
---
94+
95+
### 11. Update method `sdk.vault.getUserRewards`
96+
#### Update output format:
97+
98+
```ts
99+
type Output = {
100+
date: number
101+
dailyRewards: number
102+
dailyRewardsEur: number
103+
dailyRewardsGbp: number
104+
dailyRewardsUsd: number
105+
}[]
106+
```
107+
108+
#### New arguments:
109+
110+
| Name | Type | Type | Description |
111+
|------|----------|-------------|---|
112+
| dateFrom | `number` | **Yes** | Time to start in milliseconds |
113+
| dateTo | `number` | **Yes** | Time to end in milliseconds |
114+
| userAddress | `string` | **Yes** | The user address |
115+
| vaultAddress | `string` | **Yes** | The address of the vault |
116+
95117
---
96118
97-
### 11. Deprecated method `sdk.vault.getUserRewards`
98-
### Use `sdk.vault.getUserStats` instead
119+
### 12. Added method getFiatRates
120+
### `sdk.utils.getFiatRates`
99121
100122
---
123+

src/graphql/backend/vault/index.ts

-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,6 @@ export type { SnapshotsQueryPayload, SnapshotsQueryVariables } from './snapshots
44
export { fetchValidatorsQuery } from './validatorsQuery.graphql'
55
export type { ValidatorsQueryPayload, ValidatorsQueryVariables } from './validatorsQuery.graphql'
66

7-
export { fetchUserRewardsQuery } from './userRewardsQuery.graphql'
8-
export type { UserRewardsQueryPayload, UserRewardsQueryVariables } from './userRewardsQuery.graphql'
9-
107
export { fetchScorePercentilesQuery } from './scorePercentilesQuery.graphql'
118
export type { ScorePercentilesQueryPayload, ScorePercentilesQueryVariables } from './scorePercentilesQuery.graphql'
129

src/graphql/backend/vault/userRewardsQuery.graphql

-10
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
query FiatRates {
2+
networks {
3+
assetsUsdRate
4+
usdToEurRate
5+
usdToGbpRate
6+
}
7+
}

src/graphql/subgraph/stats/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,5 @@
11
export { fetchStatsQuery } from './statsQuery.graphql'
22
export type { StatsQueryPayload, StatsQueryVariables } from './statsQuery.graphql'
3+
4+
export { fetchFiatRatesQuery } from './fiatRatesQuery.graphql'
5+
export type { FiatRatesQueryPayload, FiatRatesQueryVariables } from './fiatRatesQuery.graphql'

src/graphql/subgraph/vault/index.ts

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@ export type { UserStatsQueryPayload, UserStatsQueryVariables } from './userStats
77
export { fetchVaultStatsQuery } from './vaultStatsQuery.graphql'
88
export type { VaultStatsQueryPayload, VaultStatsQueryVariables } from './vaultStatsQuery.graphql'
99

10+
export { fetchUserRewardsQuery } from './userRewardsQuery.graphql'
11+
export type { UserRewardsQueryPayload, UserRewardsQueryVariables } from './userRewardsQuery.graphql'
12+
1013
export { fetchHarvestParamsQuery } from './harvestParamsQuery.graphql'
1114
export type { HarvestParamsQueryPayload, HarvestParamsQueryVariables } from './harvestParamsQuery.graphql'
1215

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
query UserRewards($user: Bytes!, $vaultAddress: String!, $dateFrom: Timestamp!, $dateTo: Timestamp!) {
2+
exchangeRate: exchangeRateStats_collection(
3+
interval: day
4+
where: { timestamp_gte: $dateFrom, timestamp_lte: $dateTo }
5+
) {
6+
timestamp
7+
assetsUsdRate
8+
usdToEurRate
9+
usdToGbpRate
10+
}
11+
allocator: allocatorStats_collection(
12+
interval: day
13+
where: { timestamp_gte: $dateFrom, timestamp_lte: $dateTo, allocator_: { address: $user, vault: $vaultAddress } }
14+
) {
15+
timestamp
16+
earnedAssets
17+
}
18+
}

src/methods/utils/getFiatRates.ts

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import graphql from '../../graphql'
2+
import { apiUrls, configs, Network } from '../../utils'
3+
4+
5+
type GetFiatRatesInput = {
6+
options: StakeWise.Options
7+
}
8+
9+
const getMainnetGbpRate = () => {
10+
return graphql.subgraph.stats.fetchFiatRatesQuery({
11+
url: configs[Network.Mainnet].api.subgraph,
12+
modifyResult: (data): number => {
13+
const usdInGbp = Number(data.networks[0].usdToGbpRate)
14+
15+
return usdInGbp
16+
},
17+
})
18+
}
19+
20+
const getFiatRates = (values: GetFiatRatesInput) => {
21+
const { options } = values
22+
23+
return graphql.subgraph.stats.fetchFiatRatesQuery({
24+
url: apiUrls.getSubgraphqlUrl(options),
25+
modifyResult: async (data) => {
26+
const usd = Number(data.networks[0].assetsUsdRate)
27+
const usdInEur = Number(data.networks[0].usdToEurRate)
28+
29+
const isGnosis = [ Network.Gnosis, Network.Chiado ].includes(options.network)
30+
const usdInGbp = isGnosis ? await getMainnetGbpRate() : Number(data.networks[0].usdToGbpRate)
31+
32+
return {
33+
assetsUsdRate: usd,
34+
usdToEurRate: usdInEur,
35+
usdToGbpRate: usdInGbp,
36+
}
37+
},
38+
})
39+
}
40+
41+
42+
export default getFiatRates

src/methods/utils/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export type { BaseInput } from './types'
2+
export { default as getFiatRates } from './getFiatRates'
23
export { default as getTransactions } from './getTransactions'
34
export { default as getSwiseUsdPrice } from './getSwiseUsdPrice'
45
export { default as getStakewiseStats } from './getStakewiseStats'

0 commit comments

Comments
 (0)