Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(advanced-logic): use currency manager instance #1268

Merged
merged 8 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
26 changes: 15 additions & 11 deletions packages/advanced-logic/src/advanced-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
kevindavee marked this conversation as resolved.
Show resolved Hide resolved
this.extensions = {
addressBasedBtc: new AddressBasedBtc(),
addressBasedErc20: new AddressBasedErc20(),
addressBasedTestnetBtc: new AddressBasedTestnetBtc(),
addressBasedBtc: new AddressBasedBtc(currencyManager),
addressBasedErc20: new AddressBasedErc20(currencyManager),
addressBasedTestnetBtc: new AddressBasedTestnetBtc(currencyManager),
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),
};
}

Expand Down Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
import { ICurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
import {
CurrencyTypes,
ExtensionTypes,
Expand All @@ -16,6 +16,7 @@ export default abstract class AddressBasedPaymentNetwork<
TCreationParameters extends ExtensionTypes.PnAddressBased.ICreationParameters = ExtensionTypes.PnAddressBased.ICreationParameters,
> extends DeclarativePaymentNetwork<TCreationParameters> {
protected constructor(
protected currencyManager: ICurrencyManager,
extensionId: ExtensionTypes.PAYMENT_NETWORK_ID,
currentVersion: string,
public readonly supportedCurrencyType: RequestLogicTypes.CURRENCY,
Expand Down Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import BitcoinAddressBasedPaymentNetwork from './mainnet-address-based';
import { ExtensionTypes } from '@requestnetwork/types';
import { ICurrencyManager } from '@requestnetwork/currency';

const BITCOIN_NETWORK = 'testnet';

Expand All @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -9,8 +10,9 @@ const CURRENT_VERSION = '0.1.0';
export default class Erc20ProxyPaymentNetwork<
TCreationParameters extends ExtensionTypes.PnReferenceBased.ICreationParameters = ExtensionTypes.PnReferenceBased.ICreationParameters,
> extends ReferenceBasedPaymentNetwork<TCreationParameters> {
public constructor() {
public constructor(currencyManager: ICurrencyManager) {
super(
currencyManager,
ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_PROXY_CONTRACT,
CURRENT_VERSION,
RequestLogicTypes.CURRENCY.ERC20,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,10 +11,11 @@ export default class Erc20TransferableReceivablePaymentNetwork<
TCreationParameters extends ExtensionTypes.PnFeeReferenceBased.ICreationParameters = ExtensionTypes.PnFeeReferenceBased.ICreationParameters,
> extends FeeReferenceBasedPaymentNetwork<TCreationParameters> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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';

/**
Expand All @@ -9,8 +10,9 @@ const CURRENT_VERSION = '0.1.0';
export default class Erc777StreamPaymentNetwork<
TCreationParameters extends ExtensionTypes.PnStreamReferenceBased.ICreationParameters = ExtensionTypes.PnStreamReferenceBased.ICreationParameters,
> extends ReferenceBasedPaymentNetwork<TCreationParameters> {
public constructor() {
public constructor(currencyManager: ICurrencyManager) {
super(
currencyManager,
ExtensionTypes.PAYMENT_NETWORK_ID.ERC777_STREAM,
CURRENT_VERSION,
RequestLogicTypes.CURRENCY.ERC777,
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -10,10 +11,11 @@ export default class EthereumFeeProxyPaymentNetwork<
TCreationParameters extends ExtensionTypes.PnFeeReferenceBased.ICreationParameters = ExtensionTypes.PnFeeReferenceBased.ICreationParameters,
> extends FeeReferenceBasedPaymentNetwork<TCreationParameters> {
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);
}
}
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -10,11 +11,12 @@ export abstract class FeeReferenceBasedPaymentNetwork<
TCreationParameters extends ExtensionTypes.PnFeeReferenceBased.ICreationParameters = ExtensionTypes.PnFeeReferenceBased.ICreationParameters,
> extends ReferenceBasedPaymentNetwork<TCreationParameters> {
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),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

strange. why is this empty?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, not sure 馃し

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's strange that this near-native.ts appears here in this PR. It lives in the near/ directory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty file.
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,20 @@ 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
// 'near'
],
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,
);
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -8,14 +9,20 @@ 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
// 'near'
],
currentVersion: string = CURRENT_VERSION,
) {
super(ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN, currentVersion, supportedNetworks);
super(
currencyManager,
ExtensionTypes.PAYMENT_NETWORK_ID.NATIVE_TOKEN,
currentVersion,
supportedNetworks,
);
}

/**
Expand Down