Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

dApp Staking ranked tier update #1324

Merged
merged 8 commits into from
Jun 20, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
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
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
22 changes: 15 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,29 @@ 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()) ?? [0, 0, 0, 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 +627,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 @@ -576,7 +576,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
109 changes: 109 additions & 0 deletions src/v2/repositories/implementations/xcm/HydrationXcmRepository.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
import { BN } from '@polkadot/util';
import { decodeAddress } from '@polkadot/util-crypto';
import { XcmTokenInformation } from 'src/modules/xcm';
import { container } from 'src/v2/common';
import { ExtrinsicPayload, IApi, IApiFactory } from 'src/v2/integration';
import { Asset } from 'src/v2/models';
import { XcmChain } from 'src/v2/models/XcmModels';
import { TokensAccounts } from 'src/v2/repositories/implementations/xcm/AcalaXcmRepository';
import { Symbols } from 'src/v2/symbols';
import { XcmRepository } from '../XcmRepository';

// Mapping object for token IDs
const TOKEN_IDS: Record<string, { id: number }> = {
HDX: { id: 0 },
ASTR: { id: 9 },
};

export class HydrationXcmRepository extends XcmRepository {
constructor() {
const defaultApi = container.get<IApi>(Symbols.DefaultApi);
const apiFactory = container.get<IApiFactory>(Symbols.ApiFactory);
const registeredTokens = container.get<XcmTokenInformation[]>(Symbols.RegisteredTokens);
super(defaultApi, apiFactory, registeredTokens);
this.astarTokens = {
ASTR: 9,
};
}

public async getTransferCall(
from: XcmChain,
to: XcmChain,
recipientAddress: string,
token: Asset,
amount: BN,
endpoint: string
): Promise<ExtrinsicPayload> {
if (!to.parachainId) {
throw `Parachain id for ${to.name} is not defined`;
}

const tokenData = TOKEN_IDS[token.originAssetId];
if (!tokenData) {
throw `Token name for ${token.originAssetId} is not defined`;
}

const version = 'V3';

const AccountId32 = {
id: decodeAddress(recipientAddress),
};

const destination = {
[version]: {
parents: '1',
interior: {
X2: [
{
Parachain: to.parachainId,
},
{
AccountId32,
},
],
},
},
};

const destWeight = {
Unlimited: null,
};

return await this.buildTxCall(
from,
endpoint,
'xTokens',
'transfer',
tokenData,
amount,
destination,
destWeight
);
}

public async getTokenBalance(
address: string,
chain: XcmChain,
token: Asset,
isNativeToken: boolean,
endpoint: string
): Promise<string> {
const api = await this.apiFactory.get(endpoint);
try {
if (token.originAssetId == 'HDX') {
return (await this.getNativeBalance(address, chain, endpoint)).toString();
} else {
const tokenData = TOKEN_IDS[token.originAssetId];
if (!tokenData) {
return '0';
}

const bal = await api.query.tokens.accounts<TokensAccounts>(address, tokenData);
return bal.free.toString();
}
} catch (e) {
console.error(e);
return '0';
}
}
}
Loading