Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix more tests related to chain initialization
  • Loading branch information
alexandre-abrioux committed Feb 21, 2024
commit c1621b9b3816e4d3bb7a2e7cc0e4337fd62ff88e
13 changes: 7 additions & 6 deletions packages/advanced-logic/src/advanced-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
[ExtensionTypes.ID.CONTENT_DATA]: [ECOSYSTEM.EVM],
Copy link
Contributor

Choose a reason for hiding this comment

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

strange... CONTENT_DATA isn't a payment network...

[ExtensionTypes.PAYMENT_NETWORK_ID.BITCOIN_ADDRESS_BASED]: [ECOSYSTEM.BTC],
[ExtensionTypes.PAYMENT_NETWORK_ID.TESTNET_BITCOIN_ADDRESS_BASED]: [ECOSYSTEM.BTC],
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_DECLARATIVE]: [ECOSYSTEM.BTC],
[ExtensionTypes.PAYMENT_NETWORK_ID.ANY_DECLARATIVE]: [],
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_ADDRESS_BASED]: VM_ECOSYSTEMS,
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_PROXY_CONTRACT]: VM_ECOSYSTEMS,
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_FEE_PROXY_CONTRACT]: VM_ECOSYSTEMS,
Expand Down Expand Up @@ -173,14 +173,15 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_TRANSFERABLE_RECEIVABLE]:
this.extensions.erc20TransferableReceivable,
};
if (!chain) return extensions;
// filter-out unsupported extensions for this chain ecosystem
return (Object.keys(extensions) as ExtensionTypes.ID[]).reduce(
(filteredExtensions, extensionId) => {
filteredExtensions[extensionId] =
chain &&
AdvancedLogic.supportedEcosystemsForExtension[extensionId].includes(chain.ecosystem)
? extensions[extensionId]
: undefined;
filteredExtensions[extensionId] = AdvancedLogic.supportedEcosystemsForExtension[
extensionId
].includes(chain.ecosystem)
? extensions[extensionId]
: undefined;
return filteredExtensions;
},
{} as Record<ExtensionTypes.ID, ExtensionTypes.IExtension | undefined>,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export default class Erc20FeeProxyPaymentNetwork<
super.validate(request, extensionAction);
}

// Override `isValidAddress` to account for network-specific instanciation (non-EVM only)
// Override `isValidAddress` to account for network-specific instantiation (non-EVM only)
protected isValidAddress(address: string): boolean {
if (
this.currencyManager.chainManager.ecosystems[ChainTypes.ECOSYSTEM.NEAR].isChainSupported(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { deepCopy } from '@requestnetwork/utils';
import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from '../../utils/payment-network/any/generator-data-create';
import { AdvancedLogic } from '../../../src';
import { arbitraryTimestamp, payeeRaw, payerRaw } from '../../utils/test-data-generator';
import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import AnyToNearPaymentNetwork from '../../../src/extensions/payment-network/near/any-to-near';
import AnyToNativeTokenPaymentNetwork from '../../../src/extensions/payment-network/any-to-native';
import { CurrencyManager } from '@requestnetwork/currency';
Expand Down Expand Up @@ -206,7 +206,7 @@ describe('extensions/payment-network/any-to-native-token', () => {
expect(() => {
new AnyToNearPaymentNetwork(currencyManager).createCreationAction({
...partialCreationParams,
network: 'another-chain' as ChainTypes.INearChain,
network: 'another-chain',
});
}).toThrowError(
`Payment network 'another-chain' is not supported by this extension (only aurora)`,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { ChainTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';

import * as DataERC20FeeAddData from '../../../utils/payment-network/erc20/fee-proxy-contract-add-data-generator';
import * as DataERC20FeeCreate from '../../../utils/payment-network/erc20/fee-proxy-contract-create-data-generator';
Expand All @@ -7,6 +7,7 @@ import * as TestData from '../../../utils/test-data-generator';
import { deepCopy } from '@requestnetwork/utils';
import { CurrencyManager } from '@requestnetwork/currency';
import { AdvancedLogic } from '../../../../src';
import { ChainManager } from '@requestnetwork/chain';

const currencyManager = new CurrencyManager(CurrencyManager.getDefaultList());

Expand Down Expand Up @@ -118,7 +119,9 @@ describe('extensions/payment-network/erc20/fee-proxy-contract', () => {
});

describe('on Near testnet', () => {
const extension = advancedLogic.getFeeProxyContractErc20ForNetwork('near-testnet');
const extension = advancedLogic.getFeeProxyContractErc20ForNetwork(
ChainManager.current().fromName('near-testnet', [ChainTypes.ECOSYSTEM.NEAR]),
);
it('can create a create action with all parameters', () => {
expect(
extension.createCreationAction({
Expand Down Expand Up @@ -382,7 +385,9 @@ describe('extensions/payment-network/erc20/fee-proxy-contract', () => {
});

describe('on Near testnet', () => {
const extension = advancedLogic.getFeeProxyContractErc20ForNetwork('near-testnet');
const extension = advancedLogic.getFeeProxyContractErc20ForNetwork(
ChainManager.current().fromName('near-testnet', [ChainTypes.ECOSYSTEM.NEAR]),
);
it('can applyActionToExtensions of creation', () => {
expect(
extension.applyActionToExtension(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ describe('extensions/payment-network/native-token', () => {
paymentNetworkName: 'another-chain',
});
}).toThrowError(
`Payment network 'another-chain' is not supported by this extension (only aurora)`,
`No chain found with "name=another-chain" for ecosystem(s) "DECLARATIVE,EVM,NEAR"`,
);
});
it('createCreationAction() throws without payment network', () => {
Expand Down Expand Up @@ -274,7 +274,7 @@ describe('extensions/payment-network/native-token', () => {
arbitraryTimestamp,
);
}).toThrowError(
'Cannot apply action for network aurora-testnet on state with payment network: aurora',
'Cannot apply action for extension aurora-testnet on state with chain: aurora',
);
});
it('throws when adding a payment address a different network', () => {
Expand Down Expand Up @@ -357,7 +357,7 @@ describe('extensions/payment-network/native-token', () => {
payeeRaw.identity,
arbitraryTimestamp,
),
).toThrowError('extension with id: pn-native-token not found for network: wrong network');
).toThrowError('No chain found with "name=wrong network" for ecosystem(s) "NEAR"');
});
it('throws on a different payment network', () => {
const advancedLogic = new AdvancedLogic(currencyManager);
Expand All @@ -384,7 +384,7 @@ describe('extensions/payment-network/native-token', () => {
arbitraryTimestamp,
),
).toThrowError(
`Cannot apply action for network ${mainnetTestCase.wrongCurrency.network} on state with payment network: ${mainnetTestCase.currency.network}`,
`Cannot apply action for extension ${mainnetTestCase.wrongCurrency.network} on state with chain: ${mainnetTestCase.currency.network}`,
);
});

Expand Down
6 changes: 3 additions & 3 deletions packages/chain/src/chain-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ export class ChainManager implements ChainTypes.IChainManager {
}

getEcosystemsByCurrencyType(currencyType: RequestLogicTypes.CURRENCY): ChainTypes.ECOSYSTEM[] {
return (Object.keys(this.ecosystems) as ChainTypes.ECOSYSTEM[]).filter((ecosystemName) =>
this.ecosystems[ecosystemName].currencyType.includes(currencyType),
);
return (Object.keys(this.ecosystems) as ChainTypes.ECOSYSTEM[]).filter((ecosystemName) => {
return this.ecosystems[ecosystemName].currencyTypes.includes(currencyType);
});
}

static getName(chain: string | ChainTypes.IChain): string {
Expand Down
3 changes: 1 addition & 2 deletions packages/chain/src/chains/btc/btc-chain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChainAbstract } from '../chain-abstract';
import { ChainTypes, RequestLogicTypes } from '@requestnetwork/types';
import { ChainTypes } from '@requestnetwork/types';

export class BtcChain extends ChainAbstract implements ChainTypes.IBtcChain {
public readonly ecosystem = ChainTypes.ECOSYSTEM.BTC;
public readonly currencyType = RequestLogicTypes.CURRENCY.BTC;
}
7 changes: 2 additions & 5 deletions packages/chain/src/chains/btc/btc-ecosystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { chains } from './index';
import { BtcChain } from './btc-chain';

class BtcEcosystem extends EcosystemAbstract<ChainTypes.ECOSYSTEM.BTC> {}
export default new BtcEcosystem(
ChainTypes.ECOSYSTEM.BTC,
BtcChain,
chains,
export default new BtcEcosystem(ChainTypes.ECOSYSTEM.BTC, BtcChain, chains, [
RequestLogicTypes.CURRENCY.BTC,
);
]);
3 changes: 0 additions & 3 deletions packages/chain/src/chains/chain-abstract.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
import { ChainTypes } from '@requestnetwork/types';
import { RequestLogicTypes } from '@requestnetwork/types/src';

export abstract class ChainAbstract implements ChainTypes.IChainCommon {
public declare readonly ecosystem: ChainTypes.ECOSYSTEM;
public declare readonly currencyType: RequestLogicTypes.CURRENCY;

constructor(
public readonly id: string,
public readonly name: string,
Expand Down
3 changes: 1 addition & 2 deletions packages/chain/src/chains/declarative/declarative-chain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChainTypes, RequestLogicTypes } from '@requestnetwork/types';
import { ChainTypes } from '@requestnetwork/types';
import { ChainAbstract } from '../chain-abstract';

export class DeclarativeChain extends ChainAbstract implements ChainTypes.IDeclarativeChain {
public readonly ecosystem = ChainTypes.ECOSYSTEM.DECLARATIVE;
public readonly currencyType = RequestLogicTypes.CURRENCY.ETH;
}
11 changes: 6 additions & 5 deletions packages/chain/src/chains/declarative/declarative-ecosystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ import { chains } from './index';

class DeclarativeEcosystem extends EcosystemAbstract<ChainTypes.ECOSYSTEM.DECLARATIVE> {
constructor(chains: Record<string, DeclarativeChain>) {
super(
ChainTypes.ECOSYSTEM.DECLARATIVE,
DeclarativeChain,
chains,
super(ChainTypes.ECOSYSTEM.DECLARATIVE, DeclarativeChain, chains, [
RequestLogicTypes.CURRENCY.ISO4217,
RequestLogicTypes.CURRENCY.BTC,
RequestLogicTypes.CURRENCY.ETH,
);
RequestLogicTypes.CURRENCY.ERC20,
RequestLogicTypes.CURRENCY.ERC777,
]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions packages/chain/src/chains/ecosystem-abstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class EcosystemAbstract<ECOSYSTEM extends ChainTypes.ECOSYSTEM>
testnet?: boolean,
) => ChainTypes.ChainTypeByEcosystem[ECOSYSTEM],
public chains: Record<string, ChainTypes.ChainTypeByEcosystem[ECOSYSTEM]>,
public currencyType: RequestLogicTypes.CURRENCY,
public currencyTypes: RequestLogicTypes.CURRENCY[],
) {}

get chainNames(): string[] {
Expand Down Expand Up @@ -40,7 +40,7 @@ export abstract class EcosystemAbstract<ECOSYSTEM extends ChainTypes.ECOSYSTEM>
/**
* Check if chainName lives amongst the list of supported chains by this chain type.
*/
public isChainSupported(chainName?: string | ChainAbstract) {
public isChainSupported(chainName?: string | ChainAbstract): boolean {
return (
!!chainName &&
this.chainNames.includes(chainName instanceof ChainAbstract ? chainName.name : chainName)
Expand Down
3 changes: 1 addition & 2 deletions packages/chain/src/chains/evm/evm-chain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChainAbstract } from '../chain-abstract';
import { ChainTypes, RequestLogicTypes } from '@requestnetwork/types';
import { ChainTypes } from '@requestnetwork/types';

export class EvmChain extends ChainAbstract implements ChainTypes.IEvmChain {
public readonly ecosystem = ChainTypes.ECOSYSTEM.EVM;
public readonly currencyType = RequestLogicTypes.CURRENCY.ETH;
}
6 changes: 5 additions & 1 deletion packages/chain/src/chains/evm/evm-ecosystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,11 @@ import { EvmChain } from './evm-chain';

class EvmEcosystem extends EcosystemAbstract<ChainTypes.ECOSYSTEM.EVM> {
constructor(chains: Record<string, EvmChain>) {
super(ChainTypes.ECOSYSTEM.EVM, EvmChain, chains, RequestLogicTypes.CURRENCY.ETH);
super(ChainTypes.ECOSYSTEM.EVM, EvmChain, chains, [
RequestLogicTypes.CURRENCY.ETH,
RequestLogicTypes.CURRENCY.ERC20,
RequestLogicTypes.CURRENCY.ERC777,
]);
}
}

Expand Down
3 changes: 1 addition & 2 deletions packages/chain/src/chains/near/near-chain.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { ChainAbstract } from '../chain-abstract';
import { ChainTypes, RequestLogicTypes } from '@requestnetwork/types';
import { ChainTypes } from '@requestnetwork/types';

export class NearChain extends ChainAbstract implements ChainTypes.INearChain {
public readonly ecosystem = ChainTypes.ECOSYSTEM.NEAR;
public readonly currencyType = RequestLogicTypes.CURRENCY.ETH;
}
5 changes: 4 additions & 1 deletion packages/chain/src/chains/near/near-ecosystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { NearChain } from './near-chain';

class NearEcosystem extends EcosystemAbstract<ChainTypes.ECOSYSTEM.NEAR> {
constructor(chains: Record<string, NearChain>) {
super(ChainTypes.ECOSYSTEM.NEAR, NearChain, chains, RequestLogicTypes.CURRENCY.ETH);
super(ChainTypes.ECOSYSTEM.NEAR, NearChain, chains, [
RequestLogicTypes.CURRENCY.ETH,
RequestLogicTypes.CURRENCY.ERC20,
]);
}
}

Expand Down
8 changes: 6 additions & 2 deletions packages/chain/src/chains/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@ import { ChainAbstract } from './chain-abstract';
import { ChainDefinition } from '../types';

export const initializeChains = <CHAIN_CLASS extends ChainAbstract>(
chainClass: new (chainId: string, chainName: string) => CHAIN_CLASS,
chainClass: new (chainId: string, chainName: string, testnet?: boolean) => CHAIN_CLASS,
chainDefinitions: Record<string, ChainDefinition>,
): Record<string, CHAIN_CLASS> =>
Object.keys(chainDefinitions).reduce(
(chains, chainName) => {
chains[chainName] = new chainClass(chainDefinitions[chainName].chainId, chainName);
chains[chainName] = new chainClass(
chainDefinitions[chainName].chainId,
chainName,
chainDefinitions[chainName].testnet,
);
return chains;
},
{} as Record<string, CHAIN_CLASS>,
Expand Down
4 changes: 2 additions & 2 deletions packages/currency/src/currency-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export class CurrencyManager<TMeta = unknown> implements ICurrencyManager<TMeta>
(!network || ('network' in x && x.network === ChainManager.getName(network))) &&
(!network ||
typeof network === 'string' ||
x.type === this.chainManager.ecosystems[network.ecosystem].currencyType),
this.chainManager.ecosystems[network.ecosystem].currencyTypes.includes(x.type)),
);
}

Expand All @@ -156,7 +156,7 @@ export class CurrencyManager<TMeta = unknown> implements ICurrencyManager<TMeta>
(!network || ('network' in x && x.network === ChainManager.getName(network))) &&
(!network ||
typeof network === 'string' ||
x.type === this.chainManager.ecosystems[network.ecosystem].currencyType),
this.chainManager.ecosystems[network.ecosystem].currencyTypes.includes(x.type)),
);
}

Expand Down
7 changes: 1 addition & 6 deletions packages/types/src/chain-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,23 @@ export interface IChainCommon {
name: string;
testnet: boolean;
ecosystem: ECOSYSTEM;
currencyType: RequestLogicTypes.CURRENCY;
eq(chain: IChainCommon): boolean;
}

export interface IBtcChain extends IChainCommon {
ecosystem: ECOSYSTEM.BTC;
currencyType: RequestLogicTypes.CURRENCY.BTC;
}

export interface IDeclarativeChain extends IChainCommon {
ecosystem: ECOSYSTEM.DECLARATIVE;
currencyType: RequestLogicTypes.CURRENCY.ETH;
}

export interface IEvmChain extends IChainCommon {
ecosystem: ECOSYSTEM.EVM;
currencyType: RequestLogicTypes.CURRENCY.ETH;
}

export interface INearChain extends IChainCommon {
ecosystem: ECOSYSTEM.NEAR;
currencyType: RequestLogicTypes.CURRENCY.ETH;
}

/**
Expand All @@ -65,7 +60,7 @@ export interface IEcosystem<E extends ECOSYSTEM> {
name: E;
chainClass: new (id: string, name: string, testnet?: boolean) => ChainTypeByEcosystem[E];
chains: Record<string, ChainTypeByEcosystem[E]>;
currencyType: RequestLogicTypes.CURRENCY;
currencyTypes: RequestLogicTypes.CURRENCY[];
chainNames: string[];
assertChainNameSupported(chainName?: string): asserts chainName is string;
assertChainSupported(chain?: IChain): asserts chain is ChainTypeByEcosystem[E];
Expand Down