diff --git a/packages/advanced-logic/src/advanced-logic.ts b/packages/advanced-logic/src/advanced-logic.ts index 11ab59010e..65a15b6b73 100644 --- a/packages/advanced-logic/src/advanced-logic.ts +++ b/packages/advanced-logic/src/advanced-logic.ts @@ -5,12 +5,7 @@ import { IdentityTypes, RequestLogicTypes, } from '@requestnetwork/types'; -import { - CurrencyManager, - ICurrencyManager, - NearChains, - isSameChain, -} from '@requestnetwork/currency'; +import { NearChains, isSameChain } from '@requestnetwork/currency'; import ContentData from './extensions/content-data'; import AddressBasedBtc from './extensions/payment-network/bitcoin/mainnet-address-based'; @@ -56,10 +51,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic erc20TransferableReceivable: Erc20TransferableReceivablePaymentNetwork; }; - constructor(currencyManager?: ICurrencyManager) { - if (!currencyManager) { - currencyManager = CurrencyManager.getDefault(); - } + constructor(currencyManager: CurrencyTypes.ICurrencyManager) { this.extensions = { addressBasedBtc: new AddressBasedBtc(), addressBasedErc20: new AddressBasedErc20(), diff --git a/packages/advanced-logic/src/extensions/payment-network/any-to-erc20-proxy.ts b/packages/advanced-logic/src/extensions/payment-network/any-to-erc20-proxy.ts index 5adf9eb140..0b04f8ca72 100644 --- a/packages/advanced-logic/src/extensions/payment-network/any-to-erc20-proxy.ts +++ b/packages/advanced-logic/src/extensions/payment-network/any-to-erc20-proxy.ts @@ -1,16 +1,12 @@ -import { - conversionSupportedNetworks, - ICurrencyManager, - UnsupportedCurrencyError, -} from '@requestnetwork/currency'; -import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { conversionSupportedNetworks, UnsupportedCurrencyError } from '@requestnetwork/currency'; +import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import Erc20FeeProxyPaymentNetwork from './erc20/fee-proxy-contract'; const CURRENT_VERSION = '0.1.0'; export default class AnyToErc20ProxyPaymentNetwork extends Erc20FeeProxyPaymentNetwork { public constructor( - private currencyManager: ICurrencyManager, + private currencyManager: CurrencyTypes.ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID .ANY_TO_ERC20_PROXY, currentVersion: string = CURRENT_VERSION, diff --git a/packages/advanced-logic/src/extensions/payment-network/any-to-eth-proxy.ts b/packages/advanced-logic/src/extensions/payment-network/any-to-eth-proxy.ts index 5917f6b5ff..eb2f192a62 100644 --- a/packages/advanced-logic/src/extensions/payment-network/any-to-eth-proxy.ts +++ b/packages/advanced-logic/src/extensions/payment-network/any-to-eth-proxy.ts @@ -1,11 +1,11 @@ -import { ICurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; -import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { UnsupportedCurrencyError } from '@requestnetwork/currency'; +import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import EthereumFeeProxyPaymentNetwork from './ethereum/fee-proxy-contract'; const CURRENT_VERSION = '0.2.0'; export default class AnyToEthProxyPaymentNetwork extends EthereumFeeProxyPaymentNetwork { - public constructor(private currencyManager: ICurrencyManager) { + public constructor(private currencyManager: CurrencyTypes.ICurrencyManager) { super(ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY, CURRENT_VERSION); } diff --git a/packages/advanced-logic/src/extensions/payment-network/near/any-to-near-testnet.ts b/packages/advanced-logic/src/extensions/payment-network/near/any-to-near-testnet.ts index 0c848ea398..07d90dfe77 100644 --- a/packages/advanced-logic/src/extensions/payment-network/near/any-to-near-testnet.ts +++ b/packages/advanced-logic/src/extensions/payment-network/near/any-to-near-testnet.ts @@ -1,8 +1,8 @@ -import { ICurrencyManager } from '@requestnetwork/currency'; +import { CurrencyTypes } from 'types/dist'; import AnyToNearPaymentNetwork from './any-to-near'; export default class AnyToNearTestnetPaymentNetwork extends AnyToNearPaymentNetwork { - public constructor(currencyManager: ICurrencyManager) { + public constructor(currencyManager: CurrencyTypes.ICurrencyManager) { // testnet PN version is the same as mainnet, can be overridden here if needed super(currencyManager, ['aurora-testnet', 'near-testnet']); } diff --git a/packages/advanced-logic/src/extensions/payment-network/near/any-to-near.ts b/packages/advanced-logic/src/extensions/payment-network/near/any-to-near.ts index 5b0b802067..221512d855 100644 --- a/packages/advanced-logic/src/extensions/payment-network/near/any-to-near.ts +++ b/packages/advanced-logic/src/extensions/payment-network/near/any-to-near.ts @@ -1,4 +1,4 @@ -import { ICurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; +import { UnsupportedCurrencyError } from '@requestnetwork/currency'; import { CurrencyTypes, ExtensionTypes, @@ -11,7 +11,7 @@ const CURRENT_VERSION = '0.1.0'; export default class AnyToNearPaymentNetwork extends AnyToNativeTokenPaymentNetwork { public constructor( - private currencyManager: ICurrencyManager, + private currencyManager: CurrencyTypes.ICurrencyManager, supportedNetworks: CurrencyTypes.NearChainName[] = [ 'aurora', // FIXME: enable near network support diff --git a/packages/advanced-logic/test/advanced-logic.test.ts b/packages/advanced-logic/test/advanced-logic.test.ts index 53cba4d377..0c3d12fe35 100644 --- a/packages/advanced-logic/test/advanced-logic.test.ts +++ b/packages/advanced-logic/test/advanced-logic.test.ts @@ -1,3 +1,4 @@ +import { CurrencyManager } from '@requestnetwork/currency'; import { AdvancedLogicTypes, ExtensionTypes } from '@requestnetwork/types'; import * as DataBTCCreate from './utils/payment-network/bitcoin/generator-data-create'; @@ -15,7 +16,7 @@ let advancedLogic: AdvancedLogicTypes.IAdvancedLogic; /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('advanced-logic.ts', () => { beforeEach(() => { - advancedLogic = new AdvancedLogic(); + advancedLogic = new AdvancedLogic(CurrencyManager.getDefault()); }); describe('applyActionToExtensions', () => { it('can applyActionToExtensions', () => { diff --git a/packages/advanced-logic/test/extensions/payment-network/any-to-near.test.ts b/packages/advanced-logic/test/extensions/payment-network/any-to-near.test.ts index 0fd2e16904..f838c01c1d 100644 --- a/packages/advanced-logic/test/extensions/payment-network/any-to-near.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/any-to-near.test.ts @@ -266,7 +266,7 @@ describe('extensions/payment-network/any-to-native-token', () => { let creationAction: ExtensionTypes.IAction; let anyToNearPn: AnyToNearPaymentNetwork; beforeEach(() => { - advancedLogic = new AdvancedLogic(); + advancedLogic = new AdvancedLogic(CurrencyManager.getDefault()); anyToNearPn = new AnyToNearPaymentNetwork(currencyManager); validRequestState = { ...requestStateNoExtensions, diff --git a/packages/advanced-logic/test/extensions/payment-network/erc20/fee-proxy-contract.test.ts b/packages/advanced-logic/test/extensions/payment-network/erc20/fee-proxy-contract.test.ts index 5469971600..2dddffe730 100644 --- a/packages/advanced-logic/test/extensions/payment-network/erc20/fee-proxy-contract.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/erc20/fee-proxy-contract.test.ts @@ -6,8 +6,10 @@ import * as DataNearERC20FeeCreate from '../../../utils/payment-network/erc20/ne import * as TestData from '../../../utils/test-data-generator'; import { deepCopy } from '@requestnetwork/utils'; import { AdvancedLogic } from '../../../../src'; +import { CurrencyManager } from '@requestnetwork/currency'; + +const advancedLogic = new AdvancedLogic(CurrencyManager.getDefault()); -const advancedLogic = new AdvancedLogic(); const erc20FeeProxyContract = advancedLogic.getFeeProxyContractErc20ForNetwork(); /* eslint-disable @typescript-eslint/no-unused-expressions */ diff --git a/packages/advanced-logic/test/extensions/payment-network/native-token.test.ts b/packages/advanced-logic/test/extensions/payment-network/native-token.test.ts index c84c6f01bd..e326dec3c0 100644 --- a/packages/advanced-logic/test/extensions/payment-network/native-token.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/native-token.test.ts @@ -1,3 +1,4 @@ +import { CurrencyManager } from '@requestnetwork/currency'; import NativeTokenPaymentNetwork from '../../../src/extensions/payment-network/native-token'; import NearNativePaymentNetwork from '../../../src/extensions/payment-network/near/near-native'; import { @@ -16,6 +17,8 @@ import NearTestnetNativeNativePaymentNetwork from '../../../src/extensions/payme const salt = arbitrarySalt; +const advancedLogic = new AdvancedLogic(CurrencyManager.getDefault()); + describe('extensions/payment-network/native-token', () => { const nearCurrency = { type: RequestLogicTypes.CURRENCY.ETH, @@ -166,8 +169,6 @@ describe('extensions/payment-network/native-token', () => { describe('AdvancedLogic.applyActionToExtension', () => { const mainnetTestCase = nativeTokenTestCases[0]; it('works with state and action on the same network', () => { - const advancedLogic = new AdvancedLogic(); - const requestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, currency: mainnetTestCase.currency, @@ -192,8 +193,6 @@ describe('extensions/payment-network/native-token', () => { expect(newExtensionState).toEqual(extensionStateWithNativeTokenPaymentAndRefund); }); it('works with an action without payment network', () => { - const advancedLogic = new AdvancedLogic(); - const requestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, currency: mainnetTestCase.currency, @@ -218,7 +217,6 @@ describe('extensions/payment-network/native-token', () => { expect(newExtensionState).toEqual(extensionStateWithNativeTokenPaymentAndRefund); }); it('works when adding a payment address to a created state', () => { - const advancedLogic = new AdvancedLogic(); const nearPn = new NearNativePaymentNetwork(); const requestState: typeof requestStateNoExtensions = { @@ -251,7 +249,6 @@ describe('extensions/payment-network/native-token', () => { expect(newExtensionState).toEqual(extensionStateWithPaymentAddressAdded); }); it('throws when creating the extension on a different network from the request network', () => { - const advancedLogic = new AdvancedLogic(); const nearPn = new NearTestnetNativeNativePaymentNetwork(); const requestState: typeof requestStateNoExtensions = { @@ -272,7 +269,6 @@ describe('extensions/payment-network/native-token', () => { ); }); it('throws when adding a payment address a different network', () => { - const advancedLogic = new AdvancedLogic(); const nearPn = new NearNativePaymentNetwork(); const requestState: typeof requestStateNoExtensions = { @@ -295,8 +291,6 @@ describe('extensions/payment-network/native-token', () => { }).toThrowError("paymentAddress 'pay.testnet' is not a valid address"); }); it('throws with no state or action payment network', () => { - const advancedLogic = new AdvancedLogic(); - const wrongNativeTokenRequestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, currency: { @@ -324,7 +318,6 @@ describe('extensions/payment-network/native-token', () => { ).toThrowError('extension with id: pn-native-token not found for network: undefined'); }); it('throws on a wrong payment network', () => { - const advancedLogic = new AdvancedLogic(); const wrongNetwork = `wrong network` as CurrencyTypes.EvmChainName; const wrongNativeTokenRequestState: typeof requestStateNoExtensions = { @@ -354,8 +347,6 @@ describe('extensions/payment-network/native-token', () => { ).toThrowError('extension with id: pn-native-token not found for network: wrong network'); }); it('throws on a different payment network', () => { - const advancedLogic = new AdvancedLogic(); - const requestState = { ...requestStateNoExtensions, currency: mainnetTestCase.currency, @@ -383,7 +374,6 @@ describe('extensions/payment-network/native-token', () => { }); it('keeps the version used at creation', () => { - const advancedLogic = new AdvancedLogic(); const requestState = { ...requestStateNoExtensions, currency: mainnetTestCase.currency, @@ -400,7 +390,6 @@ describe('extensions/payment-network/native-token', () => { it('requires a version at creation', () => { expect(() => { - const advancedLogic = new AdvancedLogic(); const requestState = { ...requestStateNoExtensions, currency: mainnetTestCase.currency, diff --git a/packages/currency/src/chains/ChainsAbstract.ts b/packages/currency/src/chains/ChainsAbstract.ts index d498baa959..dba788f096 100644 --- a/packages/currency/src/chains/ChainsAbstract.ts +++ b/packages/currency/src/chains/ChainsAbstract.ts @@ -1,10 +1,9 @@ -import { Chain, NamedNativeCurrency, TokenMap } from '../types'; import { CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types'; import { nativeCurrencies } from '../native'; export abstract class ChainsAbstract< CHAIN_NAME extends CurrencyTypes.ChainName, - CHAIN extends Chain, + CHAIN extends CurrencyTypes.Chain, CHAIN_ID extends string | number, > { public chains: Record; @@ -26,11 +25,11 @@ export abstract class ChainsAbstract< currencyType: RequestLogicTypes.CURRENCY.ETH | RequestLogicTypes.CURRENCY.BTC, ): void { this.chainNames.forEach((chainName) => { - const nativeCurrency = (nativeCurrencies[currencyType] as NamedNativeCurrency[]).find( - (currency) => currency.network === chainName, - ); + const nativeCurrency = ( + nativeCurrencies[currencyType] as CurrencyTypes.NamedNativeCurrency[] + ).find((currency) => currency.network === chainName); if (nativeCurrency) { - const chainCurrencies: TokenMap = this.chains[chainName].currencies || {}; + const chainCurrencies: CurrencyTypes.TokenMap = this.chains[chainName].currencies || {}; chainCurrencies.native = nativeCurrency; this.chains[chainName].currencies = chainCurrencies; } diff --git a/packages/currency/src/chains/btc/index.ts b/packages/currency/src/chains/btc/index.ts index 7c46a09b8e..4bd034af42 100644 --- a/packages/currency/src/chains/btc/index.ts +++ b/packages/currency/src/chains/btc/index.ts @@ -1,10 +1,9 @@ import { CurrencyTypes } from '@requestnetwork/types'; -import { Chain } from '../../types'; import * as MainnetDefinition from './data/mainnet'; import * as TestnetDefinition from './data/testnet'; -export type BtcChain = Chain & { +export type BtcChain = CurrencyTypes.Chain & { chainId: string; }; diff --git a/packages/currency/src/chains/declarative/index.ts b/packages/currency/src/chains/declarative/index.ts index 6e38952bd8..a83e9163e6 100644 --- a/packages/currency/src/chains/declarative/index.ts +++ b/packages/currency/src/chains/declarative/index.ts @@ -1,10 +1,9 @@ import { CurrencyTypes } from '@requestnetwork/types'; -import { Chain } from '../../types'; import * as TronDefinition from './data/tron'; import * as SolanaDefinition from './data/solana'; -export type DeclarativeChain = Chain; +export type DeclarativeChain = CurrencyTypes.Chain; export const chains: Record = { tron: TronDefinition, diff --git a/packages/currency/src/chains/evm/data/avalanche.ts b/packages/currency/src/chains/evm/data/avalanche.ts index 6bd1c9f5d6..fb9c52d376 100644 --- a/packages/currency/src/chains/evm/data/avalanche.ts +++ b/packages/currency/src/chains/evm/data/avalanche.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from 'types/dist'; import { supportedAvalancheERC20 } from '../../../erc20/chains/avalanche'; export const chainId = 43114; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedAvalancheERC20, }; diff --git a/packages/currency/src/chains/evm/data/bsc.ts b/packages/currency/src/chains/evm/data/bsc.ts index b13f8f522a..973efc8137 100644 --- a/packages/currency/src/chains/evm/data/bsc.ts +++ b/packages/currency/src/chains/evm/data/bsc.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from 'types/dist'; import { supportedBSCERC20 } from '../../../erc20/chains/bsc'; export const chainId = 56; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedBSCERC20, }; diff --git a/packages/currency/src/chains/evm/data/bsctest.ts b/packages/currency/src/chains/evm/data/bsctest.ts index 7fc2bc374e..3999b4ebd2 100644 --- a/packages/currency/src/chains/evm/data/bsctest.ts +++ b/packages/currency/src/chains/evm/data/bsctest.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedBSCTestERC20 } from '../../../erc20/chains/bsctest'; export const chainId = 97; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedBSCTestERC20, }; diff --git a/packages/currency/src/chains/evm/data/celo.ts b/packages/currency/src/chains/evm/data/celo.ts index f07c603cd4..136aa8dc08 100644 --- a/packages/currency/src/chains/evm/data/celo.ts +++ b/packages/currency/src/chains/evm/data/celo.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedCeloERC20 } from '../../../erc20/chains/celo'; export const chainId = 42220; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedCeloERC20, }; diff --git a/packages/currency/src/chains/evm/data/fantom.ts b/packages/currency/src/chains/evm/data/fantom.ts index 596a2db0db..ac68f6b4a2 100644 --- a/packages/currency/src/chains/evm/data/fantom.ts +++ b/packages/currency/src/chains/evm/data/fantom.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedFantomERC20 } from '../../../erc20/chains/fantom'; export const chainId = 250; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedFantomERC20, }; diff --git a/packages/currency/src/chains/evm/data/goerli.ts b/packages/currency/src/chains/evm/data/goerli.ts index 2f700bcfbd..e4fee26de4 100644 --- a/packages/currency/src/chains/evm/data/goerli.ts +++ b/packages/currency/src/chains/evm/data/goerli.ts @@ -1,8 +1,8 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedGoerliERC20 } from '../../../erc20/chains/goerli'; export const chainId = 5; export const testnet = true; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedGoerliERC20, }; diff --git a/packages/currency/src/chains/evm/data/mainnet.ts b/packages/currency/src/chains/evm/data/mainnet.ts index 4e6bb83ceb..2be9434314 100644 --- a/packages/currency/src/chains/evm/data/mainnet.ts +++ b/packages/currency/src/chains/evm/data/mainnet.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedMainnetERC20 } from '../../../erc20/chains/mainnet'; export const chainId = 1; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedMainnetERC20, }; diff --git a/packages/currency/src/chains/evm/data/matic.ts b/packages/currency/src/chains/evm/data/matic.ts index a7738d898c..71ca52ee11 100644 --- a/packages/currency/src/chains/evm/data/matic.ts +++ b/packages/currency/src/chains/evm/data/matic.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedMaticERC20 } from '../../../erc20/chains/matic'; export const chainId = 137; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedMaticERC20, }; diff --git a/packages/currency/src/chains/evm/data/moonbeam.ts b/packages/currency/src/chains/evm/data/moonbeam.ts index de586f0b7b..bbd10c93ed 100644 --- a/packages/currency/src/chains/evm/data/moonbeam.ts +++ b/packages/currency/src/chains/evm/data/moonbeam.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedMoonbeamERC20 } from '../../../erc20/chains/moonbeam'; export const chainId = 1284; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedMoonbeamERC20, }; diff --git a/packages/currency/src/chains/evm/data/optimism.ts b/packages/currency/src/chains/evm/data/optimism.ts index 447d2f2300..148505bc05 100644 --- a/packages/currency/src/chains/evm/data/optimism.ts +++ b/packages/currency/src/chains/evm/data/optimism.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedOptimismERC20 } from '../../../erc20/chains/optimism'; export const chainId = 10; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedOptimismERC20, }; diff --git a/packages/currency/src/chains/evm/data/rinkeby.ts b/packages/currency/src/chains/evm/data/rinkeby.ts index aa9da936d3..41892377fd 100644 --- a/packages/currency/src/chains/evm/data/rinkeby.ts +++ b/packages/currency/src/chains/evm/data/rinkeby.ts @@ -1,10 +1,10 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedRinkebyERC20 } from '../../../erc20/chains/rinkeby'; import { supportedRinkebyERC777 } from '../../../erc777/chains/rinkeby'; export const chainId = 4; export const testnet = true; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedRinkebyERC20, ...supportedRinkebyERC777, }; diff --git a/packages/currency/src/chains/evm/data/xdai.ts b/packages/currency/src/chains/evm/data/xdai.ts index edbf8ba0ce..9d32ec8489 100644 --- a/packages/currency/src/chains/evm/data/xdai.ts +++ b/packages/currency/src/chains/evm/data/xdai.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; import { supportedXDAIERC20 } from '../../../erc20/chains/xdai'; export const chainId = 100; -export const currencies: TokenMap = { +export const currencies: CurrencyTypes.TokenMap = { ...supportedXDAIERC20, }; diff --git a/packages/currency/src/chains/evm/index.ts b/packages/currency/src/chains/evm/index.ts index de90682169..b07d0cabc1 100644 --- a/packages/currency/src/chains/evm/index.ts +++ b/packages/currency/src/chains/evm/index.ts @@ -1,5 +1,4 @@ import { CurrencyTypes } from '@requestnetwork/types'; -import { Chain } from '../../types'; import * as AlfajoresDefinition from './data/alfajores'; import * as ArbitrumOneDefinition from './data/arbitrum-one'; @@ -26,7 +25,7 @@ import * as SokolDefinition from './data/sokol'; import * as TombchainDefinition from './data/tombchain'; import * as XDaiDefinition from './data/xdai'; -export type EvmChain = Chain & { +export type EvmChain = CurrencyTypes.Chain & { chainId: number; }; diff --git a/packages/currency/src/chains/near/index.ts b/packages/currency/src/chains/near/index.ts index 923cce384f..fed367a6ce 100644 --- a/packages/currency/src/chains/near/index.ts +++ b/packages/currency/src/chains/near/index.ts @@ -1,10 +1,9 @@ import { CurrencyTypes } from '@requestnetwork/types'; -import { Chain } from '../../types'; import * as NearDefinition from './data/near'; import * as NearTestnetDefinition from './data/near-testnet'; -export type NearChain = Chain; +export type NearChain = CurrencyTypes.Chain; export const chains: Record = { aurora: NearDefinition, diff --git a/packages/currency/src/conversion-aggregators.ts b/packages/currency/src/conversion-aggregators.ts index fb90a8d2af..6426bdebb7 100644 --- a/packages/currency/src/conversion-aggregators.ts +++ b/packages/currency/src/conversion-aggregators.ts @@ -1,5 +1,5 @@ import GRAPH from 'node-dijkstra'; -import { CurrencyDefinition } from './types'; +import { CurrencyTypes } from '@requestnetwork/types'; import privateAggregator from './aggregators/private.json'; import mainnetAggregator from './aggregators/mainnet.json'; @@ -10,24 +10,9 @@ import fantomAggregator from './aggregators/fantom.json'; import nearAggregator from './aggregators/near.json'; import nearTestnetAggregator from './aggregators/near-testnet.json'; import auroraTestnetAggregator from './aggregators/aurora-testnet.json'; -import { CurrencyTypes } from '@requestnetwork/types'; - -/** - * currencyFrom => currencyTo => cost - */ -export type CurrencyPairs = Record>; - -/** - * Aggregators maps define pairs of currencies for which an onchain oracle exists, by network. - * - * Network => currencyFrom => currencyTo => cost - */ -export type AggregatorsMap = Partial< - Record ->; // Pairs supported by Chainlink (can be generated from requestNetwork/toolbox/src/chainlinkConversionPathTools.ts) -const chainlinkCurrencyPairs: AggregatorsMap = { +const chainlinkCurrencyPairs: CurrencyTypes.AggregatorsMap = { private: privateAggregator, goerli: goerliAggregator, rinkeby: rinkebyAggregator, @@ -37,7 +22,7 @@ const chainlinkCurrencyPairs: AggregatorsMap = { }; // Pairs supported by Flux Protocol -const fluxCurrencyPairs: AggregatorsMap = { +const fluxCurrencyPairs: CurrencyTypes.AggregatorsMap = { aurora: nearAggregator, 'aurora-testnet': auroraTestnetAggregator, 'near-testnet': nearTestnetAggregator, @@ -45,7 +30,7 @@ const fluxCurrencyPairs: AggregatorsMap = { // FIX ME: This fix enables to get these networks registered in conversionSupportedNetworks. // Could be improved by removing the supported network check from the protocol -const noConversionNetworks: AggregatorsMap = { +const noConversionNetworks: CurrencyTypes.AggregatorsMap = { 'arbitrum-rinkeby': {}, 'arbitrum-one': {}, xdai: {}, @@ -59,7 +44,7 @@ const noConversionNetworks: AggregatorsMap = { * Conversion paths per network used by default if no other path given to the Currency Manager. * Must be updated every time an aggregator is added to one network. */ -export const defaultConversionPairs: AggregatorsMap = { +export const defaultConversionPairs: CurrencyTypes.AggregatorsMap = { ...chainlinkCurrencyPairs, ...fluxCurrencyPairs, ...noConversionNetworks, @@ -80,8 +65,8 @@ export const conversionSupportedNetworks = Object.keys( * @returns conversion path */ export function getPath( - currencyFrom: Pick, - currencyTo: Pick, + currencyFrom: Pick, + currencyTo: Pick, network: CurrencyTypes.ChainName = 'mainnet', pairs = defaultConversionPairs, ): string[] | null { diff --git a/packages/currency/src/currency-utils.ts b/packages/currency/src/currency-utils.ts index 9da5ca87b0..0464b9d4bc 100644 --- a/packages/currency/src/currency-utils.ts +++ b/packages/currency/src/currency-utils.ts @@ -1,11 +1,4 @@ -import { - CurrencyInput, - ERC20CurrencyInput, - ERC777CurrencyInput, - ISO4217CurrencyInput, - NativeCurrencyInput, -} from './types'; -import { RequestLogicTypes } from '@requestnetwork/types'; +import { CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types'; /** * Checks if a Near address is valid according to a currency network. @@ -48,21 +41,29 @@ export const isValidNearAddress = (address: string, network?: string): boolean = * Enable filtering per currency type */ -export const isNativeCurrency = (currency: CurrencyInput): currency is NativeCurrencyInput => { +export const isNativeCurrency = ( + currency: CurrencyTypes.CurrencyInput, +): currency is CurrencyTypes.NativeCurrencyInput => { return ( currency.type === RequestLogicTypes.CURRENCY.BTC || currency.type === RequestLogicTypes.CURRENCY.ETH ); }; -export const isISO4217Currency = (currency: CurrencyInput): currency is ISO4217CurrencyInput => { +export const isISO4217Currency = ( + currency: CurrencyTypes.CurrencyInput, +): currency is CurrencyTypes.ISO4217CurrencyInput => { return currency.type === RequestLogicTypes.CURRENCY.ISO4217; }; -export const isERC20Currency = (currency: CurrencyInput): currency is ERC20CurrencyInput => { +export const isERC20Currency = ( + currency: CurrencyTypes.CurrencyInput, +): currency is CurrencyTypes.ERC20CurrencyInput => { return currency.type === RequestLogicTypes.CURRENCY.ERC20; }; -export const isERC777Currency = (currency: CurrencyInput): currency is ERC777CurrencyInput => { +export const isERC777Currency = ( + currency: CurrencyTypes.CurrencyInput, +): currency is CurrencyTypes.ERC777CurrencyInput => { return currency.type === RequestLogicTypes.CURRENCY.ERC777; }; diff --git a/packages/currency/src/currencyManager.ts b/packages/currency/src/currencyManager.ts index 8afdba3b4e..3b7b8119f3 100644 --- a/packages/currency/src/currencyManager.ts +++ b/packages/currency/src/currencyManager.ts @@ -6,16 +6,7 @@ import { getSupportedERC777Tokens } from './erc777'; import { getHash } from './getHash'; import iso4217 from './iso4217'; import { nativeCurrencies } from './native'; -import { - StorageCurrency, - CurrencyDefinition, - CurrencyInput, - ERC20Currency, - ICurrencyManager, - LegacyTokenMap, - NativeCurrencyType, -} from './types'; -import { defaultConversionPairs, AggregatorsMap, getPath } from './conversion-aggregators'; +import { defaultConversionPairs, getPath } from './conversion-aggregators'; import { isValidNearAddress } from './currency-utils'; import { NearChains } from './chains'; @@ -24,10 +15,10 @@ const { BTC, ERC20, ERC777, ETH, ISO4217 } = RequestLogicTypes.CURRENCY; /** * Handles a list of currencies and provide features to retrieve them, as well as convert to/from storage format */ -export class CurrencyManager implements ICurrencyManager { - private readonly knownCurrencies: CurrencyDefinition[]; - private readonly legacyTokens: LegacyTokenMap; - private readonly conversionPairs: AggregatorsMap; +export class CurrencyManager implements CurrencyTypes.ICurrencyManager { + private readonly knownCurrencies: CurrencyTypes.CurrencyDefinition[]; + private readonly legacyTokens: CurrencyTypes.LegacyTokenMap; + private readonly conversionPairs: CurrencyTypes.AggregatorsMap; /** * @@ -36,9 +27,9 @@ export class CurrencyManager implements ICurrencyManager * @param conversionPairs A mapping of possible conversions by network (network => currencyFrom => currencyTo => cost) */ constructor( - inputCurrencies: (CurrencyInput & { id?: string; meta?: TMeta })[], - legacyTokens?: LegacyTokenMap, - conversionPairs?: AggregatorsMap, + inputCurrencies: (CurrencyTypes.CurrencyInput & { id?: string; meta?: TMeta })[], + legacyTokens?: CurrencyTypes.LegacyTokenMap, + conversionPairs?: CurrencyTypes.AggregatorsMap, ) { this.knownCurrencies = []; for (const input of inputCurrencies) { @@ -61,7 +52,7 @@ export class CurrencyManager implements ICurrencyManager from( currencyIdentifier: string | undefined, network?: CurrencyTypes.ChainName, - ): CurrencyDefinition | undefined { + ): CurrencyTypes.CurrencyDefinition | undefined { if (!currencyIdentifier) { return; } @@ -83,9 +74,9 @@ export class CurrencyManager implements ICurrencyManager } /** - * Gets a supported currency from its CurrencyDefinition id + * Gets a supported currency from its CurrencyTypes.CurrencyDefinition id */ - fromId(id: string): CurrencyDefinition | undefined { + fromId(id: string): CurrencyTypes.CurrencyDefinition | undefined { return this.knownCurrencies.find((knownCurrency) => knownCurrency.id === id); } @@ -93,7 +84,10 @@ export class CurrencyManager implements ICurrencyManager * Gets a supported currency from its address and network. * If more than one currency are found, undefined is returned */ - fromAddress(address: string, network?: string): CurrencyDefinition | undefined { + fromAddress( + address: string, + network?: string, + ): CurrencyTypes.CurrencyDefinition | undefined { address = utils.getAddress(address); const matches = this.knownCurrencies.filter( (x) => @@ -102,7 +96,7 @@ export class CurrencyManager implements ICurrencyManager (!network || x.network === network), ); if (matches.length > 1) { - const networks = matches.map((x) => (x as ERC20Currency).network).join(', '); + const networks = matches.map((x) => (x as CurrencyTypes.ERC20Currency).network).join(', '); console.warn( `${address} has several matches on ${networks}. To avoid errors, specify a network.`, ); @@ -117,7 +111,7 @@ export class CurrencyManager implements ICurrencyManager fromSymbol( symbol: string, network?: CurrencyTypes.ChainName, - ): CurrencyDefinition | undefined { + ): CurrencyTypes.CurrencyDefinition | undefined { symbol = symbol?.toUpperCase(); network = network?.toLowerCase() as CurrencyTypes.ChainName | undefined; @@ -133,7 +127,7 @@ export class CurrencyManager implements ICurrencyManager ); } - fromHash(hash: string, network?: string): CurrencyDefinition | undefined { + fromHash(hash: string, network?: string): CurrencyTypes.CurrencyDefinition | undefined { return this.knownCurrencies.find( (x) => x.hash.toLowerCase() === hash.toLowerCase() && @@ -143,7 +137,9 @@ export class CurrencyManager implements ICurrencyManager /** * Retrieves a currency given its storage format (ICurrency) */ - fromStorageCurrency(currency: StorageCurrency): CurrencyDefinition | undefined { + fromStorageCurrency( + currency: CurrencyTypes.StorageCurrency, + ): CurrencyTypes.CurrencyDefinition | undefined { if (!currency) { return; } @@ -166,15 +162,15 @@ export class CurrencyManager implements ICurrencyManager * Retrieves a Native currency for a type and network */ getNativeCurrency( - type: NativeCurrencyType, + type: CurrencyTypes.NativeCurrencyType, network: string, - ): CurrencyDefinition | undefined { + ): CurrencyTypes.CurrencyDefinition | undefined { return this.knownCurrencies.find((x) => x.type === type && x.network === network); } getConversionPath( - from: Pick, - to: Pick, + from: Pick, + to: Pick, network: CurrencyTypes.ChainName, ): string[] | null { try { @@ -185,21 +181,25 @@ export class CurrencyManager implements ICurrencyManager } supportsConversion( - currency: Pick, + currency: Pick, network: CurrencyTypes.ChainName, ): boolean { return !!this.conversionPairs[network]?.[currency.hash.toLowerCase()]; } /** - * Adds computed parameters to a CurrencyInput + * Adds computed parameters to a CurrencyTypes.CurrencyInput */ static fromInput({ id, hash, meta, ...input - }: CurrencyInput & { id?: string; hash?: string; meta?: TMeta }): CurrencyDefinition { + }: CurrencyTypes.CurrencyInput & { + id?: string; + hash?: string; + meta?: TMeta; + }): CurrencyTypes.CurrencyDefinition { if ('address' in input) { if (input.address.startsWith('0x') && input.address.length === 42) { input.address = utils.getAddress(input.address); @@ -216,14 +216,14 @@ export class CurrencyManager implements ICurrencyManager /** * Utility function to compute the unique identifier */ - static currencyId(currency: CurrencyInput): string { + static currencyId(currency: CurrencyTypes.CurrencyInput): string { return 'network' in currency ? `${currency.symbol}-${currency.network}` : currency.symbol; } /** * Converts a currency to the storage format (ICurrency) */ - static toStorageCurrency(currency: CurrencyInput): StorageCurrency { + static toStorageCurrency(currency: CurrencyTypes.CurrencyInput): CurrencyTypes.StorageCurrency { return { type: currency.type, value: @@ -236,7 +236,10 @@ export class CurrencyManager implements ICurrencyManager * Validates an address for a given currency. * Throws if the currency is an ISO4217 currency. */ - validateAddress(address: string, currency: CurrencyInput | StorageCurrency): boolean { + validateAddress( + address: string, + currency: CurrencyTypes.CurrencyInput | CurrencyTypes.StorageCurrency, + ): boolean { if (currency.type === RequestLogicTypes.CURRENCY.ISO4217) { throw new Error(`Could not validate an address for an ISO4217 currency`); } @@ -264,7 +267,7 @@ export class CurrencyManager implements ICurrencyManager /** * Validate the correctness of a Storage Currency */ - validateCurrency(currency: StorageCurrency): boolean { + validateCurrency(currency: CurrencyTypes.StorageCurrency): boolean { if ( currency.type === RequestLogicTypes.CURRENCY.ISO4217 || currency.type === RequestLogicTypes.CURRENCY.ETH || @@ -284,22 +287,34 @@ export class CurrencyManager implements ICurrencyManager * - NEAR, YEL, ZIL, BTC * - ETH-rinkeby, FAU-rinkeby, CTBK-rinkeby */ - static getDefaultList(): CurrencyDefinition[] { - const isoCurrencies: CurrencyInput[] = iso4217.map((cc) => ({ + static getDefaultList(): CurrencyTypes.CurrencyDefinition[] { + const isoCurrencies: CurrencyTypes.CurrencyInput[] = iso4217.map((cc) => ({ decimals: cc.digits, name: cc.currency, symbol: cc.code, type: ISO4217, })); - const eth: CurrencyInput[] = nativeCurrencies.ETH.map((x) => ({ ...x, type: ETH })); - const btc: CurrencyInput[] = nativeCurrencies.BTC.map((x) => ({ ...x, type: BTC })); + const eth: CurrencyTypes.CurrencyInput[] = nativeCurrencies.ETH.map((x) => ({ + ...x, + type: ETH, + })); + const btc: CurrencyTypes.CurrencyInput[] = nativeCurrencies.BTC.map((x) => ({ + ...x, + type: BTC, + })); const erc20Tokens = getSupportedERC20Tokens(); - const erc20Currencies: CurrencyInput[] = erc20Tokens.map((x) => ({ ...x, type: ERC20 })); + const erc20Currencies: CurrencyTypes.CurrencyInput[] = erc20Tokens.map((x) => ({ + ...x, + type: ERC20, + })); const erc777Tokens = getSupportedERC777Tokens(); - const erc777Currencies: CurrencyInput[] = erc777Tokens.map((x) => ({ ...x, type: ERC777 })); + const erc777Currencies: CurrencyTypes.CurrencyInput[] = erc777Tokens.map((x) => ({ + ...x, + type: ERC777, + })); return isoCurrencies .concat(erc20Currencies) @@ -312,7 +327,7 @@ export class CurrencyManager implements ICurrencyManager /** * Returns the default list of legacy names (for symbol or network) */ - static getDefaultLegacyTokens(): LegacyTokenMap { + static getDefaultLegacyTokens(): CurrencyTypes.LegacyTokenMap { return { near: { NEAR: ['NEAR', 'aurora'], @@ -320,7 +335,7 @@ export class CurrencyManager implements ICurrencyManager }; } - static getDefaultConversionPairs(): AggregatorsMap { + static getDefaultConversionPairs(): CurrencyTypes.AggregatorsMap { return defaultConversionPairs; } diff --git a/packages/currency/src/erc20/chains/avalanche.ts b/packages/currency/src/erc20/chains/avalanche.ts index b9ca4bd490..3b01b8688c 100644 --- a/packages/currency/src/erc20/chains/avalanche.ts +++ b/packages/currency/src/erc20/chains/avalanche.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported bsc network tokens -export const supportedAvalancheERC20: TokenMap = { +export const supportedAvalancheERC20: CurrencyTypes.TokenMap = { '0x9fB1d52596c44603198fB0aee434fac3a679f702': { name: 'Jarvis Synthetic Euro', symbol: 'jEUR', diff --git a/packages/currency/src/erc20/chains/bsc.ts b/packages/currency/src/erc20/chains/bsc.ts index c423a1003d..2344cc7138 100644 --- a/packages/currency/src/erc20/chains/bsc.ts +++ b/packages/currency/src/erc20/chains/bsc.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported bsc network tokens -export const supportedBSCERC20: TokenMap = { +export const supportedBSCERC20: CurrencyTypes.TokenMap = { '0x1AF3F329e8BE154074D8769D1FFa4eE058B1DBc3': { name: 'Binance-Peg Dai Token', symbol: 'DAI', diff --git a/packages/currency/src/erc20/chains/bsctest.ts b/packages/currency/src/erc20/chains/bsctest.ts index e5dd428752..766a5dbf30 100644 --- a/packages/currency/src/erc20/chains/bsctest.ts +++ b/packages/currency/src/erc20/chains/bsctest.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported bsctest network tokens -export const supportedBSCTestERC20: TokenMap = { +export const supportedBSCTestERC20: CurrencyTypes.TokenMap = { '0xEC5dCb5Dbf4B114C9d0F65BcCAb49EC54F6A0867': { name: 'DAI Token', symbol: 'DAI', diff --git a/packages/currency/src/erc20/chains/celo.ts b/packages/currency/src/erc20/chains/celo.ts index 9e56e7882f..aa3be99dfa 100644 --- a/packages/currency/src/erc20/chains/celo.ts +++ b/packages/currency/src/erc20/chains/celo.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported celo network tokens -export const supportedCeloERC20: TokenMap = { +export const supportedCeloERC20: CurrencyTypes.TokenMap = { // https://explorer.celo.org/address/0x765de816845861e75a25fca122bb6898b8b1282a/read_contract '0x765DE816845861e75A25fCA122bb6898B8B1282a': { // FIXME: should be cUSD, need to work on the retrocompatibility diff --git a/packages/currency/src/erc20/chains/fantom.ts b/packages/currency/src/erc20/chains/fantom.ts index 33c47b9b79..e83d66a31c 100644 --- a/packages/currency/src/erc20/chains/fantom.ts +++ b/packages/currency/src/erc20/chains/fantom.ts @@ -1,6 +1,6 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; -export const supportedFantomERC20: TokenMap = { +export const supportedFantomERC20: CurrencyTypes.TokenMap = { '0xD3b71117E6C1558c1553305b44988cd944e97300': { name: 'YEL Token', symbol: 'YEL', diff --git a/packages/currency/src/erc20/chains/goerli.ts b/packages/currency/src/erc20/chains/goerli.ts index 68596661dd..dedf62dcd2 100644 --- a/packages/currency/src/erc20/chains/goerli.ts +++ b/packages/currency/src/erc20/chains/goerli.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported goerli ERC20 tokens -export const supportedGoerliERC20: TokenMap = { +export const supportedGoerliERC20: CurrencyTypes.TokenMap = { // Faucet Token on goerli network. '0xBA62BCfcAaFc6622853cca2BE6Ac7d845BC0f2Dc': { decimals: 18, diff --git a/packages/currency/src/erc20/chains/index.ts b/packages/currency/src/erc20/chains/index.ts index a87dc6c8f4..f3bf5fcec2 100644 --- a/packages/currency/src/erc20/chains/index.ts +++ b/packages/currency/src/erc20/chains/index.ts @@ -1,4 +1,3 @@ -import { TokenMap } from '../../types'; import { CurrencyTypes } from '@requestnetwork/types'; import { supportedAvalancheERC20 } from './avalanche'; @@ -14,7 +13,9 @@ import { supportedOptimismERC20 } from './optimism'; import { supportedRinkebyERC20 } from './rinkeby'; import { supportedXDAIERC20 } from './xdai'; -export const supportedNetworks: Partial> = { +export const supportedNetworks: Partial< + Record +> = { celo: supportedCeloERC20, // FIXME: Rinkeby is deprecated rinkeby: supportedRinkebyERC20, diff --git a/packages/currency/src/erc20/chains/mainnet.ts b/packages/currency/src/erc20/chains/mainnet.ts index 4026029f12..762091c181 100644 --- a/packages/currency/src/erc20/chains/mainnet.ts +++ b/packages/currency/src/erc20/chains/mainnet.ts @@ -1,5 +1,5 @@ import * as metamaskContractMap from '@metamask/contract-metadata'; -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // These interfaces are declared here because they should be used only in this context // A Token description from the eth-contract-metadata list @@ -74,7 +74,7 @@ export const extraERC20Tokens = { }; // Merge metamask contracts list with our own -export const supportedMainnetERC20: TokenMap = { +export const supportedMainnetERC20: CurrencyTypes.TokenMap = { ...Object.entries(metamaskContractMap as IMetamaskTokenMap) .filter(([, val]) => val.erc20) .reduce( diff --git a/packages/currency/src/erc20/chains/matic.ts b/packages/currency/src/erc20/chains/matic.ts index a3b8a01a7a..29bbbe3059 100644 --- a/packages/currency/src/erc20/chains/matic.ts +++ b/packages/currency/src/erc20/chains/matic.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported matic network tokens -export const supportedMaticERC20: TokenMap = { +export const supportedMaticERC20: CurrencyTypes.TokenMap = { '0x8f3Cf7ad23Cd3CaDbD9735AFf958023239c6A063': { name: '(PoS) Dai Stablecoin', symbol: 'DAI', diff --git a/packages/currency/src/erc20/chains/moonbeam.ts b/packages/currency/src/erc20/chains/moonbeam.ts index 12aca53a59..06e6e562ef 100644 --- a/packages/currency/src/erc20/chains/moonbeam.ts +++ b/packages/currency/src/erc20/chains/moonbeam.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported bsc network tokens -export const supportedMoonbeamERC20: TokenMap = { +export const supportedMoonbeamERC20: CurrencyTypes.TokenMap = { '0x818ec0A7Fe18Ff94269904fCED6AE3DaE6d6dC0b': { name: 'USDC Multichain', symbol: 'USDC-multichain', diff --git a/packages/currency/src/erc20/chains/optimism.ts b/packages/currency/src/erc20/chains/optimism.ts index 439c079c8c..9a2677cbae 100644 --- a/packages/currency/src/erc20/chains/optimism.ts +++ b/packages/currency/src/erc20/chains/optimism.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported bsc network tokens -export const supportedOptimismERC20: TokenMap = { +export const supportedOptimismERC20: CurrencyTypes.TokenMap = { '0x7F5c764cBc14f9669B88837ca1490cCa17c31607': { name: 'USD Coin', symbol: 'USDC', diff --git a/packages/currency/src/erc20/chains/rinkeby.ts b/packages/currency/src/erc20/chains/rinkeby.ts index 8540c0faab..2eff4a66cc 100644 --- a/packages/currency/src/erc20/chains/rinkeby.ts +++ b/packages/currency/src/erc20/chains/rinkeby.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported rinkeby ERC20 tokens -export const supportedRinkebyERC20: TokenMap = { +export const supportedRinkebyERC20: CurrencyTypes.TokenMap = { // Request Central Bank token, used for testing on rinkeby. '0x995d6A8C21F24be1Dd04E105DD0d83758343E258': { decimals: 18, diff --git a/packages/currency/src/erc20/chains/xdai.ts b/packages/currency/src/erc20/chains/xdai.ts index 2dbbbb4114..8268436a0a 100644 --- a/packages/currency/src/erc20/chains/xdai.ts +++ b/packages/currency/src/erc20/chains/xdai.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported bsc network tokens -export const supportedXDAIERC20: TokenMap = { +export const supportedXDAIERC20: CurrencyTypes.TokenMap = { '0xDDAfbb505ad214D7b80b1f830fcCc89B60fb7A83': { name: 'USDC token on xDai', symbol: 'USDC', diff --git a/packages/currency/src/erc20/index.ts b/packages/currency/src/erc20/index.ts index 9a8bc5b7a3..aa0acc4155 100644 --- a/packages/currency/src/erc20/index.ts +++ b/packages/currency/src/erc20/index.ts @@ -1,4 +1,3 @@ -import { ERC20Currency, TokenMap } from '../types'; import { supportedNetworks } from './chains'; import { CurrencyTypes } from '@requestnetwork/types'; @@ -7,19 +6,18 @@ import { CurrencyTypes } from '@requestnetwork/types'; * * @returns List of supported ERC20 tokens */ -export function getSupportedERC20Tokens(): ERC20Currency[] { - return (Object.entries(supportedNetworks) as [CurrencyTypes.EvmChainName, TokenMap][]).reduce( - (acc: ERC20Currency[], [networkName, supportedCurrencies]) => { - return [ - ...acc, - ...Object.entries(supportedCurrencies).map(([address, token]) => ({ - address, - network: networkName, - decimals: token.decimals, - symbol: token.symbol, - })), - ]; - }, - [], - ); +export function getSupportedERC20Tokens(): CurrencyTypes.ERC20Currency[] { + return ( + Object.entries(supportedNetworks) as [CurrencyTypes.EvmChainName, CurrencyTypes.TokenMap][] + ).reduce((acc: CurrencyTypes.ERC20Currency[], [networkName, supportedCurrencies]) => { + return [ + ...acc, + ...Object.entries(supportedCurrencies).map(([address, token]) => ({ + address, + network: networkName, + decimals: token.decimals, + symbol: token.symbol, + })), + ]; + }, []); } diff --git a/packages/currency/src/erc777/chains/index.ts b/packages/currency/src/erc777/chains/index.ts index 05d09408bb..88e78b2a78 100644 --- a/packages/currency/src/erc777/chains/index.ts +++ b/packages/currency/src/erc777/chains/index.ts @@ -1,7 +1,8 @@ import { supportedRinkebyERC777 } from './rinkeby'; -import { TokenMap } from '../../types'; import { CurrencyTypes } from '@requestnetwork/types'; -export const supportedNetworks: Partial> = { +export const supportedNetworks: Partial< + Record +> = { rinkeby: supportedRinkebyERC777, }; diff --git a/packages/currency/src/erc777/chains/rinkeby.ts b/packages/currency/src/erc777/chains/rinkeby.ts index 407297a0ea..6b3b2e9df7 100644 --- a/packages/currency/src/erc777/chains/rinkeby.ts +++ b/packages/currency/src/erc777/chains/rinkeby.ts @@ -1,7 +1,7 @@ -import { TokenMap } from '../../types'; +import { CurrencyTypes } from '@requestnetwork/types'; // List of the supported rinkeby ERC777 tokens -export const supportedRinkebyERC777: TokenMap = { +export const supportedRinkebyERC777: CurrencyTypes.TokenMap = { // Faucet Token on rinkeby network. Easy to use on tests. '0x745861AeD1EEe363b4AaA5F1994Be40b1e05Ff90': { decimals: 18, diff --git a/packages/currency/src/erc777/index.ts b/packages/currency/src/erc777/index.ts index 36e2f5f53a..f3dde1a83b 100644 --- a/packages/currency/src/erc777/index.ts +++ b/packages/currency/src/erc777/index.ts @@ -1,4 +1,3 @@ -import { ERC777Currency, TokenMap } from '../types'; import { supportedNetworks } from './chains'; import { CurrencyTypes } from '@requestnetwork/types'; @@ -7,19 +6,18 @@ import { CurrencyTypes } from '@requestnetwork/types'; * * @returns List of supported ERC777 tokens */ -export function getSupportedERC777Tokens(): ERC777Currency[] { - return (Object.entries(supportedNetworks) as [CurrencyTypes.EvmChainName, TokenMap][]).reduce( - (acc: ERC777Currency[], [networkName, supportedCurrencies]) => { - return [ - ...acc, - ...Object.entries(supportedCurrencies).map(([address, token]) => ({ - address, - network: networkName, - decimals: token.decimals, - symbol: token.symbol, - })), - ]; - }, - [], - ); +export function getSupportedERC777Tokens(): CurrencyTypes.ERC777Currency[] { + return ( + Object.entries(supportedNetworks) as [CurrencyTypes.EvmChainName, CurrencyTypes.TokenMap][] + ).reduce((acc: CurrencyTypes.ERC777Currency[], [networkName, supportedCurrencies]) => { + return [ + ...acc, + ...Object.entries(supportedCurrencies).map(([address, token]) => ({ + address, + network: networkName, + decimals: token.decimals, + symbol: token.symbol, + })), + ]; + }, []); } diff --git a/packages/currency/src/index.ts b/packages/currency/src/index.ts index e3f77834d2..986d1daac4 100644 --- a/packages/currency/src/index.ts +++ b/packages/currency/src/index.ts @@ -1,13 +1,8 @@ export * from './chains'; export { getSupportedERC20Tokens } from './erc20'; export { getSupportedERC777Tokens } from './erc777'; -export { - conversionSupportedNetworks, - CurrencyPairs, - AggregatorsMap, -} from './conversion-aggregators'; +export { conversionSupportedNetworks } from './conversion-aggregators'; export { getHash as getCurrencyHash } from './getHash'; export { CurrencyManager } from './currencyManager'; -export * from './types'; export * from './errors'; export * from './currency-utils'; diff --git a/packages/currency/src/native.ts b/packages/currency/src/native.ts index d14ea7b9ad..d13e8463bc 100644 --- a/packages/currency/src/native.ts +++ b/packages/currency/src/native.ts @@ -1,10 +1,11 @@ import { CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types'; -import { NamedNativeCurrency } from './types'; -type NativeEthCurrency = NamedNativeCurrency & { +type NativeEthCurrency = CurrencyTypes.NamedNativeCurrency & { network: CurrencyTypes.EvmChainName | CurrencyTypes.NearChainName; }; -type NativeBtcCurrency = NamedNativeCurrency & { network: CurrencyTypes.BtcChainName }; +type NativeBtcCurrency = CurrencyTypes.NamedNativeCurrency & { + network: CurrencyTypes.BtcChainName; +}; export const nativeCurrencies: Record & Record = { diff --git a/packages/currency/src/types.ts b/packages/currency/src/types.ts deleted file mode 100644 index 0cc4bc2bfc..0000000000 --- a/packages/currency/src/types.ts +++ /dev/null @@ -1,144 +0,0 @@ -import { CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types'; - -/** - * Common types used in token configuration files - */ -type TokenAddress = string; -type TokenDefinition = { name: string; symbol: string; decimals: number }; -export type TokenMap = Record; - -/** - * Common types used in chain configuration files - */ -export type Chain = { - chainId: number | string; - testnet?: boolean; - currencies?: TokenMap; -}; - -/** - * A native blockchain token (ETH, MATIC, ETH-rinkeby...) - */ -export type NativeCurrency = { - symbol: string; - decimals: number; - network: CurrencyTypes.ChainName; -}; -type NamedCurrency = { name: string }; -export type NamedNativeCurrency = NativeCurrency & NamedCurrency; - -/** Native Currency types */ -export type NativeCurrencyType = RequestLogicTypes.CURRENCY.BTC | RequestLogicTypes.CURRENCY.ETH; - -/** - * A Fiat currency (EUR, USD...) - */ -export type ISO4217Currency = { - symbol: string; - decimals: number; -}; - -/** - * An ERC20 token (DAI, USDT...) - */ -export type ERC20Currency = { - symbol: string; - decimals: number; - network: - | CurrencyTypes.EvmChainName - | CurrencyTypes.NearChainName - | CurrencyTypes.DeclarativeChainName; - address: string; -}; - -/** - * An ERC777 SuperToken (DAIx, USDCx...) - */ -export type ERC777Currency = { - symbol: string; - decimals: number; - network: CurrencyTypes.EvmChainName; - address: string; -}; - -/** - * The minimum properties of a native Currency - */ -export type NativeCurrencyInput = { - type: RequestLogicTypes.CURRENCY.ETH | RequestLogicTypes.CURRENCY.BTC; -} & NativeCurrency; - -/** - * The minimum properties of an ISO4217 Currency - */ -export type ISO4217CurrencyInput = { - type: RequestLogicTypes.CURRENCY.ISO4217; -} & ISO4217Currency; - -/** - * The minimum properties of an ERC20 Currency - */ -export type ERC20CurrencyInput = { - type: RequestLogicTypes.CURRENCY.ERC20; -} & ERC20Currency; - -/** - * The minimum properties of an ERC777 Currency - */ -export type ERC777CurrencyInput = { - type: RequestLogicTypes.CURRENCY.ERC777; -} & ERC777Currency; - -/** - * The minimum properties of a Currency - */ -export type CurrencyInput = - | NativeCurrencyInput - | ISO4217CurrencyInput - | ERC20CurrencyInput - | ERC777CurrencyInput; - -/** - * The description of Currency, its core properties and some computed properties. - * `meta` enables applications to add any metadata they need to a Currency - */ -export type CurrencyDefinition = CurrencyInput & { - id: string; - hash: string; - meta: TMeta; -}; - -/** - * Alias for ICurrency for clarity in the context - */ -export type StorageCurrency = RequestLogicTypes.ICurrency; - -/** - * A Currency manager handles a list of currencies and provides utility to retrieve and change format - */ -export interface ICurrencyManager { - from(symbolOrAddress: string, network?: string): CurrencyDefinition | undefined; - fromAddress(address: string, network?: string): CurrencyDefinition | undefined; - fromSymbol(symbol: string, network?: string): CurrencyDefinition | undefined; - fromHash(hash: string, network?: string): CurrencyDefinition | undefined; - fromStorageCurrency(currency: StorageCurrency): CurrencyDefinition | undefined; - getNativeCurrency( - type: NativeCurrencyType, - network: string, - ): CurrencyDefinition | undefined; - getConversionPath( - from: Pick, - to: Pick, - network: string, - ): string[] | null; - supportsConversion(currency: Pick, network: string): boolean; - validateAddress(address: string, currency: CurrencyInput | StorageCurrency): boolean; - validateCurrency(currency: StorageCurrency): boolean; -} - -/** - * A mapping from old to new name for a given currency. - * - * Format { "chainName": {"TOKEN": ["NEW_TOKEN","NEW_CHAIN"]}} - */ -export type LegacyTokenMap = Record>; diff --git a/packages/integration-test/test/layers.test.ts b/packages/integration-test/test/layers.test.ts index 1d9204ccd5..31b825eebb 100644 --- a/packages/integration-test/test/layers.test.ts +++ b/packages/integration-test/test/layers.test.ts @@ -1,5 +1,5 @@ import { getCurrentTimestampInSecond } from '@requestnetwork/utils'; - +import { CurrencyManager } from '@requestnetwork/currency'; import { AdvancedLogic } from '@requestnetwork/advanced-logic'; import { EthereumPrivateKeyDecryptionProvider } from '@requestnetwork/epk-decryption'; import { EthereumPrivateKeySignatureProvider } from '@requestnetwork/epk-signature'; @@ -129,7 +129,7 @@ describe('Request system', () => { const transactionManager = new TransactionManager(dataAccess, decryptionProvider); // Advanced Logic setup - advancedLogic = new AdvancedLogic(); + advancedLogic = new AdvancedLogic(CurrencyManager.getDefault()); // Logic setup requestLogic = new RequestLogic(transactionManager, signatureProvider, advancedLogic); diff --git a/packages/integration-test/test/scheduled/native-token.test.ts b/packages/integration-test/test/scheduled/native-token.test.ts index a1f5838706..1f1643495a 100644 --- a/packages/integration-test/test/scheduled/native-token.test.ts +++ b/packages/integration-test/test/scheduled/native-token.test.ts @@ -5,7 +5,7 @@ import { AdvancedLogic } from '@requestnetwork/advanced-logic'; import { CurrencyManager } from '@requestnetwork/currency'; import { omit } from 'lodash'; -const advancedLogic = new AdvancedLogic(); +const advancedLogic = new AdvancedLogic(CurrencyManager.getDefault()); const createCreationActionParams: PnReferenceBased.ICreationParameters = { paymentAddress: 'payment.testnet', diff --git a/packages/payment-detection/src/any-to-any-detector.ts b/packages/payment-detection/src/any-to-any-detector.ts index 1e135712fc..cae0fe3597 100644 --- a/packages/payment-detection/src/any-to-any-detector.ts +++ b/packages/payment-detection/src/any-to-any-detector.ts @@ -1,6 +1,5 @@ -import { ExtensionTypes } from '@requestnetwork/types'; +import { CurrencyTypes, ExtensionTypes } from '@requestnetwork/types'; import { FeeReferenceBasedDetector } from './fee-reference-based-detector'; -import { ICurrencyManager } from '@requestnetwork/currency'; import { generate8randomBytes } from '@requestnetwork/utils'; /** @@ -16,7 +15,7 @@ export abstract class AnyToAnyDetector< protected constructor( paymentNetworkId: ExtensionTypes.PAYMENT_NETWORK_ID, extension: TExtension, - currencyManager: ICurrencyManager, + currencyManager: CurrencyTypes.ICurrencyManager, ) { super(paymentNetworkId, extension, currencyManager); } diff --git a/packages/payment-detection/src/any/retrievers/any-to-any-proxy.ts b/packages/payment-detection/src/any/retrievers/any-to-any-proxy.ts index 3963d2949a..6b2ac4d3f1 100644 --- a/packages/payment-detection/src/any/retrievers/any-to-any-proxy.ts +++ b/packages/payment-detection/src/any/retrievers/any-to-any-proxy.ts @@ -1,5 +1,4 @@ -import { CurrencyDefinition } from '@requestnetwork/currency'; -import { PaymentTypes } from '@requestnetwork/types'; +import { CurrencyTypes, PaymentTypes } from '@requestnetwork/types'; import { BigNumber, ethers } from 'ethers'; import { parseLogArgs, unpadAmountFromChainlink } from '../../utils'; import type { JsonFragment } from '@ethersproject/abi'; @@ -41,7 +40,7 @@ export abstract class ConversionInfoRetriever { * @param network The Ethereum network to use */ constructor( - protected requestCurrency: CurrencyDefinition, + protected requestCurrency: CurrencyTypes.CurrencyDefinition, protected paymentReference: string, protected conversionProxyContractAddress: string, protected conversionProxyCreationBlockNumber: number, diff --git a/packages/payment-detection/src/erc20/currency.ts b/packages/payment-detection/src/erc20/currency.ts index a8d938a673..766661a187 100644 --- a/packages/payment-detection/src/erc20/currency.ts +++ b/packages/payment-detection/src/erc20/currency.ts @@ -1,19 +1,12 @@ -import { - CurrencyDefinition, - CurrencyManager, - EvmChains, - getCurrencyHash, - StorageCurrency, -} from '@requestnetwork/currency'; -import { RequestLogicTypes } from '@requestnetwork/types'; +import { CurrencyManager, EvmChains, getCurrencyHash } from '@requestnetwork/currency'; +import { CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types'; import { ERC20__factory } from '@requestnetwork/smart-contracts/types'; import { isAddress } from 'ethers/lib/utils'; import { getDefaultProvider } from '@requestnetwork/utils'; -import { ERC20CurrencyInput } from '@requestnetwork/currency/src'; export const loadCurrencyFromContract = async ( - currency: StorageCurrency, -): Promise => { + currency: CurrencyTypes.StorageCurrency, +): Promise => { try { const { network, value } = currency; @@ -34,7 +27,7 @@ export const loadCurrencyFromContract = async ( return null; } - const definition: ERC20CurrencyInput = { + const definition: CurrencyTypes.ERC20CurrencyInput = { address: value, decimals, symbol, diff --git a/packages/payment-detection/src/erc20/fee-proxy-contract.ts b/packages/payment-detection/src/erc20/fee-proxy-contract.ts index 6eea1531c4..54909af35e 100644 --- a/packages/payment-detection/src/erc20/fee-proxy-contract.ts +++ b/packages/payment-detection/src/erc20/fee-proxy-contract.ts @@ -5,13 +5,7 @@ import { PaymentTypes, RequestLogicTypes, } from '@requestnetwork/types'; -import { - CurrencyDefinition, - EvmChains, - ICurrencyManager, - NearChains, - isSameChain, -} from '@requestnetwork/currency'; +import { EvmChains, NearChains, isSameChain } from '@requestnetwork/currency'; import ProxyInfoRetriever from './proxy-info-retriever'; import { loadCurrencyFromContract } from './currency'; @@ -42,14 +36,14 @@ export abstract class ERC20FeeProxyPaymentDetectorBase< protected constructor( paymentNetworkId: ExtensionTypes.PAYMENT_NETWORK_ID, extension: TExtension, - currencyManager: ICurrencyManager, + currencyManager: CurrencyTypes.ICurrencyManager, ) { super(paymentNetworkId, extension, currencyManager); } protected async getCurrency( storageCurrency: RequestLogicTypes.ICurrency, - ): Promise { + ): Promise { const currency = this.currencyManager.fromStorageCurrency(storageCurrency); if (currency) { return currency; diff --git a/packages/payment-detection/src/fee-reference-based-detector.ts b/packages/payment-detection/src/fee-reference-based-detector.ts index 1922c65633..4bb0dde24c 100644 --- a/packages/payment-detection/src/fee-reference-based-detector.ts +++ b/packages/payment-detection/src/fee-reference-based-detector.ts @@ -1,6 +1,10 @@ import { BigNumber } from 'ethers'; -import { ExtensionTypes, PaymentTypes, RequestLogicTypes } from '@requestnetwork/types'; -import { ICurrencyManager } from '@requestnetwork/currency'; +import { + CurrencyTypes, + ExtensionTypes, + PaymentTypes, + RequestLogicTypes, +} from '@requestnetwork/types'; import { ReferenceBasedDetector } from './reference-based-detector'; import { generate8randomBytes } from '@requestnetwork/utils'; @@ -21,7 +25,7 @@ export abstract class FeeReferenceBasedDetector< protected constructor( paymentNetworkId: ExtensionTypes.PAYMENT_NETWORK_ID, extension: TExtension, - currencyManager: ICurrencyManager, + currencyManager: CurrencyTypes.ICurrencyManager, ) { super(paymentNetworkId, extension, currencyManager); } diff --git a/packages/payment-detection/src/near/retrievers/near-conversion-info-retriever.ts b/packages/payment-detection/src/near/retrievers/near-conversion-info-retriever.ts index 3e4280bf0c..081d4f1e96 100644 --- a/packages/payment-detection/src/near/retrievers/near-conversion-info-retriever.ts +++ b/packages/payment-detection/src/near/retrievers/near-conversion-info-retriever.ts @@ -1,5 +1,4 @@ import { CurrencyTypes, PaymentTypes } from '@requestnetwork/types'; -import { CurrencyDefinition } from '@requestnetwork/currency'; import { NearInfoRetriever, NearPaymentEvent } from './near-info-retriever'; import { TheGraphClient } from '../../thegraph'; @@ -7,7 +6,7 @@ export type TransferEventsParams = { /** The reference to identify the payment*/ paymentReference: string; /** Request denomination (usually fiat) */ - requestCurrency: CurrencyDefinition; + requestCurrency: CurrencyTypes.CurrencyDefinition; /** The recipient of the transfer */ toAddress: string; /** The address of the payment proxy */ diff --git a/packages/payment-detection/src/payment-network-factory.ts b/packages/payment-detection/src/payment-network-factory.ts index f2f702de75..e8c1cd7390 100644 --- a/packages/payment-detection/src/payment-network-factory.ts +++ b/packages/payment-detection/src/payment-network-factory.ts @@ -5,7 +5,6 @@ import { PaymentTypes, RequestLogicTypes, } from '@requestnetwork/types'; -import { ICurrencyManager } from '@requestnetwork/currency'; import { ContractBasedDetector, IPaymentNetworkModuleByType, @@ -96,7 +95,7 @@ export class PaymentNetworkFactory { */ constructor( private readonly advancedLogic: AdvancedLogicTypes.IAdvancedLogic, - private readonly currencyManager: ICurrencyManager, + private readonly currencyManager: CurrencyTypes.ICurrencyManager, options?: Partial, ) { this.options = this.buildOptions(options || {}); diff --git a/packages/payment-detection/src/reference-based-detector.ts b/packages/payment-detection/src/reference-based-detector.ts index 0c0cca54c1..2b6a2fe3d3 100644 --- a/packages/payment-detection/src/reference-based-detector.ts +++ b/packages/payment-detection/src/reference-based-detector.ts @@ -5,7 +5,6 @@ import { RequestLogicTypes, TypesUtils, } from '@requestnetwork/types'; -import { ICurrencyManager } from '@requestnetwork/currency'; import PaymentReferenceCalculator from './payment-reference-calculator'; import { DeclarativePaymentDetectorBase } from './declarative'; @@ -29,7 +28,7 @@ export abstract class ReferenceBasedDetector< protected constructor( paymentNetworkId: ExtensionTypes.PAYMENT_NETWORK_ID, extension: TExtension, - protected readonly currencyManager: ICurrencyManager, + protected readonly currencyManager: CurrencyTypes.ICurrencyManager, ) { super(paymentNetworkId, extension); if (!TypesUtils.isPaymentNetworkId(paymentNetworkId)) { diff --git a/packages/payment-detection/src/thegraph/conversion-info-retriever.ts b/packages/payment-detection/src/thegraph/conversion-info-retriever.ts index 2818cdeb3b..2ba45fb30c 100644 --- a/packages/payment-detection/src/thegraph/conversion-info-retriever.ts +++ b/packages/payment-detection/src/thegraph/conversion-info-retriever.ts @@ -1,5 +1,4 @@ -import { PaymentTypes } from '@requestnetwork/types'; -import { ICurrencyManager } from '@requestnetwork/currency'; +import { CurrencyTypes, PaymentTypes } from '@requestnetwork/types'; import { utils } from 'ethers'; import { TheGraphClient } from './client'; import { TheGraphInfoRetriever } from './info-retriever'; @@ -12,7 +11,7 @@ import { ConversionTransferEventsParams } from '../types'; export class TheGraphConversionInfoRetriever extends TheGraphInfoRetriever { constructor( protected readonly client: TheGraphClient, - protected readonly currencyManager: ICurrencyManager, + protected readonly currencyManager: CurrencyTypes.ICurrencyManager, ) { super(client, currencyManager); } diff --git a/packages/payment-detection/src/thegraph/info-retriever.ts b/packages/payment-detection/src/thegraph/info-retriever.ts index 638b124921..6e4295ed9f 100644 --- a/packages/payment-detection/src/thegraph/info-retriever.ts +++ b/packages/payment-detection/src/thegraph/info-retriever.ts @@ -1,5 +1,4 @@ -import { PaymentTypes } from '@requestnetwork/types'; -import { ICurrencyManager } from '@requestnetwork/currency'; +import { CurrencyTypes, PaymentTypes } from '@requestnetwork/types'; import { utils } from 'ethers'; import { pick, mapValues } from 'lodash'; import type { TheGraphClient } from './client'; @@ -15,7 +14,7 @@ export class TheGraphInfoRetriever({ args, eventFragment }: LogDescription): T => { */ export const padAmountForChainlink = ( amount: BigNumberish, - currency: Pick, + currency: Pick, ): BigNumber => { // eslint-disable-next-line no-magic-numbers return BigNumber.from(amount).mul(10 ** getChainlinkPaddingSize(currency)); @@ -34,7 +34,7 @@ export const padAmountForChainlink = ( export const unpadAmountFromChainlink = ( amount: BigNumberish, - currency: Pick, + currency: Pick, ): BigNumber => { // eslint-disable-next-line no-magic-numbers return BigNumber.from(amount).div(10 ** getChainlinkPaddingSize(currency)); @@ -43,7 +43,7 @@ export const unpadAmountFromChainlink = ( const getChainlinkPaddingSize = ({ type, decimals, -}: Pick): number => { +}: Pick): number => { switch (type) { case RequestLogicTypes.CURRENCY.ISO4217: { const chainlinkFiatDecimal = 8; diff --git a/packages/payment-processor/src/payment/any-to-erc20-proxy.ts b/packages/payment-processor/src/payment/any-to-erc20-proxy.ts index 224ae89192..8f49533a59 100644 --- a/packages/payment-processor/src/payment/any-to-erc20-proxy.ts +++ b/packages/payment-processor/src/payment/any-to-erc20-proxy.ts @@ -1,13 +1,9 @@ import { constants, ContractTransaction, Signer, providers, BigNumberish, BigNumber } from 'ethers'; -import { - CurrencyDefinition, - CurrencyManager, - UnsupportedCurrencyError, -} from '@requestnetwork/currency'; +import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; import { AnyToERC20PaymentDetector } from '@requestnetwork/payment-detection'; import { Erc20ConversionProxy__factory } from '@requestnetwork/smart-contracts/types'; -import { ClientTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { ClientTypes, CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types'; import { ITransactionOverrides } from './transaction-overrides'; import { @@ -96,7 +92,7 @@ export function encodePayAnyToErc20ProxyRequest( export function getConversionPathForErc20Request( request: ClientTypes.IRequestData, paymentSettings: IConversionPaymentSettings, -): { path: string[]; requestCurrency: CurrencyDefinition } { +): { path: string[]; requestCurrency: CurrencyTypes.CurrencyDefinition } { if (!paymentSettings.currency) { throw new Error('currency must be provided in the paymentSettings'); } diff --git a/packages/payment-processor/src/payment/any-to-eth-proxy.ts b/packages/payment-processor/src/payment/any-to-eth-proxy.ts index 6c311160d6..d638a3452a 100644 --- a/packages/payment-processor/src/payment/any-to-eth-proxy.ts +++ b/packages/payment-processor/src/payment/any-to-eth-proxy.ts @@ -1,13 +1,9 @@ import { constants, ContractTransaction, Signer, providers, BigNumberish, BigNumber } from 'ethers'; -import { - CurrencyDefinition, - CurrencyManager, - UnsupportedCurrencyError, -} from '@requestnetwork/currency'; +import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; import { AnyToEthFeeProxyPaymentDetector } from '@requestnetwork/payment-detection'; import { EthConversionProxy__factory } from '@requestnetwork/smart-contracts/types'; -import { ClientTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { ClientTypes, CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types'; import { ITransactionOverrides } from './transaction-overrides'; import { getAmountToPay, getProvider, getRequestPaymentValues, getSigner } from './utils'; @@ -104,7 +100,7 @@ export function prepareAnyToEthProxyPaymentTransaction( export function getConversionPathForEthRequest( request: ClientTypes.IRequestData, paymentSettings: IConversionPaymentSettings, -): { path: string[]; requestCurrency: CurrencyDefinition } { +): { path: string[]; requestCurrency: CurrencyTypes.CurrencyDefinition } { const currencyManager = paymentSettings.currencyManager || CurrencyManager.getDefault(); if (!request.currencyInfo) { diff --git a/packages/payment-processor/src/payment/batch-conversion-proxy.ts b/packages/payment-processor/src/payment/batch-conversion-proxy.ts index 897f84ce16..cc925e7faf 100644 --- a/packages/payment-processor/src/payment/batch-conversion-proxy.ts +++ b/packages/payment-processor/src/payment/batch-conversion-proxy.ts @@ -30,7 +30,7 @@ import { IConversionPaymentSettings } from './index'; import { getConversionPathForErc20Request } from './any-to-erc20-proxy'; import { checkErc20Allowance, encodeApproveAnyErc20 } from './erc20'; import { IState } from 'types/dist/extension-types'; -import { CurrencyDefinition, CurrencyManager, ICurrencyManager } from '@requestnetwork/currency'; +import { CurrencyManager } from '@requestnetwork/currency'; import { BatchPaymentNetworks, EnrichedRequest, @@ -331,7 +331,7 @@ function getUSDPathsForFeeLimit( requestDetails: PaymentTypes.RequestDetail[], network: string, skipFeeUSDLimit: boolean, - currencyManager: ICurrencyManager, + currencyManager: CurrencyTypes.ICurrencyManager, hasNativePayment: boolean, ): string[][] { if (skipFeeUSDLimit) return []; @@ -357,9 +357,9 @@ function getUSDPathsForFeeLimit( .filter((value, index, self) => self.indexOf(value) === index); // get the token currencies and keep the one that are defined - const tokenCurrencies: Array> = tokenAddresses + const tokenCurrencies: Array> = tokenAddresses .map((token) => currencyManager.fromAddress(token, network)) - .filter((value): value is CurrencyDefinition => !!value); + .filter((value): value is CurrencyTypes.CurrencyDefinition => !!value); // get all the conversion paths to USD when it exists and return it const path = tokenCurrencies diff --git a/packages/payment-processor/src/payment/index.ts b/packages/payment-processor/src/payment/index.ts index 0f2218e163..cbc31989ad 100644 --- a/packages/payment-processor/src/payment/index.ts +++ b/packages/payment-processor/src/payment/index.ts @@ -1,6 +1,6 @@ import { ContractTransaction, Signer, BigNumber, BigNumberish, providers } from 'ethers'; -import { ClientTypes, ExtensionTypes, TypesUtils } from '@requestnetwork/types'; +import { ClientTypes, CurrencyTypes, ExtensionTypes, TypesUtils } from '@requestnetwork/types'; import { getBtcPaymentUrl } from './btc-address-based'; import { _getErc20PaymentUrl, getAnyErc20Balance } from './erc20'; @@ -16,7 +16,7 @@ import { payAnyToErc20ProxyRequest } from './any-to-erc20-proxy'; import { payAnyToEthProxyRequest } from './any-to-eth-proxy'; import { WalletConnection } from 'near-api-js'; import { isNearAccountSolvent } from './utils-near'; -import { ICurrencyManager, NearChains } from '@requestnetwork/currency'; +import { NearChains } from '@requestnetwork/currency'; import { encodeRequestErc20Approval } from './encoder-approval'; import { encodeRequestPayment } from './encoder-payment'; import { IPreparedTransaction } from './prepared-transaction'; @@ -36,7 +36,7 @@ export const noConversionNetworks = [ export interface IConversionPaymentSettings { currency?: RequestLogicTypes.ICurrency; maxToSpend: BigNumberish; - currencyManager?: ICurrencyManager; + currencyManager?: CurrencyTypes.ICurrencyManager; } const getPaymentNetwork = ( diff --git a/packages/payment-processor/src/types.ts b/packages/payment-processor/src/types.ts index c82e94872a..37947419f8 100644 --- a/packages/payment-processor/src/types.ts +++ b/packages/payment-processor/src/types.ts @@ -1,5 +1,9 @@ -import { ICurrencyManager } from '@requestnetwork/currency'; -import { ClientTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { + ClientTypes, + CurrencyTypes, + ExtensionTypes, + RequestLogicTypes, +} from '@requestnetwork/types'; import { BigNumber, BigNumberish } from 'ethers'; import { IConversionPaymentSettings } from './payment'; import { ITransactionOverrides } from './payment/transaction-overrides'; @@ -33,7 +37,7 @@ export interface IConversionSettings { /** maximum number of tokens to be spent when the conversion is made */ maxToSpend?: BigNumberish; /** a currency manager to access currencies property, like decimals */ - currencyManager?: ICurrencyManager; + currencyManager?: CurrencyTypes.ICurrencyManager; /** maximum time in seconds of how old chainlink rate can be used, default is zero for infinitely old */ maxRateAge?: number; } diff --git a/packages/request-client.js/src/api/request-network.ts b/packages/request-client.js/src/api/request-network.ts index 602ec54e52..fe1fb76cad 100644 --- a/packages/request-client.js/src/api/request-network.ts +++ b/packages/request-client.js/src/api/request-network.ts @@ -4,6 +4,7 @@ import { RequestLogic } from '@requestnetwork/request-logic'; import { TransactionManager } from '@requestnetwork/transaction-manager'; import { AdvancedLogicTypes, + CurrencyTypes, DataAccessTypes, DecryptionProviderTypes, EncryptionTypes, @@ -14,11 +15,7 @@ import { TransactionTypes, } from '@requestnetwork/types'; import { deepCopy, supportedIdentities } from '@requestnetwork/utils'; -import { - CurrencyManager, - ICurrencyManager, - UnsupportedCurrencyError, -} from '@requestnetwork/currency'; +import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; import * as Types from '../types'; import ContentDataExtension from './content-data-extension'; import Request from './request'; @@ -36,7 +33,7 @@ export default class RequestNetwork { private advancedLogic: AdvancedLogicTypes.IAdvancedLogic; private contentData: ContentDataExtension; - private currencyManager: ICurrencyManager; + private currencyManager: CurrencyTypes.ICurrencyManager; /** * @param dataAccess instance of data-access layer @@ -54,7 +51,7 @@ export default class RequestNetwork { dataAccess: DataAccessTypes.IDataAccess; signatureProvider?: SignatureProviderTypes.ISignatureProvider; decryptionProvider?: DecryptionProviderTypes.IDecryptionProvider; - currencyManager?: ICurrencyManager; + currencyManager?: CurrencyTypes.ICurrencyManager; paymentOptions?: Partial; }) { this.currencyManager = currencyManager || CurrencyManager.getDefault(); diff --git a/packages/request-client.js/src/api/request.ts b/packages/request-client.js/src/api/request.ts index 841b799fda..f87aa5ffdc 100644 --- a/packages/request-client.js/src/api/request.ts +++ b/packages/request-client.js/src/api/request.ts @@ -10,7 +10,6 @@ import { PaymentTypes, RequestLogicTypes, } from '@requestnetwork/types'; -import { ICurrencyManager } from '@requestnetwork/currency'; import * as Types from '../types'; import ContentDataExtension from './content-data-extension'; import localUtils from './utils'; @@ -77,7 +76,7 @@ export default class Request { /** * A list of known tokens */ - private currencyManager: ICurrencyManager; + private currencyManager: CurrencyTypes.ICurrencyManager; /** * Creates an instance of Request @@ -92,7 +91,7 @@ export default class Request { constructor( requestId: RequestLogicTypes.RequestId, requestLogic: RequestLogicTypes.IRequestLogic, - currencyManager: ICurrencyManager, + currencyManager: CurrencyTypes.ICurrencyManager, options?: { paymentNetwork?: PaymentTypes.IPaymentNetwork | null; contentDataExtension?: ContentDataExtension | null; diff --git a/packages/request-client.js/src/http-request-network.ts b/packages/request-client.js/src/http-request-network.ts index 6762f1343d..bb4ad85fac 100644 --- a/packages/request-client.js/src/http-request-network.ts +++ b/packages/request-client.js/src/http-request-network.ts @@ -1,6 +1,7 @@ -import { CurrencyInput, CurrencyManager, ICurrencyManager } from '@requestnetwork/currency'; +import { CurrencyManager } from '@requestnetwork/currency'; import { ClientTypes, + CurrencyTypes, DataAccessTypes, DecryptionProviderTypes, SignatureProviderTypes, @@ -32,7 +33,6 @@ export default class HttpRequestNetwork extends RequestNetwork { nodeConnectionConfig, signatureProvider, useMockStorage, - currencies, currencyManager, paymentOptions, }: { @@ -41,8 +41,7 @@ export default class HttpRequestNetwork extends RequestNetwork { nodeConnectionConfig?: Partial; signatureProvider?: SignatureProviderTypes.ISignatureProvider; useMockStorage?: boolean; - currencies?: CurrencyInput[]; - currencyManager?: ICurrencyManager; + currencyManager?: CurrencyTypes.ICurrencyManager; paymentOptions?: Partial; } = { httpConfig: {}, @@ -54,7 +53,7 @@ export default class HttpRequestNetwork extends RequestNetwork { : new HttpDataAccess({ httpConfig, nodeConnectionConfig }); if (!currencyManager) { - currencyManager = new CurrencyManager(currencies || CurrencyManager.getDefaultList()); + currencyManager = CurrencyManager.getDefault(); } super({ dataAccess, signatureProvider, decryptionProvider, currencyManager, paymentOptions }); diff --git a/packages/types/src/currency-types.ts b/packages/types/src/currency-types.ts index 1502d6f159..34d6273716 100644 --- a/packages/types/src/currency-types.ts +++ b/packages/types/src/currency-types.ts @@ -1,3 +1,5 @@ +import * as RequestLogicTypes from './request-logic-types'; + /** * List of supported EVM chains */ @@ -49,3 +51,155 @@ export type ChainName = EvmChainName | BtcChainName | NearChainName | Declarativ * Virtual machin chains, where payment proxy contracts can be deployed */ export type VMChainName = EvmChainName | NearChainName; + +/** + * Common types used in token configuration files + */ +type TokenAddress = string; +type TokenDefinition = { name: string; symbol: string; decimals: number }; +export type TokenMap = Record; + +/** + * Common types used in chain configuration files + */ +export type Chain = { + chainId: number | string; + testnet?: boolean; + currencies?: TokenMap; +}; + +/** + * A native blockchain token (ETH, MATIC, ETH-rinkeby...) + */ +export type NativeCurrency = { + symbol: string; + decimals: number; + network: ChainName; +}; +type NamedCurrency = { name: string }; +export type NamedNativeCurrency = NativeCurrency & NamedCurrency; + +/** Native Currency types */ +export type NativeCurrencyType = RequestLogicTypes.CURRENCY.BTC | RequestLogicTypes.CURRENCY.ETH; + +/** + * A Fiat currency (EUR, USD...) + */ +export type ISO4217Currency = { + symbol: string; + decimals: number; +}; + +/** + * An ERC20 token (DAI, USDT...) + */ +export type ERC20Currency = { + symbol: string; + decimals: number; + network: EvmChainName | NearChainName | DeclarativeChainName; + address: string; +}; + +/** + * An ERC777 SuperToken (DAIx, USDCx...) + */ +export type ERC777Currency = { + symbol: string; + decimals: number; + network: EvmChainName; + address: string; +}; + +/** + * The minimum properties of a native Currency + */ +export type NativeCurrencyInput = { + type: RequestLogicTypes.CURRENCY.ETH | RequestLogicTypes.CURRENCY.BTC; +} & NativeCurrency; + +/** + * The minimum properties of an ISO4217 Currency + */ +export type ISO4217CurrencyInput = { + type: RequestLogicTypes.CURRENCY.ISO4217; +} & ISO4217Currency; + +/** + * The minimum properties of an ERC20 Currency + */ +export type ERC20CurrencyInput = { + type: RequestLogicTypes.CURRENCY.ERC20; +} & ERC20Currency; + +/** + * The minimum properties of an ERC777 Currency + */ +export type ERC777CurrencyInput = { + type: RequestLogicTypes.CURRENCY.ERC777; +} & ERC777Currency; + +/** + * The minimum properties of a Currency + */ +export type CurrencyInput = + | NativeCurrencyInput + | ISO4217CurrencyInput + | ERC20CurrencyInput + | ERC777CurrencyInput; + +/** + * The description of Currency, its core properties and some computed properties. + * `meta` enables applications to add any metadata they need to a Currency + */ +export type CurrencyDefinition = CurrencyInput & { + id: string; + hash: string; + meta: TMeta; +}; + +/** + * Alias for ICurrency for clarity in the context + */ +export type StorageCurrency = RequestLogicTypes.ICurrency; + +/** + * A Currency manager handles a list of currencies and provides utility to retrieve and change format + */ +export interface ICurrencyManager { + from(symbolOrAddress: string, network?: string): CurrencyDefinition | undefined; + fromAddress(address: string, network?: string): CurrencyDefinition | undefined; + fromSymbol(symbol: string, network?: string): CurrencyDefinition | undefined; + fromHash(hash: string, network?: string): CurrencyDefinition | undefined; + fromStorageCurrency(currency: StorageCurrency): CurrencyDefinition | undefined; + getNativeCurrency( + type: NativeCurrencyType, + network: string, + ): CurrencyDefinition | undefined; + getConversionPath( + from: Pick, + to: Pick, + network: string, + ): string[] | null; + supportsConversion(currency: Pick, network: string): boolean; + validateAddress(address: string, currency: CurrencyInput | StorageCurrency): boolean; + validateCurrency(currency: StorageCurrency): boolean; +} + +/** + * A mapping from old to new name for a given currency. + * + * Format { "chainName": {"TOKEN": ["NEW_TOKEN","NEW_CHAIN"]}} + */ +export type LegacyTokenMap = Record>; + +/** + * currencyFrom => currencyTo => cost + */ +export type CurrencyPairs = Record>; + +/** + * Aggregators maps define pairs of currencies for which an onchain oracle exists, by network. + * + * Network => currencyFrom => currencyTo => cost + */ +export type AggregatorsMap = Partial>;