diff --git a/README.md b/README.md index 729796b9..ea3ded76 100644 --- a/README.md +++ b/README.md @@ -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** | @@ -205,6 +205,44 @@ 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 Output = { + consensusRewardsEarned: bigint + consensusRewardsMissed: bigint + executionMevEarned: bigint + executionMevMissed: bigint +} +``` + +| 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: diff --git a/src/graphql/backend/vault/index.ts b/src/graphql/backend/vault/index.ts index 18d9b460..4a8c7906 100644 --- a/src/graphql/backend/vault/index.ts +++ b/src/graphql/backend/vault/index.ts @@ -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' diff --git a/src/graphql/backend/vault/scoringQuery.graphql b/src/graphql/backend/vault/scoringQuery.graphql new file mode 100644 index 00000000..4de9dad8 --- /dev/null +++ b/src/graphql/backend/vault/scoringQuery.graphql @@ -0,0 +1,10 @@ +query Scoring($vaultAddress: String!) { + vaults(id: $vaultAddress) { + scoring { + consensusRewardsEarned, + consensusRewardsMissed, + executionMevEarned, + executionMevMissed + } + } +} diff --git a/src/methods/vault/index.ts b/src/methods/vault/index.ts index 887e0dad..163881c3 100644 --- a/src/methods/vault/index.ts +++ b/src/methods/vault/index.ts @@ -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' @@ -25,6 +26,7 @@ export default { getHarvestParams, getStakerActions, getSnapshots, + getScoring, getExitQueuePositions, }, transactions: { diff --git a/src/methods/vault/requests/getScoring/index.ts b/src/methods/vault/requests/getScoring/index.ts new file mode 100644 index 00000000..064b5139 --- /dev/null +++ b/src/methods/vault/requests/getScoring/index.ts @@ -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({ + url: apiUrls.getBackendUrl(options), + variables: { + vaultAddress: vaultAddress.toLowerCase(), + } as ScoringQueryVariables, + modifyResult: (data: ScoringQueryPayload) => modifyScoring(data), + }) + + return data +} + + +export default getScoring diff --git a/src/methods/vault/requests/getScoring/modifyScoring.spec.ts b/src/methods/vault/requests/getScoring/modifyScoring.spec.ts new file mode 100644 index 00000000..f1e6b0c0 --- /dev/null +++ b/src/methods/vault/requests/getScoring/modifyScoring.spec.ts @@ -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, + }) + }) +}) diff --git a/src/methods/vault/requests/getScoring/modifyScoring.ts b/src/methods/vault/requests/getScoring/modifyScoring.ts new file mode 100644 index 00000000..1b1bf111 --- /dev/null +++ b/src/methods/vault/requests/getScoring/modifyScoring.ts @@ -0,0 +1,19 @@ +import type { ScoringQueryPayload } from '../../../../graphql/backend/vault' +import { ModifiedScoring } from './types' + + +const modifyScoring = (input: ScoringQueryPayload): ModifiedScoring => { + const { vaults } = input + + const { scoring } = vaults[0] + + return ({ + consensusRewardsEarned: BigInt(scoring?.consensusRewardsEarned || 0), + consensusRewardsMissed: BigInt(scoring?.consensusRewardsMissed || 0), + executionMevEarned: BigInt(scoring?.executionMevEarned || 0), + executionMevMissed: BigInt(scoring?.executionMevMissed || 0), + }) +} + + +export default modifyScoring diff --git a/src/methods/vault/requests/getScoring/types.ts b/src/methods/vault/requests/getScoring/types.ts new file mode 100644 index 00000000..7ce9d621 --- /dev/null +++ b/src/methods/vault/requests/getScoring/types.ts @@ -0,0 +1,6 @@ +export type ModifiedScoring = { + consensusRewardsEarned: bigint, + consensusRewardsMissed: bigint, + executionMevEarned: bigint, + executionMevMissed: bigint, +}