Skip to content

Commit

Permalink
coin config refacto
Browse files Browse the repository at this point in the history
  • Loading branch information
Wozacosta committed Aug 30, 2024
1 parent bab4e8a commit 26f3ef2
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 69 deletions.
8 changes: 5 additions & 3 deletions libs/coin-framework/src/config.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
import { MissingCoinConfig } from "./errors";

type ConfigStatus =
Expand All @@ -21,7 +22,8 @@ export type CurrencyConfig = {
[key: string]: unknown;
};

export type CoinConfig<T extends CurrencyConfig> = () => T;
// export type CoinConfig<T extends CurrencyConfig> = () => T;
export type CoinConfig<T extends CurrencyConfig> = (currency?: CryptoCurrency) => T;

function buildCoinConfig<T extends CurrencyConfig>() {
let coinConfig: CoinConfig<T> | undefined;
Expand All @@ -30,12 +32,12 @@ function buildCoinConfig<T extends CurrencyConfig>() {
coinConfig = config;
};

const getCoinConfig = (): T => {
const getCoinConfig = (currency?: CryptoCurrency): T => {
if (!coinConfig) {
throw new MissingCoinConfig();
}

return coinConfig();
return coinConfig(currency);
};

return {
Expand Down
16 changes: 8 additions & 8 deletions libs/coin-modules/coin-cosmos/src/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import {
import { SignerContext } from "@ledgerhq/coin-framework/signer";
import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live";

import { CoinConfig } from "@ledgerhq/coin-framework/lib/config";
import { CosmosAPI } from "../api/Cosmos";
import { CoinConfig, setCoinConfig } from "../config";
import cosmosCoinConfig, { CosmosCoinConfig } from "../config";
import { createTransaction } from "../createTransaction";
import { estimateMaxSpendable } from "../estimateMaxSpendable";
import getTransactionStatus from "../getTransactionStatus";
Expand All @@ -22,11 +23,7 @@ import {
} from "../serialization";
import { buildSignOperation } from "../signOperation";
import { getAccountShape } from "../synchronisation";
import type {
CosmosAccount,
Transaction,
TransactionStatus,
} from "../types";
import type { CosmosAccount, Transaction, TransactionStatus } from "../types";
import { CosmosSigner } from "../types/signer";
import { updateTransaction } from "../updateTransaction";
import { getPreloadStrategy, hydrate, preload } from "./preload";
Expand Down Expand Up @@ -78,8 +75,11 @@ function buildAccountBridge(
};
}

export function createBridges(signerContext: SignerContext<CosmosSigner>, coinConfig: CoinConfig) {
setCoinConfig(coinConfig);
export function createBridges(
signerContext: SignerContext<CosmosSigner>,
coinConfig: CoinConfig<CosmosCoinConfig>,
) {
cosmosCoinConfig.setCoinConfig(coinConfig);
return {
currencyBridge: buildCurrencyBridge(signerContext),
accountBridge: buildAccountBridge(signerContext),
Expand Down
28 changes: 2 additions & 26 deletions libs/coin-modules/coin-cosmos/src/bridge/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";

import { CosmosValidatorsManager } from "../CosmosValidatorsManager";
import cryptoFactory from "../chain/chain";
import { getCoinConfig } from "../config";
import cosmosCoinConfig from "../config";
import { asSafeCosmosPreloadData, setCosmosPreloadData } from "../preloadedData";
import type { CosmosCurrencyConfig, CosmosValidatorItem } from "../types";

Expand All @@ -12,7 +12,7 @@ export const getPreloadStrategy = () => ({
});

export const preload = async (currency: CryptoCurrency) => {
const config = getCoinConfig(currency);
const config = cosmosCoinConfig.getCoinConfig(currency);
const cosmosValidatorsManager = new CosmosValidatorsManager(getCryptoCurrencyById(currency.id), {
endPoint: (config as unknown as CosmosCurrencyConfig).lcd,
});
Expand All @@ -27,13 +27,6 @@ export const preload = async (currency: CryptoCurrency) => {
});
};

// export const preload = async () => {
// const superRepresentatives = await getTronSuperRepresentatives();
// return {
// superRepresentatives,
// };
// };

export const hydrate = (
data: { validators?: CosmosValidatorItem[]; config: CosmosCurrencyConfig },
currency: CryptoCurrency,
Expand All @@ -49,20 +42,3 @@ export const hydrate = (
cosmosValidatorsManager.hydrateValidators(validators);
setCosmosPreloadData(currency.id, asSafeCosmosPreloadData(data));
};

// export const hydrate = (data?: { superRepresentatives?: SuperRepresentative[] }) => {
// if (!data || !data.superRepresentatives) return;

// const { superRepresentatives } = data;

// if (
// !superRepresentatives ||
// typeof superRepresentatives !== "object" ||
// !Array.isArray(superRepresentatives)
// )
// return;

// hydrateSuperRepresentatives(superRepresentatives);
// };

// export c
12 changes: 6 additions & 6 deletions libs/coin-modules/coin-cosmos/src/cli.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { from, Observable } from "rxjs";
import { map } from "rxjs/operators";
import { BigNumber } from "bignumber.js";
import invariant from "invariant";
import flatMap from "lodash/flatMap";
import zipWith from "lodash/zipWith";
import { BigNumber } from "bignumber.js";
import { Transaction as CosmosTransaction } from "./types";
import type { CosmosDelegationInfo } from "./types";
import { AccountLike } from "@ledgerhq/types-live";
import { from, Observable } from "rxjs";
import { map } from "rxjs/operators";
import { getCryptoCurrencyById } from "@ledgerhq/cryptoassets/index";
import { AccountLike } from "@ledgerhq/types-live";
import { CosmosValidatorsManager } from "./CosmosValidatorsManager";
import type { CosmosDelegationInfo } from "./types";
import { Transaction as CosmosTransaction } from "./types";

const options = [
{
Expand Down
26 changes: 4 additions & 22 deletions libs/coin-modules/coin-cosmos/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { ConfigInfo } from "@ledgerhq/live-config/LiveConfig";

type CosmosConfig = Record<string, ConfigInfo>;

const cosmosConfig: CosmosConfig = {
export const cosmosConfig: CosmosConfig = {
config_currency_axelar: {
type: "object",
default: {
Expand Down Expand Up @@ -160,26 +160,8 @@ const cosmosConfig: CosmosConfig = {
},
};

import { CurrencyConfig } from "@ledgerhq/coin-framework/config";
import { MissingCoinConfig } from "@ledgerhq/coin-framework/errors";
import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
import buildCoinConfig, { type CurrencyConfig } from "@ledgerhq/coin-framework/config";

export type CosmosCoinConfig = CurrencyConfig & CosmosConfig;

export type CoinConfig = (currency: CryptoCurrency) => CosmosCoinConfig;

let coinConfig: CoinConfig | undefined;

export const setCoinConfig = (config: CoinConfig): void => {
coinConfig = config;
};

export const getCoinConfig = (currency: CryptoCurrency): CosmosCoinConfig => {
if (!coinConfig) {
throw new MissingCoinConfig();
}

return coinConfig(currency);
};

export { cosmosConfig };
const coinConfig = buildCoinConfig<CosmosCoinConfig>();
export default coinConfig;
11 changes: 7 additions & 4 deletions libs/ledger-live-common/src/families/cosmos/setup.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Goal of this file is to inject all necessary device/signer dependency to coin-modules

import { createBridges } from "@ledgerhq/coin-cosmos/bridge/js";
import { createBridges } from "@ledgerhq/coin-cosmos/bridge/index";
import makeCliTools from "@ledgerhq/coin-cosmos/cli";
import { CosmosCoinConfig } from "@ledgerhq/coin-cosmos/config";
import cosmosResolver from "@ledgerhq/coin-cosmos/hw-getAddress";
Expand All @@ -24,10 +24,13 @@ const createSigner: CreateSigner<CosmosSigner> = (transport: Transport) => {
getAddress: hwCosmos.getAddress.bind(hwCosmos),
};
};
const getCurrencyConfig = (currency: CryptoCurrency): CosmosCoinConfig => {
return getCurrencyConfiguration(currency);
};

const getCurrencyConfig = (currency?: CryptoCurrency) => {
if (!currency) {
throw new Error("No currency provided");
}
return getCurrencyConfiguration<CosmosCoinConfig>(currency);
};
const bridge: Bridge<Transaction, CosmosAccount, TransactionStatus> = createBridges(
executeWithSigner(createSigner),
getCurrencyConfig,
Expand Down

0 comments on commit 26f3ef2

Please sign in to comment.