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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refacto for the advanced logic other extensions #508

Merged
merged 28 commits into from Jun 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
2f8b537
refacto any-to-erc20
vrolland May 4, 2021
6f36506
fix currency supported check
vrolland May 4, 2021
94f400b
fix comments
vrolland May 5, 2021
9fd651e
more inheritance
vrolland May 5, 2021
64b11f7
wip
vrolland May 5, 2021
4091ce0
wip bitcoin
vrolland May 5, 2021
38178ca
fix tests
vrolland May 6, 2021
9fed086
Merge branch 'master' into refactor-2-advanced-logic-payment-network-…
vrolland May 6, 2021
eddffde
fix inheritance... thanks BLE !
vrolland May 6, 2021
869bc71
Merge branch 'refactor-2-advanced-logic-payment-network-deeper' of gi…
vrolland May 6, 2021
6d36259
rename class
vrolland May 6, 2021
fc80050
Merge branch 'master' into refactor-2-advanced-logic-payment-network-…
vrolland May 6, 2021
2286d98
Merge branch 'master' into refactor-2-advanced-logic-payment-network-…
vrolland May 6, 2021
ca8f457
fix tests
vrolland May 6, 2021
a51557d
Merge branch 'master' into refactor-2-advanced-logic-payment-network-…
vrolland May 10, 2021
2730395
from LBE review part1
vrolland May 10, 2021
da1c616
Merge branch 'master' into refactor-2-advanced-logic-payment-network-…
vrolland May 10, 2021
abfb9ee
add type
vrolland May 10, 2021
0dec12a
Merge branch 'refactor-2-advanced-logic-payment-network-deeper' of gi…
vrolland May 10, 2021
2eb11f6
more refacto
vrolland May 10, 2021
2bc04e9
Merge branch 'master' into refactor-2-advanced-logic-payment-network-…
vrolland May 10, 2021
3f720bb
refacto declarative
vrolland May 10, 2021
6b971c1
Update packages/advanced-logic/src/extensions/payment-network/referen…
vrolland May 10, 2021
1f9ac08
refacto content data
vrolland May 10, 2021
db33375
Merge branch 'refactor-2-advanced-logic-payment-network-deeper' into …
vrolland May 10, 2021
b3dd279
Merge branch 'master' into refactor-2-advanced-logic-others-extensions
vrolland May 12, 2021
bec23a3
fix comment
vrolland May 12, 2021
044165e
Merge branch 'master' into refactor-2-advanced-logic-others-extensions
vrolland Jun 8, 2021
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
10 changes: 5 additions & 5 deletions packages/advanced-logic/src/advanced-logic.ts
Expand Up @@ -5,10 +5,10 @@ import {
RequestLogicTypes,
} from '@requestnetwork/types';

import contentData from './extensions/content-data';
import ContentData from './extensions/content-data';
import AddressBasedBtc from './extensions/payment-network/bitcoin/mainnet-address-based';
import AddressBasedTestnetBtc from './extensions/payment-network/bitcoin/testnet-address-based';
import declarative from './extensions/payment-network/declarative';
import Declarative from './extensions/payment-network/declarative';
import AddressBasedErc20 from './extensions/payment-network/erc20/address-based';
import FeeProxyContractErc20 from './extensions/payment-network/erc20/fee-proxy-contract';
import ProxyContractErc20 from './extensions/payment-network/erc20/proxy-contract';
Expand All @@ -25,9 +25,9 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
addressBasedBtc: new AddressBasedBtc(),
addressBasedErc20: new AddressBasedErc20(),
addressBasedTestnetBtc: new AddressBasedTestnetBtc(),
contentData,
contentData: new ContentData(),
anyToErc20Proxy: new AnyToErc20Proxy(),
declarative,
declarative: new Declarative(),
ethereumInputData: new EthereumInputData(),
feeProxyContractErc20: new FeeProxyContractErc20(),
proxyContractErc20: new ProxyContractErc20(),
Expand All @@ -53,7 +53,7 @@ export default class AdvancedLogic implements AdvancedLogicTypes.IAdvancedLogic
): RequestLogicTypes.IExtensionStates {
const id: ExtensionTypes.ID = extensionAction.id;
const extension: ExtensionTypes.IExtension | undefined = {
[ExtensionTypes.ID.CONTENT_DATA]: contentData,
[ExtensionTypes.ID.CONTENT_DATA]: this.extensions.contentData,
[ExtensionTypes.ID.PAYMENT_NETWORK_BITCOIN_ADDRESS_BASED]: this.extensions.addressBasedBtc,
[ExtensionTypes.ID.PAYMENT_NETWORK_TESTNET_BITCOIN_ADDRESS_BASED]: this.extensions
.addressBasedTestnetBtc,
Expand Down
11 changes: 7 additions & 4 deletions packages/advanced-logic/src/extensions/abstract-extension.ts
Expand Up @@ -119,8 +119,11 @@ export default abstract class AbstractExtension<TCreationParameters> {
* @param request
* @param extensionAction action to apply
*/
protected abstract validate(
request: RequestLogicTypes.IRequest,
extensionAction: ExtensionTypes.IAction,
): void;
protected validate(
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_request: RequestLogicTypes.IRequest,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_extensionAction: ExtensionTypes.IAction,
// eslint-disable-next-line @typescript-eslint/no-empty-function
): void {}
}
121 changes: 56 additions & 65 deletions packages/advanced-logic/src/extensions/content-data.ts
@@ -1,78 +1,69 @@
import { ExtensionTypes, RequestLogicTypes } from '@requestnetwork/types';

import Utils from '@requestnetwork/utils';

/**
* Implementation of the content data extension
*/
const contentData: ExtensionTypes.ContentData.IContentData = {
applyActionToExtension,
createCreationAction,
};
export default contentData;
import { ExtensionTypes } from '@requestnetwork/types';
import AbstractExtension from './abstract-extension';

const CURRENT_VERSION = '0.1.0';

/**
* Creates the extensionsData to create the extension content-data
* Should be called to create the extensionsData of a request
*
* @param extensions IAdvancedLogicExtensionsCreationParameters extensions parameters to create
*
* @returns IExtensionCreationAction the extensionsData to be store in the request
*/
function createCreationAction(
creationParameters: ExtensionTypes.ContentData.ICreationParameters,
): ExtensionTypes.IAction {
if (!creationParameters.content) {
throw Error('No content has been given for the extension content-data');
}

return {
action: ExtensionTypes.ContentData.ACTION.CREATE,
id: ExtensionTypes.ID.CONTENT_DATA,
parameters: creationParameters,
version: CURRENT_VERSION,
};
}

/**
* Applies the extension action to the request
* Is called to interpret the extensions data when applying the transaction
*
* @param extensionsState IExtensionStates previous state of the extensions
* @param extensionAction IAction action to apply
* @param requestState IRequest request state read-only
*
* @returns state of the request updated
* Implementation of the content data extension
*/
function applyActionToExtension(
extensionsState: RequestLogicTypes.IExtensionStates,
extensionAction: ExtensionTypes.IAction,
requestState: RequestLogicTypes.IRequest,
): RequestLogicTypes.IExtensionStates {
if (extensionAction.action !== ExtensionTypes.ContentData.ACTION.CREATE) {
throw Error(`Unknown action: ${extensionAction.action}`);
export default class ContentDataExtension<
TCreationParameters extends ExtensionTypes.ContentData.ICreationParameters = ExtensionTypes.ContentData.ICreationParameters
> extends AbstractExtension<TCreationParameters> {
public constructor(
public extensionId: ExtensionTypes.ID = ExtensionTypes.ID.CONTENT_DATA,
public currentVersion: string = CURRENT_VERSION,
) {
super(ExtensionTypes.TYPE.CONTENT_DATA, extensionId, currentVersion);
}

if (requestState.extensions[ExtensionTypes.ID.CONTENT_DATA]) {
throw Error(`This extension has already been created`);
}
/**
* Creates the extensionsData to create the extension content-data
* Should be called to create the extensionsData of a request
*
* @param extensions IAdvancedLogicExtensionsCreationParameters extensions parameters to create
*
* @returns IExtensionCreationAction the extensionsData to be store in the request
*/
public createCreationAction(
creationParameters: TCreationParameters,
): ExtensionTypes.IAction<TCreationParameters> {
if (!creationParameters.content) {
throw Error('No content has been given for the extension content-data');
}

if (!extensionAction.parameters.content) {
throw Error('No content has been given for the extension content-data');
return super.createCreationAction(
creationParameters,
) as ExtensionTypes.IAction<TCreationParameters>;
}

// Deep copy to not mutate the input parameter
const copiedExtensionState: RequestLogicTypes.IExtensionStates = Utils.deepCopy(extensionsState);
/**
* Applies a creation
*
* @param extensionAction action to apply
* @param timestamp
*
* @returns state of the extension created
*/
protected applyCreation(
extensionAction: ExtensionTypes.IAction,
timestamp: number,
): ExtensionTypes.IState {
if (!extensionAction.parameters.content) {
throw Error('No content has been given for the extension content-data');
}

copiedExtensionState[ExtensionTypes.ID.CONTENT_DATA] = {
events: [],
id: ExtensionTypes.ID.CONTENT_DATA,
type: ExtensionTypes.TYPE.CONTENT_DATA,
values: { content: extensionAction.parameters.content },
version: CURRENT_VERSION,
};
const genericCreationAction = super.applyCreation(extensionAction, timestamp);

return copiedExtensionState;
return {
...genericCreationAction,
events: [],
id: this.extensionId,
type: this.extensionType,
values: {
...genericCreationAction.values,
content: extensionAction.parameters.content,
},
version: this.currentVersion,
};
}
}