diff --git a/.changeset/yellow-parents-dance.md b/.changeset/yellow-parents-dance.md new file mode 100644 index 00000000000..ae24d57bb2b --- /dev/null +++ b/.changeset/yellow-parents-dance.md @@ -0,0 +1,5 @@ +--- +"@ledgerhq/live-common": patch +--- + +Fix getRedelegations cosmos coin-module diff --git a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts index 2cd7c399b01..bd4fc2f5b19 100644 --- a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts +++ b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts @@ -280,7 +280,9 @@ export class CosmosAPI { }); for (const { entries, redelegation } of redelegationResponses) { - for (const { initial_balance: initalBalance, completion_time: completionTime } of entries) { + for (const { + redelegation_entry: { initial_balance: initalBalance, completion_time: completionTime }, + } of entries) { redelegations.push({ validatorSrcAddress: redelegation.validator_src_address, validatorDstAddress: redelegation.validator_dst_address, diff --git a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.unit.test.ts b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.unit.test.ts index 583323b6521..e37e6faf940 100644 --- a/libs/ledger-live-common/src/families/cosmos/api/Cosmos.unit.test.ts +++ b/libs/ledger-live-common/src/families/cosmos/api/Cosmos.unit.test.ts @@ -111,6 +111,101 @@ describe("CosmosApi", () => { }); }); + describe("getRedelegations", () => { + it("should return redelegations correctly (sdk v0.41/0.42/0.44/0.45)", async () => { + const getApiResponseSinceSdk041 = { + redelegation_responses: [ + { + redelegation: { + delegator_address: "cosmosDelegatorAddress", + validator_src_address: "cosmosValidatorSrcAddress", + validator_dst_address: "cosmosValidatorDstAddress", + entries: [ + { + creation_height: "1", + completion_time: "2023-06-04T15:12:58.567Z", + initial_balance: "100", + shares_dst: "100", + }, + ], + }, + entries: [ + { + redelegation_entry: { + creation_height: "1", + completion_time: "2023-06-04T15:12:58.567Z", + initial_balance: "100", + shares_dst: "100", + }, + balance: "100", + }, + ], + }, + ], + pagination: { + next_key: null, + total: "1", + }, + }; + mockedNetwork.mockResolvedValue({ + data: getApiResponseSinceSdk041, + } as AxiosResponse); + const cosmosRedelegations = await cosmosApi.getRedelegations("cosmosDelegatorAddress"); + expect(cosmosRedelegations[0].amount).toEqual(new BigNumber(100)); + expect(cosmosRedelegations[0].completionDate).toEqual(new Date("2023-06-04T15:12:58.567Z")); + expect(cosmosRedelegations[0].validatorDstAddress).toEqual("cosmosValidatorDstAddress"); + expect(cosmosRedelegations[0].validatorSrcAddress).toEqual("cosmosValidatorSrcAddress"); + }); + + it("should return redelegations correctly (sdk v0.46/0.47)", async () => { + const getApiResponseSinceSdk046 = { + redelegation_responses: [ + { + redelegation: { + delegator_address: "cosmosDelegatorAddress", + validator_src_address: "cosmosValidatorSrcAddress", + validator_dst_address: "cosmosValidatorDstAddress", + entries: null, + }, + entries: [ + { + redelegation_entry: { + creation_height: 42, + completion_time: "2024-04-29T09:38:35.273743543Z", + initial_balance: "100", + shares_dst: "100.000000000000000000", + unbonding_id: 394, + }, + balance: "100", + }, + ], + }, + ], + pagination: { next_key: null, total: "1" }, + }; + mockedNetwork.mockResolvedValue({ + data: getApiResponseSinceSdk046, + } as AxiosResponse); + + const cosmosRedelegations = await cosmosApi.getRedelegations("cosmosDelegatorAddress"); + expect(cosmosRedelegations[0].amount).toEqual(new BigNumber(100)); + expect(cosmosRedelegations[0].completionDate).toEqual( + new Date("2024-04-29T09:38:35.273743543Z"), + ); + expect(cosmosRedelegations[0].validatorDstAddress).toEqual("cosmosValidatorDstAddress"); + expect(cosmosRedelegations[0].validatorSrcAddress).toEqual("cosmosValidatorSrcAddress"); + }); + + it("should return no redelegations if there are none", async () => { + mockedNetwork.mockResolvedValue({ + data: { redelegation_responses: [], pagination: { next_key: null, total: "0" } }, + } as AxiosResponse); + + const cosmosRedelegations = await cosmosApi.getRedelegations("cosmosDelegatorAddress"); + expect(cosmosRedelegations.length).toEqual(0); + }); + }); + describe("simulate", () => { it("should return gas used when the network call returns gas used", async () => { mockedNetwork.mockResolvedValue({ diff --git a/libs/ledger-live-common/src/families/cosmos/api/types.ts b/libs/ledger-live-common/src/families/cosmos/api/types.ts index 48c38c815c1..3a9dca88c01 100644 --- a/libs/ledger-live-common/src/families/cosmos/api/types.ts +++ b/libs/ledger-live-common/src/families/cosmos/api/types.ts @@ -264,14 +264,26 @@ export type GetRedelegations = { delegator_address: string; validator_src_address: string; validator_dst_address: string; + entries: + | { + creation_height: string; + completion_time: string; + initial_balance: string; + shares_dst: string; + }[] + // null since sdk 0.46 + | null; }; entries: { - creation_height: string; - completion_time: string; - initial_balance: string; - shares_dst: string; - unbonding_id: string; - unbonding_on_hold_ref_count: string; + redelegation_entry: { + creation_height: string; + completion_time: string; + initial_balance: string; + shares_dst: string; + // only since sdk 0.46 + unbonding_id?: string; + }; + balance: string; }[]; }[]; pagination: {