Skip to content

Commit

Permalink
Pallet version check
Browse files Browse the repository at this point in the history
  • Loading branch information
bobo-k2 committed Aug 22, 2024
1 parent 0c0d7b5 commit 24af84e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
2 changes: 1 addition & 1 deletion src/staking-v3/components/leaderboard/Tier.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div class="text--reward">{{ $t('stakingV3.threshold') }}</div>
<div class="text--amount">
<token-balance-native
:balance="tiersConfiguration?.tierThresholds[tier - 1].toString() ?? '0'"
:balance="tiersConfiguration?.tierThresholds[tier - 1]?.toString() ?? '0'"
:decimals="0"
/>
</div>
Expand Down
12 changes: 9 additions & 3 deletions src/staking-v3/logic/interfaces/DappStakingV3.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Balance } from '@astar-network/metamask-astar-types';
import { AccountId32, Permill } from '@polkadot/types/interfaces';
import { AccountId32, Perbill, Permill } from '@polkadot/types/interfaces';
import {
BTreeMap,
Compact,
Expand Down Expand Up @@ -126,10 +126,16 @@ export interface PalletDappStakingV3ContractStakeAmount extends Struct {
readonly tierLabel: Option<PalletDappStakingV3TierLabel>;
}

export interface PalletDappStakingV3TiersConfiguration extends Struct {
export interface PalletDappStakingV3TiersConfigurationLegacy extends Struct {
readonly slotsPerTier: Vec<u16>;
readonly rewardPortion: Vec<Permill>;
readonly tierThresholds: Vec<PalletDappStakingV3TierThreshold> | Vec<u128>;
readonly tierThresholds: Vec<PalletDappStakingV3TierThreshold>;
}

export interface PalletDappStakingV3TiersConfiguration extends Struct {
readonly slotsPerTier: Vec<u16>;
readonly rewardPortion: Vec<Perbill>;
readonly tierThresholds: Vec<u128>;
}

export interface PalletDappStakingV3TierThreshold extends Enum {
Expand Down
30 changes: 18 additions & 12 deletions src/staking-v3/logic/repositories/DappStakingRepository.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import {
TOKEN_API_URL,
ExtrinsicPayload,
getDappAddressEnum,
hasProperty,
} from '@astar-network/astar-sdk-core';
import { TOKEN_API_URL, ExtrinsicPayload, getDappAddressEnum } from '@astar-network/astar-sdk-core';
import {
AccountLedger,
AccountLedgerChangedMessage,
Expand All @@ -25,7 +20,6 @@ import {
SingularStakingInfo,
StakeAmount,
TiersConfiguration,
TvlAmountType,
} from '../models';
import axios from 'axios';
import { inject, injectable } from 'inversify';
Expand All @@ -43,11 +37,12 @@ import {
PalletDappStakingV3SingularStakingInfo,
PalletDappStakingV3StakeAmount,
PalletDappStakingV3TiersConfiguration,
PalletDappStakingV3TiersConfigurationLegacy,
PalletDappStakingV3TierThreshold,
SmartContractAddress,
} from '../interfaces';
import { IEventAggregator } from 'src/v2/messaging';
import { Option, StorageKey, u32, u128, Bytes } from '@polkadot/types';
import { Option, StorageKey, u32, u128, Bytes, u16 } from '@polkadot/types';
import { IDappStakingRepository } from './IDappStakingRepository';
import { Guard } from 'src/v2/common';
import { ethers } from 'ethers';
Expand Down Expand Up @@ -518,9 +513,20 @@ export class DappStakingRepository implements IDappStakingRepository {
}

public async getTiersConfiguration(): Promise<TiersConfiguration> {
const TIER_DERIVATION_PALLET_VERSION = 8;
const api = await this.api.getApi();
const configuration =
await api.query.dappStaking.tierConfig<PalletDappStakingV3TiersConfiguration>();
const palletVersion = (await api.query.dappStaking.palletVersion<u16>()).toNumber();
let configuration:
| PalletDappStakingV3TiersConfiguration
| PalletDappStakingV3TiersConfigurationLegacy;

if (palletVersion >= TIER_DERIVATION_PALLET_VERSION) {
configuration =
await api.query.dappStaking.tierConfig<PalletDappStakingV3TiersConfiguration>();
} else {
configuration =
await api.query.dappStaking.tierConfig<PalletDappStakingV3TiersConfigurationLegacy>();
}

return {
numberOfSlots: configuration.slotsPerTier.reduce((acc, val) => acc + val.toNumber(), 0),
Expand All @@ -529,8 +535,8 @@ export class DappStakingRepository implements IDappStakingRepository {
tierThresholds: configuration.tierThresholds.map((threshold) => {
// Support both u128 and PalletDappStakingV3TierThreshold.
// If threshold has isUnsigned property it's u128.
// memo: remove isUnsigned check when u128 is used for all thresholds. Most likely in Astar period 003.
if (!hasProperty(threshold, 'isUnsigned')) {
// TODO: remove palletVersion check when u128 is used for all thresholds. Most likely in Astar period 003.
if (palletVersion < TIER_DERIVATION_PALLET_VERSION) {
const t = <PalletDappStakingV3TierThreshold>threshold;
if (t.isDynamicTvlAmount) {
return t.asDynamicTvlAmount.amount.toBigInt();
Expand Down

0 comments on commit 24af84e

Please sign in to comment.