Skip to content

Commit

Permalink
Merge branch 'main' into kloet/rm-icrc1-stake
Browse files Browse the repository at this point in the history
  • Loading branch information
dskloetd authored Oct 30, 2024
2 parents cb74462 + ed81d22 commit 83984cb
Showing 1 changed file with 62 additions and 5 deletions.
67 changes: 62 additions & 5 deletions packages/nns/src/governance.canister.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
import { ActorSubclass, AnonymousIdentity } from "@dfinity/agent";
import { InvalidAccountIDError, LedgerCanister } from "@dfinity/ledger-icp";
import {
AccountIdentifier,
InvalidAccountIDError,
LedgerCanister,
} from "@dfinity/ledger-icp";
import { Principal } from "@dfinity/principal";
import { InvalidPercentageError } from "@dfinity/utils";
import { mock } from "jest-mock-extended";
Expand Down Expand Up @@ -46,6 +50,24 @@ import {
UpdateCanisterSettings,
} from "./types/governance_converters";

const nextRandomBytes: number[] = [];

jest.mock("randombytes", () => ({
__esModule: true,
default: function (n: number) {
const nums: number[] = [];
for (let i = 0; i < n; i++) {
const nextByte = nextRandomBytes.shift();
if (nextByte !== undefined) {
nums.push(nextByte);
} else {
nums.push(Math.floor(Math.random() * 0x100));
}
}
return new Uint8Array(nums);
},
}));

const unexpectedGovernanceError: GovernanceErrorDetail = {
error_message: "Error updating neuron",
error_type: 0,
Expand Down Expand Up @@ -197,17 +219,52 @@ describe("GovernanceCanister", () => {
jest.fn().mockResolvedValue(BigInt(1)),
);

const stake = 100_000_000n;
const controller = Principal.fromText("2amoo-gaqe4-3ekjz-wiu");

nextRandomBytes.push(123, 4, 56, 7, 8, 90, 1, 234, 5, 67);
const expectedMemo = 8864271569428021738n;
const expectedIcpAccount = AccountIdentifier.fromHex(
"911c1b8826981b7126dc62cf176f30550eefee5d31e2071143ca17e727b251e5",
);

expect(mockLedger.transfer).toBeCalledTimes(0);
expect(service.manage_neuron).toBeCalledTimes(0);

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

expect(mockLedger.transfer).toBeCalled();
expect(service.manage_neuron).toBeCalled();
expect(mockLedger.transfer).toBeCalledTimes(1);
expect(mockLedger.transfer).toBeCalledWith({
amount: stake,
memo: expectedMemo,
to: expectedIcpAccount,
});
expect(service.manage_neuron).toBeCalledTimes(1);
expect(service.manage_neuron).toBeCalledWith({
command: [
{
ClaimOrRefresh: {
by: [
{
MemoAndController: {
controller: [controller],
memo: expectedMemo,
},
},
],
},
},
],
id: [],
neuron_id_or_subaccount: [],
});
expect(response).toEqual(neuronId);
});

Expand Down

0 comments on commit 83984cb

Please sign in to comment.