Skip to content

Commit

Permalink
Add toRefreshVotingPowerRequest API (#787)
Browse files Browse the repository at this point in the history
# Motivation

There is a new API available to reset the following. To make it
accessible for the nns-dapp, here we’ve created a function to call it.

# Changes

- Added `toRefreshVotingPowerRequest`.

# Tests

- Updated.
- Tested locally by calling it from the nns-dapp and verifying that the
neurons’ `votingPowerRefreshedTimestampSeconds` field was reset to the
current time.

# Todos

- [ ] Add entry to changelog (if necessary).
Not necessary.

---------

Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
mstrasinskis and github-actions[bot] authored Dec 10, 2024
1 parent 652a5d1 commit 9f6bf8c
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 32 deletions.
77 changes: 45 additions & 32 deletions packages/nns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ Parameters:

### :factory: GovernanceCanister

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L88)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L89)

#### Methods

Expand All @@ -200,6 +200,7 @@ Parameters:
- [registerVote](#gear-registervote)
- [setFollowees](#gear-setfollowees)
- [disburse](#gear-disburse)
- [refreshVotingPower](#gear-refreshvotingpower)
- [mergeMaturity](#gear-mergematurity)
- [stakeMaturity](#gear-stakematurity)
- [spawnNeuron](#gear-spawnneuron)
Expand All @@ -215,7 +216,7 @@ Parameters:
| -------- | ------------------------------------------------------------- |
| `create` | `(options?: GovernanceCanisterOptions) => GovernanceCanister` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L103)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L104)

##### :gear: listNeurons

Expand All @@ -232,7 +233,7 @@ The backend treats `includeEmptyNeurons` as true if absent.
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `listNeurons` | `({ certified, neuronIds, includeEmptyNeurons, includePublicNeurons, }: { certified: boolean; neuronIds?: bigint[] or undefined; includeEmptyNeurons?: boolean or undefined; includePublicNeurons?: boolean or undefined; }) => Promise<NeuronInfo[]>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L143)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L144)

##### :gear: listKnownNeurons

Expand All @@ -246,7 +247,7 @@ it is fetched using a query call.
| ------------------ | ------------------------------------------------- |
| `listKnownNeurons` | `(certified?: boolean) => Promise<KnownNeuron[]>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L186)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L187)

##### :gear: getLastestRewardEvent

Expand All @@ -259,7 +260,7 @@ it's fetched using a query call.
| ----------------------- | ----------------------------------------------- |
| `getLastestRewardEvent` | `(certified?: boolean) => Promise<RewardEvent>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L208)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L209)

##### :gear: listProposals

Expand All @@ -278,15 +279,15 @@ Parameters:
- `request`: the options to list the proposals (limit number of results, topics to search for, etc.)
- `certified`: query or update calls

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L224)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L225)

##### :gear: stakeNeuron

| Method | Type |
| ------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `stakeNeuron` | `({ stake, principal, fromSubAccount, ledgerCanister, createdAt, fee, }: { stake: bigint; principal: Principal; fromSubAccount?: number[] or undefined; ledgerCanister: LedgerCanister; createdAt?: bigint or undefined; fee?: bigint or undefined; }) => Promise<...>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L243)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L244)

##### :gear: increaseDissolveDelay

Expand All @@ -296,7 +297,7 @@ Increases dissolve delay of a neuron
| ----------------------- | --------------------------------------------------------------------------------------------------------------------------------- |
| `increaseDissolveDelay` | `({ neuronId, additionalDissolveDelaySeconds, }: { neuronId: bigint; additionalDissolveDelaySeconds: number; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L304)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L305)

##### :gear: setDissolveDelay

Expand All @@ -307,7 +308,7 @@ The new date is now + dissolveDelaySeconds.
| ------------------ | ------------------------------------------------------------------------------------------------------------- |
| `setDissolveDelay` | `({ neuronId, dissolveDelaySeconds, }: { neuronId: bigint; dissolveDelaySeconds: number; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L330)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L331)

##### :gear: startDissolving

Expand All @@ -317,7 +318,7 @@ Start dissolving process of a neuron
| ----------------- | ------------------------------------- |
| `startDissolving` | `(neuronId: bigint) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L353)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L354)

##### :gear: stopDissolving

Expand All @@ -327,7 +328,7 @@ Stop dissolving process of a neuron
| ---------------- | ------------------------------------- |
| `stopDissolving` | `(neuronId: bigint) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L367)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L368)

##### :gear: joinCommunityFund

Expand All @@ -337,7 +338,7 @@ Neuron joins the community fund
| ------------------- | ------------------------------------- |
| `joinCommunityFund` | `(neuronId: bigint) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L381)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L382)

##### :gear: autoStakeMaturity

Expand All @@ -352,7 +353,7 @@ Parameters:
- `neuronId`: The id of the neuron for which to request a change of the auto stake feature
- `autoStake`: `true` to enable the auto-stake maturity for this neuron, `false` to turn it off

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L399)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L400)

##### :gear: leaveCommunityFund

Expand All @@ -362,7 +363,7 @@ Neuron leaves the community fund
| -------------------- | ------------------------------------- |
| `leaveCommunityFund` | `(neuronId: bigint) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L414)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L415)

##### :gear: setVisibility

Expand All @@ -372,7 +373,7 @@ Set visibility of a neuron
| --------------- | ------------------------------------------------------------------- |
| `setVisibility` | `(neuronId: bigint, visibility: NeuronVisibility) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L428)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L429)

##### :gear: setNodeProviderAccount

Expand All @@ -383,7 +384,7 @@ Where the reward is paid to.
| ------------------------ | ---------------------------------------------- |
| `setNodeProviderAccount` | `(accountIdentifier: string) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L448)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L449)

##### :gear: mergeNeurons

Expand All @@ -393,7 +394,7 @@ Merge two neurons
| -------------- | --------------------------------------------------------------------------------- |
| `mergeNeurons` | `(request: { sourceNeuronId: bigint; targetNeuronId: bigint; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L468)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L469)

##### :gear: simulateMergeNeurons

Expand All @@ -403,7 +404,7 @@ Simulate merging two neurons
| ---------------------- | --------------------------------------------------------------------------------------- |
| `simulateMergeNeurons` | `(request: { sourceNeuronId: bigint; targetNeuronId: bigint; }) => Promise<NeuronInfo>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L485)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L486)

##### :gear: splitNeuron

Expand All @@ -413,7 +414,7 @@ Splits a neuron creating a new one
| ------------- | ----------------------------------------------------------------------------------- |
| `splitNeuron` | `({ neuronId, amount, }: { neuronId: bigint; amount: bigint; }) => Promise<bigint>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L530)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L531)

##### :gear: getProposal

Expand All @@ -426,7 +427,7 @@ it is fetched using a query call.
| ------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| `getProposal` | `({ proposalId, certified, }: { proposalId: bigint; certified?: boolean or undefined; }) => Promise<ProposalInfo or undefined>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L570)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L571)

##### :gear: makeProposal

Expand All @@ -436,7 +437,7 @@ Create new proposal
| -------------- | ---------------------------------------------------------------- |
| `makeProposal` | `(request: MakeProposalRequest) => Promise<bigint or undefined>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L588)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L589)

##### :gear: registerVote

Expand All @@ -446,7 +447,7 @@ Registers vote for a proposal from the neuron passed.
| -------------- | ----------------------------------------------------------------------------------------------------------- |
| `registerVote` | `({ neuronId, vote, proposalId, }: { neuronId: bigint; vote: Vote; proposalId: bigint; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L609)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L610)

##### :gear: setFollowees

Expand All @@ -456,7 +457,7 @@ Edit neuron followees per topic
| -------------- | ------------------------------------------------- |
| `setFollowees` | `(followRequest: FollowRequest) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L631)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L632)

##### :gear: disburse

Expand All @@ -466,7 +467,19 @@ Disburse neuron on Account
| ---------- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
| `disburse` | `({ neuronId, toAccountId, amount, }: { neuronId: bigint; toAccountId?: string or undefined; amount?: bigint or undefined; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L646)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L647)

##### :gear: refreshVotingPower

Refreshes voting power of a neuron
(Resets the `votingPowerRefreshedTimestampSeconds`
parameter of the neuron to the current time).

| Method | Type |
| -------------------- | --------------------------------------------------------- |
| `refreshVotingPower` | `({ neuronId, }: { neuronId: bigint; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L683)

##### :gear: mergeMaturity

Expand All @@ -476,7 +489,7 @@ Merge Maturity of a neuron
| --------------- | ------------------------------------------------------------------------------------------------------- |
| `mergeMaturity` | `({ neuronId, percentageToMerge, }: { neuronId: bigint; percentageToMerge: number; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L682)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L705)

##### :gear: stakeMaturity

Expand All @@ -491,7 +504,7 @@ Parameters:
- `neuronId`: The id of the neuron for which to stake the maturity
- `percentageToStake`: Optional. Percentage of the current maturity to stake. If not provided, all of the neuron's current maturity will be staked.

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L711)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L734)

##### :gear: spawnNeuron

Expand All @@ -501,7 +514,7 @@ Merge Maturity of a neuron
| ------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `spawnNeuron` | `({ neuronId, percentageToSpawn, newController, nonce, }: { neuronId: bigint; percentageToSpawn?: number or undefined; newController?: Principal or undefined; nonce?: bigint or undefined; }) => Promise<bigint>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L733)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L756)

##### :gear: addHotkey

Expand All @@ -511,7 +524,7 @@ Add hotkey to neuron
| ----------- | ------------------------------------------------------------------------------------------ |
| `addHotkey` | `({ neuronId, principal, }: { neuronId: bigint; principal: Principal; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L780)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L803)

##### :gear: removeHotkey

Expand All @@ -521,7 +534,7 @@ Remove hotkey to neuron
| -------------- | ------------------------------------------------------------------------------------------ |
| `removeHotkey` | `({ neuronId, principal, }: { neuronId: bigint; principal: Principal; }) => Promise<void>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L800)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L823)

##### :gear: claimOrRefreshNeuronFromAccount

Expand All @@ -531,7 +544,7 @@ Gets the NeuronID of a newly created neuron.
| --------------------------------- | ----------------------------------------------------------------------------------------------------------------- |
| `claimOrRefreshNeuronFromAccount` | `({ memo, controller, }: { memo: bigint; controller?: Principal or undefined; }) => Promise<bigint or undefined>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L818)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L841)

##### :gear: claimOrRefreshNeuron

Expand All @@ -542,7 +555,7 @@ Uses query call only.
| ---------------------- | ------------------------------------------------------------------------ |
| `claimOrRefreshNeuron` | `(request: ClaimOrRefreshNeuronRequest) => Promise<bigint or undefined>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L849)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L872)

##### :gear: getNeuron

Expand All @@ -552,7 +565,7 @@ Return the data of the neuron provided as id.
| ----------- | ----------------------------------------------------------------------------------------------------------- |
| `getNeuron` | `({ certified, neuronId, }: { certified: boolean; neuronId: bigint; }) => Promise<NeuronInfo or undefined>` |

[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L874)
[:link: Source](https://github.com/dfinity/ic-js/tree/main/packages/nns/src/governance.canister.ts#L897)

### :factory: SnsWasmCanister

Expand Down
14 changes: 14 additions & 0 deletions packages/nns/src/canisters/governance/request.converters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1436,6 +1436,20 @@ export const toDisburseNeuronRequest = ({
},
});

export const toRefreshVotingPowerRequest = ({
neuronId,
}: {
neuronId: NeuronId;
}): RawManageNeuron =>
toCommand({
neuronId,
command: {
RefreshVotingPower: {
// Intentionally left blank.
},
},
});

export const toMergeMaturityRequest = ({
neuronId,
percentageToMerge,
Expand Down
37 changes: 37 additions & 0 deletions packages/nns/src/governance.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1924,6 +1924,43 @@ describe("GovernanceCanister", () => {
});
});

describe("GovernanceCanister.refreshVotingPower", () => {
it("successfully refreshes voting power of a neuron", async () => {
const neuronId = BigInt(10);
const serviceResponse: ManageNeuronResponse = {
command: [{ RefreshVotingPower: {} }],
};
const service = mock<ActorSubclass<GovernanceService>>();
service.manage_neuron.mockResolvedValue(serviceResponse);

const governance = GovernanceCanister.create({
certifiedServiceOverride: service,
});
await governance.refreshVotingPower({
neuronId,
});
expect(service.manage_neuron).toBeCalledTimes(1);
});

it("throws error if response is error", async () => {
const neuronId = BigInt(10);
const serviceResponse: ManageNeuronResponse = {
command: [{ Error: error }],
};
const service = mock<ActorSubclass<GovernanceService>>();
service.manage_neuron.mockResolvedValue(serviceResponse);

const governance = GovernanceCanister.create({
certifiedServiceOverride: service,
});
const call = () =>
governance.refreshVotingPower({
neuronId,
});
expect(call).rejects.toThrow(new GovernanceError(error));
});
});

describe("GovernanceCanister.splitNeuron", () => {
it("successfully splits neuron", async () => {
const neuronId = BigInt(10);
Expand Down
Loading

0 comments on commit 9f6bf8c

Please sign in to comment.