import { mockNearContext } from "../../test/context"; import type { Balance, StakingTransactionIntent, TransactionIntent, } from "@ledgerhq/coin-module-framework/api/index"; import BigNumber from "bignumber.js"; import { fetchAccountDetails, getActionCosts, getStakingPositions } from "../../network"; import { getYoctoThreshold } from "../../logic"; import { validateIntent } from "../validateIntent"; jest.mock("../../network", () => ({ fetchAccountDetails: jest.fn(), getActionCosts: jest.fn(), getStakingPositions: jest.fn(), })); const SENDER = "sender.near"; const NAMED_RECIPIENT = "recipient.near"; const IMPLICIT_RECIPIENT = "4e7de0a21d8a20f970c86b6edf407906d7ba9e205979c3268270eef80a286e2d"; const VALIDATOR = "astro-stakers.poolv1.near"; const ONE_NEAR = 1_000_000_000_000_000_000_000_000n; const FEES = { value: 100_000_000_000_000_000_000n }; const ABOVE_THRESHOLD = BigInt(getYoctoThreshold().multipliedBy(3).toFixed(0)); const nativeBalance = (value: bigint, locked = 0n): Balance => ({ value, locked, asset: { type: "native" }, }); const stakeBalance = (amount: bigint, state: string): Balance => ({ value: amount, asset: { type: "native" }, stake: { uid: `${SENDER}:${VALIDATOR}:${state}`, address: SENDER, delegate: VALIDATOR, state, actions: [], asset: { type: "native" }, amount, }, }) as Balance; /** exactOptionalPropertyTypes is on, so an explicit `undefined` override needs the union. */ type Overrides = { [K in keyof T]?: T[K] | undefined }; const sendIntent = (overrides: Overrides = {}): TransactionIntent => ({ intentType: "transaction", type: "send", sender: SENDER, recipient: NAMED_RECIPIENT, amount: ONE_NEAR, asset: { type: "native" }, ...overrides, }) as TransactionIntent; const stakingIntent = ( mode: StakingTransactionIntent["mode"], overrides: Overrides = {}, ): StakingTransactionIntent => ({ ...sendIntent(), intentType: "staking", type: mode, mode, valAddress: VALIDATOR, amount: ABOVE_THRESHOLD, ...overrides, }) as StakingTransactionIntent; describe("validateIntent", () => { beforeEach(() => { jest.clearAllMocks(); (getActionCosts as unknown as jest.Mock).mockResolvedValue({ storageCost: new BigNumber("10000000000000000000"), accountCreationCharge: new BigNumber(0), }); // Sender and recipient alike: a funded account holding 2 NEAR, storage deposit included. (fetchAccountDetails as jest.Mock).mockResolvedValue({ amount: (ONE_NEAR * 2n).toString(), storage_usage: 182, }); (getStakingPositions as jest.Mock).mockResolvedValue({ stakingPositions: [] }); }); describe("transfers", () => { it("accepts a funded transfer to an existing account", async () => { const result = await validateIntent( mockNearContext, sendIntent(), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors).toEqual({}); expect(result.amount).toBe(ONE_NEAR); expect(result.totalSpent).toBe(ONE_NEAR + FEES.value); }); it("subtracts the locked storage deposit from what can be spent", async () => { const result = await validateIntent( mockNearContext, sendIntent({ amount: ONE_NEAR * 2n }), [nativeBalance(ONE_NEAR * 3n, ONE_NEAR * 2n)], FEES, ); expect(result.errors.amount?.name).toBe("NotEnoughBalance"); }); it("requires a recipient", async () => { const result = await validateIntent( mockNearContext, sendIntent({ recipient: "" }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors.recipient?.name).toBe("RecipientRequired"); }); it("rejects a malformed recipient", async () => { const result = await validateIntent( mockNearContext, sendIntent({ recipient: "NOT VALID" }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors.recipient?.name).toBe("InvalidAddress"); }); it("requires a positive amount", async () => { const result = await validateIntent( mockNearContext, sendIntent({ amount: 0n }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors.amount?.name).toBe("AmountRequired"); }); it("warns when sending to yourself", async () => { const result = await validateIntent( mockNearContext, sendIntent({ recipient: SENDER }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.warnings.recipient?.name).toBe("InvalidAddressBecauseDestinationIsAlsoSource"); }); it("warns that an implicit recipient will be created by the transfer", async () => { (fetchAccountDetails as jest.Mock).mockResolvedValue(undefined); const result = await validateIntent( mockNearContext, sendIntent({ recipient: IMPLICIT_RECIPIENT }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.warnings.recipient?.name).toBe("NearNewAccountWarning"); expect(result.errors).toEqual({}); expect( (result.warnings.recipient as unknown as { formattedNewAccountStorageCost: string }) .formattedNewAccountStorageCost, ).toContain("0.00182"); }); it("rejects an amount too small to cover a new account's storage", async () => { (fetchAccountDetails as jest.Mock).mockResolvedValue(undefined); const result = await validateIntent( mockNearContext, sendIntent({ recipient: IMPLICIT_RECIPIENT, amount: 1n }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors.amount?.name).toBe("NearActivationFeeNotCovered"); expect( (result.errors.amount as unknown as { formattedNewAccountStorageCost: string }) .formattedNewAccountStorageCost, ).toContain("0.00182"); }); it("rejects a named recipient that does not exist yet", async () => { (fetchAccountDetails as jest.Mock).mockResolvedValue(undefined); const result = await validateIntent( mockNearContext, sendIntent(), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors.recipient?.name).toBe("NearNewNamedAccountError"); }); it("spends the balance minus fees when sending everything", async () => { const result = await validateIntent( mockNearContext, sendIntent({ useAllAmount: true }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.amount).toBe(ONE_NEAR * 10n - FEES.value); expect(result.errors).toEqual({}); }); it("recommends unstaking first when sending everything with an open position", async () => { const result = await validateIntent( mockNearContext, sendIntent({ useAllAmount: true }), [nativeBalance(ONE_NEAR * 10n), stakeBalance(ABOVE_THRESHOLD, "active")], FEES, ); expect(result.warnings.amount?.name).toBe("NearRecommendUnstake"); }); it("recommends unstaking first even when the caller's balances carry no staking entries", async () => { (getStakingPositions as jest.Mock).mockResolvedValue({ stakingPositions: [ { validatorId: VALIDATOR, staked: new BigNumber(ABOVE_THRESHOLD.toString()) }, ], }); const result = await validateIntent( mockNearContext, sendIntent({ useAllAmount: true }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(getStakingPositions).toHaveBeenCalledWith(expect.anything(), SENDER); expect(result.warnings.amount?.name).toBe("NearRecommendUnstake"); }); it("does not warn to unstake when the sender has no delegation at all", async () => { (getStakingPositions as jest.Mock).mockResolvedValue({ stakingPositions: [] }); const result = await validateIntent( mockNearContext, sendIntent({ useAllAmount: true }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.warnings.amount).toBeUndefined(); }); }); describe("staking", () => { it("accepts a funded delegation", async () => { const result = await validateIntent( mockNearContext, stakingIntent("delegate"), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors).toEqual({}); expect(result.totalSpent).toBe(ABOVE_THRESHOLD + FEES.value); }); it("reads the pool when the caller's balances carry no staking entries", async () => { (getStakingPositions as jest.Mock).mockResolvedValue({ stakingPositions: [ { validatorId: VALIDATOR, staked: new BigNumber(0), available: new BigNumber(ONE_NEAR.toString()), pending: new BigNumber(0), }, ], }); const result = await validateIntent( mockNearContext, stakingIntent("withdraw", { amount: ONE_NEAR / 2n }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(getStakingPositions).toHaveBeenCalledWith(expect.anything(), SENDER); expect(result.errors).toEqual({}); expect(result.amount).toBe(ONE_NEAR / 2n); }); it("rejects a withdrawal the pool cannot cover", async () => { (getStakingPositions as jest.Mock).mockResolvedValue({ stakingPositions: [ { validatorId: VALIDATOR, staked: new BigNumber(0), available: new BigNumber(ABOVE_THRESHOLD.toString()), pending: new BigNumber(0), }, ], }); const result = await validateIntent( mockNearContext, stakingIntent("withdraw", { amount: ONE_NEAR }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors.amount?.name).toBe("NearNotEnoughAvailable"); }); it("rejects an amount below the staking threshold", async () => { const result = await validateIntent( mockNearContext, stakingIntent("delegate", { amount: 1n }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.errors.amount?.name).toBe("NearStakingThresholdNotMet"); expect((result.errors.amount as unknown as { threshold: string }).threshold).toMatch(/NEAR$/); }); it("rejects a delegation the liquid balance cannot cover", async () => { const result = await validateIntent( mockNearContext, stakingIntent("delegate", { amount: ONE_NEAR * 100n }), [nativeBalance(ONE_NEAR)], FEES, ); expect(result.errors.amount?.name).toBe("NotEnoughBalance"); }); it("only spends the fee when unstaking, since the funds are already delegated", async () => { const result = await validateIntent( mockNearContext, stakingIntent("undelegate"), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD * 2n, "active")], FEES, ); expect(result.errors).toEqual({}); expect(result.totalSpent).toBe(FEES.value); }); it("rejects unstaking more than is delegated to that pool", async () => { const result = await validateIntent( mockNearContext, stakingIntent("undelegate", { amount: ABOVE_THRESHOLD * 5n }), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "active")], FEES, ); expect(result.errors.amount?.name).toBe("NearNotEnoughStaked"); }); it("rejects withdrawing more than has been released", async () => { const result = await validateIntent( mockNearContext, stakingIntent("withdraw", { amount: ABOVE_THRESHOLD * 5n }), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "withdrawable")], FEES, ); expect(result.errors.amount?.name).toBe("NearNotEnoughAvailable"); }); it("does not count an unbonding position as withdrawable", async () => { const result = await validateIntent( mockNearContext, stakingIntent("withdraw"), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "deactivating")], FEES, ); expect(result.errors.amount?.name).toBe("NearNotEnoughAvailable"); }); it("rejects staking when the liquid balance cannot even cover the fee", async () => { const result = await validateIntent( mockNearContext, stakingIntent("delegate"), [nativeBalance(1n)], FEES, ); expect(result.errors.amount?.name).toBe("NotEnoughBalance"); }); it("prices the withdraw fee against the storage floor, not our own reserve", async () => { // 0.055 NEAR on chain: under the fee once MIN_ACCOUNT_BALANCE_BUFFER is taken out too, // over it once only the storage deposit is. (fetchAccountDetails as jest.Mock).mockResolvedValue({ amount: "55000000000000000000000", storage_usage: 182, }); const result = await validateIntent( mockNearContext, stakingIntent("withdraw"), [nativeBalance(ONE_NEAR, ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "withdrawable")], { value: 20_000_000_000_000_000_000_000n }, ); expect(result.errors).toEqual({}); }); it("rejects a withdraw the liquid balance cannot pay the fee for", async () => { (fetchAccountDetails as jest.Mock).mockResolvedValue({ amount: "10000000000000000000000", // 0.01 NEAR storage_usage: 182, }); const result = await validateIntent( mockNearContext, stakingIntent("withdraw"), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "withdrawable")], { value: 20_000_000_000_000_000_000_000n }, ); expect(result.errors.amount?.name).toBe("NotEnoughBalance"); }); it("treats a sender the node does not know as unable to pay the fee", async () => { (fetchAccountDetails as jest.Mock).mockResolvedValue(null); const result = await validateIntent( mockNearContext, stakingIntent("withdraw"), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "withdrawable")], FEES, ); expect(result.errors.amount?.name).toBe("NotEnoughBalance"); }); it("rejects an unstake the liquid balance cannot pay the fee for", async () => { (fetchAccountDetails as jest.Mock).mockResolvedValue({ amount: "1", storage_usage: 182 }); const result = await validateIntent( mockNearContext, stakingIntent("undelegate"), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD * 2n, "active")], FEES, ); expect(result.errors.amount?.name).toBe("NotEnoughBalance"); }); it("warns when delegating the entire spendable balance", async () => { const result = await validateIntent( mockNearContext, stakingIntent("delegate", { useAllAmount: true }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.warnings.amount?.name).toBe("NearUseAllAmountStakeWarning"); }); it("rejects delegating the entire spendable balance when it cannot cover the fee", async () => { const result = await validateIntent( mockNearContext, stakingIntent("delegate", { useAllAmount: true }), [nativeBalance(15_332_000_000_000_000_000_000n)], { value: 55_000_000_000_000_000_000_000n }, ); expect(result.errors.amount?.name).toBe("NotEnoughBalance"); expect(result.warnings).toEqual({}); }); it("rejects unstaking below the staking threshold", async () => { const result = await validateIntent( mockNearContext, stakingIntent("undelegate", { amount: 1n }), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "active")], FEES, ); expect(result.errors.amount?.name).toBe("NearStakingThresholdNotMet"); expect((result.errors.amount as unknown as { threshold: string }).threshold).toMatch(/NEAR$/); }); describe("when the intent only carries the framework type", () => { it("rejects withdrawing more than has been released for type finalize_unstake", async () => { const result = await validateIntent( mockNearContext, stakingIntent("withdraw", { mode: undefined, valAddress: undefined, type: "finalize_unstake", recipient: VALIDATOR, amount: ABOVE_THRESHOLD * 5n, }), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "withdrawable")], FEES, ); expect(result.errors.amount?.name).toBe("NearNotEnoughAvailable"); }); it("rejects unstaking more than is delegated for type unstake", async () => { const result = await validateIntent( mockNearContext, stakingIntent("undelegate", { mode: undefined, valAddress: undefined, type: "unstake", recipient: VALIDATOR, amount: ABOVE_THRESHOLD * 5n, }), [nativeBalance(ONE_NEAR), stakeBalance(ABOVE_THRESHOLD, "active")], FEES, ); expect(result.errors.amount?.name).toBe("NearNotEnoughStaked"); }); it("warns when staking the entire spendable balance for type stake", async () => { const result = await validateIntent( mockNearContext, stakingIntent("delegate", { mode: undefined, valAddress: undefined, type: "stake", recipient: VALIDATOR, useAllAmount: true, }), [nativeBalance(ONE_NEAR * 10n)], FEES, ); expect(result.warnings.amount?.name).toBe("NearUseAllAmountStakeWarning"); }); }); }); });