Skip to content

Commit

Permalink
handle currencies passed to get/setcurrencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Wozacosta committed Aug 30, 2024
1 parent 6911f37 commit f56c642
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 16 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
3 changes: 2 additions & 1 deletion libs/coin-modules/coin-cosmos/src/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live";

import { CoinConfig } from "@ledgerhq/coin-framework/lib/config";
import { CosmosAPI } from "../api/Cosmos";
import cosmosCoinConfig, { CosmosCoinConfig } from "../config";
import cosmosCoinConfig, { CosmosCoinConfig, CosmosCoinConfigGetter } from "../config";
import { createTransaction } from "../createTransaction";
import { estimateMaxSpendable } from "../estimateMaxSpendable";
import getTransactionStatus from "../getTransactionStatus";
Expand Down Expand Up @@ -80,6 +80,7 @@ function buildAccountBridge(
}

export function createBridges(signerContext: SignerContext<CosmosSigner>, coinConfig: CoinConfig<CosmosCoinConfig>) {
// export function createBridges(signerContext: SignerContext<CosmosSigner>, coinConfig: CosmosCoinConfigGetter) {
cosmosCoinConfig.setCoinConfig(coinConfig);
return {
currencyBridge: buildCurrencyBridge(signerContext),
Expand Down
2 changes: 1 addition & 1 deletion libs/coin-modules/coin-cosmos/src/bridge/preload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const getPreloadStrategy = () => ({
});

export const preload = async (currency: CryptoCurrency) => {
const config = cosmosCoinConfig.getCoinConfig();
const config = cosmosCoinConfig.getCoinConfig(currency);
const cosmosValidatorsManager = new CosmosValidatorsManager(getCryptoCurrencyById(currency.id), {
endPoint: (config as unknown as CosmosCurrencyConfig).lcd,
});
Expand Down
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
5 changes: 3 additions & 2 deletions libs/coin-modules/coin-cosmos/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,7 @@ import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
// export type CosmosCoinConfig = CurrencyConfig & CosmosConfig;

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

// let coinConfig: CoinConfig | undefined;
Expand All @@ -185,7 +186,7 @@ import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";

// export { cosmosConfig };

import buildConConfig, { type CurrencyConfig } from "@ledgerhq/coin-framework/config";
import buildCoinConfig, { type CurrencyConfig } from "@ledgerhq/coin-framework/config";

// export type XrpConfig = {
// node: string;
Expand All @@ -196,7 +197,7 @@ export type CosmosCoinConfig = CurrencyConfig & CosmosConfig;

// const coinConfig = buildConConfig<XrpCoinConfig>();
// export const {setCoinConfig, getCoinConfig} = buildConConfig<CosmosCoinConfig>();
const coinConfig = buildConConfig<CosmosCoinConfig>();
const coinConfig = buildCoinConfig<CosmosCoinConfig>();
export default coinConfig;

// export default coinConfig;
Expand Down
20 changes: 17 additions & 3 deletions libs/ledger-live-common/src/families/cosmos/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,27 @@ const createSigner: CreateSigner<CosmosSigner> = (transport: Transport) => {
getAddress: hwCosmos.getAddress.bind(hwCosmos),
};
};
const getCurrencyConfig = (currency: CryptoCurrency): CosmosCoinConfig => {
return getCurrencyConfiguration(currency);
};
// const getCurrencyConfig = (currency: CryptoCurrency): CosmosCoinConfig => {
// return getCurrencyConfiguration(currency);
// };

// const getCoinConfig = () =>
// getCurrencyConfiguration<XrpCoinConfig>(getCryptoCurrencyById("ripple"));

// const getCoinConfig = (currency: CryptoCurrency) =>
// getCurrencyConfiguration<CosmosCoinConfig>(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,
// getCoinConfig,
);

const resolver: Resolver = createResolver(createSigner, cosmosResolver);
Expand Down

0 comments on commit f56c642

Please sign in to comment.