Skip to content

Commit

Permalink
[Coin Fmk] Batcher moving to Network (#6819)
Browse files Browse the repository at this point in the history
* chore: move batcher to live-network

Signed-off-by: Stéphane Prohaszka <[email protected]>

* feat: add new network method to avoid expose Axos types

Signed-off-by: Stéphane Prohaszka <[email protected]>

* fix: network package

Signed-off-by: Stéphane Prohaszka <[email protected]>

* feat: mark as deprecated old network function

Signed-off-by: Stéphane Prohaszka <[email protected]>

---------

Signed-off-by: Stéphane Prohaszka <[email protected]>
  • Loading branch information
sprohaszka-ledger committed May 22, 2024
1 parent dbe40aa commit 326cae0
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 26 deletions.
6 changes: 6 additions & 0 deletions .changeset/good-ducks-own.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@ledgerhq/live-network": minor
"@ledgerhq/coin-framework": patch
---

Add Batcher to live-network and partition Axios types
1 change: 1 addition & 0 deletions libs/coin-framework/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@
"@ledgerhq/devices": "workspace:^",
"@ledgerhq/errors": "workspace:^",
"@ledgerhq/live-env": "workspace:^",
"@ledgerhq/live-network": "workspace:^",
"@ledgerhq/logs": "workspace:^",
"@ledgerhq/types-cryptoassets": "workspace:^",
"@ledgerhq/types-live": "workspace:^",
Expand Down
2 changes: 1 addition & 1 deletion libs/coin-framework/src/nft/support.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import type {
import { getEnv } from "@ledgerhq/live-env";
import type { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
import { CollectionMetadataInput, NftMetadataInput, NftRequestsBatcher } from "./types";
import { makeBatcher } from "../batcher";
import { makeBatcher } from "@ledgerhq/live-network/batcher";

export function isNFTActive(currency: CryptoCurrency | undefined | null): boolean {
return !!currency && getEnv("NFT_CURRENCIES").split(",").includes(currency?.id);
Expand Down
2 changes: 1 addition & 1 deletion libs/coin-framework/src/nft/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { NFTCollectionMetadataResponse, NFTMetadataResponse } from "@ledgerhq/types-live";
import { Batcher } from "../batcher/types";
import { Batcher } from "@ledgerhq/live-network/batcher/types";

export type NftMetadataInput = {
contract: string;
Expand Down
4 changes: 2 additions & 2 deletions libs/coin-modules/coin-evm/src/api/node/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ import { getEnv } from "@ledgerhq/live-env";
import { delay } from "@ledgerhq/live-promise";
import axios, { AxiosRequestConfig } from "axios";
import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
import { Batcher } from "@ledgerhq/coin-framework/batcher/types";
import { makeBatcher } from "@ledgerhq/coin-framework/batcher/index";
import { Batcher } from "@ledgerhq/live-network/batcher/types";
import { makeBatcher } from "@ledgerhq/live-network/batcher/index";
import { GasEstimationError, LedgerNodeUsedIncorrectly } from "../../errors";
import OptimismGasPriceOracleAbi from "../../abis/optimismGasPriceOracle.abi.json";
import { getSerializedTransaction } from "../../transaction";
Expand Down
9 changes: 2 additions & 7 deletions libs/coin-modules/coin-polkadot/src/network/sidecar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import querystring from "querystring";
import { TypeRegistry } from "@polkadot/types";
import { Extrinsics } from "@polkadot/types/metadata/decorate/types";
import { getEnv } from "@ledgerhq/live-env";
import network from "@ledgerhq/live-network/network";
import network from "@ledgerhq/live-network";
import { hours, makeLRUCache } from "@ledgerhq/live-network/cache";
import type {
PolkadotValidator,
Expand Down Expand Up @@ -58,12 +58,7 @@ const ELECTION_STATUS_OPTIMISTIC_THRESHOLD = getEnv("POLKADOT_ELECTION_STATUS_TH
* @returns {SidecarAccountBalanceInfo}
*/
const fetchBalanceInfo = async (addr: string): Promise<SidecarAccountBalanceInfo> => {
const {
data,
}: {
data: SidecarAccountBalanceInfo;
} = await network({
method: "GET",
const { data } = await network<SidecarAccountBalanceInfo>({
url: getSidecarUrl(`/accounts/${addr}/balance-info`),
});
return data;
Expand Down
8 changes: 7 additions & 1 deletion libs/live-network/.unimportedrc.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
{
"entry": ["src/network.ts", "src/cache.ts"],
"entry": [
"src/batcher/index.ts",
"src/batcher/types.ts",
"src/cache.ts",
"src/index.ts",
"src/network.ts"
],
"ignoreUnimported": [],
"ignoreUnresolved": []
}
26 changes: 19 additions & 7 deletions libs/live-network/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,18 @@
},
"typesVersions": {
"*": {
"*.json": [
"*.json"
],
"*": [
"lib/*"
],
"lib/*": [
"lib/*"
],
"lib-es/*": [
"lib-es/*"
],
"batcher": [
"lib/batcher/index"
],
"*": [
"lib/*",
"lib/batcher/*"
]
}
},
Expand All @@ -37,6 +38,14 @@
"./lib/*.js": "./lib/*.js",
"./lib-es/*": "./lib-es/*.js",
"./lib-es/*.js": "./lib-es/*.js",
"./batcher": {
"require": "./lib/batcher/index.js",
"default": "./lib-es/batcher/index.js"
},
"./batcher/*": {
"require": "./lib/batcher/*.js",
"default": "./lib-es/batcher/*.js"
},
"./*": {
"require": "./lib/*.js",
"default": "./lib-es/*.js"
Expand All @@ -45,6 +54,10 @@
"require": "./lib/*.js",
"default": "./lib-es/*.js"
},
".": {
"require": "./lib/index.js",
"default": "./lib-es/index.js"
},
"./package.json": "./package.json"
},
"license": "Apache-2.0",
Expand All @@ -54,7 +67,6 @@
"@ledgerhq/live-promise": "workspace:^",
"@ledgerhq/logs": "workspace:^",
"axios": "0.26.1",
"invariant": "^2.2.2",
"lru-cache": "^7.14.1"
},
"devDependencies": {
Expand Down
File renamed without changes.
File renamed without changes.
2 changes: 2 additions & 0 deletions libs/live-network/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
import { newImplementation } from "./network";
export default newImplementation;
68 changes: 64 additions & 4 deletions libs/live-network/src/network.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import axios, {
AxiosPromise,
AxiosResponse,
type AxiosError,
type AxiosRequestConfig,
type Method,
} from "axios";
import { LedgerAPI4xx, LedgerAPI5xx, NetworkDown } from "@ledgerhq/errors";
import { changes, getEnv } from "@ledgerhq/live-env";
import { retry } from "@ledgerhq/live-promise";
import { log } from "@ledgerhq/logs";
import type { AxiosError, AxiosRequestConfig, Method } from "axios";
import axios, { AxiosPromise, AxiosResponse } from "axios";
import invariant from "invariant";

type Metadata = { startTime: number };
type ExtendedXHRConfig = AxiosRequestConfig & { metadata?: Metadata };
Expand Down Expand Up @@ -158,8 +162,64 @@ const extractErrorMessage = (raw: string): string | undefined => {
return;
};

export type LiveNetworkRequest<T> = {
url?: string;
method?: "GET" | "POST" | "PUT" | "DELETE";
headers?: Record<string, string | number | boolean>;
baseURL?: string;
params?: unknown;
data?: T;
timeout?: number;
};
export type LiveNetworkResponse<T> = {
data: T;
status: number;
};
/**
* Network call
* @param request
* @returns
*/
export const newImplementation = async <T = unknown, U = unknown>(
request: LiveNetworkRequest<U>,
): Promise<LiveNetworkResponse<T>> => {
let response: AxiosResponse<T>;

if (!("method" in request)) {
request.method = "GET";
}

if (request.method === "GET") {
if (!("timeout" in request)) {
request.timeout = getEnv("GET_CALLS_TIMEOUT");
}

response = await retry(() => axios(request), {
maxRetry: getEnv("GET_CALLS_RETRY"),
retryCondition: error => {
if (error && error.status) {
// A 422 shouldn't be retried without change as explained in this documentation
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/422
return error.status !== 422;
}
return true;
},
});
} else {
response = await axios(request);
}

const { data, status } = response;
return { data, status };
};

/**
* Network call
* @deprecated Use new method by updating your import: `@ledgerhq/live-network`
* @param arg Axios request type
* @returns Axios response type
*/
const implementation = <T = any>(arg: AxiosRequestConfig): AxiosPromise<T> => {
invariant(typeof arg === "object", "network takes an object as parameter");
let promise: AxiosPromise;

if (arg.method === "GET") {
Expand Down
6 changes: 3 additions & 3 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 326cae0

Please sign in to comment.