@@ -13,7 +13,8 @@ import { Array, BigDecimal, Data, Effect, pipe, Schema } from "effect"
13
13
const REST_BASE_URL = "https://rest.union.build"
14
14
15
15
// Staker address that delegates to validators on behalf of liquid stakers
16
- const LIQUID_STAKING_STAKER_ADDRESS = "union19ydrfy0d80vgpvs6p0cljlahgxwrkz54ps8455q7jfdfape7ld7quaq69v"
16
+ const LIQUID_STAKING_STAKER_ADDRESS =
17
+ "union19ydrfy0d80vgpvs6p0cljlahgxwrkz54ps8455q7jfdfape7ld7quaq69v"
17
18
18
19
export class IncentiveError extends Data . TaggedError ( "IncentiveError" ) < {
19
20
message : string
@@ -179,29 +180,37 @@ const getValidators = pipe(
179
180
) ,
180
181
)
181
182
182
- const getDelegatorDelegations = ( delegatorAddress : string ) => pipe (
183
- HttpClient . HttpClient ,
184
- Effect . map ( HttpClient . withTracerDisabledWhen ( ( ) => true ) ) ,
185
- Effect . andThen ( ( client ) =>
186
- pipe (
187
- client . get ( `${ REST_BASE_URL } /cosmos/staking/v1beta1/delegations/${ delegatorAddress } ` ) ,
188
- Effect . flatMap ( HttpClientResponse . schemaBodyJson ( DelegatorDelegationsResponse ) ) ,
189
- Effect . mapError ( ( cause ) =>
190
- new IncentiveError ( {
191
- message : "Failed to fetch delegator delegations" ,
192
- cause,
193
- } )
194
- ) ,
195
- )
196
- ) ,
197
- )
183
+ const getDelegatorDelegations = ( delegatorAddress : string ) =>
184
+ pipe (
185
+ HttpClient . HttpClient ,
186
+ Effect . map ( HttpClient . withTracerDisabledWhen ( ( ) => true ) ) ,
187
+ Effect . andThen ( ( client ) =>
188
+ pipe (
189
+ client . get ( `${ REST_BASE_URL } /cosmos/staking/v1beta1/delegations/${ delegatorAddress } ` ) ,
190
+ Effect . flatMap ( HttpClientResponse . schemaBodyJson ( DelegatorDelegationsResponse ) ) ,
191
+ Effect . mapError ( ( cause ) =>
192
+ new IncentiveError ( {
193
+ message : "Failed to fetch delegator delegations" ,
194
+ cause,
195
+ } )
196
+ ) ,
197
+ )
198
+ ) ,
199
+ )
198
200
199
201
export const calculateIncentive : Effect . Effect <
200
202
IncentiveResult ,
201
203
IncentiveError ,
202
204
HttpClient . HttpClient
203
205
> = Effect . gen ( function * ( ) {
204
- const [ inflationData , stakingPoolData , distributionData , circulatingSupplyData , validatorsData , delegationsData ] = yield * Effect
206
+ const [
207
+ inflationData ,
208
+ stakingPoolData ,
209
+ distributionData ,
210
+ circulatingSupplyData ,
211
+ validatorsData ,
212
+ delegationsData ,
213
+ ] = yield * Effect
205
214
. all ( [
206
215
getInflation ,
207
216
getStakingPool ,
@@ -263,7 +272,7 @@ export const calculateIncentive: Effect.Effect<
263
272
264
273
// Calculate weighted average validator commission
265
274
const validatorMap = new Map ( validatorsData . validators . map ( v => [ v . operator_address , v ] ) )
266
-
275
+
267
276
const validDelegations = pipe (
268
277
delegationsData . delegation_responses ,
269
278
Array . filter ( delegation => {
@@ -272,8 +281,9 @@ export const calculateIncentive: Effect.Effect<
272
281
} ) ,
273
282
Array . map ( delegation => ( {
274
283
amount : delegation . balance . amount ,
275
- commission : validatorMap . get ( delegation . delegation . validator_address ) ! . commission . commission_rates . rate
276
- } ) )
284
+ commission :
285
+ validatorMap . get ( delegation . delegation . validator_address ) ! . commission . commission_rates . rate ,
286
+ } ) ) ,
277
287
)
278
288
279
289
const { totalAmount, weightedSum } = pipe (
@@ -282,22 +292,25 @@ export const calculateIncentive: Effect.Effect<
282
292
{ totalAmount : BigDecimal . fromBigInt ( 0n ) , weightedSum : BigDecimal . fromBigInt ( 0n ) } ,
283
293
( acc , { amount, commission } ) => ( {
284
294
totalAmount : BigDecimal . sum ( acc . totalAmount , amount ) ,
285
- weightedSum : BigDecimal . sum ( acc . weightedSum , BigDecimal . multiply ( amount , commission ) )
286
- } )
287
- )
295
+ weightedSum : BigDecimal . sum ( acc . weightedSum , BigDecimal . multiply ( amount , commission ) ) ,
296
+ } ) ,
297
+ ) ,
288
298
)
289
299
290
300
const weightedAverageCommission = BigDecimal . isZero ( totalAmount )
291
301
? BigDecimal . fromBigInt ( 0n )
292
302
: yield * BigDecimal . divide ( weightedSum , totalAmount ) . pipe (
293
- Effect . mapError ( ( ) =>
294
- new IncentiveError ( {
295
- message : "Could not calculate weighted average commission" ,
296
- } )
297
- ) ,
298
- )
303
+ Effect . mapError ( ( ) =>
304
+ new IncentiveError ( {
305
+ message : "Could not calculate weighted average commission" ,
306
+ } )
307
+ ) ,
308
+ )
299
309
300
- const validatorCommissionAmount = BigDecimal . multiply ( incentiveAfterTax , weightedAverageCommission )
310
+ const validatorCommissionAmount = BigDecimal . multiply (
311
+ incentiveAfterTax ,
312
+ weightedAverageCommission ,
313
+ )
301
314
const incentiveAfterCommission = BigDecimal . subtract ( incentiveAfterTax , validatorCommissionAmount )
302
315
303
316
const bondedRatio = yield * BigDecimal . divide ( bondedTokens , totalSupply ) . pipe (
0 commit comments