Skip to content

Commit

Permalink
Chore: Add fee to nns stake neuron method (#349)
Browse files Browse the repository at this point in the history
# Motivation

Prepare for migration of HW to Candid.

If the fee is not passed, the transfer fetches it. Yet, the Ledger App
doesn't support fetching the transfer fee.

One solution is to pass a fee when staking a neuron, which is then
passed when making the transfer.

Another solution (not here) is to use an anonymous identity when
fetching the fee. This is not straightforward because we use classes.
The identity is in the agent, which is passed in the initialization of
the class.

# Changes

* Add a `fee` parameter when staking an NNS neuron.

# Tests

* Add a test to check that the `fee` parameter is passed then to the
`transfer` method.
  • Loading branch information
lmuntaner authored Jun 15, 2023
1 parent 065c58d commit 5433952
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions packages/nns/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -452,9 +452,9 @@ Parameters:

##### :gear: stakeNeuron

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

##### :gear: increaseDissolveDelay

Expand Down
31 changes: 31 additions & 0 deletions packages/nns/src/governance.canister.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,37 @@ describe("GovernanceCanister", () => {
expect(response).toEqual(neuronId);
});

it("stakeNeuron passes fee to the ledger transfer", async () => {
const neuronId = BigInt(10);
const serviceResponse: ManageNeuronResponse = {
command: [
{ ClaimOrRefresh: { refreshed_neuron_id: [{ id: neuronId }] } },
],
};
const service = mock<ActorSubclass<GovernanceService>>();
service.manage_neuron.mockResolvedValue(serviceResponse);

const mockLedger = mock<LedgerCanister>();
mockLedger.transfer.mockImplementation(
jest.fn().mockResolvedValue(BigInt(1))
);
const fee = BigInt(10_000);

const governance = GovernanceCanister.create({
certifiedServiceOverride: service,
});
const response = await governance.stakeNeuron({
stake: BigInt(100_000_000),
principal: new AnonymousIdentity().getPrincipal(),
ledgerCanister: mockLedger,
fee,
});

expect(mockLedger.transfer).toBeCalledWith(
expect.objectContaining({ fee })
);
});

it("creates new neuron from subaccount successfully", async () => {
const neuronId = BigInt(10);
const serviceResponse: ManageNeuronResponse = {
Expand Down
3 changes: 3 additions & 0 deletions packages/nns/src/governance.canister.ts
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ export class GovernanceCanister {
fromSubAccount,
ledgerCanister,
createdAt,
fee,
}: {
stake: bigint;
principal: Principal;
Expand All @@ -255,6 +256,7 @@ export class GovernanceCanister {
// Used for the TransferRequest parameters.
// Check the TransferRequest type for more information.
createdAt?: bigint;
fee?: E8s;
}): Promise<NeuronId> => {
if (stake < E8S_PER_TOKEN) {
throw new InsufficientAmountError(stake);
Expand All @@ -275,6 +277,7 @@ export class GovernanceCanister {
fromSubAccount,
to: accountIdentifier,
createdAt,
fee,
});

// Notify the governance of the transaction so that the neuron is created.
Expand Down

0 comments on commit 5433952

Please sign in to comment.