Skip to content

Commit

Permalink
fix cosmos redelegations
Browse files Browse the repository at this point in the history
  • Loading branch information
hedi-edelbloute committed Apr 11, 2024
1 parent 8c23ae7 commit 194cb09
Show file tree
Hide file tree
Showing 4 changed files with 121 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/yellow-parents-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

Fix getRedelegations cosmos coin-module
4 changes: 3 additions & 1 deletion libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down
24 changes: 18 additions & 6 deletions libs/ledger-live-common/src/families/cosmos/api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 194cb09

Please sign in to comment.