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

Release-hotfix to main #1145

Merged
merged 1 commit into from
Jan 19, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion src/staking-v3/hooks/useDappStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,13 @@ export function useDappStaking() {

const fetchTiersConfigurationToStore = async (): Promise<void> => {
const stakingRepo = container.get<IDappStakingRepository>(Symbols.DappStakingRepositoryV3);
const tiersConfiguration = await stakingRepo.getTiersConfiguration();
const [tiersConfiguration, leaderboard] = await Promise.all([
stakingRepo.getTiersConfiguration(),
stakingRepo.getLeaderboard(),
]);

store.commit('stakingV3/setTiersConfiguration', tiersConfiguration);
store.commit('stakingV3/setLeaderboard', leaderboard);
};

const fetchEraLengthsToStore = async (): Promise<void> => {
Expand Down
1 change: 0 additions & 1 deletion src/staking-v3/logic/astar.json

This file was deleted.

23 changes: 15 additions & 8 deletions src/staking-v3/logic/interfaces/DappStakingV3.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,16 @@
import { Compact, Enum, Option, Struct, Vec, bool, u128, u16, u32, u8 } from '@polkadot/types';
import {
BTreeMap,
Compact,
Enum,
Option,
Struct,
Vec,
bool,
u128,
u16,
u32,
u8,
} from '@polkadot/types';
import { AccountId32, Permill } from '@polkadot/types/interfaces';
import { Codec } from '@polkadot/types/types';

Expand Down Expand Up @@ -57,7 +69,7 @@ export interface PalletDappStakingV3DAppInfo extends Struct {
readonly owner: AccountId32;
readonly id: Compact<u16>;
readonly state: PalletDappStakingV3DAppState;
readonly rewardDestination: Option<AccountId32>;
readonly rewardBeneficiary: Option<AccountId32>;
}

export interface SmartContractAddress extends Struct {
Expand Down Expand Up @@ -106,16 +118,11 @@ interface PalletDappStakingV3EraReward extends Struct {
}

export interface PalletDappStakingV3DAppTierRewards extends Struct {
readonly dapps: Vec<PalletDappStakingV3DAppTier>;
readonly dapps: BTreeMap<Compact<u16>, Compact<u8>>;
readonly rewards: Vec<u128>;
readonly period: Compact<u32>;
}

interface PalletDappStakingV3DAppTier extends Struct {
readonly dappId: Compact<u16>;
readonly tierId: Option<u8>;
}

export interface PalletDappStakingV3EraInfo extends Struct {
readonly activeEraLocked: Compact<u128>;
readonly totalLocked: Compact<u128>;
Expand Down
3 changes: 2 additions & 1 deletion src/staking-v3/logic/models/DappStaking.ts
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ export interface EraLengths {
standardErasPerBuildAndEarnPeriod: number;
standardErasPerVotingPeriod: number;
standardEraLength: number;
periodsPerCycle: number;
}

export interface DAppTierRewards {
Expand All @@ -139,7 +140,7 @@ export interface DAppTierRewards {
readonly period: number;
}

interface DAppTier {
export interface DAppTier {
readonly dappId: number;
readonly tierId: number | undefined;
}
Expand Down
34 changes: 28 additions & 6 deletions src/staking-v3/logic/repositories/DappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
AccountLedgerChangedMessage,
Constants,
ContractStakeAmount,
DAppTier,
DAppTierRewards,
Dapp,
DappBase,
Expand Down Expand Up @@ -277,16 +278,18 @@ export class DappStakingRepository implements IDappStakingRepository {
const getNumber = (bytes: Bytes): number => u8aToNumber(bytes.toU8a().slice(1, 4));
const api = await this.api.getApi();

const [erasPerBuildAndEarn, erasPerVoting, eraLength] = await Promise.all([
const [erasPerBuildAndEarn, erasPerVoting, eraLength, periodsPerCycle] = await Promise.all([
api.rpc.state.call('DappStakingApi_eras_per_build_and_earn_subperiod', ''),
api.rpc.state.call('DappStakingApi_eras_per_voting_subperiod', ''),
api.rpc.state.call('DappStakingApi_blocks_per_era', ''),
api.rpc.state.call('DappStakingApi_periods_per_cycle', ''),
]);

return {
standardErasPerBuildAndEarnPeriod: getNumber(erasPerBuildAndEarn),
standardErasPerVotingPeriod: getNumber(erasPerVoting),
standardEraLength: getNumber(eraLength),
periodsPerCycle: getNumber(periodsPerCycle),
};
}

Expand Down Expand Up @@ -320,16 +323,35 @@ 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(),
})
);
return {
period: tiers.period.toNumber(),
dapps: tiers.dapps.map((dapp) => ({
dappId: dapp.dappId.toNumber(),
tierId: dapp.tierId.unwrapOr(undefined)?.toNumber(),
})),
dapps,
rewards: tiers.rewards.map((reward) => reward.toBigInt()),
};
}

//* @inheritdoc
public async getLeaderboard(): Promise<Map<number, number>> {
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()));

return result;
}

public async getStakerInfo(
address: string,
includePreviousPeriods = false
Expand Down Expand Up @@ -517,7 +539,7 @@ export class DappStakingRepository implements IDappStakingRepository {
owner: dapp.owner.toString(),
id: dapp.id.toNumber(),
state: dapp.state.isUnregistered ? DappState.Unregistered : DappState.Registered,
rewardDestination: dapp.rewardDestination.unwrapOr(undefined)?.toString(),
rewardDestination: dapp.rewardBeneficiary.unwrapOr(undefined)?.toString(),
};
}

Expand Down
5 changes: 5 additions & 0 deletions src/staking-v3/logic/repositories/IDappStakingRepository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -219,4 +219,9 @@ export interface IDappStakingRepository {
getEraLengths(): Promise<EraLengths>;

getCleanupExpiredEntriesCall(): Promise<ExtrinsicPayload>;

/**
* Gets dApps tier assignment map.
*/
getLeaderboard(): Promise<Map<number, number>>;
}
2 changes: 2 additions & 0 deletions src/staking-v3/store/getters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export interface DappStakingGetters {
getDappTiers(state: DappStakingState): DAppTierRewards;
getTiersConfiguration(state: DappStakingState): TiersConfiguration;
getEraLengths(state: DappStakingState): EraLengths;
getLeaderboard(state: DappStakingState): Map<number, number>;
}

const getters: GetterTree<DappStakingState, StateInterface> & DappStakingGetters = {
Expand All @@ -46,6 +47,7 @@ const getters: GetterTree<DappStakingState, StateInterface> & DappStakingGetters
getDappTiers: (state) => state.dAppTiers,
getTiersConfiguration: (state) => state.tiersConfiguration,
getEraLengths: (state) => state.eraLengths,
getLeaderboard: (state) => state.leaderboard,
};

export default getters;
4 changes: 4 additions & 0 deletions src/staking-v3/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,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;
}

const updateDapp = <T>(
Expand Down Expand Up @@ -99,6 +100,9 @@ const mutations: MutationTree<DappStakingState> & DappStakingMutations = {
setEraLengths(state, eraLengths) {
state.eraLengths = eraLengths;
},
setLeaderboard(state, leaderboard) {
state.leaderboard = leaderboard;
},
};

export default mutations;
3 changes: 3 additions & 0 deletions src/staking-v3/store/state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export interface DappStakingState {
dAppTiers: DAppTierRewards;
tiersConfiguration: TiersConfiguration;
eraLengths: EraLengths;
leaderboard: Map<number, number>;
}

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

Expand All @@ -61,6 +63,7 @@ export const initialEraLengths: EraLengths = {
standardErasPerBuildAndEarnPeriod: 0,
standardErasPerVotingPeriod: 0,
standardEraLength: 0,
periodsPerCycle: 0,
};

export default state;
Loading