Skip to content

Commit

Permalink
refactor: currency types
Browse files Browse the repository at this point in the history
  • Loading branch information
benjlevesque committed Nov 14, 2023
1 parent 42aaac9 commit 8cf8cc2
Show file tree
Hide file tree
Showing 72 changed files with 417 additions and 463 deletions.
12 changes: 2 additions & 10 deletions packages/advanced-logic/src/advanced-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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(),
Expand Down
Original file line number Diff line number Diff line change
@@ -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,
Expand Down
Original file line number Diff line number Diff line change
@@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -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']);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ICurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
import { UnsupportedCurrencyError } from '@requestnetwork/currency';
import {
CurrencyTypes,
ExtensionTypes,
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion packages/advanced-logic/test/advanced-logic.test.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -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', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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 = {
Expand All @@ -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: {
Expand Down Expand Up @@ -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 = {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
11 changes: 5 additions & 6 deletions packages/currency/src/chains/ChainsAbstract.ts
Original file line number Diff line number Diff line change
@@ -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<CHAIN_NAME, CHAIN>;
Expand All @@ -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;
}
Expand Down
3 changes: 1 addition & 2 deletions packages/currency/src/chains/btc/index.ts
Original file line number Diff line number Diff line change
@@ -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;
};

Expand Down
3 changes: 1 addition & 2 deletions packages/currency/src/chains/declarative/index.ts
Original file line number Diff line number Diff line change
@@ -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<CurrencyTypes.DeclarativeChainName, DeclarativeChain> = {
tron: TronDefinition,
Expand Down
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/avalanche.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/bsc.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/bsctest.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/celo.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/fantom.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/goerli.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/mainnet.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/matic.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/moonbeam.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/optimism.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/rinkeby.ts
Original file line number Diff line number Diff line change
@@ -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,
};
4 changes: 2 additions & 2 deletions packages/currency/src/chains/evm/data/xdai.ts
Original file line number Diff line number Diff line change
@@ -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,
};
Loading

0 comments on commit 8cf8cc2

Please sign in to comment.