-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathabstract-extension.ts
More file actions
136 lines (120 loc) · 4.09 KB
/
abstract-extension.ts
File metadata and controls
136 lines (120 loc) · 4.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import {
ChainTypes,
ExtensionTypes,
IdentityTypes,
RequestLogicTypes,
} from '@requestnetwork/types';
import { deepCopy } from '@requestnetwork/utils';
/**
* Abstract class to create extension
*/
export abstract class AbstractExtension<TCreationParameters> implements ExtensionTypes.IExtension {
protected actions: ExtensionTypes.SupportedActions;
static supportedEcosystems: ChainTypes.ECOSYSTEM[] = [ChainTypes.ECOSYSTEM.EVM];
protected constructor(
public readonly extensionType: ExtensionTypes.TYPE,
public readonly extensionId: ExtensionTypes.ID,
public readonly currentVersion: string,
) {
this.actions = {};
}
/**
* Creates the extensionsData to create the extension
*
* @param creationParameters extensions parameters to create
*
* @returns IExtensionCreationAction the extensionsData to be stored in the request
*/
public createCreationAction(
creationParameters: TCreationParameters,
): ExtensionTypes.IAction<TCreationParameters> {
return {
action: 'create',
id: this.extensionId,
parameters: creationParameters,
version: this.currentVersion,
};
}
/**
* Applies the extension action to the request
* Is called to interpret the extensions data when applying the transaction
*
* @param extensionsState previous state of the extensions
* @param extensionAction action to apply
* @param requestState request state read-only
* @param actionSigner identity of the signer
*
* @returns state of the request updated
*/
public applyActionToExtension(
extensionsState: RequestLogicTypes.IExtensionStates,
extensionAction: ExtensionTypes.IAction,
requestState: RequestLogicTypes.IRequest,
actionSigner: IdentityTypes.IIdentity,
timestamp: number,
): RequestLogicTypes.IExtensionStates {
this.validate(requestState, extensionAction);
const copiedExtensionState: RequestLogicTypes.IExtensionStates = deepCopy(extensionsState);
if (extensionAction.action === ExtensionTypes.PnFeeReferenceBased.ACTION.CREATE) {
if (requestState.extensions[extensionAction.id]) {
throw Error(`This extension has already been created`);
}
copiedExtensionState[extensionAction.id] = this.applyCreation(extensionAction, timestamp);
return copiedExtensionState;
}
// if the action is not "create", the state must have been created before
if (!extensionsState[extensionAction.id]) {
throw Error(`The extension should be created before receiving any other action`);
}
const actionToApply: ExtensionTypes.ApplyAction = this.actions[extensionAction.action];
if (!actionToApply) {
throw Error(`Unknown action: ${extensionAction.action}`);
}
copiedExtensionState[extensionAction.id] = actionToApply(
copiedExtensionState[extensionAction.id],
extensionAction,
requestState,
actionSigner,
timestamp,
);
return copiedExtensionState;
}
/**
* Applies an extension creation action
*
* @param extensionAction action to apply
* @param _timestamp action timestamp
*
* @returns state of the extension created
*/
protected applyCreation(
extensionAction: ExtensionTypes.IAction,
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_timestamp: number,
): ExtensionTypes.IState {
if (!extensionAction.version) {
throw Error('version is required at creation');
}
return {
events: [],
id: extensionAction.id,
type: this.extensionType,
values: {},
version: extensionAction.version,
};
}
/**
* Validate the extension action regarding the request
* It is called at the beginning of every applyActionToExtension()
* It must throw in case of error
*
* @param extensionAction action to apply
*/
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 {}
}