From 36bbdcbc12c7dd1f969b1e52ece8f15988439ce2 Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 14:05:57 +0700 Subject: [PATCH 1/8] feat(advanced-logic): use currency manager instance --- packages/advanced-logic/src/advanced-logic.ts | 22 +++++++++------- .../payment-network/address-based.ts | 8 +++--- .../payment-network/any-to-erc20-proxy.ts | 4 +-- .../payment-network/any-to-eth-proxy.ts | 4 +-- .../payment-network/any-to-native.ts | 4 ++- .../payment-network/erc20/address-based.ts | 4 ++- .../erc20/fee-proxy-contract.ts | 4 ++- .../payment-network/erc20/proxy-contract.ts | 4 ++- .../erc20/transferable-receivable.ts | 4 ++- .../payment-network/erc777/stream.ts | 4 ++- .../ethereum/fee-proxy-contract.ts | 4 ++- .../payment-network/ethereum/input-data.ts | 4 ++- .../payment-network/fee-reference-based.ts | 4 ++- .../payment-network/native-token.ts | 4 ++- .../extensions/payment-network/near-native.ts | 0 .../payment-network/near/any-to-near.ts | 9 +++++-- .../payment-network/near/near-native.ts | 9 ++++++- .../near/near-testnet-native.ts | 5 ++-- .../payment-network/address-based.test.ts | 6 ++--- .../erc20/address-based.test.ts | 5 +++- .../erc20/proxy-contract.test.ts | 3 ++- .../payment-network/erc777/stream.test.ts | 3 ++- .../ethereum/fee-proxy-contract.test.ts | 3 ++- .../ethereum/input-data.test.ts | 5 +++- .../payment-network/native-token.test.ts | 25 ++++++++++++------- 25 files changed, 102 insertions(+), 49 deletions(-) delete mode 100644 packages/advanced-logic/src/extensions/payment-network/near-native.ts diff --git a/packages/advanced-logic/src/advanced-logic.ts b/packages/advanced-logic/src/advanced-logic.ts index 11ab59010e..98cd0d00b0 100644 --- a/packages/advanced-logic/src/advanced-logic.ts +++ b/packages/advanced-logic/src/advanced-logic.ts @@ -56,26 +56,30 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic erc20TransferableReceivable: Erc20TransferableReceivablePaymentNetwork; }; + private currencyManager: ICurrencyManager; + constructor(currencyManager?: ICurrencyManager) { if (!currencyManager) { currencyManager = CurrencyManager.getDefault(); } + + this.currencyManager = currencyManager; this.extensions = { addressBasedBtc: new AddressBasedBtc(), - addressBasedErc20: new AddressBasedErc20(), + addressBasedErc20: new AddressBasedErc20(currencyManager), addressBasedTestnetBtc: new AddressBasedTestnetBtc(), contentData: new ContentData(), anyToErc20Proxy: new AnyToErc20Proxy(currencyManager), declarative: new Declarative(), - ethereumInputData: new EthereumInputData(), - feeProxyContractErc20: new FeeProxyContractErc20(), - proxyContractErc20: new ProxyContractErc20(), - erc777Stream: new Erc777Stream(), - feeProxyContractEth: new FeeProxyContractEth(), + ethereumInputData: new EthereumInputData(currencyManager), + feeProxyContractErc20: new FeeProxyContractErc20(currencyManager), + proxyContractErc20: new ProxyContractErc20(currencyManager), + erc777Stream: new Erc777Stream(currencyManager), + feeProxyContractEth: new FeeProxyContractEth(currencyManager), anyToEthProxy: new AnyToEthProxy(currencyManager), - nativeToken: [new NearNative(), new NearTestnetNative()], + nativeToken: [new NearNative(currencyManager), new NearTestnetNative(currencyManager)], anyToNativeToken: [new AnyToNear(currencyManager), new AnyToNearTestnet(currencyManager)], - erc20TransferableReceivable: new Erc20TransferableReceivablePaymentNetwork(), + erc20TransferableReceivable: new Erc20TransferableReceivablePaymentNetwork(currencyManager), }; } @@ -173,7 +177,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic public getFeeProxyContractErc20ForNetwork(network?: string): FeeProxyContractErc20 { return NearChains.isChainSupported(network) - ? new FeeProxyContractErc20(undefined, undefined, network) + ? new FeeProxyContractErc20(this.currencyManager, undefined, undefined, network) : this.extensions.feeProxyContractErc20; } diff --git a/packages/advanced-logic/src/extensions/payment-network/address-based.ts b/packages/advanced-logic/src/extensions/payment-network/address-based.ts index 97454413ce..aeda680c5b 100644 --- a/packages/advanced-logic/src/extensions/payment-network/address-based.ts +++ b/packages/advanced-logic/src/extensions/payment-network/address-based.ts @@ -1,4 +1,4 @@ -import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; +import { ICurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; import { CurrencyTypes, ExtensionTypes, @@ -16,6 +16,7 @@ export default abstract class AddressBasedPaymentNetwork< TCreationParameters extends ExtensionTypes.PnAddressBased.ICreationParameters = ExtensionTypes.PnAddressBased.ICreationParameters, > extends DeclarativePaymentNetwork { protected constructor( + protected currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID, currentVersion: string, public readonly supportedCurrencyType: RequestLogicTypes.CURRENCY, @@ -162,12 +163,11 @@ export default abstract class AddressBasedPaymentNetwork< symbol: string, network: CurrencyTypes.ChainName, ): boolean { - const currencyManager = CurrencyManager.getDefault(); - const currency = currencyManager.from(symbol, network); + const currency = this.currencyManager.from(symbol, network); if (!currency) { throw new UnsupportedCurrencyError({ value: symbol, network }); } - return currencyManager.validateAddress(address, currency); + return this.currencyManager.validateAddress(address, currency); } /** 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..00aa3d2ace 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 @@ -10,12 +10,12 @@ const CURRENT_VERSION = '0.1.0'; export default class AnyToErc20ProxyPaymentNetwork extends Erc20FeeProxyPaymentNetwork { public constructor( - private currencyManager: ICurrencyManager, + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID .ANY_TO_ERC20_PROXY, currentVersion: string = CURRENT_VERSION, ) { - super(extensionId, currentVersion); + super(currencyManager, extensionId, currentVersion); } /** 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..7e90e22c06 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 @@ -5,8 +5,8 @@ import EthereumFeeProxyPaymentNetwork from './ethereum/fee-proxy-contract'; const CURRENT_VERSION = '0.2.0'; export default class AnyToEthProxyPaymentNetwork extends EthereumFeeProxyPaymentNetwork { - public constructor(private currencyManager: ICurrencyManager) { - super(ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY, CURRENT_VERSION); + public constructor(currencyManager: ICurrencyManager) { + super(currencyManager, ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY, CURRENT_VERSION); } /** diff --git a/packages/advanced-logic/src/extensions/payment-network/any-to-native.ts b/packages/advanced-logic/src/extensions/payment-network/any-to-native.ts index a7a6bc0587..2c0fead29f 100644 --- a/packages/advanced-logic/src/extensions/payment-network/any-to-native.ts +++ b/packages/advanced-logic/src/extensions/payment-network/any-to-native.ts @@ -1,14 +1,16 @@ import { FeeReferenceBasedPaymentNetwork } from './fee-reference-based'; import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import { InvalidPaymentAddressError, UnsupportedNetworkError } from './address-based'; +import { ICurrencyManager } from '@requestnetwork/currency'; export default abstract class AnyToNativeTokenPaymentNetwork extends FeeReferenceBasedPaymentNetwork { protected constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID, currentVersion: string, public readonly supportedNetworks: CurrencyTypes.ChainName[], ) { - super(extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); + super(currencyManager, extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); } public createCreationAction( diff --git a/packages/advanced-logic/src/extensions/payment-network/erc20/address-based.ts b/packages/advanced-logic/src/extensions/payment-network/erc20/address-based.ts index e2fcca70c5..fa99dd6b13 100644 --- a/packages/advanced-logic/src/extensions/payment-network/erc20/address-based.ts +++ b/packages/advanced-logic/src/extensions/payment-network/erc20/address-based.ts @@ -1,5 +1,6 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import AddressBasedPaymentNetwork from '../address-based'; +import { ICurrencyManager } from '@requestnetwork/currency'; const CURRENT_VERSION = '0.1.0'; @@ -10,8 +11,9 @@ const CURRENT_VERSION = '0.1.0'; * Important: the addresses must be exclusive to the request */ export default class Erc20AddressBasedPaymentNetwork extends AddressBasedPaymentNetwork { - public constructor() { + public constructor(currencyManager: ICurrencyManager) { super( + currencyManager, ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_ADDRESS_BASED, CURRENT_VERSION, RequestLogicTypes.CURRENCY.ERC20, diff --git a/packages/advanced-logic/src/extensions/payment-network/erc20/fee-proxy-contract.ts b/packages/advanced-logic/src/extensions/payment-network/erc20/fee-proxy-contract.ts index bf4780b073..8e7b10d445 100644 --- a/packages/advanced-logic/src/extensions/payment-network/erc20/fee-proxy-contract.ts +++ b/packages/advanced-logic/src/extensions/payment-network/erc20/fee-proxy-contract.ts @@ -1,5 +1,5 @@ import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; -import { NearChains, isSameChain } from '@requestnetwork/currency'; +import { ICurrencyManager, NearChains, isSameChain } from '@requestnetwork/currency'; import { UnsupportedNetworkError } from '../address-based'; import { FeeReferenceBasedPaymentNetwork } from '../fee-reference-based'; @@ -16,12 +16,14 @@ export default class Erc20FeeProxyPaymentNetwork< * @param network is only relevant for non-EVM chains (Near and Near testnet) */ public constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID .ERC20_FEE_PROXY_CONTRACT, currentVersion?: string | undefined, protected network?: string | undefined, ) { super( + currencyManager, extensionId, currentVersion ?? Erc20FeeProxyPaymentNetwork.getDefaultCurrencyVersion(network), RequestLogicTypes.CURRENCY.ERC20, diff --git a/packages/advanced-logic/src/extensions/payment-network/erc20/proxy-contract.ts b/packages/advanced-logic/src/extensions/payment-network/erc20/proxy-contract.ts index 63aa185392..86303b907b 100644 --- a/packages/advanced-logic/src/extensions/payment-network/erc20/proxy-contract.ts +++ b/packages/advanced-logic/src/extensions/payment-network/erc20/proxy-contract.ts @@ -1,4 +1,5 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { ICurrencyManager } from '@requestnetwork/currency'; import ReferenceBasedPaymentNetwork from '../reference-based'; const CURRENT_VERSION = '0.1.0'; @@ -9,8 +10,9 @@ const CURRENT_VERSION = '0.1.0'; export default class Erc20ProxyPaymentNetwork< TCreationParameters extends ExtensionTypes.PnReferenceBased.ICreationParameters = ExtensionTypes.PnReferenceBased.ICreationParameters, > extends ReferenceBasedPaymentNetwork { - public constructor() { + public constructor(currencyManager: ICurrencyManager) { super( + currencyManager, ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_PROXY_CONTRACT, CURRENT_VERSION, RequestLogicTypes.CURRENCY.ERC20, diff --git a/packages/advanced-logic/src/extensions/payment-network/erc20/transferable-receivable.ts b/packages/advanced-logic/src/extensions/payment-network/erc20/transferable-receivable.ts index 50b7171b1b..5f6466e628 100644 --- a/packages/advanced-logic/src/extensions/payment-network/erc20/transferable-receivable.ts +++ b/packages/advanced-logic/src/extensions/payment-network/erc20/transferable-receivable.ts @@ -1,5 +1,6 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import { FeeReferenceBasedPaymentNetwork } from '../fee-reference-based'; +import { ICurrencyManager } from '@requestnetwork/currency'; const CURRENT_VERSION = '0.2.0'; @@ -10,10 +11,11 @@ export default class Erc20TransferableReceivablePaymentNetwork< TCreationParameters extends ExtensionTypes.PnFeeReferenceBased.ICreationParameters = ExtensionTypes.PnFeeReferenceBased.ICreationParameters, > extends FeeReferenceBasedPaymentNetwork { public constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID .ERC20_TRANSFERABLE_RECEIVABLE, currentVersion: string = CURRENT_VERSION, ) { - super(extensionId, currentVersion, RequestLogicTypes.CURRENCY.ERC20); + super(currencyManager, extensionId, currentVersion, RequestLogicTypes.CURRENCY.ERC20); } } diff --git a/packages/advanced-logic/src/extensions/payment-network/erc777/stream.ts b/packages/advanced-logic/src/extensions/payment-network/erc777/stream.ts index 1b8e2da50e..4c1f33067e 100644 --- a/packages/advanced-logic/src/extensions/payment-network/erc777/stream.ts +++ b/packages/advanced-logic/src/extensions/payment-network/erc777/stream.ts @@ -1,6 +1,7 @@ import { ExtensionTypes, RequestLogicTypes, TypesUtils } from '@requestnetwork/types'; import ReferenceBasedPaymentNetwork from '../reference-based'; import { isValidAmount } from '@requestnetwork/utils'; +import { ICurrencyManager } from '@requestnetwork/currency'; const CURRENT_VERSION = '0.1.0'; /** @@ -9,8 +10,9 @@ const CURRENT_VERSION = '0.1.0'; export default class Erc777StreamPaymentNetwork< TCreationParameters extends ExtensionTypes.PnStreamReferenceBased.ICreationParameters = ExtensionTypes.PnStreamReferenceBased.ICreationParameters, > extends ReferenceBasedPaymentNetwork { - public constructor() { + public constructor(currencyManager: ICurrencyManager) { super( + currencyManager, ExtensionTypes.PAYMENT_NETWORK_ID.ERC777_STREAM, CURRENT_VERSION, RequestLogicTypes.CURRENCY.ERC777, diff --git a/packages/advanced-logic/src/extensions/payment-network/ethereum/fee-proxy-contract.ts b/packages/advanced-logic/src/extensions/payment-network/ethereum/fee-proxy-contract.ts index cda2443463..9ca1ff4728 100644 --- a/packages/advanced-logic/src/extensions/payment-network/ethereum/fee-proxy-contract.ts +++ b/packages/advanced-logic/src/extensions/payment-network/ethereum/fee-proxy-contract.ts @@ -1,5 +1,6 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import { FeeReferenceBasedPaymentNetwork } from '../fee-reference-based'; +import { ICurrencyManager } from '@requestnetwork/currency'; const CURRENT_VERSION = '0.2.0'; @@ -10,10 +11,11 @@ export default class EthereumFeeProxyPaymentNetwork< TCreationParameters extends ExtensionTypes.PnFeeReferenceBased.ICreationParameters = ExtensionTypes.PnFeeReferenceBased.ICreationParameters, > extends FeeReferenceBasedPaymentNetwork { public constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID .ETH_FEE_PROXY_CONTRACT, currentVersion: string = CURRENT_VERSION, ) { - super(extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); + super(currencyManager, extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); } } diff --git a/packages/advanced-logic/src/extensions/payment-network/ethereum/input-data.ts b/packages/advanced-logic/src/extensions/payment-network/ethereum/input-data.ts index fb83b250e1..03fe8edb10 100644 --- a/packages/advanced-logic/src/extensions/payment-network/ethereum/input-data.ts +++ b/packages/advanced-logic/src/extensions/payment-network/ethereum/input-data.ts @@ -1,5 +1,6 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import ReferenceBasedPaymentNetwork from '../reference-based'; +import { ICurrencyManager } from '@requestnetwork/currency'; const CURRENT_VERSION = '0.3.0'; @@ -9,10 +10,11 @@ const CURRENT_VERSION = '0.3.0'; */ export default class EthInputPaymentNetwork extends ReferenceBasedPaymentNetwork { public constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID .ETH_INPUT_DATA, currentVersion: string = CURRENT_VERSION, ) { - super(extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); + super(currencyManager, extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); } } diff --git a/packages/advanced-logic/src/extensions/payment-network/fee-reference-based.ts b/packages/advanced-logic/src/extensions/payment-network/fee-reference-based.ts index 132f264ff0..4966fa74c9 100644 --- a/packages/advanced-logic/src/extensions/payment-network/fee-reference-based.ts +++ b/packages/advanced-logic/src/extensions/payment-network/fee-reference-based.ts @@ -1,6 +1,7 @@ import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types'; import ReferenceBasedPaymentNetwork from './reference-based'; import { areEqualIdentities, deepCopy, isValidAmount } from '@requestnetwork/utils'; +import { ICurrencyManager } from '@requestnetwork/currency'; /** * Core of the reference based with fee payment networks @@ -10,11 +11,12 @@ export abstract class FeeReferenceBasedPaymentNetwork< TCreationParameters extends ExtensionTypes.PnFeeReferenceBased.ICreationParameters = ExtensionTypes.PnFeeReferenceBased.ICreationParameters, > extends ReferenceBasedPaymentNetwork { protected constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID, currentVersion: string, supportedCurrencyType: RequestLogicTypes.CURRENCY, ) { - super(extensionId, currentVersion, supportedCurrencyType); + super(currencyManager, extensionId, currentVersion, supportedCurrencyType); this.actions = { ...this.actions, [ExtensionTypes.PnFeeReferenceBased.ACTION.ADD_FEE]: this.applyAddFee.bind(this), diff --git a/packages/advanced-logic/src/extensions/payment-network/native-token.ts b/packages/advanced-logic/src/extensions/payment-network/native-token.ts index 13c0def2c6..03548c40eb 100644 --- a/packages/advanced-logic/src/extensions/payment-network/native-token.ts +++ b/packages/advanced-logic/src/extensions/payment-network/native-token.ts @@ -2,17 +2,19 @@ import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwor import { InvalidPaymentAddressError, UnsupportedNetworkError } from './address-based'; import ReferenceBasedPaymentNetwork from './reference-based'; +import { ICurrencyManager } from '@requestnetwork/currency'; /** * Implementation of the payment network to pay in ETH based on input data. */ export default abstract class NativeTokenPaymentNetwork extends ReferenceBasedPaymentNetwork { public constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID, currentVersion: string, public readonly supportedNetworks: CurrencyTypes.ChainName[], ) { - super(extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); + super(currencyManager, extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH); } public createCreationAction( diff --git a/packages/advanced-logic/src/extensions/payment-network/near-native.ts b/packages/advanced-logic/src/extensions/payment-network/near-native.ts deleted file mode 100644 index e69de29bb2..0000000000 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..187550fee6 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 @@ -11,7 +11,7 @@ const CURRENT_VERSION = '0.1.0'; export default class AnyToNearPaymentNetwork extends AnyToNativeTokenPaymentNetwork { public constructor( - private currencyManager: ICurrencyManager, + currencyManager: ICurrencyManager, supportedNetworks: CurrencyTypes.NearChainName[] = [ 'aurora', // FIXME: enable near network support @@ -19,7 +19,12 @@ export default class AnyToNearPaymentNetwork extends AnyToNativeTokenPaymentNetw ], currentVersion: string = CURRENT_VERSION, ) { - super(ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE_TOKEN, currentVersion, supportedNetworks); + super( + currencyManager, + ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_NATIVE_TOKEN, + currentVersion, + supportedNetworks, + ); } /** diff --git a/packages/advanced-logic/src/extensions/payment-network/near/near-native.ts b/packages/advanced-logic/src/extensions/payment-network/near/near-native.ts index 4584b99b27..245ebf40e7 100644 --- a/packages/advanced-logic/src/extensions/payment-network/near/near-native.ts +++ b/packages/advanced-logic/src/extensions/payment-network/near/near-native.ts @@ -1,5 +1,6 @@ import { CurrencyTypes, ExtensionTypes } from '@requestnetwork/types'; import NativeTokenPaymentNetwork from '../native-token'; +import { ICurrencyManager } from '@requestnetwork/currency'; const CURRENT_VERSION = '0.2.0'; @@ -8,6 +9,7 @@ const CURRENT_VERSION = '0.2.0'; */ export default class NearNativePaymentNetwork extends NativeTokenPaymentNetwork { public constructor( + currencyManager: ICurrencyManager, supportedNetworks: CurrencyTypes.NearChainName[] = [ 'aurora', // FIXME: enable near network support @@ -15,7 +17,12 @@ export default class NearNativePaymentNetwork extends NativeTokenPaymentNetwork ], currentVersion: string = CURRENT_VERSION, ) { - super(ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN, currentVersion, supportedNetworks); + super( + currencyManager, + ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN, + currentVersion, + supportedNetworks, + ); } /** diff --git a/packages/advanced-logic/src/extensions/payment-network/near/near-testnet-native.ts b/packages/advanced-logic/src/extensions/payment-network/near/near-testnet-native.ts index 79d8220c58..af114b2753 100644 --- a/packages/advanced-logic/src/extensions/payment-network/near/near-testnet-native.ts +++ b/packages/advanced-logic/src/extensions/payment-network/near/near-testnet-native.ts @@ -1,12 +1,13 @@ import NearNativePaymentNetwork from './near-native'; +import { ICurrencyManager } from '@requestnetwork/currency'; /** * Implementation of the payment network to pay in Near on testnet based on input data. */ export default class NearTestnetNativeNativePaymentNetwork extends NearNativePaymentNetwork { - public constructor() { + public constructor(currencyManager: ICurrencyManager) { // testnet PN version is the same as mainnet, can be overridden here if needed - super(['aurora-testnet', 'near-testnet']); + super(currencyManager, ['aurora-testnet', 'near-testnet']); } /** diff --git a/packages/advanced-logic/test/extensions/payment-network/address-based.test.ts b/packages/advanced-logic/test/extensions/payment-network/address-based.test.ts index 7b04f15ad8..90eb197ae9 100644 --- a/packages/advanced-logic/test/extensions/payment-network/address-based.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/address-based.test.ts @@ -1,4 +1,4 @@ -import { UnsupportedCurrencyError } from '@requestnetwork/currency'; +import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency'; import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import AddressBasedPaymentNetwork from '../../../src/extensions/payment-network/address-based'; @@ -10,7 +10,7 @@ describe('extensions/payment-network/address-based', () => { currentVersion: string, supportedCurrencyType: RequestLogicTypes.CURRENCY, ) { - super(extensionId, currentVersion, supportedCurrencyType); + super(CurrencyManager.getDefault(), extensionId, currentVersion, supportedCurrencyType); } public testIsValidAddress() { this.isValidAddress('test'); @@ -34,7 +34,7 @@ describe('extensions/payment-network/address-based', () => { currentVersion: string, supportedCurrencyType: RequestLogicTypes.CURRENCY, ) { - super(extensionId, currentVersion, supportedCurrencyType); + super(CurrencyManager.getDefault(), extensionId, currentVersion, supportedCurrencyType); } public testIsValidAddress() { this.isValidAddressForSymbolAndNetwork('test', 'test', 'mainnet'); diff --git a/packages/advanced-logic/test/extensions/payment-network/erc20/address-based.test.ts b/packages/advanced-logic/test/extensions/payment-network/erc20/address-based.test.ts index f55fbe41c6..6af4daf2c6 100644 --- a/packages/advanced-logic/test/extensions/payment-network/erc20/address-based.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/erc20/address-based.test.ts @@ -1,3 +1,4 @@ +import { CurrencyManager } from '@requestnetwork/currency'; import Erc20AddressBasedPaymentNetwork from '../../../../src/extensions/payment-network/erc20/address-based'; import * as DataERC20AddPaymentAddress from '../../../utils/payment-network/erc20/address-based-add-payment-address-data-generator'; @@ -5,7 +6,9 @@ import * as DataERC20Create from '../../../utils/payment-network/erc20/address-b import * as TestData from '../../../utils/test-data-generator'; import { deepCopy } from '@requestnetwork/utils'; -const erc20AddressBasedPaymentNetwork = new Erc20AddressBasedPaymentNetwork(); +const erc20AddressBasedPaymentNetwork = new Erc20AddressBasedPaymentNetwork( + CurrencyManager.getDefault(), +); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/erc20/mainnet-address-based', () => { describe('createCreationAction', () => { diff --git a/packages/advanced-logic/test/extensions/payment-network/erc20/proxy-contract.test.ts b/packages/advanced-logic/test/extensions/payment-network/erc20/proxy-contract.test.ts index af4e4e4205..165b7249c9 100644 --- a/packages/advanced-logic/test/extensions/payment-network/erc20/proxy-contract.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/erc20/proxy-contract.test.ts @@ -1,4 +1,5 @@ import { ExtensionTypes, RequestLogicTypes, IdentityTypes } from '@requestnetwork/types'; +import { CurrencyManager } from '@requestnetwork/currency'; import Erc20ProxyContract from '../../../../src/extensions/payment-network/erc20/proxy-contract'; @@ -7,7 +8,7 @@ import * as DataERC20Create from '../../../utils/payment-network/erc20/proxy-con import * as TestData from '../../../utils/test-data-generator'; import { deepCopy } from '@requestnetwork/utils'; -const erc20ProxyContract = new Erc20ProxyContract(); +const erc20ProxyContract = new Erc20ProxyContract(CurrencyManager.getDefault()); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/erc20/proxy-contract', () => { diff --git a/packages/advanced-logic/test/extensions/payment-network/erc777/stream.test.ts b/packages/advanced-logic/test/extensions/payment-network/erc777/stream.test.ts index 640eec5547..dd50f1755c 100644 --- a/packages/advanced-logic/test/extensions/payment-network/erc777/stream.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/erc777/stream.test.ts @@ -1,5 +1,6 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import { deepCopy } from '@requestnetwork/utils'; +import { CurrencyManager } from '@requestnetwork/currency'; import Erc777StreamPaymentNetwork from '../../../../src/extensions/payment-network/erc777/stream'; @@ -7,7 +8,7 @@ import * as DataERC777StreamAddData from '../../../utils/payment-network/erc777/ import * as DataERC777StreamCreate from '../../../utils/payment-network/erc777/stream-create-data-generator'; import * as TestData from '../../../utils/test-data-generator'; -const erc777StreamPaymentNetwork = new Erc777StreamPaymentNetwork(); +const erc777StreamPaymentNetwork = new Erc777StreamPaymentNetwork(CurrencyManager.getDefault()); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/erc777/stream', () => { diff --git a/packages/advanced-logic/test/extensions/payment-network/ethereum/fee-proxy-contract.test.ts b/packages/advanced-logic/test/extensions/payment-network/ethereum/fee-proxy-contract.test.ts index 8bfd5557b7..ddae6d2123 100644 --- a/packages/advanced-logic/test/extensions/payment-network/ethereum/fee-proxy-contract.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/ethereum/fee-proxy-contract.test.ts @@ -1,4 +1,5 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { CurrencyManager } from '@requestnetwork/currency'; import EthereumFeeProxyContract from '../../../../src/extensions/payment-network/ethereum/fee-proxy-contract'; @@ -7,7 +8,7 @@ import * as DataEthFeeCreate from '../../../utils/payment-network/ethereum/fee-p import * as TestData from '../../../utils/test-data-generator'; import { deepCopy } from '@requestnetwork/utils'; -const ethFeeProxyContract = new EthereumFeeProxyContract(); +const ethFeeProxyContract = new EthereumFeeProxyContract(CurrencyManager.getDefault()); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/ethereum/fee-proxy-contract', () => { diff --git a/packages/advanced-logic/test/extensions/payment-network/ethereum/input-data.test.ts b/packages/advanced-logic/test/extensions/payment-network/ethereum/input-data.test.ts index a29f0e2d76..dab68f7fde 100644 --- a/packages/advanced-logic/test/extensions/payment-network/ethereum/input-data.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/ethereum/input-data.test.ts @@ -1,5 +1,6 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import { deepCopy } from '@requestnetwork/utils'; +import { CurrencyManager } from '@requestnetwork/currency'; import EthereumInputDataPaymentNetwork from '../../../../src/extensions/payment-network/ethereum/input-data'; @@ -7,7 +8,9 @@ import * as DataEthAddPaymentAddress from '../../../utils/payment-network/ethere import * as DataEthCreate from '../../../utils/payment-network/ethereum/create-data-generator'; import * as TestData from '../../../utils/test-data-generator'; -const ethereumInputDataPaymentNetwork = new EthereumInputDataPaymentNetwork(); +const ethereumInputDataPaymentNetwork = new EthereumInputDataPaymentNetwork( + CurrencyManager.getDefault(), +); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/ethereum/input-data', () => { 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..2359998055 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 @@ -12,9 +12,11 @@ import { import { AdvancedLogic } from '../../../src'; import { arbitraryTimestamp, payeeRaw } from '../../utils/test-data-generator'; import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; +import { CurrencyManager } from '@requestnetwork/currency'; import NearTestnetNativeNativePaymentNetwork from '../../../src/extensions/payment-network/near/near-testnet-native'; const salt = arbitrarySalt; +const currencyManager = CurrencyManager.getDefault(); describe('extensions/payment-network/native-token', () => { const nearCurrency = { @@ -35,7 +37,7 @@ describe('extensions/payment-network/native-token', () => { const nativeTokenTestCases = [ { name: 'Near', - paymentNetwork: new NearNativePaymentNetwork() as NativeTokenPaymentNetwork, + paymentNetwork: new NearNativePaymentNetwork(currencyManager) as NativeTokenPaymentNetwork, networkName: 'aurora', suffix: 'near', wrongSuffix: 'testnet', @@ -44,7 +46,9 @@ describe('extensions/payment-network/native-token', () => { }, { name: 'Aurora testnet', - paymentNetwork: new NearTestnetNativeNativePaymentNetwork() as NativeTokenPaymentNetwork, + paymentNetwork: new NearTestnetNativeNativePaymentNetwork( + currencyManager, + ) as NativeTokenPaymentNetwork, networkName: 'aurora-testnet', suffix: 'testnet', wrongSuffix: 'near', @@ -53,7 +57,9 @@ describe('extensions/payment-network/native-token', () => { }, { name: 'Near testnet', - paymentNetwork: new NearTestnetNativeNativePaymentNetwork() as NativeTokenPaymentNetwork, + paymentNetwork: new NearTestnetNativeNativePaymentNetwork( + currencyManager, + ) as NativeTokenPaymentNetwork, networkName: 'near-testnet', suffix: 'testnet', wrongSuffix: 'near', @@ -139,14 +145,14 @@ describe('extensions/payment-network/native-token', () => { }; it('createCreationAction() works with no payment or refund address nor network name', () => { expect( - new NearNativePaymentNetwork().createCreationAction({ + new NearNativePaymentNetwork(currencyManager).createCreationAction({ salt, }), ).toBeTruthy(); }); it('createCreationAction() throws with unsupported payment network', () => { expect(() => { - new NearNativePaymentNetwork().createCreationAction({ + new NearNativePaymentNetwork(currencyManager).createCreationAction({ ...partialCreationParams, paymentNetworkName: 'another-chain' as CurrencyTypes.NearChainName, }); @@ -156,7 +162,7 @@ describe('extensions/payment-network/native-token', () => { }); it('createCreationAction() throws without payment network', () => { expect(() => { - new NearNativePaymentNetwork().createCreationAction(partialCreationParams); + new NearNativePaymentNetwork(currencyManager).createCreationAction(partialCreationParams); }).toThrowError( `The network name is mandatory for the creation of the extension pn-native-token.`, ); @@ -219,7 +225,7 @@ describe('extensions/payment-network/native-token', () => { }); it('works when adding a payment address to a created state', () => { const advancedLogic = new AdvancedLogic(); - const nearPn = new NearNativePaymentNetwork(); + const nearPn = new NearNativePaymentNetwork(currencyManager); const requestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, @@ -252,7 +258,7 @@ describe('extensions/payment-network/native-token', () => { }); it('throws when creating the extension on a different network from the request network', () => { const advancedLogic = new AdvancedLogic(); - const nearPn = new NearTestnetNativeNativePaymentNetwork(); + const nearPn = new NearTestnetNativeNativePaymentNetwork(currencyManager); const requestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, @@ -273,7 +279,7 @@ 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 nearPn = new NearNativePaymentNetwork(currencyManager); const requestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, @@ -424,6 +430,7 @@ describe('extensions/payment-network/native-token', () => { } expect(() => { const testNativePaymentNetwork = new TestNativePaymentNetwork( + currencyManager, ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN, 'test', [], From 4efb96a83169e40a6f3b2cb6416474a0ec87476d Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 14:18:21 +0700 Subject: [PATCH 2/8] inject ccyManager for Bitcoin payment network --- .../payment-network/bitcoin/mainnet-address-based.ts | 4 +++- .../payment-network/bitcoin/testnet-address-based.ts | 5 +++-- .../payment-network/bitcoin/mainnet-address-based.test.ts | 3 ++- .../payment-network/bitcoin/testnet-address-based.test.ts | 3 ++- 4 files changed, 10 insertions(+), 5 deletions(-) diff --git a/packages/advanced-logic/src/extensions/payment-network/bitcoin/mainnet-address-based.ts b/packages/advanced-logic/src/extensions/payment-network/bitcoin/mainnet-address-based.ts index 3907e9d49a..e8ca2ab647 100644 --- a/packages/advanced-logic/src/extensions/payment-network/bitcoin/mainnet-address-based.ts +++ b/packages/advanced-logic/src/extensions/payment-network/bitcoin/mainnet-address-based.ts @@ -1,5 +1,6 @@ import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types'; import AddressBasedPaymentNetwork from '../address-based'; +import { ICurrencyManager } from '@requestnetwork/currency'; const CURRENT_VERSION = '0.1.0'; const BITCOIN_NETWORK = 'mainnet'; @@ -12,10 +13,11 @@ const BITCOIN_NETWORK = 'mainnet'; */ export default class BitcoinAddressBasedPaymentNetwork extends AddressBasedPaymentNetwork { public constructor( + currencyManager: ICurrencyManager, extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID .BITCOIN_ADDRESS_BASED, ) { - super(extensionId, CURRENT_VERSION, RequestLogicTypes.CURRENCY.BTC); + super(currencyManager, extensionId, CURRENT_VERSION, RequestLogicTypes.CURRENCY.BTC); } protected isValidAddress(address: string): boolean { diff --git a/packages/advanced-logic/src/extensions/payment-network/bitcoin/testnet-address-based.ts b/packages/advanced-logic/src/extensions/payment-network/bitcoin/testnet-address-based.ts index f0636d9809..962d62d51b 100644 --- a/packages/advanced-logic/src/extensions/payment-network/bitcoin/testnet-address-based.ts +++ b/packages/advanced-logic/src/extensions/payment-network/bitcoin/testnet-address-based.ts @@ -1,5 +1,6 @@ import BitcoinAddressBasedPaymentNetwork from './mainnet-address-based'; import { ExtensionTypes } from '@requestnetwork/types'; +import { ICurrencyManager } from '@requestnetwork/currency'; const BITCOIN_NETWORK = 'testnet'; @@ -11,8 +12,8 @@ const BITCOIN_NETWORK = 'testnet'; * Important: the addresses must be exclusive to the request */ export default class BitcoinTestnetAddressBasedPaymentNetwork extends BitcoinAddressBasedPaymentNetwork { - public constructor() { - super(ExtensionTypes.PAYMENT_NETWORK_ID.TESTNET_BITCOIN_ADDRESS_BASED); + public constructor(currencyManager: ICurrencyManager) { + super(currencyManager, ExtensionTypes.PAYMENT_NETWORK_ID.TESTNET_BITCOIN_ADDRESS_BASED); } protected isValidAddress(address: string): boolean { diff --git a/packages/advanced-logic/test/extensions/payment-network/bitcoin/mainnet-address-based.test.ts b/packages/advanced-logic/test/extensions/payment-network/bitcoin/mainnet-address-based.test.ts index 1671625232..4b67d8c945 100644 --- a/packages/advanced-logic/test/extensions/payment-network/bitcoin/mainnet-address-based.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/bitcoin/mainnet-address-based.test.ts @@ -3,8 +3,9 @@ import { deepCopy } from '@requestnetwork/utils'; import * as DataBTCAddPaymentAddress from '../../../utils/payment-network/bitcoin/generator-data-add-payment-address'; import * as DataBTCCreate from '../../../utils/payment-network/bitcoin/generator-data-create'; import * as TestData from '../../../utils/test-data-generator'; +import { CurrencyManager } from '@requestnetwork/currency'; -const mainnetBitcoinAddressBasedPN = new MainnetBitcoinAddressBasedPN(); +const mainnetBitcoinAddressBasedPN = new MainnetBitcoinAddressBasedPN(CurrencyManager.getDefault()); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/bitcoin/mainnet-address-based', () => { diff --git a/packages/advanced-logic/test/extensions/payment-network/bitcoin/testnet-address-based.test.ts b/packages/advanced-logic/test/extensions/payment-network/bitcoin/testnet-address-based.test.ts index 7133bad547..9ae33b41d4 100644 --- a/packages/advanced-logic/test/extensions/payment-network/bitcoin/testnet-address-based.test.ts +++ b/packages/advanced-logic/test/extensions/payment-network/bitcoin/testnet-address-based.test.ts @@ -3,8 +3,9 @@ import { deepCopy } from '@requestnetwork/utils'; import * as DataBTCAddPaymentAddress from '../../../utils/payment-network/bitcoin/testnet-generator-data-add-payment-address'; import * as DataBTCCreate from '../../../utils/payment-network/bitcoin/testnet-generator-data-create'; import * as TestData from '../../../utils/test-data-generator'; +import { CurrencyManager } from '@requestnetwork/currency'; -const testnetBitcoinAddressBasedPN = new TestnetBitcoinAddressBasedPN(); +const testnetBitcoinAddressBasedPN = new TestnetBitcoinAddressBasedPN(CurrencyManager.getDefault()); /* eslint-disable @typescript-eslint/no-unused-expressions */ describe('extensions/payment-network/bitcoin/testnet-address-based', () => { From 16a671ee87d63306d63e0b49a21f34e572633fd9 Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 14:23:39 +0700 Subject: [PATCH 3/8] fix --- packages/advanced-logic/src/advanced-logic.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/advanced-logic/src/advanced-logic.ts b/packages/advanced-logic/src/advanced-logic.ts index 98cd0d00b0..af6fdfa5ba 100644 --- a/packages/advanced-logic/src/advanced-logic.ts +++ b/packages/advanced-logic/src/advanced-logic.ts @@ -65,9 +65,9 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic this.currencyManager = currencyManager; this.extensions = { - addressBasedBtc: new AddressBasedBtc(), + addressBasedBtc: new AddressBasedBtc(currencyManager), addressBasedErc20: new AddressBasedErc20(currencyManager), - addressBasedTestnetBtc: new AddressBasedTestnetBtc(), + addressBasedTestnetBtc: new AddressBasedTestnetBtc(currencyManager), contentData: new ContentData(), anyToErc20Proxy: new AnyToErc20Proxy(currencyManager), declarative: new Declarative(), From 4bf4ad701b064f3e6797a3e55971522e8c777fdf Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 15:17:43 +0700 Subject: [PATCH 4/8] inject ETH --- packages/request-client.js/test/index.test.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/packages/request-client.js/test/index.test.ts b/packages/request-client.js/test/index.test.ts index fa4c083aba..226994af04 100644 --- a/packages/request-client.js/test/index.test.ts +++ b/packages/request-client.js/test/index.test.ts @@ -23,6 +23,7 @@ import httpConfigDefaults from '../src/http-config-defaults'; import { IRequestDataWithEvents } from '../src/types'; import HttpMetaMaskDataAccess from '../src/http-metamask-data-access'; import { MockDataAccess } from '@requestnetwork/data-access'; +import { CurrencyManager } from '@requestnetwork/currency'; import { MockStorage } from '../src/mock-storage'; import * as RequestLogic from '@requestnetwork/types/src/request-logic-types'; @@ -2031,10 +2032,19 @@ describe('request-client.js', () => { }); describe('allows overriding the default currencies', () => { + const currencyManager = new CurrencyManager(CurrencyManager.getDefaultList()); + const ETH = currencyManager.from('ETH', 'mainnet'); + const requestNetwork = new RequestNetwork({ signatureProvider: TestData.fakeSignatureProvider, useMockStorage: true, currencies: [ + { + network: 'mainnet', + type: RequestLogicTypes.CURRENCY.ETH, + decimals: ETH!.decimals, + symbol: ETH!.symbol, + }, { network: 'private', address: testErc20Data.currency.value, From 9ca6eeabc001f1a46506b6e1baf155de14ffdc6e Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 16:19:21 +0700 Subject: [PATCH 5/8] enforce currency manager --- packages/advanced-logic/src/advanced-logic.ts | 13 ++----------- packages/integration-test/test/layers.test.ts | 3 ++- .../test/scheduled/native-token.test.ts | 2 +- 3 files changed, 5 insertions(+), 13 deletions(-) diff --git a/packages/advanced-logic/src/advanced-logic.ts b/packages/advanced-logic/src/advanced-logic.ts index af6fdfa5ba..4784ee0179 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 { ICurrencyManager, NearChains, isSameChain } from '@requestnetwork/currency'; import ContentData from './extensions/content-data'; import AddressBasedBtc from './extensions/payment-network/bitcoin/mainnet-address-based'; @@ -58,11 +53,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic private currencyManager: ICurrencyManager; - constructor(currencyManager?: ICurrencyManager) { - if (!currencyManager) { - currencyManager = CurrencyManager.getDefault(); - } - + constructor(currencyManager: ICurrencyManager) { this.currencyManager = currencyManager; this.extensions = { addressBasedBtc: new AddressBasedBtc(currencyManager), diff --git a/packages/integration-test/test/layers.test.ts b/packages/integration-test/test/layers.test.ts index 1d9204ccd5..5e4512426f 100644 --- a/packages/integration-test/test/layers.test.ts +++ b/packages/integration-test/test/layers.test.ts @@ -1,5 +1,6 @@ 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 +130,7 @@ describe('Request system', () => { const transactionManager = new TransactionManager(dataAccess, decryptionProvider); // Advanced Logic setup - advancedLogic = new AdvancedLogic(); + advancedLogic = new AdvancedLogic(new CurrencyManager(CurrencyManager.getDefaultList())); // 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..0923f07e4d 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(new CurrencyManager(CurrencyManager.getDefaultList())); const createCreationActionParams: PnReferenceBased.ICreationParameters = { paymentAddress: 'payment.testnet', From 23ab1714a957a5a5f7dc50986a8f9f51f3a79778 Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 16:27:57 +0700 Subject: [PATCH 6/8] fix test --- .../payment-network/any-to-near.test.ts | 2 +- .../erc20/fee-proxy-contract.test.ts | 5 ++++- .../payment-network/native-token.test.ts | 20 +++++++++---------- 3 files changed, 15 insertions(+), 12 deletions(-) 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..011f931b06 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); 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..d3e831c9a7 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 @@ -5,9 +5,12 @@ import * as DataERC20FeeCreate from '../../../utils/payment-network/erc20/fee-pr import * as DataNearERC20FeeCreate from '../../../utils/payment-network/erc20/near-fee-proxy-contract'; import * as TestData from '../../../utils/test-data-generator'; import { deepCopy } from '@requestnetwork/utils'; +import { CurrencyManager } from '@requestnetwork/currency'; import { AdvancedLogic } from '../../../../src'; -const advancedLogic = new AdvancedLogic(); +const currencyManager = new CurrencyManager(CurrencyManager.getDefaultList()); + +const advancedLogic = new AdvancedLogic(currencyManager); 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 2359998055..e6a2be6c7e 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 @@ -172,7 +172,7 @@ 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 advancedLogic = new AdvancedLogic(currencyManager); const requestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, @@ -198,7 +198,7 @@ describe('extensions/payment-network/native-token', () => { expect(newExtensionState).toEqual(extensionStateWithNativeTokenPaymentAndRefund); }); it('works with an action without payment network', () => { - const advancedLogic = new AdvancedLogic(); + const advancedLogic = new AdvancedLogic(currencyManager); const requestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, @@ -224,7 +224,7 @@ 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 advancedLogic = new AdvancedLogic(currencyManager); const nearPn = new NearNativePaymentNetwork(currencyManager); const requestState: typeof requestStateNoExtensions = { @@ -257,7 +257,7 @@ 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 advancedLogic = new AdvancedLogic(currencyManager); const nearPn = new NearTestnetNativeNativePaymentNetwork(currencyManager); const requestState: typeof requestStateNoExtensions = { @@ -278,7 +278,7 @@ describe('extensions/payment-network/native-token', () => { ); }); it('throws when adding a payment address a different network', () => { - const advancedLogic = new AdvancedLogic(); + const advancedLogic = new AdvancedLogic(currencyManager); const nearPn = new NearNativePaymentNetwork(currencyManager); const requestState: typeof requestStateNoExtensions = { @@ -301,7 +301,7 @@ 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 advancedLogic = new AdvancedLogic(currencyManager); const wrongNativeTokenRequestState: typeof requestStateNoExtensions = { ...requestStateNoExtensions, @@ -330,7 +330,7 @@ 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 advancedLogic = new AdvancedLogic(currencyManager); const wrongNetwork = `wrong network` as CurrencyTypes.EvmChainName; const wrongNativeTokenRequestState: typeof requestStateNoExtensions = { @@ -360,7 +360,7 @@ 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 advancedLogic = new AdvancedLogic(currencyManager); const requestState = { ...requestStateNoExtensions, @@ -389,7 +389,7 @@ describe('extensions/payment-network/native-token', () => { }); it('keeps the version used at creation', () => { - const advancedLogic = new AdvancedLogic(); + const advancedLogic = new AdvancedLogic(currencyManager); const requestState = { ...requestStateNoExtensions, currency: mainnetTestCase.currency, @@ -406,7 +406,7 @@ describe('extensions/payment-network/native-token', () => { it('requires a version at creation', () => { expect(() => { - const advancedLogic = new AdvancedLogic(); + const advancedLogic = new AdvancedLogic(currencyManager); const requestState = { ...requestStateNoExtensions, currency: mainnetTestCase.currency, From b78b8213b17bf8ab2065b436691a75db676c9d96 Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 16:35:24 +0700 Subject: [PATCH 7/8] fix --- packages/advanced-logic/test/advanced-logic.test.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/packages/advanced-logic/test/advanced-logic.test.ts b/packages/advanced-logic/test/advanced-logic.test.ts index 53cba4d377..baaf086b6c 100644 --- a/packages/advanced-logic/test/advanced-logic.test.ts +++ b/packages/advanced-logic/test/advanced-logic.test.ts @@ -5,6 +5,7 @@ import * as DataDeclarativeCreate from './utils/payment-network/any/generator-da import * as DataTestnetBTCCreate from './utils/payment-network/bitcoin/testnet-generator-data-create'; import { deepCopy } from '@requestnetwork/utils'; +import { CurrencyManager } from '@requestnetwork/currency'; import { AdvancedLogic } from '../src/index'; @@ -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', () => { From beb5c1d6c3b0c61b8e357d00ef94f7870b52925d Mon Sep 17 00:00:00 2001 From: Kevin Dave Date: Tue, 28 Nov 2023 18:52:25 +0700 Subject: [PATCH 8/8] add static instance of currency manager --- packages/currency/src/currencyManager.ts | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/packages/currency/src/currencyManager.ts b/packages/currency/src/currencyManager.ts index 8afdba3b4e..d63df374e1 100644 --- a/packages/currency/src/currencyManager.ts +++ b/packages/currency/src/currencyManager.ts @@ -29,6 +29,8 @@ export class CurrencyManager implements ICurrencyManager private readonly legacyTokens: LegacyTokenMap; private readonly conversionPairs: AggregatorsMap; + private static defaultInstance: CurrencyManager; + /** * * @param inputCurrencies The list of currencies known by the Manager. @@ -328,9 +330,12 @@ export class CurrencyManager implements ICurrencyManager * Returns a default instance of CurrencyManager based on default lists */ static getDefault(): CurrencyManager { - return new CurrencyManager( + if (this.defaultInstance) return this.defaultInstance; + + this.defaultInstance = new CurrencyManager( CurrencyManager.getDefaultList(), CurrencyManager.getDefaultLegacyTokens(), ); + return this.defaultInstance; } }