Skip to content

Commit 30ee704

Browse files
committed
Fix rewards fetching test
1 parent 87906fc commit 30ee704

File tree

5 files changed

+249
-107
lines changed

5 files changed

+249
-107
lines changed

packages/ethereum/src/lib/connector.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ export class StakewiseConnector {
4141
})
4242

4343
this.baseAPI = 'https://holesky-api.stakewise.io/graphql'
44-
this.baseGraph = 'https://holesky-graph.stakewise.io/subgraphs/name/stakewise/stakewise'
44+
this.baseGraph = 'https://graphs.stakewise.io/holesky/subgraphs/name/stakewise/prod'
4545
// Stakewise keeper contract
4646
this.keeper = '0xB580799Bf7d62721D1a523f0FDF2f5Ed7BA4e259'
4747
this.priceOracle = '0xe31FAf135A6047Cbe595F91B4b6802cDB9B46E2b'
@@ -56,7 +56,7 @@ export class StakewiseConnector {
5656
})
5757

5858
this.baseAPI = 'https://mainnet-api.stakewise.io/graphql'
59-
this.baseGraph = 'https://mainnet-graph.stakewise.io/subgraphs/name/stakewise/stakewise'
59+
this.baseGraph = 'https://graphs.stakewise.io/mainnet-b/subgraphs/name/stakewise/prod'
6060
// Stakewise keeper contract
6161
this.keeper = '0x6B5815467da09DaA7DC83Db21c9239d98Bb487b5'
6262
this.priceOracle = '0x8023518b2192FB5384DAdc596765B3dD1cdFe471'
@@ -75,6 +75,7 @@ export class StakewiseConnector {
7575
query: request.query.trim(),
7676
variables: request.variables
7777
})
78+
7879
const params: RequestInit = {
7980
method: 'POST',
8081
headers: {
Original file line numberDiff line numberDiff line change
@@ -1,58 +1,61 @@
11
import { Hex } from 'viem'
22
import { StakewiseConnector } from '../connector'
3-
import { RewardsDataPoint } from '../types/rewards'
4-
5-
async function extractVaultUserRewards (
6-
connector: StakewiseConnector,
7-
vault: Hex,
8-
allocatorAddress: string,
9-
dateFrom: Date,
10-
dateTo: Date
11-
): Promise<RewardsDataPoint[]> {
12-
const vars_getRewards = {
13-
vaultAddress: vault,
14-
user: allocatorAddress.toLowerCase(),
15-
dateFrom: Math.floor(dateFrom.getTime() / 1000).toString()
16-
}
17-
18-
const rewardsData = await connector.graphqlRequest({
19-
type: 'api',
20-
op: 'UserRewards',
21-
query: `query UserRewards($user: String!, $vaultAddress: String!, $dateFrom: DateAsTimestamp!) { userRewards(user: $user, vaultAddress: $vaultAddress, dateFrom: $dateFrom) { date, sumRewards, }}`,
22-
variables: vars_getRewards
23-
})
24-
25-
if (!rewardsData.data.userRewards) {
26-
throw new Error(`Rewards data is missing the userRewards field`)
27-
}
28-
const dataPoints: RewardsDataPoint[] = []
29-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
30-
rewardsData.data.userRewards.forEach((reward: any) => {
31-
const when: Date = new Date(parseInt(reward.date) * 1000)
32-
if (when <= dateTo) {
33-
const sumRewards: string = reward.sumRewards
34-
dataPoints.push({
35-
when: when,
36-
amount: BigInt(sumRewards),
37-
vault: vault
38-
})
39-
}
40-
})
41-
42-
return dataPoints
43-
}
443

454
export async function getRewardsHistory (params: {
465
connector: StakewiseConnector
47-
from: Date
48-
to: Date
6+
from: number
7+
to: number
498
vault: Hex
509
userAccount: Hex
51-
}): Promise<Array<RewardsDataPoint>> {
10+
}) {
5211
const { connector, from, to, vault, userAccount } = params
53-
let vaultRewards: RewardsDataPoint[] = []
12+
const rewardsData = await connector.graphqlRequest({
13+
type: 'graph',
14+
op: 'UserRewards',
15+
query:
16+
'query UserRewards( $where: AllocatorStats_filter $limit: Int) { allocator: allocatorStats_collection( interval: day first: $limit where: $where ) { apy timestamp earnedAssets totalAssets }}',
17+
// variables: vars_getRewards
18+
variables: {
19+
limit: 365,
20+
where: {
21+
timestamp_gte: (from * 1000).toString(),
22+
allocator_: {
23+
address: userAccount.toLowerCase(),
24+
vault: vault.toLowerCase()
25+
}
26+
}
27+
}
28+
})
29+
30+
if (!rewardsData.data.allocator) {
31+
throw new Error(`Rewards data is missing the allocator field`)
32+
}
5433

55-
vaultRewards = await extractVaultUserRewards(connector, vault, userAccount, from, to)
34+
const data = rewardsData.data.allocator as {
35+
timestamp: string
36+
earnedAssets: string
37+
totalAssets: string
38+
}[]
5639

57-
return vaultRewards
40+
return data
41+
.reduce(
42+
(acc, reward) => {
43+
const timestamp = Math.floor(parseInt(reward.timestamp) / 1000)
44+
if (timestamp > to) return acc
45+
return [
46+
...acc,
47+
{
48+
timestamp,
49+
totalRewards: BigInt(reward.totalAssets),
50+
dailyRewards: BigInt(reward.earnedAssets)
51+
}
52+
]
53+
},
54+
[] as {
55+
timestamp: number
56+
totalRewards: bigint
57+
dailyRewards: bigint
58+
}[]
59+
)
60+
.sort((a, b) => a.timestamp - b.timestamp)
5861
}

packages/ethereum/src/lib/types/rewards.ts

-19
This file was deleted.

packages/ethereum/src/staker.ts

+9-4
Original file line numberDiff line numberDiff line change
@@ -279,15 +279,20 @@ export class EthereumStaker {
279279
}) {
280280
const rewards = await getRewardsHistory({
281281
connector: this.connector,
282-
from: new Date(params.startTime),
283-
to: new Date(params.endTime),
282+
from: params.startTime,
283+
to: params.endTime,
284284
vault: params.validatorAddress,
285285
userAccount: params.delegatorAddress
286286
})
287287

288288
return rewards.map((item) => ({
289-
timestamp: item.when.getTime(),
290-
amount: formatEther(item.amount)
289+
timestamp: item.timestamp,
290+
/**
291+
* @deprecated Use `totalRewards` instead.
292+
*/
293+
amount: formatEther(item.totalRewards),
294+
totalRewards: formatEther(item.totalRewards),
295+
dailyRewards: formatEther(item.dailyRewards)
291296
}))
292297
}
293298

0 commit comments

Comments
 (0)