1
- import type {
2
- DelegationDelegatorReward
3
- } from "cosmjs-types/cosmos/distribution/v1beta1/distribution" ;
1
+ import type { DelegationDelegatorReward } from "cosmjs-types/cosmos/distribution/v1beta1/distribution" ;
4
2
import type {
5
3
DelegationResponse ,
6
4
Validator ,
@@ -19,7 +17,7 @@ const createBabylonClient = ({ request }: Dependencies) => ({
19
17
return await fetchAllPages (
20
18
request ,
21
19
`/cosmos/staking/v1beta1/delegations/${ address } ` ,
22
- "delegationResponses"
20
+ "delegationResponses" ,
23
21
) ;
24
22
} catch ( error ) {
25
23
throw new Error ( `Failed to fetch delegations for ${ address } ` , {
@@ -46,7 +44,11 @@ const createBabylonClient = ({ request }: Dependencies) => ({
46
44
47
45
async getValidators ( ) : Promise < Validator [ ] > {
48
46
try {
49
- return await fetchAllPages ( request , "/cosmos/staking/v1beta1/validators" , "validators" ) ;
47
+ return await fetchAllPages (
48
+ request ,
49
+ "/cosmos/staking/v1beta1/validators" ,
50
+ "validators" ,
51
+ ) ;
50
52
} catch ( error ) {
51
53
throw new Error ( `Failed to fetch validators` , {
52
54
cause : error ,
@@ -92,31 +94,100 @@ const createBabylonClient = ({ request }: Dependencies) => ({
92
94
const btcStakingPortion = Number ( params ?. btcStakingPortion ?? 0 ) ;
93
95
const fpPortion = Number ( params ?. fpPortion ?? 0 ) ;
94
96
return {
95
- btcStakingPortion : Number . isFinite ( btcStakingPortion ) ? btcStakingPortion : 0 ,
97
+ btcStakingPortion : Number . isFinite ( btcStakingPortion )
98
+ ? btcStakingPortion
99
+ : 0 ,
96
100
fpPortion : Number . isFinite ( fpPortion ) ? fpPortion : 0 ,
97
101
} ;
98
102
} ,
99
103
100
104
async getCostakingParams ( ) : Promise < {
101
105
costakingPortion : number ;
102
106
validatorsPortion : number ;
107
+ scoreRatioBtcByBaby : string ;
103
108
} > {
104
109
const response = await request ( "/babylon/costaking/v1/params" ) ;
105
110
const params = response ?. params ?? response ;
106
- const costakingPortion = Number ( params ?. costakingPortion ?? 0 ) ;
107
- const validatorsPortion = Number ( params ?. validatorsPortion ?? 0 ) ;
111
+ const costakingPortion = Number (
112
+ params ?. costakingPortion ?? params ?. costaking_portion ?? 0 ,
113
+ ) ;
114
+ const validatorsPortion = Number (
115
+ params ?. validatorsPortion ?? params ?. validators_portion ?? 0 ,
116
+ ) ;
117
+ const scoreRatioBtcByBaby =
118
+ params ?. scoreRatioBtcByBaby ?? params ?. score_ratio_btc_by_baby ?? "50" ;
108
119
return {
109
- costakingPortion : Number . isFinite ( costakingPortion ) ? costakingPortion : 0 ,
110
- validatorsPortion : Number . isFinite ( validatorsPortion ) ? validatorsPortion : 0 ,
120
+ costakingPortion : Number . isFinite ( costakingPortion )
121
+ ? costakingPortion
122
+ : 0 ,
123
+ validatorsPortion : Number . isFinite ( validatorsPortion )
124
+ ? validatorsPortion
125
+ : 0 ,
126
+ scoreRatioBtcByBaby : scoreRatioBtcByBaby ,
111
127
} ;
112
128
} ,
113
129
114
- async getAnnualProvisions ( ) : Promise < number > {
130
+ async getCoStakerRewardsTracker ( costakerAddress : string ) : Promise < {
131
+ startPeriodCumulativeReward : number ;
132
+ activeSatoshis : string ;
133
+ activeBaby : string ;
134
+ totalScore : string ;
135
+ } | null > {
136
+ if ( ! costakerAddress ) {
137
+ return null ;
138
+ }
139
+
115
140
try {
116
141
const response = await request (
117
- "/cosmos/mint/v1beta1/annual_provisions" ,
142
+ `/babylon/costaking/v1/costakers/${ costakerAddress } /rewards_tracker` ,
143
+ ) ;
144
+
145
+ return {
146
+ startPeriodCumulativeReward :
147
+ response ?. start_period_cumulative_reward ?? 0 ,
148
+ activeSatoshis : response ?. active_satoshis ?? "0" ,
149
+ activeBaby : response ?. active_baby ?? "0" ,
150
+ totalScore : response ?. total_score ?? "0" ,
151
+ } ;
152
+ } catch ( error : any ) {
153
+ // Return null for 404 errors (user has not co-staked yet)
154
+ if ( error ?. message ?. includes ( "404" ) || error ?. status === 404 ) {
155
+ return null ;
156
+ }
157
+ throw new Error (
158
+ `Failed to fetch co-staker rewards tracker for ${ costakerAddress } ` ,
159
+ {
160
+ cause : error ,
161
+ } ,
118
162
) ;
119
- const annualProvisions = response ?. annualProvisions ?? response ?. annual_provisions ?? response ;
163
+ }
164
+ } ,
165
+
166
+ async getCurrentCoStakingRewards ( ) : Promise < {
167
+ rewards : Array < { denom : string ; amount : string } > ;
168
+ period : number ;
169
+ totalScore : string ;
170
+ } > {
171
+ try {
172
+ const response = await request ( "/babylon/costaking/v1/current_rewards" ) ;
173
+
174
+ return {
175
+ rewards : response ?. rewards ?? [ ] ,
176
+ period : response ?. period ?? 0 ,
177
+ totalScore : response ?. total_score ?? "0" ,
178
+ } ;
179
+ } catch ( error ) {
180
+ throw new Error ( "Failed to fetch current co-staking rewards" , {
181
+ cause : error ,
182
+ } ) ;
183
+ }
184
+ } ,
185
+
186
+ async getAnnualProvisions ( ) : Promise < number > {
187
+ try {
188
+ const response = await request ( "/cosmos/mint/v1beta1/annual_provisions" ) ;
189
+ const annualProvisions =
190
+ response ?. annualProvisions ?? response ?. annual_provisions ?? response ;
120
191
const result = Number ( annualProvisions ) ;
121
192
return result ;
122
193
} catch ( error ) {
@@ -126,13 +197,11 @@ const createBabylonClient = ({ request }: Dependencies) => ({
126
197
}
127
198
} ,
128
199
129
-
130
200
async getSupply ( denom : string = "ubbn" ) : Promise < bigint > {
131
201
try {
132
- const response = await request (
133
- "/cosmos/bank/v1beta1/supply/by_denom" ,
134
- { denom } ,
135
- ) ;
202
+ const response = await request ( "/cosmos/bank/v1beta1/supply/by_denom" , {
203
+ denom,
204
+ } ) ;
136
205
const amount = response ?. amount ?. amount ?? 0 ;
137
206
return BigInt ( amount ) ;
138
207
} catch ( error : any ) {
@@ -144,9 +213,9 @@ const createBabylonClient = ({ request }: Dependencies) => ({
144
213
145
214
async getCurrentEpoch ( ) {
146
215
try {
147
- const {
148
- current_epoch, epoch_boundary
149
- } = await request ( "/babylon/epoching/v1/current_epoch" ) ;
216
+ const { current_epoch , epoch_boundary } = await request (
217
+ "/babylon/epoching/v1/ current_epoch" ,
218
+ ) ;
150
219
151
220
return {
152
221
epochBoundary : parseInt ( epoch_boundary , 10 ) ,
0 commit comments