Skip to content

Commit

Permalink
dApp Staking ranked tier update (#1324)
Browse files Browse the repository at this point in the history
* Fetch rank rewards

* Add Hydration to XCM (#1315)

* Add rank reward calculation

* Map rankTier

* Type bug fix

* voting banner wording fix

---------

Co-authored-by: green-jay <75261756+green-jay@users.noreply.github.com>
  • Loading branch information
bobo-k2 and green-jay authored Jun 20, 2024
1 parent 9236605 commit 4bfeaca
Show file tree
Hide file tree
Showing 18 changed files with 181 additions and 20 deletions.
Binary file added src/assets/img/token/hdx.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/i18n/en-US/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1050,7 +1050,7 @@ export default {
percentageLocked: 'Percentage of Supply Locked',
unclaimedRewards: 'Unclaimed rewards',
rewardsClaimedOnStake: 'Rewards will be claimed when stake',
ifYouStakeNow: 'If you stake now',
ifYouStakeNow: 'Basic APR + Bonus (If you stake now)',
defiDescription: 'Discover the world of decentralized finance solutions on Astar.',
'unstoppable-grantsDescription':
'Support projects in the Unstoppable Community Grants program, powered by dApp staking.',
Expand Down
14 changes: 14 additions & 0 deletions src/modules/xcm/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,20 @@ export let xcmChainObj: XcmChainObj = {
subscan: 'https://unique.subscan.io',
isAstarNativeToken: false,
},
[Chain.HYDRATION]: {
name: Chain.HYDRATION,
relayChain: Chain.POLKADOT,
img: require('/src/assets/img/token/hdx.png'),
parachainId: parachainIds.HYDRATION,
endpoints: [
'wss://hydradx-rpc.dwellir.com',
'wss://rpc.hydradx.cloud',
'wss://hydradx.paras.ibp.network',
'wss://rpc.helikon.io/hydradx',
],
subscan: 'https://hydration.subscan.io',
isAstarNativeToken: true,
},
};

export const xcmChains = objToArray(xcmChainObj);
Expand Down
10 changes: 10 additions & 0 deletions src/modules/xcm/tokens/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ export const xcmToken = {
originChain: Chain.ASSET_HUB,
minBridgeAmount: '1',
},
{
symbol: 'HDX',
isNativeToken: true,
assetId: '18446744073709551630',
originAssetId: 'HDX',
logo: require('/src/assets/img/token/hdx.png'),
isXcmCompatible: true,
originChain: Chain.HYDRATION,
minBridgeAmount: '5',
},
],
[endpointKey.SHIDEN]: [
{
Expand Down
2 changes: 1 addition & 1 deletion src/staking-v3/components/PeriodInfoVote.vue
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
</div>
<div class="reward-info">
<div class="reward-label">
<div>{{ $t('stakingV3.basicAprPlusBonus') }}</div>
<div>{{ $t('stakingV3.estimatedRewards') }}</div>
<div>{{ $t('stakingV3.ifYouStakeNow') }}</div>
</div>
<div class="percentage">
Expand Down
8 changes: 4 additions & 4 deletions src/staking-v3/hooks/useLeaderboard.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { watch, ref, computed } from 'vue';
import { CombinedDappInfo, PeriodType, useDappStaking, useDapps } from '..';
import { CombinedDappInfo, DAppTier, PeriodType, useDappStaking, useDapps } from '..';
import { useStore } from 'src/store';
import { sort } from 'src/v2/common';

Expand All @@ -9,7 +9,7 @@ export function useLeaderboard() {
const { dAppTiers, protocolState, eraLengths } = useDappStaking();
// Map key is a dApp tier.
const leaderBoards = ref<Map<number, CombinedDappInfo[]>>(new Map());
const leaderboard = computed<Map<number, number>>(
const leaderboard = computed<Map<number, DAppTier>>(
() => store.getters['stakingV3/getLeaderboard']
);

Expand Down Expand Up @@ -43,8 +43,8 @@ export function useLeaderboard() {
]);

sortedDapps.value.forEach((dapp) => {
const tier = leaderboard.value.get(dapp.chain.id);
tier !== undefined && leaderBoards.value.get(tier + 1)?.push(dapp);
const dappTier = leaderboard.value.get(dapp.chain.id);
dappTier !== undefined && leaderBoards.value.get(dappTier.tierId + 1)?.push(dapp);
});
};

Expand Down
1 change: 1 addition & 0 deletions src/staking-v3/logic/interfaces/DappStakingV3.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export interface PalletDappStakingV3DAppTierRewards extends Struct {
readonly dapps: BTreeMap<Compact<u16>, Compact<u8>>;
readonly rewards: Vec<u128>;
readonly period: Compact<u32>;
readonly rankRewards: Vec<u128>;
}

export interface PalletDappStakingV3EraInfo extends Struct {
Expand Down
4 changes: 3 additions & 1 deletion src/staking-v3/logic/models/DappStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,13 @@ export interface DAppTierRewards {
readonly dapps: DAppTier[];
readonly rewards: bigint[];
readonly period: number;
readonly rankRewards: bigint[];
}

export interface DAppTier {
readonly dappId: number;
readonly tierId: number | undefined;
readonly tierId: number;
readonly rank: number;
}

export interface Rewards {
Expand Down
27 changes: 20 additions & 7 deletions src/staking-v3/logic/repositories/DappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -380,29 +380,34 @@ export class DappStakingRepository implements IDappStakingRepository {
const tiers = tiersWrapped.unwrap();
const dapps: DAppTier[] = [];
tiers.dapps.forEach((value, key) =>
dapps.push({
dappId: key.toNumber(),
tierId: value.toNumber(),
})
dapps.push(this.mapDappTier(key.toNumber(), value.toNumber()))
);
return {
period: tiers.period.toNumber(),
dapps,
rewards: tiers.rewards.map((reward) => reward.toBigInt()),
rankRewards: tiers.rankRewards?.map((reward) => reward.toBigInt()) ?? [
BigInt(0),
BigInt(0),
BigInt(0),
BigInt(0),
], // for backward compatibility in case when not deployed on all networks
};
}

//* @inheritdoc
public async getLeaderboard(): Promise<Map<number, number>> {
public async getLeaderboard(): Promise<Map<number, DAppTier>> {
const api = await this.api.getApi();
const tierAssignmentsBytes = await api.rpc.state.call(
'DappStakingApi_get_dapp_tier_assignment',
''
);
const tierAssignment = api.createType('BTreeMap<u16, u8>', tierAssignmentsBytes);

const result = new Map<number, number>();
tierAssignment.forEach((value, key) => result.set(key.toNumber(), value.toNumber()));
const result = new Map<number, DAppTier>();
tierAssignment.forEach((value, key) =>
result.set(key.toNumber(), this.mapDappTier(key.toNumber(), value.toNumber()))
);

return result;
}
Expand Down Expand Up @@ -627,4 +632,12 @@ export class DappStakingRepository implements IDappStakingRepository {
console.log('Staker info size: ' + result.size);
return result;
}

private mapDappTier(dappId: number, rankTier: number): DAppTier {
return {
dappId: dappId,
tierId: rankTier & 0xf,
rank: rankTier >> 4,
};
}
}
3 changes: 2 additions & 1 deletion src/staking-v3/logic/repositories/IDappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
AccountLedger,
Constants,
ContractStakeAmount,
DAppTier,
DAppTierRewards,
Dapp,
DappBase,
Expand Down Expand Up @@ -212,7 +213,7 @@ export interface IDappStakingRepository {
/**
* Gets dApps tier assignment map.
*/
getLeaderboard(): Promise<Map<number, number>>;
getLeaderboard(): Promise<Map<number, DAppTier>>;

/**
* Gets a call to the legacy code to support v2 ledger stakers to unlock their funds.
Expand Down
4 changes: 3 additions & 1 deletion src/staking-v3/logic/services/DappStakingService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,9 @@ export class DappStakingService extends SignerService implements IDappStakingSer
if (tierReward) {
const dApp = tierReward?.dapps.find((d) => d.dappId === dapp.id);
if (dApp && dApp.tierId !== undefined) {
result.rewards += tierReward.rewards[dApp.tierId];
result.rewards +=
tierReward.rewards[dApp.tierId] +
BigInt(dApp.rank) * tierReward.rankRewards[dApp.tierId];
result.erasToClaim.push(firstEra + index);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/staking-v3/store/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
TiersConfiguration,
EraLengths,
DappInfo,
DAppTier,
} from '../logic';

export interface DappStakingGetters {
Expand All @@ -30,7 +31,7 @@ export interface DappStakingGetters {
getDappTiers(state: DappStakingState): DAppTierRewards;
getTiersConfiguration(state: DappStakingState): TiersConfiguration;
getEraLengths(state: DappStakingState): EraLengths;
getLeaderboard(state: DappStakingState): Map<number, number>;
getLeaderboard(state: DappStakingState): Map<number, DAppTier>;
}

const getters: GetterTree<DappStakingState, StateInterface> & DappStakingGetters = {
Expand Down
3 changes: 2 additions & 1 deletion src/staking-v3/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
TiersConfiguration,
EraLengths,
ProviderDappData,
DAppTier,
} from '../logic';

export interface DappStakingMutations<S = DappStakingState> {
Expand All @@ -37,7 +38,7 @@ export interface DappStakingMutations<S = DappStakingState> {
setDappTiers(state: DappStakingState, dAppTiers: DAppTierRewards): void;
setTiersConfiguration(state: DappStakingState, tiersConfiguration: TiersConfiguration): void;
setEraLengths(state: DappStakingState, eraLengths: EraLengths): void;
setLeaderboard(state: DappStakingState, leaderboard: Map<number, number>): void;
setLeaderboard(state: DappStakingState, leaderboard: Map<number, DAppTier>): void;
}

const updateDapp = <T>(
Expand Down
6 changes: 4 additions & 2 deletions src/staking-v3/store/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
TiersConfiguration,
EraLengths,
DappInfo,
DAppTier,
} from '../logic';

export interface DappStakingState {
Expand All @@ -27,7 +28,7 @@ export interface DappStakingState {
dAppTiers: DAppTierRewards;
tiersConfiguration: TiersConfiguration;
eraLengths: EraLengths;
leaderboard: Map<number, number>;
leaderboard: Map<number, DAppTier>;
}

function state(): DappStakingState {
Expand All @@ -45,7 +46,7 @@ function state(): DappStakingState {
dAppTiers: initialDappTiersConfiguration,
tiersConfiguration: initialTiersConfiguration,
eraLengths: initialEraLengths,
leaderboard: new Map<number, number>(),
leaderboard: new Map<number, DAppTier>(),
};
}

Expand All @@ -68,6 +69,7 @@ export const initialDappTiersConfiguration: DAppTierRewards = {
dapps: [],
rewards: [],
period: 0,
rankRewards: [],
};

export const initialEraLengths: EraLengths = {
Expand Down
2 changes: 2 additions & 0 deletions src/v2/config/xcm/XcmRepositoryConfiguration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
BifrostXcmRepository,
EquilibriumXcmRepository,
UniqueXcmRepository,
HydrationXcmRepository,
} from 'src/v2/repositories/implementations';
import { Chain } from 'src/v2/models/XcmModels';
import { TypeMapping } from 'src/v2/config/types';
Expand All @@ -34,6 +35,7 @@ export const XcmRepositoryConfiguration: TypeMapping = {
[Chain.BIFROST_KUSAMA]: BifrostXcmRepository,
[Chain.EQUILIBRIUM]: EquilibriumXcmRepository,
[Chain.UNIQUE]: UniqueXcmRepository,
[Chain.HYDRATION]: HydrationXcmRepository,
};

export type AstarToken = 'ASTR' | 'SDN';
Expand Down
2 changes: 2 additions & 0 deletions src/v2/models/XcmModels.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export enum Chain {
BIFROST_KUSAMA = 'Bifrost',
EQUILIBRIUM = 'Equilibrium',
UNIQUE = 'Unique',
HYDRATION = 'Hydration',
}

export enum parachainIds {
Expand All @@ -41,6 +42,7 @@ export enum parachainIds {
BIFROST_KUSAMA = 2001,
EQUILIBRIUM = 2011,
UNIQUE = 2037,
HYDRATION = 2034,
}

export interface XcmChain {
Expand Down
1 change: 1 addition & 0 deletions src/v2/repositories/implementations/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './ZkBridgeRepository';
export * from './xcm/BifrostXcmRepository';
export * from './xcm/EquilibriumXcmRepository';
export * from './xcm/UniqueXcmRepository';
export * from './xcm/HydrationXcmRepository';
export * from './NftRepository';
export * from './AccountUnificationRepository';
export * from './InflationRepository';
Expand Down
Loading

0 comments on commit 4bfeaca

Please sign in to comment.