Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Split os token logic #42

Merged
merged 3 commits into from
Dec 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 25 additions & 11 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

The official SDK designed for effortless data retrieval from the StakeWise platform. This SDK provides a streamlined interface over GraphQL requests and contract interactions.

![Version](https://img.shields.io/badge/version-1.4.2-blue)
![Version](https://img.shields.io/badge/version-1.5.0-blue)
![Unit Tests](https://github.com/stakewise/v3-sdk/actions/workflows/unit-tests.yml/badge.svg)
![GitHub issues](https://img.shields.io/github/issues-raw/stakewise/v3-sdk)
![GitHub pull requests](https://img.shields.io/github/issues-pr-raw/stakewise/v3-sdk)
Expand Down Expand Up @@ -113,15 +113,19 @@ type Output = Array<{
actionType: AllocatorActionType
createdAt: number
assets: string
shares: string
link: string
id: string
}>
```

In events related to osToken you can use shares, in all other assets

| Name | Description |
|------|-------------|
| `id` | Event identifier |
| `assets` | Transaction amount |
| `shares` | Transaction amount |
| `createdAt` | Transaction date |
| `actionType` | Type of action |
| `link` | Transaction link (etherscan/blockscout) |
Expand Down Expand Up @@ -546,26 +550,36 @@ sdk.osToken.getHealthFactor({

#### Description:

Token reward data
Current os token APY

#### Returns:

```ts
type Output = {
apy: string;
averageRewardsPerSecond: bigint;
}
type Output = string
```

| Name | Description |
|------|-------------|
| `apy` | Current APY |
| `averageRewardsPerSecond` | 2-week average of awards per second |
#### Example:

```ts
const apy = await sdk.osToken.getAPY()
```
---
### `sdk.osToken.getAvgRewardsPerSecond`

#### Description:

Os token average rewards per second

#### Returns:

```ts
type Output = bigint
```

#### Example:

```ts
await sdk.osToken.getAPY()
const averageRewardsPerSecond = await sdk.osToken.getAvgRewardsPerSecond()
```
---
### `sdk.osToken.getPosition`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ query AllocatorActions(
) {
id
assets
shares
createdAt
actionType
}
Expand Down
7 changes: 5 additions & 2 deletions src/graphql/subgraph/osToken/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export { fetchOsTokenSnapshotsQuery } from './osTokenSnapshotsQuery.graphql'
export type { OsTokenSnapshotsQueryPayload, OsTokenSnapshotsQueryVariables } from './osTokenSnapshotsQuery.graphql'
export { fetchOsTokenApyQuery } from './osTokenApyQuery.graphql'
export type { OsTokenApyQueryPayload, OsTokenApyQueryVariables } from './osTokenApyQuery.graphql'

export { fetchOsTokenPositionsQuery } from './osTokenPositionsQuery.graphql'
export type { OsTokenPositionsQueryPayload, OsTokenPositionsQueryVariables } from './osTokenPositionsQuery.graphql'

export { fetchOsTokenRewardPerSecondQuery } from './osTokenRewardPerSecondQuery.graphql'
export type { OsTokenRewardPerSecondQueryPayload, OsTokenRewardPerSecondQueryVariables } from './osTokenRewardPerSecondQuery.graphql'
5 changes: 5 additions & 0 deletions src/graphql/subgraph/osToken/osTokenApyQuery.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
query osTokenApy($first: Int, $orderBy: OsTokenSnapshot_orderBy, $orderDirection: OrderDirection) {
osTokens {
apy
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
query osTokenSnapshots($first: Int, $orderBy: OsTokenSnapshot_orderBy, $orderDirection: OrderDirection) {
query osTokenRewardPerSecond($first: Int, $orderBy: OsTokenSnapshot_orderBy, $orderDirection: OrderDirection) {
osTokenSnapshots(first: $first, orderBy: $orderBy, orderDirection: $orderDirection) {
avgRewardPerSecond
}
Expand Down
2 changes: 2 additions & 0 deletions src/methods/osToken/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import getPosition from './requests/getOsTokenPosition'
import getHealthFactor from './helpers/getHealthFactor'
import getSharesFromAssets from './requests/getSharesFromAssets'
import getAssetsFromShares from './requests/getAssetsFromShares'
import getAvgRewardsPerSecond from './requests/getAvgRewardsPerSecond'

// Transactions
import { mint } from './transactions/mint'
Expand All @@ -23,6 +24,7 @@ export default {
getHealthFactor,
getSharesFromAssets,
getAssetsFromShares,
getAvgRewardsPerSecond,
},
transactions: {
mint,
Expand Down
38 changes: 38 additions & 0 deletions src/methods/osToken/requests/getAvgRewardsPerSecond.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import { apiUrls } from '../../../utils'
import graphql from '../../../graphql'


export type FetchOsTokenSnapshotsInput = {
options: StakeWise.Options
}

const getAvgRewardsPerSecond = async (input: FetchOsTokenSnapshotsInput) => {
const { options } = input

const data = await graphql.subgraph.osToken.fetchOsTokenRewardPerSecondQuery({
url: apiUrls.getSubgraphqlUrl(options),
variables: {
first: 14,
orderBy: 'createdAt',
orderDirection: 'desc',
},
})

let rewardPerSecond = 0n

const count = BigInt(data.osTokenSnapshots.length || 0)

if (count) {
const sum = data.osTokenSnapshots.reduce(
(acc, { avgRewardPerSecond }) => acc + BigInt(avgRewardPerSecond),
0n
)

rewardPerSecond = sum / count
}

return rewardPerSecond
}


export default getAvgRewardsPerSecond
20 changes: 20 additions & 0 deletions src/methods/osToken/requests/getOsTokenAPY.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { apiUrls } from '../../../utils'
import graphql from '../../../graphql'


export type FetchOsTokenSnapshotsInput = {
options: StakeWise.Options
}

const getOsTokenAPY = async (input: FetchOsTokenSnapshotsInput) => {
const { options } = input

const data = await graphql.subgraph.osToken.fetchOsTokenApyQuery({
url: apiUrls.getSubgraphqlUrl(options),
})

return data?.osTokens?.[0].apy || '0'
}


export default getOsTokenAPY
41 changes: 0 additions & 41 deletions src/methods/osToken/requests/getOsTokenAPY/calculateAPY.ts

This file was deleted.

This file was deleted.

18 changes: 0 additions & 18 deletions src/methods/osToken/requests/getOsTokenAPY/index.ts

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ describe('modifyStakerActions function', () => {
{
createdAt: '1691309736',
assets: '260000000000000000',
shares: '160000000000000000',
actionType: AllocatorActionType.Deposited,
id: '0x285e4dcf02bfa8f8c6a656b85a565d962d1831556f5d2df03c082e9b82f4344f-52',
},
{
createdAt: '1690890396',
assets: '5000000000000000000',
shares: '4000000000000000000',
actionType: AllocatorActionType.Redeemed,
id: '0x1898c66c9898f060352e13c8a74a7748749a5774e36dc30ade8b36fce16c4817-357',
},
Expand All @@ -30,13 +32,15 @@ describe('modifyStakerActions function', () => {
const expectedResult = [
{
assets: '0.26',
shares: '0.16',
createdAt: 1691309736000,
actionType: AllocatorActionType.Deposited,
id: '0x285e4dcf02bfa8f8c6a656b85a565d962d1831556f5d2df03c082e9b82f4344f-52',
link: `${explorerUrl}/tx/0x285e4dcf02bfa8f8c6a656b85a565d962d1831556f5d2df03c082e9b82f4344f`,
},
{
assets: '5.0',
shares: '4.0',
createdAt: 1690890396000,
actionType: AllocatorActionType.Redeemed,
id: '0x1898c66c9898f060352e13c8a74a7748749a5774e36dc30ade8b36fce16c4817-357',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const modifyStakerActions = (input: ModifyStakerActionsInput): ModifiedStakerAct

return data.allocatorActions.map((item) => ({
...item,
shares: formatEther(item.shares || 0),
assets: formatEther(item.assets || 0),
createdAt: Number(item.createdAt) * 1000,
link: `${configs[network].network.blockExplorerUrl}/tx/${item.id.replace(/-.*/, '')}`,
Expand Down
2 changes: 1 addition & 1 deletion src/methods/vault/requests/getStakerActions/types.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AllocatorActionsQueryPayload } from '../../../../graphql/subgraph/allocatorActions'


type AllocatorActionsPayload = Pick<AllocatorActionsQueryPayload['allocatorActions'][number], 'id' | 'actionType' | 'assets'>
type AllocatorActionsPayload = Pick<AllocatorActionsQueryPayload['allocatorActions'][number], 'id' | 'actionType' | 'assets' | 'shares'>

export type ModifiedStakerActions = Array<AllocatorActionsPayload & {
link: string
Expand Down
Loading