-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: query validators uptime on demand [wip] [PTD-1313]
- Loading branch information
1 parent
959c313
commit 4da6db4
Showing
18 changed files
with
586 additions
and
427 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<script lang="ts"> | ||
import { | ||
uptimeUiOptions as options, | ||
type UptimeValue | ||
} from '@api/utils/entities/component/validator' | ||
import SimplePicker from '@components/_base/picker/simple-picker/SimplePicker.svelte' | ||
import { uptimeModule } from './uptime-module' | ||
export let selected: UptimeValue | ||
let selectedValue: (typeof options)[number] | ||
$: selected = selectedValue?.value | ||
$: { | ||
console.log('selected value uptime', selected) | ||
uptimeModule.maybeQueryUptime(selected) | ||
} | ||
</script> | ||
|
||
<div class="picker"> | ||
<SimplePicker {options} bind:selected={selectedValue} /> | ||
</div> | ||
|
||
<style lang="scss"> | ||
.picker { | ||
width: var(--uptime-selector-width, 6rem); | ||
min-width: 6rem; | ||
} | ||
</style> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
import { BehaviorSubject } from 'rxjs' | ||
import { | ||
calculateApy, | ||
getValidatorUptimeSinceDate, | ||
uptimePeriodDefinition, | ||
type UptimeValue | ||
} from '@api/utils/entities/component/validator' | ||
import type BigNumber from 'bignumber.js' | ||
import type { ValidatorCollectionItem } from '@common/gateway-sdk' | ||
|
||
type ValidatorAddress = string | ||
|
||
export type UptimeData = Partial< | ||
Record< | ||
UptimeValue, | ||
Record< | ||
ValidatorAddress, | ||
number | undefined | ||
> | ||
> | ||
> | ||
|
||
export const UptimeModule = () => { | ||
const isLoading = new BehaviorSubject<boolean>(false) | ||
let validators: Promise<{ address: string }[]> | undefined | ||
let uptimeData: any = {} | ||
return { | ||
setValidators: (promise: typeof validators) => { | ||
validators = promise | ||
}, | ||
hasUptimeData: (uptime: UptimeValue) => { | ||
return !!uptimeData[uptime] | ||
}, | ||
getDataForUptime: (uptime: UptimeValue) => { | ||
return uptimeData[uptime] || {} | ||
}, | ||
getPercentage: (uptime: UptimeValue, address: ValidatorAddress) => {}, | ||
getApy: ( | ||
validator: ValidatorCollectionItem, | ||
uptime: UptimeValue, | ||
totalAmountStaked: BigNumber | ||
) => { | ||
const address = validator.address | ||
// const fee = Number(validator.effective_fee_factor.current.fee_factor) | ||
const fee = Number(validator.effective_fee_factor?.current?.fee_factor || (validator as any).fee()) // TODO: fix this | ||
const uptimePercentage = uptimeData[uptime]?.[address] | ||
return calculateApy(fee, uptimePercentage, totalAmountStaked) | ||
}, | ||
maybeQueryUptime: async (uptime: UptimeValue | undefined) => { | ||
if (!uptime) return | ||
if (!validators) { | ||
throw new Error('Validators not set') | ||
} | ||
if (uptimeData[uptime]) { | ||
return uptimeData[uptime] | ||
} | ||
|
||
uptimeData[uptime] = {} | ||
|
||
isLoading.next(true) | ||
|
||
return validators | ||
.then((v) => | ||
getValidatorUptimeSinceDate(v.map((v) => v.address))( | ||
uptimePeriodDefinition[uptime].getStartingPoint() | ||
) | ||
) | ||
.then((value) => { | ||
isLoading.next(false) | ||
if (value.isOk()) { | ||
uptimeData[uptime] = value.value | ||
return uptimeData[uptime] | ||
} | ||
}) | ||
.catch((e) => { | ||
console.error(e) | ||
uptimeData[uptime] = undefined | ||
}) | ||
}, | ||
clean: () => { | ||
validators = undefined | ||
uptimeData = {} | ||
}, | ||
isLoading$: isLoading.asObservable() | ||
} | ||
} | ||
|
||
export const uptimeModule = UptimeModule() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.