Skip to content

Commit

Permalink
fix: update coinConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
weeddotv committed Jun 11, 2024
1 parent 5bf02a4 commit 1748e0e
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 10 deletions.
7 changes: 4 additions & 3 deletions libs/coin-modules/coin-icon/src/api/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import network from "@ledgerhq/live-network/network";
import { BigNumber } from "bignumber.js";
import type { Operation, OperationType } from "@ledgerhq/types-live";
import { getEnv } from "@ledgerhq/live-env";
import { encodeOperationId } from "@ledgerhq/coin-framework/operation";
import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
import { LIMIT } from "../constants";
Expand All @@ -10,16 +9,18 @@ import { AccountType, IconTransactionType } from "./api-type";
import { log } from "@ledgerhq/logs";
import { IconOperation } from "../types";
import querystring from "querystring";
import { getCoinConfig } from "../config";

/**
* Returns Testnet API URL if the current network is testnet
*
* @param {network} network
*/
function getApiUrl(network: CryptoCurrency): string {
let apiUrl = getEnv("ICON_INDEXER_ENDPOINT");
const currencyConfig = getCoinConfig();
let apiUrl = currencyConfig.infra.indexer;
if (isTestnet(network)) {
apiUrl = getEnv("ICON_TESTNET_INDEXER_ENDPOINT");
apiUrl = currencyConfig.infra.indexer_testnet;
}
return apiUrl;
}
Expand Down
12 changes: 7 additions & 5 deletions libs/coin-modules/coin-icon/src/api/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@ import { BigNumber } from "bignumber.js";
import IconService from "icon-sdk-js";
import type { IcxTransaction, SignedTransaction } from "icon-sdk-js";
import { CryptoCurrency } from "@ledgerhq/types-cryptoassets";
import { getEnv } from "@ledgerhq/live-env";
import { isTestnet } from "../logic";
import { GOVERNANCE_SCORE_ADDRESS, IISS_SCORE_ADDRESS } from "../constants";
import { IconAccount } from "../types";
import { SignedOperation } from "@ledgerhq/types-live";
import { IconDelegationType } from "./api-type";
import { getCoinConfig } from "../config";

const { HttpProvider } = IconService;
const { IconBuilder } = IconService;
Expand All @@ -17,17 +17,19 @@ const { IconBuilder } = IconService;
* @param {currency} currency
*/
export function getRpcUrl(currency: CryptoCurrency): string {
let rpcUrl = getEnv("ICON_NODE_ENDPOINT");
const currencyConfig = getCoinConfig();
let rpcUrl = currencyConfig.infra.node_endpoint;
if (isTestnet(currency)) {
rpcUrl = getEnv("ICON_TESTNET_NODE_ENDPOINT");
rpcUrl = currencyConfig.infra.node_testnet_endpoint;
}
return rpcUrl;
}

export function getDebugRpcUrl(currency: CryptoCurrency): string {
let rpcUrl = getEnv("ICON_DEBUG_ENDPOINT");
const currencyConfig = getCoinConfig();
let rpcUrl = currencyConfig.infra.debug_endpoint;
if (isTestnet(currency)) {
rpcUrl = getEnv("ICON_TESTNET_DEBUG_ENDPOINT");
rpcUrl = currencyConfig.infra.debug_testnet_endpoint;
}
return rpcUrl;
}
Expand Down
6 changes: 5 additions & 1 deletion libs/coin-modules/coin-icon/src/bridge/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
makeSync,
} from "@ledgerhq/coin-framework/bridge/jsHelpers";
import { SignerContext } from "@ledgerhq/coin-framework/signer";
import { CoinConfig } from "@ledgerhq/coin-framework/config";

import type { AccountBridge, CurrencyBridge } from "@ledgerhq/types-live";
import resolver from "../hw-getAddress";
Expand All @@ -20,6 +21,7 @@ import { getAccountShape } from "../synchronization";
import { assignFromAccountRaw, assignToAccountRaw } from "../serialization";
import type { Transaction } from "../types";
import { IconSigner } from "../signer";
import { IconCoinConfig, setCoinConfig } from "../config";

export function buildCurrencyBridge(signerContext: SignerContext<IconSigner>): CurrencyBridge {
const getAddress = resolver(signerContext);
Expand Down Expand Up @@ -61,7 +63,9 @@ export function buildAccountBridge(
};
}

export function createBridges(signerContext: SignerContext<IconSigner>) {
export function createBridges(signerContext: SignerContext<IconSigner>, coinConfig: CoinConfig<IconCoinConfig>,) {
setCoinConfig(coinConfig);

return {
currencyBridge: buildCurrencyBridge(signerContext),
accountBridge: buildAccountBridge(signerContext),
Expand Down
29 changes: 29 additions & 0 deletions libs/coin-modules/coin-icon/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { CurrencyConfig, CoinConfig } from "@ledgerhq/coin-framework/config";
import { MissingCoinConfig } from "@ledgerhq/coin-framework/errors";

export type IconConfig = {
infra: {
indexer: string;
indexer_testnet: string;
node_endpoint: string;
node_testnet_endpoint: string;
debug_endpoint: string;
debug_testnet_endpoint: string;
};
};

export type IconCoinConfig = CurrencyConfig & IconConfig;

let coinConfig: CoinConfig<IconCoinConfig> | undefined;

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

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

return coinConfig();
};
24 changes: 23 additions & 1 deletion libs/ledger-live-common/src/families/icon/setup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,34 @@ import Transport from "@ledgerhq/hw-transport";
import type { Bridge } from "@ledgerhq/types-live";
import { CreateSigner, createResolver, executeWithSigner } from "../../bridge/setup";
import type { Resolver } from "../../hw/getAddress/types";
import { IconCoinConfig } from "@ledgerhq/coin-icon/config";
import { getEnv } from "@ledgerhq/live-env";

const createSigner: CreateSigner<Icon> = (transport: Transport) => {
return new Icon(transport);
};

const bridge: Bridge<Transaction> = createBridges(executeWithSigner(createSigner));
const getCurrencyConfig = (): IconCoinConfig => {
return {
status: {
type: "active",
},
infra: {
indexer: getEnv("ICON_INDEXER_ENDPOINT"),
indexer_testnet: getEnv("ICON_TESTNET_INDEXER_ENDPOINT"),
node_endpoint: getEnv("ICON_NODE_ENDPOINT"),
node_testnet_endpoint: getEnv("ICON_TESTNET_NODE_ENDPOINT"),
debug_endpoint: getEnv("ICON_DEBUG_ENDPOINT"),
debug_testnet_endpoint: getEnv("ICON_TESTNET_DEBUG_ENDPOINT"),
}
}

};

const bridge: Bridge<Transaction> = createBridges(
executeWithSigner(createSigner),
getCurrencyConfig,
);

const resolver: Resolver = createResolver(createSigner, iconResolver);

Expand Down

0 comments on commit 1748e0e

Please sign in to comment.