otomato-sdk 1.5.64 → 1.5.65
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/src/models/Action.js +0 -69
- package/dist/src/models/SessionKeyPermission.js +1 -185
- package/dist/src/models/Workflow.js +4 -16
- package/dist/src/services/ApiService.js +21 -0
- package/dist/types/src/models/Action.d.ts +0 -7
- package/dist/types/src/models/SessionKeyPermission.d.ts +3 -26
- package/dist/types/src/services/ApiService.d.ts +1 -0
- package/dist/types/test/e2e/e2e.spec.d.ts +1 -0
- package/package.json +1 -1
|
@@ -10,79 +10,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import { Node } from './Node.js';
|
|
11
11
|
import { ACTIONS } from '../constants/Blocks.js';
|
|
12
12
|
import { typeIsNumber } from '../utils/typeValidator.js';
|
|
13
|
-
import { SessionKeyPermission } from './SessionKeyPermission.js';
|
|
14
13
|
export class Action extends Node {
|
|
15
14
|
constructor(action) {
|
|
16
15
|
super(Object.assign(Object.assign({}, action), { class: 'action', parentInfo: findActionByBlockId(action.blockId).parentInfo }));
|
|
17
16
|
}
|
|
18
|
-
getSessionKeyPermissions() {
|
|
19
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
20
|
-
const parentBlock = findActionByBlockId(this.blockId).block;
|
|
21
|
-
if (!parentBlock.permissions)
|
|
22
|
-
return null;
|
|
23
|
-
const permissions = SessionKeyPermission.fromJSON(parentBlock.permissions);
|
|
24
|
-
permissions.fillParameters(this.getParameters());
|
|
25
|
-
yield permissions.fillMethod();
|
|
26
|
-
// Handle 'before' code for the main action
|
|
27
|
-
if (parentBlock.before) {
|
|
28
|
-
yield permissions.fillBeforeVariables(parentBlock.before, this.getParameters());
|
|
29
|
-
}
|
|
30
|
-
// Handle batchWith: for any batched actions, merge their permissions
|
|
31
|
-
if (parentBlock.batchWith && parentBlock.batchWith.length > 0) {
|
|
32
|
-
for (const batch of parentBlock.batchWith) {
|
|
33
|
-
const batchedAction = findActionByBlockId(batch.id).block;
|
|
34
|
-
if (batchedAction.permissions) {
|
|
35
|
-
const batchedActionInstance = new Action(batchedAction);
|
|
36
|
-
// Replace placeholders in parameters
|
|
37
|
-
const resolvedBatchParams = batch.parameters;
|
|
38
|
-
Object.keys(resolvedBatchParams).forEach(key => {
|
|
39
|
-
var _a;
|
|
40
|
-
// If it's an ABI parameter, handle it separately
|
|
41
|
-
if (key === 'abi' && ((_a = resolvedBatchParams.abi) === null || _a === void 0 ? void 0 : _a.parameters)) {
|
|
42
|
-
Object.keys(resolvedBatchParams.abi.parameters).forEach(abiKey => {
|
|
43
|
-
const abiValue = this.replaceVariables(resolvedBatchParams.abi.parameters[abiKey]);
|
|
44
|
-
batchedActionInstance.setParams(`${abiKey}`, abiValue);
|
|
45
|
-
});
|
|
46
|
-
}
|
|
47
|
-
else {
|
|
48
|
-
let value = this.replaceVariables(resolvedBatchParams[key]);
|
|
49
|
-
batchedActionInstance.setParams(key, value);
|
|
50
|
-
}
|
|
51
|
-
});
|
|
52
|
-
// Get batched permissions
|
|
53
|
-
const batchedPermissions = yield batchedActionInstance.getSessionKeyPermissions();
|
|
54
|
-
if (!batchedPermissions)
|
|
55
|
-
continue;
|
|
56
|
-
batchedPermissions.fillParameters(batch.parameters); // Pass batched action parameters
|
|
57
|
-
yield batchedPermissions.fillMethod();
|
|
58
|
-
// Handle 'before' code for the batched action
|
|
59
|
-
if (batchedAction.before) {
|
|
60
|
-
yield batchedPermissions.fillBeforeVariables(batchedAction.before, batch.parameters);
|
|
61
|
-
}
|
|
62
|
-
// Merge the batched action's permissions with the main action's permissions
|
|
63
|
-
permissions.merge(batchedPermissions);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
}
|
|
67
|
-
return permissions;
|
|
68
|
-
});
|
|
69
|
-
}
|
|
70
|
-
/**
|
|
71
|
-
* Replace any placeholders like {{parameters.tokenToDeposit}} with actual values
|
|
72
|
-
* from the parameters of the current action. Supports both strings and arrays.
|
|
73
|
-
*/
|
|
74
|
-
replaceVariables(value) {
|
|
75
|
-
if (typeof value === 'string') {
|
|
76
|
-
return value.replace(/\{\{parameters\.(.*?)\}\}/g, (_, key) => {
|
|
77
|
-
return this.getParameter(key) || '';
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
if (Array.isArray(value)) {
|
|
81
|
-
return value.map(item => this.replaceVariables(item)); // Recursively handle each array element
|
|
82
|
-
}
|
|
83
|
-
// Return the value unchanged if it's neither a string nor an array
|
|
84
|
-
return value;
|
|
85
|
-
}
|
|
86
17
|
static fromJSON(json) {
|
|
87
18
|
return __awaiter(this, void 0, void 0, function* () {
|
|
88
19
|
const enriched = findActionByBlockId(json.blockId);
|
|
@@ -1,185 +1 @@
|
|
|
1
|
-
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
|
-
import { CHAINS } from "../constants/chains.js";
|
|
11
|
-
import { getToken, getTokenFromSymbol } from "../constants/tokens.js";
|
|
12
|
-
export class SessionKeyPermission {
|
|
13
|
-
constructor({ approvedTargets = [], label = [], labelNotAuthorized = [], } = {}) {
|
|
14
|
-
this.approvedTargets = approvedTargets;
|
|
15
|
-
this.label = label;
|
|
16
|
-
this.labelNotAuthorized = labelNotAuthorized;
|
|
17
|
-
}
|
|
18
|
-
fill(key, value) {
|
|
19
|
-
// Escape special regex characters in the key
|
|
20
|
-
const escapedKey = key.replace(/[.*+?^${}()|[\]\\[\]]/g, '\\$&');
|
|
21
|
-
const placeholder = new RegExp(`{{${escapedKey}}}`, 'g');
|
|
22
|
-
// Function to replace placeholders in a target string
|
|
23
|
-
const replacePlaceholder = (target) => target.replace(placeholder, value);
|
|
24
|
-
// Apply the placeholder replacement logic
|
|
25
|
-
this.approvedTargets = this.approvedTargets.map(replacePlaceholder);
|
|
26
|
-
this.label = this.label.map(replacePlaceholder);
|
|
27
|
-
this.labelNotAuthorized = this.labelNotAuthorized.map(replacePlaceholder);
|
|
28
|
-
}
|
|
29
|
-
/**
|
|
30
|
-
* Fill placeholders using variables returned by the 'before' string execution.
|
|
31
|
-
* @param beforeCode - A string representing the 'before' logic to execute.
|
|
32
|
-
*/
|
|
33
|
-
fillBeforeVariables(beforeCode, parameters) {
|
|
34
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
35
|
-
console.log("filling before variable");
|
|
36
|
-
try {
|
|
37
|
-
// Prepare the otomatoSdk object with required functions
|
|
38
|
-
const otomatoSdk = {
|
|
39
|
-
getToken,
|
|
40
|
-
getTokenFromSymbol,
|
|
41
|
-
CHAINS
|
|
42
|
-
// Include any other functions you need from otomato-sdk
|
|
43
|
-
};
|
|
44
|
-
// Prepare the environment with parameters and otomatoSdk
|
|
45
|
-
const env = { parameters, otomatoSdk };
|
|
46
|
-
// Replace the dynamic import in beforeCode
|
|
47
|
-
const beforeCodeUpdated = beforeCode.replace(/const\s+otomatoSdk\s*=\s*await\s*import\(['"]otomato-sdk['"]\);/, 'const otomatoSdk = env.otomatoSdk;');
|
|
48
|
-
// Wrap the beforeCode in an async function and immediately invoke it
|
|
49
|
-
const asyncBeforeFn = new Function('env', `
|
|
50
|
-
return (async function() {
|
|
51
|
-
return await (${beforeCodeUpdated})(env);
|
|
52
|
-
})();
|
|
53
|
-
`);
|
|
54
|
-
// Execute the async function and await the result
|
|
55
|
-
const beforeResult = yield asyncBeforeFn(env);
|
|
56
|
-
// Replace placeholders like {{before.variableName}} with the corresponding values
|
|
57
|
-
if (beforeResult && typeof beforeResult === 'object') {
|
|
58
|
-
Object.keys(beforeResult).forEach((key) => {
|
|
59
|
-
this.fill(`before.${key}`, beforeResult[key]);
|
|
60
|
-
});
|
|
61
|
-
}
|
|
62
|
-
else {
|
|
63
|
-
console.error("Before function did not return an object:", beforeResult);
|
|
64
|
-
}
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
console.error("Error executing before code:", error);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
// The rest of your existing code
|
|
72
|
-
fillParameters(params) {
|
|
73
|
-
var _a;
|
|
74
|
-
// Helper to handle the replacement in case of arrays
|
|
75
|
-
const fillArray = (key, array) => {
|
|
76
|
-
array.forEach((item, index) => {
|
|
77
|
-
if (typeof item === 'string') {
|
|
78
|
-
this.fill(`${key}[${index}]`, item);
|
|
79
|
-
}
|
|
80
|
-
});
|
|
81
|
-
};
|
|
82
|
-
// 1. Replace non-ABI params
|
|
83
|
-
for (let key in params) {
|
|
84
|
-
if (key !== 'abi') {
|
|
85
|
-
if (Array.isArray(params[key])) {
|
|
86
|
-
fillArray(`parameters.${key}`, params[key]);
|
|
87
|
-
}
|
|
88
|
-
else {
|
|
89
|
-
this.fill(`parameters.${key}`, params[key]);
|
|
90
|
-
}
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
// 2. Replace ABI params
|
|
94
|
-
const abiParams = (_a = params.abi) === null || _a === void 0 ? void 0 : _a.parameters;
|
|
95
|
-
for (let key in abiParams) {
|
|
96
|
-
if (Array.isArray(abiParams[key])) {
|
|
97
|
-
fillArray(`parameters.abi.parameters.${key}`, abiParams[key]);
|
|
98
|
-
}
|
|
99
|
-
else {
|
|
100
|
-
this.fill(`parameters.abi.parameters.${key}`, abiParams[key]);
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
}
|
|
104
|
-
fillMethod() {
|
|
105
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
106
|
-
const executeMethod = (target) => __awaiter(this, void 0, void 0, function* () {
|
|
107
|
-
const methodPattern = /\{\{(\w+)\(([^)]+)\)\}\}/g;
|
|
108
|
-
// Find all matches of the pattern in the target string
|
|
109
|
-
const matches = [...target.matchAll(methodPattern)];
|
|
110
|
-
// Process each match asynchronously
|
|
111
|
-
for (const match of matches) {
|
|
112
|
-
const [fullMatch, methodName, params] = match;
|
|
113
|
-
const paramList = params.split(',').map((param) => param.trim());
|
|
114
|
-
let replacement;
|
|
115
|
-
switch (methodName) {
|
|
116
|
-
case 'tokenSymbol': {
|
|
117
|
-
const [chainId, address] = paramList;
|
|
118
|
-
const token = yield getToken(parseInt(chainId, 10), address);
|
|
119
|
-
replacement = token ? token.symbol : fullMatch;
|
|
120
|
-
break;
|
|
121
|
-
}
|
|
122
|
-
case 'otherTokenSymbol': {
|
|
123
|
-
const [chainId, address] = paramList;
|
|
124
|
-
const symbol = yield getDifferentToken(parseInt(chainId, 10), address);
|
|
125
|
-
replacement = symbol;
|
|
126
|
-
break;
|
|
127
|
-
}
|
|
128
|
-
// Add other methods here as needed
|
|
129
|
-
default:
|
|
130
|
-
replacement = fullMatch; // If no method matches, leave it unchanged
|
|
131
|
-
}
|
|
132
|
-
// Replace the matched pattern with the resolved value
|
|
133
|
-
target = target.replace(fullMatch, replacement || fullMatch);
|
|
134
|
-
}
|
|
135
|
-
return target;
|
|
136
|
-
});
|
|
137
|
-
// Apply executeMethod asynchronously to approvedTargets, label, and labelNotAuthorized
|
|
138
|
-
this.approvedTargets = yield Promise.all(this.approvedTargets.map(executeMethod));
|
|
139
|
-
this.label = yield Promise.all(this.label.map(executeMethod));
|
|
140
|
-
this.labelNotAuthorized = yield Promise.all(this.labelNotAuthorized.map(executeMethod));
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
toJSON() {
|
|
144
|
-
const containsPlaceholder = this.approvedTargets.some(target => target.includes('$'));
|
|
145
|
-
if (containsPlaceholder) {
|
|
146
|
-
throw new Error('Approved targets contain unresolved placeholders.');
|
|
147
|
-
}
|
|
148
|
-
return {
|
|
149
|
-
approvedTargets: this.approvedTargets,
|
|
150
|
-
label: this.label,
|
|
151
|
-
labelNotAuthorized: this.labelNotAuthorized,
|
|
152
|
-
};
|
|
153
|
-
}
|
|
154
|
-
static fromJSON(json) {
|
|
155
|
-
if (!json || !Array.isArray(json.approvedTargets)) {
|
|
156
|
-
console.error('Invalid JSON object for SessionKeyPermission:', json);
|
|
157
|
-
throw new Error('Invalid JSON object for SessionKeyPermission');
|
|
158
|
-
}
|
|
159
|
-
return new SessionKeyPermission(json);
|
|
160
|
-
}
|
|
161
|
-
merge(other) {
|
|
162
|
-
// Merge approvedTargets
|
|
163
|
-
const uniqueTargets = new Set(this.approvedTargets);
|
|
164
|
-
other.approvedTargets.forEach(target => uniqueTargets.add(target));
|
|
165
|
-
this.approvedTargets = Array.from(uniqueTargets);
|
|
166
|
-
// Merge labels
|
|
167
|
-
const uniqueLabels = new Set(this.label);
|
|
168
|
-
other.label.forEach(lbl => uniqueLabels.add(lbl));
|
|
169
|
-
this.label = Array.from(uniqueLabels);
|
|
170
|
-
// Merge labelNotAuthorized
|
|
171
|
-
const uniqueLabelsNotAuthorized = new Set(this.labelNotAuthorized);
|
|
172
|
-
other.labelNotAuthorized.forEach(lbl => uniqueLabelsNotAuthorized.add(lbl));
|
|
173
|
-
this.labelNotAuthorized = Array.from(uniqueLabelsNotAuthorized);
|
|
174
|
-
// Remove labels from labelNotAuthorized if they are present in label
|
|
175
|
-
this.labelNotAuthorized = this.labelNotAuthorized.filter(lbl => !this.label.includes(lbl));
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
// Helper function to get a different token
|
|
179
|
-
const getDifferentToken = (chain, contractAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
180
|
-
var _a;
|
|
181
|
-
const tokenSymbol = (_a = (yield getToken(chain, contractAddress))) === null || _a === void 0 ? void 0 : _a.symbol;
|
|
182
|
-
if (!tokenSymbol || tokenSymbol !== 'WETH')
|
|
183
|
-
return 'WETH';
|
|
184
|
-
return 'USDT';
|
|
185
|
-
});
|
|
1
|
+
export {};
|
|
@@ -10,7 +10,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
10
10
|
import { Node } from './Node.js';
|
|
11
11
|
import { Edge } from './Edge.js';
|
|
12
12
|
import { apiServices } from '../services/ApiService.js';
|
|
13
|
-
import { SessionKeyPermission } from './SessionKeyPermission.js';
|
|
14
13
|
export class Workflow {
|
|
15
14
|
constructor(name = '', nodes = [], edges = []) {
|
|
16
15
|
this.id = null;
|
|
@@ -93,6 +92,7 @@ export class Workflow {
|
|
|
93
92
|
}
|
|
94
93
|
}
|
|
95
94
|
catch (error) {
|
|
95
|
+
console.log(error);
|
|
96
96
|
return { success: false, error: error.message || 'Unknown error' };
|
|
97
97
|
}
|
|
98
98
|
});
|
|
@@ -207,21 +207,9 @@ export class Workflow {
|
|
|
207
207
|
}
|
|
208
208
|
getSessionKeyPermissions() {
|
|
209
209
|
return __awaiter(this, void 0, void 0, function* () {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
if (node.class === 'action' && 'getSessionKeyPermissions' in node) {
|
|
214
|
-
const nodePermissions = yield node.getSessionKeyPermissions();
|
|
215
|
-
if (nodePermissions) {
|
|
216
|
-
permissions.merge(nodePermissions);
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
}
|
|
220
|
-
catch (error) {
|
|
221
|
-
console.error(`Error getting session key permissions for node ${node.getRef()}:`, error);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
return permissions;
|
|
210
|
+
if (!this.id)
|
|
211
|
+
throw new Error('The workflow needs to be published first');
|
|
212
|
+
return apiServices.getSessionKeyPermissions(this.id);
|
|
225
213
|
});
|
|
226
214
|
}
|
|
227
215
|
}
|
|
@@ -81,10 +81,31 @@ class ApiServices {
|
|
|
81
81
|
}
|
|
82
82
|
getWorkflowsOfUser() {
|
|
83
83
|
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
if (!this.auth) {
|
|
85
|
+
throw new Error('Authorization token is required');
|
|
86
|
+
}
|
|
84
87
|
const headers = this.auth ? { 'Authorization': this.auth } : {};
|
|
85
88
|
const response = yield axiosInstance.get('/workflows', { headers });
|
|
86
89
|
return response.data;
|
|
87
90
|
});
|
|
88
91
|
}
|
|
92
|
+
getSessionKeyPermissions(workflowId) {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
var _a, _b, _c;
|
|
95
|
+
if (!this.auth) {
|
|
96
|
+
throw new Error('Authorization token is required');
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
const url = `/workflows/${workflowId}/verify-contracts`;
|
|
100
|
+
const headers = { Authorization: this.auth };
|
|
101
|
+
const response = yield axiosInstance.post(url, {}, { headers });
|
|
102
|
+
return response.data; // Return the data from the response
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error('Error verifying contracts:', ((_a = error.response) === null || _a === void 0 ? void 0 : _a.data) || error.message);
|
|
106
|
+
throw new Error(((_c = (_b = error.response) === null || _b === void 0 ? void 0 : _b.data) === null || _c === void 0 ? void 0 : _c.message) || 'Failed to verify contracts');
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
}
|
|
89
110
|
}
|
|
90
111
|
export const apiServices = new ApiServices();
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
import { Parameter } from './Parameter.js';
|
|
2
2
|
import { Node, ParentInfo, Position, NodeState } from './Node.js';
|
|
3
|
-
import { SessionKeyPermission } from './SessionKeyPermission.js';
|
|
4
3
|
export declare class Action extends Node {
|
|
5
4
|
constructor(action: {
|
|
6
5
|
blockId: number;
|
|
@@ -16,12 +15,6 @@ export declare class Action extends Node {
|
|
|
16
15
|
parentInfo?: ParentInfo;
|
|
17
16
|
state?: NodeState;
|
|
18
17
|
});
|
|
19
|
-
getSessionKeyPermissions(): Promise<SessionKeyPermission | null>;
|
|
20
|
-
/**
|
|
21
|
-
* Replace any placeholders like {{parameters.tokenToDeposit}} with actual values
|
|
22
|
-
* from the parameters of the current action. Supports both strings and arrays.
|
|
23
|
-
*/
|
|
24
|
-
replaceVariables(value: any): any;
|
|
25
18
|
static fromJSON(json: {
|
|
26
19
|
[key: string]: any;
|
|
27
20
|
}): Promise<Action>;
|
|
@@ -1,29 +1,6 @@
|
|
|
1
|
-
export
|
|
1
|
+
export type SessionKeyPermission = {
|
|
2
|
+
isValid: boolean;
|
|
2
3
|
approvedTargets: string[];
|
|
3
4
|
label: string[];
|
|
4
5
|
labelNotAuthorized: string[];
|
|
5
|
-
|
|
6
|
-
approvedTargets?: string[];
|
|
7
|
-
label?: string[];
|
|
8
|
-
labelNotAuthorized?: string[];
|
|
9
|
-
});
|
|
10
|
-
fill(key: string, value: any): void;
|
|
11
|
-
/**
|
|
12
|
-
* Fill placeholders using variables returned by the 'before' string execution.
|
|
13
|
-
* @param beforeCode - A string representing the 'before' logic to execute.
|
|
14
|
-
*/
|
|
15
|
-
fillBeforeVariables(beforeCode: string, parameters: {
|
|
16
|
-
[key: string]: any;
|
|
17
|
-
}): Promise<void>;
|
|
18
|
-
fillParameters(params: {
|
|
19
|
-
[key: string]: any;
|
|
20
|
-
}): void;
|
|
21
|
-
fillMethod(): Promise<void>;
|
|
22
|
-
toJSON(): {
|
|
23
|
-
[key: string]: any;
|
|
24
|
-
};
|
|
25
|
-
static fromJSON(json: {
|
|
26
|
-
[key: string]: any;
|
|
27
|
-
}): SessionKeyPermission;
|
|
28
|
-
merge(other: SessionKeyPermission): void;
|
|
29
|
-
}
|
|
6
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|