Skip to content

Commit

Permalink
NNS1-3139: Utils to get maturity from nns/sns neurons (#5235)
Browse files Browse the repository at this point in the history
# Motivation

Make getting available/staked maturity from nns/sns neurons consistent
and straightforward.
I plan to use these for the projects table as well as the neurons table.

# Changes

1. Add utility functions get available and staked maturity from NNS and
SNS neurons.
2. Use them in `neurons-table.utils.ts` instead of getting the maturity
directly.

# Tests

1. Unit tests added for the new functions.
2. Existing `frontend/src/lib/utils/neurons-table.utils.ts` tests pass.

# Todos

- [ ] Add entry to changelog (if necessary).
not necessary
  • Loading branch information
dskloetd authored Jul 25, 2024
1 parent 7faeb1e commit 69a2148
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 11 deletions.
6 changes: 6 additions & 0 deletions frontend/src/lib/utils/neuron.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -472,6 +472,12 @@ export const neuronStake = (neuron: NeuronInfo): bigint =>
? neuron.fullNeuron?.cachedNeuronStake - neuron.fullNeuron?.neuronFees
: 0n;

export const neuronAvailableMaturity = (neuron: NeuronInfo): bigint =>
neuron.fullNeuron?.maturityE8sEquivalent ?? 0n;

export const neuronStakedMaturity = (neuron: NeuronInfo): bigint =>
neuron.fullNeuron?.stakedMaturityE8sEquivalent ?? 0n;

export interface FolloweesNeuron {
neuronId: NeuronId;
topics: [Topic, ...Topic[]];
Expand Down
20 changes: 9 additions & 11 deletions frontend/src/lib/utils/neurons-table.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { buildNeuronUrl } from "$lib/utils/navigation.utils";
import {
getNeuronTags,
isSpawning,
neuronAvailableMaturity,
neuronStake,
neuronStakedMaturity,
} from "$lib/utils/neuron.utils";
import {
createAscendingComparator,
Expand All @@ -15,21 +17,18 @@ import {
} from "$lib/utils/responsive-table.utils";
import {
getSnsDissolveDelaySeconds,
getSnsNeuronAvailableMaturity,
getSnsNeuronIdAsHexString,
getSnsNeuronStake,
getSnsNeuronStakedMaturity,
getSnsNeuronState,
getSnsNeuronTags,
} from "$lib/utils/sns-neuron.utils";
import type { Identity } from "@dfinity/agent";
import type { NeuronInfo } from "@dfinity/nns";
import { NeuronState } from "@dfinity/nns";
import type { SnsNeuron } from "@dfinity/sns";
import {
ICPToken,
TokenAmountV2,
fromNullable,
type Token,
} from "@dfinity/utils";
import { ICPToken, TokenAmountV2, type Token } from "@dfinity/utils";

export const tableNeuronsFromNeuronInfos = ({
neuronInfos,
Expand Down Expand Up @@ -60,8 +59,8 @@ export const tableNeuronsFromNeuronInfos = ({
amount: neuronStake(neuronInfo),
token: ICPToken,
}),
availableMaturity: neuronInfo.fullNeuron?.maturityE8sEquivalent ?? 0n,
stakedMaturity: neuronInfo.fullNeuron?.stakedMaturityE8sEquivalent ?? 0n,
availableMaturity: neuronAvailableMaturity(neuronInfo),
stakedMaturity: neuronStakedMaturity(neuronInfo),
dissolveDelaySeconds,
state: neuronInfo.state,
tags: getNeuronTags({
Expand Down Expand Up @@ -102,9 +101,8 @@ export const tableNeuronsFromSnsNeurons = ({
amount: getSnsNeuronStake(snsNeuron),
token,
}),
availableMaturity: snsNeuron.maturity_e8s_equivalent ?? 0n,
stakedMaturity:
fromNullable(snsNeuron.staked_maturity_e8s_equivalent) ?? 0n,
availableMaturity: getSnsNeuronAvailableMaturity(snsNeuron),
stakedMaturity: getSnsNeuronStakedMaturity(snsNeuron),
dissolveDelaySeconds,
state: getSnsNeuronState(snsNeuron),
tags: getSnsNeuronTags({
Expand Down
6 changes: 6 additions & 0 deletions frontend/src/lib/utils/sns-neuron.utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,12 @@ export const getSnsNeuronStake = ({
neuron_fees_e8s,
}: SnsNeuron): bigint => cached_neuron_stake_e8s - neuron_fees_e8s;

export const getSnsNeuronAvailableMaturity = (neuron: SnsNeuron): bigint =>
neuron.maturity_e8s_equivalent;

export const getSnsNeuronStakedMaturity = (neuron: SnsNeuron): bigint =>
fromNullable(neuron.staked_maturity_e8s_equivalent) ?? 0n;

export const getSnsNeuronByHexId = ({
neuronIdHex,
neurons,
Expand Down
46 changes: 46 additions & 0 deletions frontend/src/tests/lib/utils/neuron.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ import {
maturityLastDistribution,
minNeuronSplittable,
neuronAge,
neuronAvailableMaturity,
neuronCanBeSplit,
neuronStake,
neuronStakedMaturity,
neuronVotingPower,
neuronsVotingPower,
sortNeuronsByStake,
Expand Down Expand Up @@ -989,6 +991,50 @@ describe("neuron-utils", () => {
});
});

describe("neuronAvailableMaturity", () => {
it("should calculate available maturity", () => {
const maturity = 100234n;
const neuron = {
...mockNeuron,
fullNeuron: {
...mockFullNeuron,
maturityE8sEquivalent: maturity,
},
};
expect(neuronAvailableMaturity(neuron)).toBe(maturity);
});

it("should return 0n when maturity is not available", () => {
const neuron = {
...mockNeuron,
fullNeuron: undefined,
};
expect(neuronAvailableMaturity(neuron)).toBe(0n);
});
});

describe("neuronStakedMaturity", () => {
it("should calculate staked maturity", () => {
const maturity = 100235n;
const neuron = {
...mockNeuron,
fullNeuron: {
...mockFullNeuron,
stakedMaturityE8sEquivalent: maturity,
},
};
expect(neuronStakedMaturity(neuron)).toBe(maturity);
});

it("should return 0n when maturity is not available", () => {
const neuron = {
...mockNeuron,
fullNeuron: undefined,
};
expect(neuronStakedMaturity(neuron)).toBe(0n);
});
});

describe("ballotsWithDefinedProposal", () => {
const ballot: BallotInfo = {
vote: Vote.Yes,
Expand Down
32 changes: 32 additions & 0 deletions frontend/src/tests/lib/utils/sns-neuron.utils.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@ import {
getSnsDissolvingTimeInSeconds,
getSnsDissolvingTimestampSeconds,
getSnsLockedTimeInSeconds,
getSnsNeuronAvailableMaturity,
getSnsNeuronByHexId,
getSnsNeuronHotkeys,
getSnsNeuronIdAsHexString,
getSnsNeuronStake,
getSnsNeuronStakedMaturity,
getSnsNeuronState,
getSnsNeuronTags,
getSnsNeuronVote,
Expand Down Expand Up @@ -352,6 +354,36 @@ describe("sns-neuron utils", () => {
});
});

describe("getSnsNeuronAvailableMaturity", () => {
it("returns available maturity", () => {
const maturity = 1234n;
const neuron: SnsNeuron = {
...mockSnsNeuron,
maturity_e8s_equivalent: maturity,
};
expect(getSnsNeuronAvailableMaturity(neuron)).toBe(maturity);
});
});

describe("getSnsNeuronStakedMaturity", () => {
it("returns staked maturity", () => {
const maturity = 5432n;
const neuron: SnsNeuron = {
...mockSnsNeuron,
staked_maturity_e8s_equivalent: [maturity],
};
expect(getSnsNeuronStakedMaturity(neuron)).toBe(maturity);
});

it("returns 0 when absent", () => {
const neuron: SnsNeuron = {
...mockSnsNeuron,
staked_maturity_e8s_equivalent: [],
};
expect(getSnsNeuronStakedMaturity(neuron)).toBe(0n);
});
});

describe("getSnsNeuronIdAsHexString", () => {
it("returns id numbers concatenated", () => {
const id = [
Expand Down

0 comments on commit 69a2148

Please sign in to comment.