Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
dc410f6
WIP
alexandre-abrioux Jan 31, 2023
ed3b672
WIP
alexandre-abrioux Feb 3, 2023
924a70e
fixes
alexandre-abrioux Feb 3, 2023
4aee325
Merge branch 'master' into networks
alexandre-abrioux Feb 3, 2023
bae4830
fix ERC20 order
alexandre-abrioux Feb 3, 2023
e7ecfb9
fix indentation
alexandre-abrioux Feb 3, 2023
8771389
fix tests
alexandre-abrioux Feb 3, 2023
d48802d
rollback changes
alexandre-abrioux Feb 3, 2023
753fe99
fix tests
alexandre-abrioux Feb 3, 2023
5bc4f96
fix tests
alexandre-abrioux Feb 3, 2023
5363d3c
fix tests
alexandre-abrioux Feb 3, 2023
256b6df
fix tests
alexandre-abrioux Feb 3, 2023
21b92fc
rollback test
alexandre-abrioux Feb 3, 2023
7bd75c7
fix tests
alexandre-abrioux Feb 3, 2023
b2ed93b
Merge branch 'master' into networks
alexandre-abrioux Feb 7, 2023
d6ae451
remove aurora from evm
alexandre-abrioux Feb 7, 2023
c7b3112
remove unused export
alexandre-abrioux Feb 7, 2023
e52eb10
fix missing export
alexandre-abrioux Feb 7, 2023
a446daf
change chaindefinition type name
alexandre-abrioux Feb 7, 2023
f454936
edit README
alexandre-abrioux Feb 7, 2023
05ab7d5
refactor chain types to classes
alexandre-abrioux Feb 7, 2023
ddf73ac
fix test
alexandre-abrioux Feb 7, 2023
94cf0e8
remove unusued utils
alexandre-abrioux Feb 7, 2023
c3d01ad
fix some more types
alexandre-abrioux Feb 7, 2023
ead5d9b
fix some more types
alexandre-abrioux Feb 7, 2023
a9a3029
fix test types
alexandre-abrioux Feb 7, 2023
737aee6
fix test
alexandre-abrioux Feb 7, 2023
f44f683
fix near typing
alexandre-abrioux Feb 7, 2023
87c5cd0
fix currencyManager
alexandre-abrioux Feb 7, 2023
3d7c730
fix aggregators type
alexandre-abrioux Feb 7, 2023
2b1fd4f
fix aggregators type
alexandre-abrioux Feb 8, 2023
9baa2f8
fix chainlink test
alexandre-abrioux Feb 8, 2023
100f709
rollback currency manager change
alexandre-abrioux Feb 8, 2023
1c240b7
rollback addagregator change
alexandre-abrioux Feb 8, 2023
729096e
fix ArtifactDeploymentInfo
alexandre-abrioux Feb 8, 2023
97b3438
fix type for ERC20Currency
alexandre-abrioux Feb 16, 2023
8f56b91
Merge branch 'master' into networks
alexandre-abrioux Feb 16, 2023
3452fd1
add README
alexandre-abrioux Feb 16, 2023
71f0809
Merge branch 'master' into networks
alexandre-abrioux Feb 22, 2023
9c014a9
Merge remote-tracking branch 'origin/master' into networks
alexandre-abrioux Mar 7, 2023
8d815f0
fix build
alexandre-abrioux Mar 7, 2023
51030c1
fix missing type
alexandre-abrioux Mar 7, 2023
0190ec2
Merge branch 'master' into networks
alexandre-abrioux Mar 7, 2023
0b99099
fix type errors
alexandre-abrioux Mar 7, 2023
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
7 changes: 4 additions & 3 deletions packages/advanced-logic/src/advanced-logic.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {
AdvancedLogicTypes,
CurrencyTypes,
ExtensionTypes,
IdentityTypes,
RequestLogicTypes,
Expand Down Expand Up @@ -147,7 +148,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
}

public getNativeTokenExtensionForNetwork(
network: string,
network: CurrencyTypes.ChainName,
): ExtensionTypes.IExtension<ExtensionTypes.PnReferenceBased.ICreationParameters> | undefined {
return this.extensions.nativeToken.find((nativeTokenExtension) =>
nativeTokenExtension.supportedNetworks.includes(network),
Expand All @@ -172,7 +173,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
}

public getAnyToNativeTokenExtensionForNetwork(
network: string,
network: CurrencyTypes.ChainName,
): ExtensionTypes.IExtension<ExtensionTypes.PnAnyToEth.ICreationParameters> | undefined {
return this.extensions.anyToNativeToken.find((anyToNativeTokenExtension) =>
anyToNativeTokenExtension.supportedNetworks.includes(network),
Expand All @@ -190,7 +191,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
protected getNetwork(
extensionAction: ExtensionTypes.IAction,
requestState: RequestLogicTypes.IRequest,
): string | undefined {
): CurrencyTypes.ChainName | undefined {
const network =
extensionAction.action === 'create'
? extensionAction.parameters.network
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import {
CurrencyTypes,
ExtensionTypes,
IdentityTypes,
RequestLogicTypes,
} from '@requestnetwork/types';
import { areEqualIdentities, deepCopy } from '@requestnetwork/utils';
import DeclarativePaymentNetwork from './declarative';

Expand Down Expand Up @@ -155,7 +160,7 @@ export default abstract class AddressBasedPaymentNetwork<
protected isValidAddressForSymbolAndNetwork(
address: string,
symbol: string,
network: string,
network: CurrencyTypes.ChainName,
): boolean {
const currencyManager = CurrencyManager.getDefault();
const currency = currencyManager.from(symbol, network);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { FeeReferenceBasedPaymentNetwork } from './fee-reference-based';
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { InvalidPaymentAddressError, UnsupportedNetworkError } from './address-based';

export default abstract class AnyToNativeTokenPaymentNetwork extends FeeReferenceBasedPaymentNetwork {
protected constructor(
extensionId: ExtensionTypes.PAYMENT_NETWORK_ID,
currentVersion: string,
public readonly supportedNetworks: string[],
public readonly supportedNetworks: CurrencyTypes.ChainName[],
) {
super(extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH);
}
Expand Down Expand Up @@ -44,7 +44,9 @@ export default abstract class AnyToNativeTokenPaymentNetwork extends FeeReferenc
);
}

protected throwIfInvalidNetwork(network?: string): asserts network is string {
protected throwIfInvalidNetwork(
network?: CurrencyTypes.ChainName,
): asserts network is CurrencyTypes.ChainName {
super.throwIfInvalidNetwork(network);
if (this.supportedNetworks && !this.supportedNetworks.includes(network)) {
throw new UnsupportedNetworkError(network, this.supportedNetworks);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { InvalidPaymentAddressError, UnsupportedNetworkError } from './address-based';

import ReferenceBasedPaymentNetwork from './reference-based';
Expand All @@ -10,7 +10,7 @@ export default abstract class NativeTokenPaymentNetwork extends ReferenceBasedPa
public constructor(
extensionId: ExtensionTypes.PAYMENT_NETWORK_ID,
currentVersion: string,
public readonly supportedNetworks: string[],
public readonly supportedNetworks: CurrencyTypes.ChainName[],
) {
super(extensionId, currentVersion, RequestLogicTypes.CURRENCY.ETH);
}
Expand Down Expand Up @@ -50,7 +50,9 @@ export default abstract class NativeTokenPaymentNetwork extends ReferenceBasedPa
);
}

protected throwIfInvalidNetwork(network?: string): asserts network is string {
protected throwIfInvalidNetwork(
network?: CurrencyTypes.ChainName,
): asserts network is CurrencyTypes.ChainName {
super.throwIfInvalidNetwork(network);
if (this.supportedNetworks && !this.supportedNetworks.includes(network)) {
throw new UnsupportedNetworkError(network, this.supportedNetworks);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { ICurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import {
CurrencyTypes,
ExtensionTypes,
IdentityTypes,
RequestLogicTypes,
} from '@requestnetwork/types';
import AnyToNativeTokenPaymentNetwork from '../any-to-native';

const CURRENT_VERSION = '0.1.0';

export default class AnyToNearPaymentNetwork extends AnyToNativeTokenPaymentNetwork {
public constructor(
private currencyManager: ICurrencyManager,
supportedNetworks: string[] = [
supportedNetworks: CurrencyTypes.NearChainName[] = [
'aurora',
// FIXME: enable near network support
// 'near'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtensionTypes } from '@requestnetwork/types';
import { CurrencyTypes, ExtensionTypes } from '@requestnetwork/types';
import NativeTokenPaymentNetwork from '../native-token';

const CURRENT_VERSION = '0.2.0';
Expand All @@ -8,7 +8,7 @@ const CURRENT_VERSION = '0.2.0';
*/
export default class NearNativePaymentNetwork extends NativeTokenPaymentNetwork {
public constructor(
supportedNetworks: string[] = [
supportedNetworks: CurrencyTypes.NearChainName[] = [
'aurora',
// FIXME: enable near network support
// 'near'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ describe('extensions/payment-network/address-based', () => {
super(extensionId, currentVersion, supportedCurrencyType);
}
public testIsValidAddress() {
this.isValidAddressForSymbolAndNetwork('test', 'test', 'test');
this.isValidAddressForSymbolAndNetwork('test', 'test', 'mainnet');
}
}
expect(() => {
Expand All @@ -47,6 +47,6 @@ describe('extensions/payment-network/address-based', () => {
RequestLogicTypes.CURRENCY.ERC20,
);
testAddressBasedPaymentNetwork.testIsValidAddress();
}).toThrowError(new UnsupportedCurrencyError({ value: 'test', network: 'test' }));
}).toThrowError(new UnsupportedCurrencyError({ value: 'test', network: 'mainnet' }));
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,11 @@ describe('extensions/payment-network/erc20/any-to-erc20-fee-proxy-contract', ()
anyToErc20Proxy.createCreationAction({
paymentAddress: '0x0000000000000000000000000000000000000001',
salt: 'ea3bc7caf64110ca',
network: 'kovan',
network: 'goerli',
acceptedTokens: ['0x0000000000000000000000000000000000000003'],
});
}).toThrowError(
"The currency '0x0000000000000000000000000000000000000003' on kovan is unknown or not supported.",
"The currency '0x0000000000000000000000000000000000000003' on goerli is unknown or not supported.",
);
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { deepCopy } from '@requestnetwork/utils';
import { CurrencyManager, UnsupportedCurrencyError } from '@requestnetwork/currency';

Expand Down Expand Up @@ -146,7 +146,7 @@ describe('extensions/payment-network/ethereum/any-to-eth-fee-proxy-contract', ()
requestCreatedNoExtension.currency = {
type: RequestLogicTypes.CURRENCY.ETH,
value: 'invalid value',
network: 'invalid network',
network: 'invalid network' as CurrencyTypes.EvmChainName,
};

const action: ExtensionTypes.IAction = deepCopy(
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 { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { CurrencyTypes, 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 @@ -52,7 +52,7 @@ describe('extensions/payment-network/any-to-native-token', () => {
maxRateTimespan: 100000,
feeAmount: '100',
},
];
] as const;

anyToNativeTokenTestCases.forEach((testCase) => {
describe(`action creations for ${testCase.name}`, () => {
Expand Down Expand Up @@ -184,7 +184,7 @@ describe('extensions/payment-network/any-to-native-token', () => {
expect(() => {
new AnyToNearPaymentNetwork(currencyManager).createCreationAction({
...partialCreationParams,
network: 'another-chain',
network: 'another-chain' as CurrencyTypes.NearChainName,
});
}).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
Expand Up @@ -11,7 +11,7 @@ import {
} from '../../utils/payment-network/mocked_native_data';
import { AdvancedLogic } from '../../../src';
import { arbitraryTimestamp, payeeRaw } from '../../utils/test-data-generator';
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import { CurrencyTypes, ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';
import NearTestnetNativeNativePaymentNetwork from '../../../src/extensions/payment-network/near/near-testnet-native';

const salt = arbitrarySalt;
Expand All @@ -21,17 +21,17 @@ describe('extensions/payment-network/native-token', () => {
type: RequestLogicTypes.CURRENCY.ETH,
value: 'NEAR',
network: 'aurora',
};
} as const;
const auroraTestnetCurrency = {
type: RequestLogicTypes.CURRENCY.ETH,
value: 'NEAR-testnet',
network: 'aurora-testnet',
};
} as const;
const nearTestnetCurrency = {
type: RequestLogicTypes.CURRENCY.ETH,
value: 'NEAR-testnet',
network: 'near-testnet',
};
} as const;
const nativeTokenTestCases = [
{
name: 'Near',
Expand Down Expand Up @@ -60,7 +60,7 @@ describe('extensions/payment-network/native-token', () => {
currency: nearTestnetCurrency,
wrongCurrency: nearCurrency,
},
];
] as const;

nativeTokenTestCases.forEach((testCase) => {
describe(`action creations for ${testCase.name}`, () => {
Expand Down Expand Up @@ -148,7 +148,7 @@ describe('extensions/payment-network/native-token', () => {
expect(() => {
new NearNativePaymentNetwork().createCreationAction({
...partialCreationParams,
paymentNetworkName: 'another-chain',
paymentNetworkName: 'another-chain' as CurrencyTypes.NearChainName,
});
}).toThrowError(
`Payment network 'another-chain' is not supported by this extension (only aurora)`,
Expand Down Expand Up @@ -325,7 +325,7 @@ describe('extensions/payment-network/native-token', () => {
});
it('throws on a wrong payment network', () => {
const advancedLogic = new AdvancedLogic();
const wrongNetwork = `wrong network`;
const wrongNetwork = `wrong network` as CurrencyTypes.EvmChainName;

const wrongNativeTokenRequestState: typeof requestStateNoExtensions = {
...requestStateNoExtensions,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ const baseRequestState = {
state: RequestLogicTypes.STATE.CREATED,
timestamp: TestData.arbitraryTimestamp,
version,
};
} as const;

const baseRequestEvent = {
actionSigner: {
Expand All @@ -174,7 +174,7 @@ const baseRequestEvent = {
isSignedRequest: false,
},
timestamp: arbitraryTimestamp,
};
} as const;

export const requestStateNoExtensions: RequestLogicTypes.IRequest = {
...baseRequestState,
Expand Down
23 changes: 22 additions & 1 deletion packages/currency/README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# @requestnetwork/currency

`@requestnetwork/currency` is a typescript library part of the [Request Network protocol](https://github.com/RequestNetwork/requestNetwork).
It is a collection of tools for the currencies shared between the @requestnetwork packages.
It is a collection of tools for the currencies and chains shared between the @requestnetwork packages.

## Installation

Expand Down Expand Up @@ -51,6 +51,27 @@ console.log(FAUToken.symbol); // FAU
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
[Read the contributing guide](/CONTRIBUTING.md)

### Adding a new chain

Supported chains are listed in `src/chains`:

- `src/chains/btc/data` for BTC type chains
- `src/chains/evm/data` for EVM type chains
- `src/chains/near/data` for NEAR type chains

The chain names are subjective, but they are unique and uniform across all Request Network packages.
They are formatted with the kebab-case naming convention.

In order to add a new chain, first create a file `[nameOfTheChain].ts` in the correct directory.
Its internal structure should conform with the corresponding type, respectively:

- `BtcChain`
- `EvmChain`
- `NearChain`

These types are described in the `index.ts` file of each chain subdirectory.
Please add the `testnet: true` property for staging chains.

## License

[MIT](/LICENSE)
62 changes: 62 additions & 0 deletions packages/currency/src/chains/ChainsAbstract.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
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_ID extends string | number,
> {
public chains: Record<CHAIN_NAME, CHAIN>;
public chainNames: CHAIN_NAME[];

constructor(
chains: Record<CHAIN_NAME, CHAIN>,
currencyType: RequestLogicTypes.CURRENCY.ETH | RequestLogicTypes.CURRENCY.BTC,
) {
this.chains = chains;
this.chainNames = Object.keys(chains) as CHAIN_NAME[];
this.addNativeCurrenciesToChains(currencyType);
}

/**
* Adds the native currency to the list of currencies supported by each chain
*/
private addNativeCurrenciesToChains(
currencyType: RequestLogicTypes.CURRENCY.ETH | RequestLogicTypes.CURRENCY.BTC,
): void {
this.chainNames.forEach((chainName) => {
const nativeCurrency = (nativeCurrencies[currencyType] as NamedNativeCurrency[]).find(
(currency) => currency.network === chainName,
);
if (nativeCurrency) {
const chainCurrencies: TokenMap = this.chains[chainName].currencies || {};
chainCurrencies.native = nativeCurrency;
this.chains[chainName].currencies = chainCurrencies;
}
});
}

/**
* Check if chainName lives amongst the list of supported chains by this chain type.
* Throws in the case it's not supported.
*/
public assertChainSupported(chainName?: string): asserts chainName is CHAIN_NAME {
if (!chainName || !(this.chainNames as string[]).includes(chainName))
throw new Error(`Unsupported chain ${chainName}`);
}

/**
* Retrieve the corresponding chain ID from Request Network's internal chain name representation
*/
public getChainId(chainName: CHAIN_NAME): CHAIN_ID {
return this.chains[chainName].chainId as CHAIN_ID;
}

/**
* Retrieve Request Network's internal chain name representation from the corresponding chain ID
*/
public getChainName(chainId: CHAIN_ID): CHAIN_NAME | undefined {
return this.chainNames.find((chainName) => this.chains[chainName].chainId === chainId);
}
}
6 changes: 6 additions & 0 deletions packages/currency/src/chains/btc/BtcChains.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { ChainsAbstract } from '../ChainsAbstract';
import { CurrencyTypes, RequestLogicTypes } from '@requestnetwork/types';
import { BtcChain, chains } from './index';

class BtcChains extends ChainsAbstract<CurrencyTypes.BtcChainName, BtcChain, string> {}
export default new BtcChains(chains, RequestLogicTypes.CURRENCY.BTC);
1 change: 1 addition & 0 deletions packages/currency/src/chains/btc/data/mainnet.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const chainId = '000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f';
Loading