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

Scoring #56

Merged
merged 4 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
47 changes: 46 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ const sdk = new StakeWiseSDK({ network: Network.Mainnet })
| [sdk.vault.getMaxWithdraw](#sdkvaultgetmaxwithdraw) | [sdk.osToken.getBaseData](#sdkostokengetbasedata) |
| [sdk.vault.getHarvestParams](#sdkvaultgetharvestparams) | [sdk.osToken.getSharesFromAssets](#sdkostokengetsharesfromassets) |
| [sdk.vault.getStakeBalance](#sdkvaultgetstakebalance) | [sdk.osToken.getAssetsFromShares](#sdkostokengetassetsfromshares) |
|[sdk.vault.getUserRewards](#sdkvaultgetuserrewards)|
|[sdk.vault.getUserRewards](#sdkvaultgetuserrewards) | [sdk.vault.getScoring](#sdkvaultgetscoring) |

##### Table of transactions:
| **Vault** | **osToken** |
Expand Down Expand Up @@ -205,6 +205,51 @@ await sdk.vault.getSnapshots({
})
```
---
### `sdk.vault.getScoring`

#### Description:

Fetch components for performance score calculation.

#### Arguments:

| Name | Type | Type | Description |
|--------------|----------|-----------------|---------|
| vaultAddress | `string` | **Require** | - |

#### Returns:

```ts
type Scoring = {
dfkadyr-old marked this conversation as resolved.
Show resolved Hide resolved
consensusRewardsEarned: number
consensusRewardsMissed: number
executionMevEarned: number
executionMevMissed: number
}

type Output = {
consensusRewardsEarned: string
consensusRewardsMissed: string
executionMevEarned: string
executionMevMissed: string
}
```

| Name | Description |
|------|-------------|
| `consensusRewardsEarned` | The total amount of consensus rewards earned by the Vault |
| `consensusRewardsMissed` | The total amount of consensus rewards missed by the Vault |
| `executionMevEarned` | The total amount of execution rewards earned by the Vault |
| `executionMevMissed` | The total amount of execution rewards missed by the Vault. It will include the ones missed due to the invalid fee recipient address set for the validator |

#### Example:

```ts
await sdk.vault.getScoring({
vaultAddress: '0x...',
})
```
---
### `sdk.vault.getUserRewards`

#### Description:
Expand Down
3 changes: 3 additions & 0 deletions src/graphql/backend/vault/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
export { fetchScoringQuery } from './scoringQuery.graphql'
export type { ScoringQueryPayload, ScoringQueryVariables } from './scoringQuery.graphql'

export { fetchSnapshotsQuery } from './snapshotsQuery.graphql'
export type { SnapshotsQueryPayload, SnapshotsQueryVariables } from './snapshotsQuery.graphql'

Expand Down
10 changes: 10 additions & 0 deletions src/graphql/backend/vault/scoringQuery.graphql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
query Scoring($vaultAddress: String!) {
vaults(id: $vaultAddress) {
scoring {
consensusRewardsEarned,
consensusRewardsMissed,
executionMevEarned,
executionMevMissed
}
}
}
2 changes: 2 additions & 0 deletions src/methods/vault/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import getHarvestParams from './requests/getHarvestParams'
import getStakerActions from './requests/getStakerActions'
import getExitQueuePositions from './requests/getExitQueuePositions'
import getSnapshots from './requests/getSnapshots'
import getScoring from './requests/getScoring'

// Transactions
import { deposit } from './transactions/deposit'
Expand All @@ -25,6 +26,7 @@ export default {
getHarvestParams,
getStakerActions,
getSnapshots,
getScoring,
getExitQueuePositions,
},
transactions: {
Expand Down
30 changes: 30 additions & 0 deletions src/methods/vault/requests/getScoring/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import type { ScoringQueryVariables, ScoringQueryPayload } from '../../../../graphql/backend/vault'
import { apiUrls, validateArgs } from '../../../../utils'
import graphql from '../../../../graphql'
import modifyScoring from './modifyScoring'
import { ModifiedScoring } from './types'


type GetScoringInput = {
options: StakeWise.Options
vaultAddress: ScoringQueryVariables['vaultAddress']
}

const getScoring = async (input: GetScoringInput) => {
const { options, vaultAddress } = input

validateArgs.address({ vaultAddress })

const data = await graphql.backend.vault.fetchScoringQuery<ModifiedScoring>({
url: apiUrls.getBackendUrl(options),
variables: {
vaultAddress: vaultAddress.toLowerCase(),
} as ScoringQueryVariables,
modifyResult: (data: ScoringQueryPayload) => modifyScoring(data),
})

return data
}


export default getScoring
29 changes: 29 additions & 0 deletions src/methods/vault/requests/getScoring/modifyScoring.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import type { ScoringQueryPayload } from '../../../../graphql/backend/vault'
import modifyScoring from './modifyScoring'


describe('modifyScoring functions', () => {
const sampleInput: ScoringQueryPayload = {
vaults: [
{
scoring: {
consensusRewardsEarned: '89163004',
consensusRewardsMissed: '478650',
executionMevEarned: '21005814693398160',
executionMevMissed: '0',
},
},
],
}

it('should correctly modify a single vault snapshot', () => {
const result = modifyScoring(sampleInput)

expect(result).toEqual({
consensusRewardsEarned: 89163004n,
consensusRewardsMissed: 478650n,
executionMevEarned: 21005814693398160n,
executionMevMissed: 0n,
})
})
})
19 changes: 19 additions & 0 deletions src/methods/vault/requests/getScoring/modifyScoring.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import type { ScoringQueryPayload } from '../../../../graphql/backend/vault'
import { ModifiedScoring } from './types'


const modifyScoring = (input: ScoringQueryPayload): ModifiedScoring => {
const { vaults } = input

const { consensusRewardsEarned, consensusRewardsMissed, executionMevEarned, executionMevMissed } = vaults[0].scoring

return ({
consensusRewardsEarned: BigInt(consensusRewardsEarned),
consensusRewardsMissed: BigInt(consensusRewardsMissed),
executionMevEarned: BigInt(executionMevEarned),
executionMevMissed: BigInt(executionMevMissed),
})
}


export default modifyScoring
6 changes: 6 additions & 0 deletions src/methods/vault/requests/getScoring/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export type ModifiedScoring = {
consensusRewardsEarned: bigint,
consensusRewardsMissed: bigint,
executionMevEarned: bigint,
executionMevMissed: bigint,
}
Loading