Skip to content

Commit

Permalink
Fix: LIVE-12068 [cosmos]Adapt LL code to avoid using deprecated endpo…
Browse files Browse the repository at this point in the history
…ints (#6735)

* fix: [cosmos]Adapt LL code to avoid using deprecated endpoints

* fix: unit tests

* fix: update cosmos unit tests

* update snapshot

* fix: update unit test

* fix: update unit test
  • Loading branch information
hzheng-ledger committed May 15, 2024
1 parent 801265b commit bbb1e8d
Show file tree
Hide file tree
Showing 9 changed files with 712 additions and 1,015 deletions.
5 changes: 5 additions & 0 deletions .changeset/neat-ads-brake.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@ledgerhq/live-common": patch
---

fix cosmos code to avoid using deprecated endpoints
41 changes: 30 additions & 11 deletions libs/ledger-live-common/src/families/cosmos/api/Cosmos.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { patchOperationWithHash } from "../../../operation";
import cryptoFactory from "../chain/chain";
import cosmosBase from "../chain/cosmosBase";
import * as CosmosSDKTypes from "./types";
import semver from "semver";
import {
CosmosDelegation,
CosmosDelegationStatus,
Expand All @@ -23,6 +24,13 @@ export class CosmosAPI {
protected defaultEndpoint: string;
private version: string;
private chainInstance: cosmosBase;
private _cosmosSDKVersion: Promise<string> | null = null;
private get cosmosSDKVersion(): Promise<string> {
if (!this._cosmosSDKVersion) {
this._cosmosSDKVersion = this.getCosmosSDKVersion();
}
return this._cosmosSDKVersion;
}

constructor(currencyId: string) {
const crypto = cryptoFactory(currencyId);
Expand Down Expand Up @@ -85,6 +93,12 @@ export class CosmosAPI {
}
};

private getCosmosSDKVersion = async (): Promise<string> => {
const { application_version } = await this.getNodeInfo();
const cosmosSDKVersion = application_version.cosmos_sdk_version;
return cosmosSDKVersion;
};

/**
* @sdk https://docs.cosmos.network/api#tag/Query/operation/Account
* @warning return is technically "any" based on documentation and may differ depending on the chain
Expand Down Expand Up @@ -138,15 +152,14 @@ export class CosmosAPI {
* @sdk https://docs.cosmos.network/api#tag/Service/operation/GetNodeInfo
* @notice returns { application_versoin: { ..., cosmos_sdk_version } } (Since: cosmos-sdk 0.43)
*/
getChainId = async (): Promise<string> => {
const {
data: { default_node_info: defaultNodeInfo },
} = await network<CosmosSDKTypes.GetNodeInfosSDK>({
method: "GET",
url: `${this.defaultEndpoint}/cosmos/base/tendermint/${this.version}/node_info`,
});

return defaultNodeInfo.network;
getNodeInfo = async (): Promise<CosmosSDKTypes.GetNodeInfosSDK> => {
const data = (
await network<CosmosSDKTypes.GetNodeInfosSDK>({
method: "GET",
url: `${this.defaultEndpoint}/cosmos/base/tendermint/${this.version}/node_info`,
})
).data;
return data;
};

/**
Expand Down Expand Up @@ -403,14 +416,20 @@ export class CosmosAPI {
txs: CosmosTx[];
total: number;
}> {
let cosmosSDKVersion = await this.cosmosSDKVersion;
cosmosSDKVersion = semver.coerce(cosmosSDKVersion).version;
let queryparam = "events";
if (semver.gte(cosmosSDKVersion, "0.50.0")) {
queryparam = "query";
}
let serializedOptions = "";
for (const key of Object.keys(options)) {
serializedOptions += options[key] != null ? `&${key}=${options[key]}` : "";
}
const { data } = await network<CosmosSDKTypes.GetTxsEvents>({
method: "GET",
url:
`${nodeUrl}/cosmos/tx/${this.version}/txs?events=` +
`${nodeUrl}/cosmos/tx/${this.version}/txs?${queryparam}=` +
encodeURI(`${filterOn}='${address}'`) +
serializedOptions,
});
Expand All @@ -423,7 +442,7 @@ export class CosmosAPI {

/**
* @sdk https://docs.cosmos.network/api#tag/Service/operation/BroadcastTx
* @depreacted body {..., mode } -> BROADCAST_MODE_BLOCK (Deprecated: post v0.47 use BROADCAST_MODE_SYNC instead)
* @deprecated body {..., mode } -> BROADCAST_MODE_BLOCK (Deprecated: post v0.47 use BROADCAST_MODE_SYNC instead)
* @notice returns {..., events } (Since: cosmos-sdk 0.42.11, 0.44.5, 0.45)
*/
broadcast = async ({ signedOperation: { operation, signature } }): Promise<Operation> => {
Expand Down
Loading

0 comments on commit bbb1e8d

Please sign in to comment.