Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 7 additions & 1 deletion packages/advanced-logic/src/advanced-logic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import AnyToNearTestnet from './extensions/payment-network/near/any-to-near-test
import NativeToken from './extensions/payment-network/native-token';
import AnyToNative from './extensions/payment-network/any-to-native';
import Erc20TransferableReceivablePaymentNetwork from './extensions/payment-network/erc20/transferable-receivable';
import MetaPaymentNetwork from './extensions/payment-network/meta';

/**
* Module to manage Advanced logic extensions
Expand All @@ -49,6 +50,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
anyToEthProxy: AnyToEthProxy;
anyToNativeToken: AnyToNative[];
erc20TransferableReceivable: Erc20TransferableReceivablePaymentNetwork;
metaPn: MetaPaymentNetwork;
};

private currencyManager: CurrencyTypes.ICurrencyManager;
Expand All @@ -71,6 +73,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
nativeToken: [new NearNative(currencyManager), new NearTestnetNative(currencyManager)],
anyToNativeToken: [new AnyToNear(currencyManager), new AnyToNearTestnet(currencyManager)],
erc20TransferableReceivable: new Erc20TransferableReceivablePaymentNetwork(currencyManager),
metaPn: new MetaPaymentNetwork(currencyManager),
};
}

Expand Down Expand Up @@ -131,6 +134,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
this.getAnyToNativeTokenExtensionForNetwork(network),
[ExtensionTypes.PAYMENT_NETWORK_ID.ERC20_TRANSFERABLE_RECEIVABLE]:
this.extensions.erc20TransferableReceivable,
[ExtensionTypes.PAYMENT_NETWORK_ID.META]: this.extensions.metaPn,
}[id];

if (!extension) {
Expand Down Expand Up @@ -158,7 +162,9 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic

public getAnyToNativeTokenExtensionForNetwork(
network?: CurrencyTypes.ChainName,
): AnyToNative | undefined {
):
| ExtensionTypes.IExtension<ExtensionTypes.PnAnyToAnyConversion.ICreationParameters>
| undefined {
Comment on lines -161 to +167
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unrelated but align with the method above.

return network
? this.extensions.anyToNativeToken.find((anyToNativeTokenExtension) =>
anyToNativeTokenExtension.supportedNetworks.includes(network),
Expand Down
16 changes: 15 additions & 1 deletion packages/advanced-logic/src/extensions/abstract-extension.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { ExtensionTypes, IdentityTypes, RequestLogicTypes } from '@requestnetwork/types';
import { deepCopy } from '@requestnetwork/utils';

export interface ICreationContext {
extensionsState: RequestLogicTypes.IExtensionStates;
extensionAction: ExtensionTypes.IAction;
requestState: RequestLogicTypes.IRequest;
actionSigner: IdentityTypes.IIdentity;
}

/**
* Abstract class to create extension
*/
Expand Down Expand Up @@ -60,7 +67,12 @@ export abstract class AbstractExtension<TCreationParameters> implements Extensio
throw Error(`This extension has already been created`);
}

copiedExtensionState[extensionAction.id] = this.applyCreation(extensionAction, timestamp);
copiedExtensionState[extensionAction.id] = this.applyCreation(extensionAction, timestamp, {
extensionsState,
extensionAction,
requestState,
actionSigner,
});
Comment on lines +70 to +75
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Necessary so we can call applyActionToExtension from the meta-pn context to create sub-pn states (see here).


return copiedExtensionState;
}
Expand Down Expand Up @@ -99,6 +111,8 @@ export abstract class AbstractExtension<TCreationParameters> implements Extensio
extensionAction: ExtensionTypes.IAction,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_timestamp: number,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
context?: ICreationContext,
): ExtensionTypes.IState {
if (!extensionAction.version) {
throw Error('version is required at creation');
Expand Down
239 changes: 239 additions & 0 deletions packages/advanced-logic/src/extensions/payment-network/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,239 @@
import {
CurrencyTypes,
ExtensionTypes,
IdentityTypes,
RequestLogicTypes,
} from '@requestnetwork/types';
import { ICreationContext } from '../abstract-extension';
import AnyToErc20ProxyPaymentNetwork from './any-to-erc20-proxy';
import AnyToEthProxyPaymentNetwork from './any-to-eth-proxy';
import { deepCopy } from '@requestnetwork/utils';
import DeclarativePaymentNetwork from './declarative';

const CURRENT_VERSION = '0.1.0';

export default class MetaPaymentNetwork<
TCreationParameters extends
ExtensionTypes.PnMeta.ICreationParameters = ExtensionTypes.PnMeta.ICreationParameters,
> extends DeclarativePaymentNetwork<TCreationParameters> {
public constructor(
protected currencyManager: CurrencyTypes.ICurrencyManager,
public extensionId: ExtensionTypes.PAYMENT_NETWORK_ID = ExtensionTypes.PAYMENT_NETWORK_ID.META,
public currentVersion: string = CURRENT_VERSION,
) {
super(extensionId, currentVersion);
this.actions = {
...this.actions,
[ExtensionTypes.PnMeta.ACTION.APPLY_ACTION_TO_PN]:
this.applyApplyActionToExtension.bind(this),
};
}

/**
* Creates the extensionsData to create the meta extension payment detection
*
* @param creationParameters extensions parameters to create
*
* @returns IExtensionCreationAction the extensionsData to be stored in the request
*/
public createCreationAction(
creationParameters: TCreationParameters,
): ExtensionTypes.IAction<TCreationParameters> {
Object.entries(creationParameters).forEach(([pnId, creationParameters]) => {
const pn = this.getExtension(pnId);
const subPnIdentifiers: string[] = [];

// Perform validation on sub-pn creation parameters
for (const param of creationParameters) {
pn.createCreationAction(param);
if (subPnIdentifiers.includes(param.salt)) {
throw new Error('Duplicate payment network identifier (salt)');
}
subPnIdentifiers.push(param.salt);
}
});

return super.createCreationAction(creationParameters);
}

/**
* Creates the extensionsData to perform an action on a sub-pn
*
* @param parameters parameters to create the action to perform
*
* @returns IAction the extensionsData to be stored in the request
*/
public createApplyActionToPn(
parameters: ExtensionTypes.PnMeta.IApplyActionToPn,
): ExtensionTypes.IAction {
return {
action: ExtensionTypes.PnMeta.ACTION.APPLY_ACTION_TO_PN,
id: this.extensionId,
parameters: {
pnIdentifier: parameters.pnIdentifier,
action: parameters.action,
parameters: parameters.parameters,
},
};
}

/**
* Applies a creation extension action
*
* @param extensionAction action to apply
* @param timestamp action timestamp
*
* @returns state of the extension created
*/
protected applyCreation(
extensionAction: ExtensionTypes.IAction,
timestamp: number,
context?: ICreationContext,
): ExtensionTypes.IState {
if (!context) {
throw new Error('Context is required');
}
const values: Record<string, ExtensionTypes.IState> = {};
Object.entries(extensionAction.parameters).forEach(([pnId, parameters]) => {
const pn = this.getExtension(pnId);

(parameters as any[]).forEach((params) => {
values[params.salt] = pn.applyActionToExtension(
{},
{
action: 'create',
id: pnId as ExtensionTypes.PAYMENT_NETWORK_ID,
parameters: params,
version: pn.currentVersion,
},
context.requestState,
context.actionSigner,
timestamp,
)[pnId];
});
});

return {
...super.applyCreation(extensionAction, timestamp),
events: [
{
name: 'create',
parameters: {
...extensionAction.parameters,
},
timestamp,
},
],
values,
};
}

/** Applies an action on a sub-payment network
*
* @param extensionsState previous state of the extensions
* @param extensionAction action to apply
* @param requestState request state read-only
* @param actionSigner identity of the signer
* @param timestamp timestamp of the action
*
* @returns state of the extension created
*/
protected applyApplyActionToExtension(
extensionState: ExtensionTypes.IState,
extensionAction: ExtensionTypes.IAction,
requestState: RequestLogicTypes.IRequest,
actionSigner: IdentityTypes.IIdentity,
timestamp: number,
): ExtensionTypes.IState {
const copiedExtensionState: ExtensionTypes.IState<any> = deepCopy(extensionState);
const { pnIdentifier, action, parameters } = extensionAction.parameters;
const extensionToActOn: ExtensionTypes.IState = copiedExtensionState.values[pnIdentifier];

const pn = this.getExtension(extensionToActOn.id);

const subExtensionState = {
[extensionToActOn.id]: extensionToActOn,
};

copiedExtensionState.values[pnIdentifier] = pn.applyActionToExtension(
subExtensionState,
{
id: extensionToActOn.id,
action,
parameters,
},
requestState,
actionSigner,
timestamp,
)[extensionToActOn.id];

// update events
copiedExtensionState.events.push({
name: ExtensionTypes.PnMeta.ACTION.APPLY_ACTION_TO_PN,
parameters: {
pnIdentifier,
action,
parameters,
},
timestamp,
from: actionSigner,
});
return copiedExtensionState;
}

/**
* Validate the extension action regarding the currency and network
* It must throw in case of error
*/
protected validate(
request: RequestLogicTypes.IRequest,
extensionAction: ExtensionTypes.IAction,
): void {
const pnIdentifiers: string[] = [];
if (extensionAction.action === ExtensionTypes.PnMeta.ACTION.CREATE) {
Object.entries(extensionAction.parameters).forEach(([pnId, parameters]: [string, any]) => {
// Checks that the PN is supported
this.getExtension(pnId);

if (parameters.action) {
throw new Error('Invalid action');
}

for (const param of parameters) {
if (pnIdentifiers.includes(param.salt)) {
throw new Error('Duplicate payment network identifier');
}
pnIdentifiers.push(param.salt);
}
});
} else if (extensionAction.action === ExtensionTypes.PnMeta.ACTION.APPLY_ACTION_TO_PN) {
const { pnIdentifier } = extensionAction.parameters;

const subPnState: ExtensionTypes.IState =
request.extensions[ExtensionTypes.PAYMENT_NETWORK_ID.META]?.values?.[pnIdentifier];
if (!subPnState) {
throw new Error(`No payment network with identifier ${pnIdentifier}`);
}

// Checks that the PN is supported
this.getExtension(subPnState.id);
}
}

private getExtension(pnId: string): ExtensionTypes.IExtension {
switch (pnId) {
case ExtensionTypes.PAYMENT_NETWORK_ID.ANY_DECLARATIVE: {
return new DeclarativePaymentNetwork();
}
case ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ERC20_PROXY: {
return new AnyToErc20ProxyPaymentNetwork(this.currencyManager);
}
case ExtensionTypes.PAYMENT_NETWORK_ID.ANY_TO_ETH_PROXY: {
return new AnyToEthProxyPaymentNetwork(this.currencyManager);
}
default: {
throw new Error(`Invalid PN: ${pnId}`);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ describe('extensions/payment-network/erc20/any-to-erc20-fee-proxy-contract', ()
TestData.otherIdRaw.identity,
TestData.arbitraryTimestamp,
),
).toEqual(DataConversionERC20FeeCreate.extensionFullState);
).toEqual(DataConversionERC20FeeCreate.extensionFullState());
});

it('can applyActionToExtensions of creation when address is checksumed', () => {
Expand All @@ -372,7 +372,7 @@ describe('extensions/payment-network/erc20/any-to-erc20-fee-proxy-contract', ()
TestData.otherIdRaw.identity,
TestData.arbitraryTimestamp,
),
).toEqual(DataConversionERC20FeeCreate.extensionFullState);
).toEqual(DataConversionERC20FeeCreate.extensionFullState());
});

it('cannot applyActionToExtensions of creation with a previous state', () => {
Expand Down
Loading