Skip to content

Commit

Permalink
Merge pull request #6206 from StricaHQ/bugfix/cardano-stake-btn
Browse files Browse the repository at this point in the history
fix canStake method
  • Loading branch information
hedi-edelbloute authored Feb 23, 2024
2 parents 2ac0ae9 + ccd8e2f commit 826a498
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 37 deletions.
6 changes: 6 additions & 0 deletions .changeset/tough-hairs-retire.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"ledger-live-desktop": patch
"@ledgerhq/live-common": patch
---

Cardano stake button fix
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { getMainAccount } from "@ledgerhq/live-common/account/index";
import { CardanoAccount } from "@ledgerhq/live-common/families/cardano/types";
import { canStake } from "@ledgerhq/live-common/families/cardano/logic";
import { canStake, isAlreadyStaking } from "@ledgerhq/live-common/families/cardano/logic";
import { Account, AccountLike } from "@ledgerhq/types-live";
import invariant from "invariant";
import { useCallback } from "react";
Expand All @@ -21,7 +21,8 @@ const AccountHeaderActions = ({ account, parentAccount }: Props) => {
const { cardanoResources } = mainAccount as CardanoAccount;
invariant(cardanoResources, "cardano account expected");

const disableStakeButton = !canStake(account as CardanoAccount);
const disableStakeButton =
!canStake(account as CardanoAccount) || isAlreadyStaking(account as CardanoAccount);

const disabledLabel =
cardanoResources.delegation && cardanoResources.delegation.poolId
Expand Down
10 changes: 9 additions & 1 deletion libs/ledger-live-common/src/families/cardano/logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,15 @@ export function getBipPathFromString(path: string): BipPath {
* @returns true if the account can stake, false otherwise
*/
export function canStake(account: CardanoAccount): boolean {
return !!account?.cardanoResources?.delegation?.poolId && !account.balance.isZero();
return !!account.balance?.gt(0);
}

/**
*
* @returns true if account is staked, false otherwise
*/
export function isAlreadyStaking(account: CardanoAccount): boolean {
return !!account?.cardanoResources?.delegation?.poolId;
}

/**
Expand Down
61 changes: 27 additions & 34 deletions libs/ledger-live-common/src/families/cardano/logic.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,51 +1,44 @@
import BigNumber from "bignumber.js";
import { canStake } from "./logic";
import { canStake, isAlreadyStaking } from "./logic";
import { CardanoAccount } from "./types";

describe("canStake", () => {
it("should return false when acc delegation data is incomplete", () => {
it("should return false when acc not present", () => {
const noResourcesAcc = {} as CardanoAccount;
expect(canStake(noResourcesAcc)).toEqual(false);
const noDelegationAcc = {
cardanoResources: {},
balance: new BigNumber(1),
} as CardanoAccount;
expect(canStake(noDelegationAcc)).toEqual(false);
const noPoolIdAcc = {
balance: new BigNumber(1),
cardanoResources: {
delegation: {},
},
} as CardanoAccount;
expect(canStake(noPoolIdAcc)).toEqual(false);
});

it("should return false when acc already has a delegation but no funds", () => {
const poolIdNoFundsAcc = {
it("should return false when acc has no funds", () => {
const accWithNoFunds = {
balance: new BigNumber(0),
cardanoResources: {
delegation: {
poolId: "helloiamapoolid",
},
},
} as CardanoAccount;
expect(canStake(poolIdNoFundsAcc)).toEqual(false);
expect(canStake(accWithNoFunds)).toEqual(false);
});

it("should return false when acc has no funds", () => {
const noResourcesAcc = { balance: new BigNumber(0) } as CardanoAccount;
expect(canStake(noResourcesAcc)).toEqual(false);
it("should return true when acc has funds", () => {
const accWithFunds = {
balance: new BigNumber(1),
} as CardanoAccount;
expect(canStake(accWithFunds)).toEqual(true);
});
});

it("should return true when acc already has a delegation and funds", () => {
const poolIdAcc = {
balance: new BigNumber(1),
cardanoResources: {
delegation: {
poolId: "helloiamapoolid",
},
},
describe("isAlreadyStaking", () => {
it("should return false when acc isn't delegating", () => {
const noResourcesAcc = {} as CardanoAccount;
expect(isAlreadyStaking(noResourcesAcc)).toEqual(false);
const noDelegationAcc = {
cardanoResources: {},
} as CardanoAccount;
expect(isAlreadyStaking(noDelegationAcc)).toEqual(false);
const noPoolIdAcc = { cardanoResources: { delegation: {} } } as CardanoAccount;
expect(isAlreadyStaking(noPoolIdAcc)).toEqual(false);
});

it("should return true when acc is delegating", () => {
const noResourcesAcc = {
cardanoResources: { delegation: { poolId: "itspoolid" } },
} as CardanoAccount;
expect(canStake(poolIdAcc)).toEqual(true);
expect(isAlreadyStaking(noResourcesAcc)).toEqual(true);
});
});

0 comments on commit 826a498

Please sign in to comment.