otomato-sdk 1.5.52 → 1.5.53
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/constants/tokens.js +25 -8
- package/dist/src/models/Action.js +2 -3
- package/dist/src/models/SessionKeyPermission.js +37 -24
- package/dist/src/services/RpcServices.js +73 -0
- package/dist/types/src/constants/tokens.d.ts +1 -1
- package/dist/types/src/models/SessionKeyPermission.d.ts +1 -1
- package/dist/types/src/services/RpcServices.d.ts +14 -0
- package/dist/types/test/rpcServices.spec.d.ts +1 -0
- package/package.json +1 -1
|
@@ -1,3 +1,13 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
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 rpcServices from "../services/RpcServices";
|
|
1
11
|
export const TOKENS = {
|
|
2
12
|
1: [
|
|
3
13
|
{
|
|
@@ -233,14 +243,21 @@ export const NFTS = {
|
|
|
233
243
|
]
|
|
234
244
|
};
|
|
235
245
|
export function getToken(chain, contractAddress) {
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
246
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
247
|
+
if (!(chain in TOKENS)) {
|
|
248
|
+
throw new Error(`Unsupported chain: ${chain}`);
|
|
249
|
+
}
|
|
250
|
+
let token = TOKENS[chain].find(token => token.contractAddress.toLowerCase() === contractAddress.toLowerCase());
|
|
251
|
+
if (!token) {
|
|
252
|
+
try {
|
|
253
|
+
token = yield rpcServices.getTokenDetails(chain, contractAddress);
|
|
254
|
+
}
|
|
255
|
+
catch (e) {
|
|
256
|
+
throw new Error(`Token with contract address ${contractAddress} not found on chain ${chain}`);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
return token;
|
|
260
|
+
});
|
|
244
261
|
}
|
|
245
262
|
export function getTokenFromSymbol(chain, symbol) {
|
|
246
263
|
if (!(chain in TOKENS)) {
|
|
@@ -22,7 +22,7 @@ export class Action extends Node {
|
|
|
22
22
|
return null;
|
|
23
23
|
const permissions = SessionKeyPermission.fromJSON(parentBlock.permissions);
|
|
24
24
|
permissions.fillParameters(this.getParameters());
|
|
25
|
-
permissions.fillMethod();
|
|
25
|
+
yield permissions.fillMethod();
|
|
26
26
|
// Handle 'before' code for the main action
|
|
27
27
|
if (parentBlock.before) {
|
|
28
28
|
yield permissions.fillBeforeVariables(parentBlock.before, this.getParameters());
|
|
@@ -54,7 +54,7 @@ export class Action extends Node {
|
|
|
54
54
|
if (!batchedPermissions)
|
|
55
55
|
continue;
|
|
56
56
|
batchedPermissions.fillParameters(batch.parameters); // Pass batched action parameters
|
|
57
|
-
batchedPermissions.fillMethod();
|
|
57
|
+
yield batchedPermissions.fillMethod();
|
|
58
58
|
// Handle 'before' code for the batched action
|
|
59
59
|
if (batchedAction.before) {
|
|
60
60
|
yield batchedPermissions.fillBeforeVariables(batchedAction.before, batch.parameters);
|
|
@@ -74,7 +74,6 @@ export class Action extends Node {
|
|
|
74
74
|
replaceVariables(value) {
|
|
75
75
|
if (typeof value === 'string') {
|
|
76
76
|
return value.replace(/\{\{parameters\.(.*?)\}\}/g, (_, key) => {
|
|
77
|
-
console.log(`Looking for ${key} parameter`);
|
|
78
77
|
return this.getParameter(key) || '';
|
|
79
78
|
});
|
|
80
79
|
}
|
|
@@ -102,30 +102,43 @@ export class SessionKeyPermission {
|
|
|
102
102
|
}
|
|
103
103
|
}
|
|
104
104
|
fillMethod() {
|
|
105
|
-
|
|
106
|
-
const
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
|
119
131
|
}
|
|
120
|
-
//
|
|
121
|
-
|
|
122
|
-
return match;
|
|
132
|
+
// Replace the matched pattern with the resolved value
|
|
133
|
+
target = target.replace(fullMatch, replacement || fullMatch);
|
|
123
134
|
}
|
|
135
|
+
return target;
|
|
124
136
|
});
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
+
});
|
|
129
142
|
}
|
|
130
143
|
toJSON() {
|
|
131
144
|
const containsPlaceholder = this.approvedTargets.some(target => target.includes('$'));
|
|
@@ -163,10 +176,10 @@ export class SessionKeyPermission {
|
|
|
163
176
|
}
|
|
164
177
|
}
|
|
165
178
|
// Helper function to get a different token
|
|
166
|
-
const getDifferentToken = (chain, contractAddress) => {
|
|
179
|
+
const getDifferentToken = (chain, contractAddress) => __awaiter(void 0, void 0, void 0, function* () {
|
|
167
180
|
var _a;
|
|
168
|
-
const tokenSymbol = (_a = getToken(chain, contractAddress)) === null || _a === void 0 ? void 0 : _a.symbol;
|
|
181
|
+
const tokenSymbol = (_a = (yield getToken(chain, contractAddress))) === null || _a === void 0 ? void 0 : _a.symbol;
|
|
169
182
|
if (!tokenSymbol || tokenSymbol !== 'WETH')
|
|
170
183
|
return 'WETH';
|
|
171
184
|
return 'USDT';
|
|
172
|
-
};
|
|
185
|
+
});
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
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 { ethers } from 'ethers';
|
|
11
|
+
class RPCServices {
|
|
12
|
+
constructor() {
|
|
13
|
+
this.rpcUrls = {}; // Store the RPC URLs
|
|
14
|
+
}
|
|
15
|
+
// Function to set RPC URLs
|
|
16
|
+
setRPCs(rpcs) {
|
|
17
|
+
this.rpcUrls = Object.assign(Object.assign({}, this.rpcUrls), rpcs);
|
|
18
|
+
}
|
|
19
|
+
setRPCsFromTMS(env) {
|
|
20
|
+
if (env.MODE_HTTPS_PROVIDER) {
|
|
21
|
+
this.rpcUrls[43334] = env.MODE_HTTPS_PROVIDER; // Chain ID 43334 is for Mode network
|
|
22
|
+
}
|
|
23
|
+
if (env.INFURA_HTTPS_PROVIDER) {
|
|
24
|
+
this.rpcUrls[1] = env.INFURA_HTTPS_PROVIDER; // Chain ID 1 is for Ethereum mainnet
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
// Function to get the RPC URL for a specific chainId
|
|
28
|
+
getRPC(chainId) {
|
|
29
|
+
const rpcUrl = this.rpcUrls[chainId];
|
|
30
|
+
if (!rpcUrl) {
|
|
31
|
+
throw new Error(`No RPC URL configured for chain ID ${chainId}`);
|
|
32
|
+
}
|
|
33
|
+
return rpcUrl;
|
|
34
|
+
}
|
|
35
|
+
// Function to fetch token details from the blockchain
|
|
36
|
+
getTokenDetails(chainId, contractAddress) {
|
|
37
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
38
|
+
const rpcUrl = this.getRPC(chainId); // Get the RPC URL for the chain
|
|
39
|
+
// Create a provider using ethers.js
|
|
40
|
+
const provider = new ethers.JsonRpcProvider(rpcUrl);
|
|
41
|
+
// Define the contract ABI for the read-only functions (decimals, name, symbol)
|
|
42
|
+
const erc20ABI = [
|
|
43
|
+
'function decimals() view returns (uint8)',
|
|
44
|
+
'function name() view returns (string)',
|
|
45
|
+
'function symbol() view returns (string)',
|
|
46
|
+
];
|
|
47
|
+
// Create a contract instance
|
|
48
|
+
const contract = new ethers.Contract(contractAddress, erc20ABI, provider);
|
|
49
|
+
// Fetch the decimals, name, and symbol in parallel using Promise.all
|
|
50
|
+
try {
|
|
51
|
+
const [decimals, name, symbol] = yield Promise.all([
|
|
52
|
+
contract.decimals(),
|
|
53
|
+
contract.name(),
|
|
54
|
+
contract.symbol(),
|
|
55
|
+
]);
|
|
56
|
+
const token = {
|
|
57
|
+
contractAddress,
|
|
58
|
+
symbol,
|
|
59
|
+
name,
|
|
60
|
+
decimals,
|
|
61
|
+
image: null,
|
|
62
|
+
};
|
|
63
|
+
return token;
|
|
64
|
+
}
|
|
65
|
+
catch (error) {
|
|
66
|
+
throw new Error(`Error fetching token details: ${error.message}`);
|
|
67
|
+
}
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
// Export RPC services instance
|
|
72
|
+
const rpcServices = new RPCServices();
|
|
73
|
+
export default rpcServices;
|
|
@@ -18,6 +18,6 @@ export interface NFTs {
|
|
|
18
18
|
[key: number]: NFT[];
|
|
19
19
|
}
|
|
20
20
|
export declare const NFTS: NFTs;
|
|
21
|
-
export declare function getToken(chain: number, contractAddress: string): Token
|
|
21
|
+
export declare function getToken(chain: number, contractAddress: string): Promise<Token>;
|
|
22
22
|
export declare function getTokenFromSymbol(chain: number, symbol: string): Token;
|
|
23
23
|
export declare function getNFT(chain: number, name: string): NFT;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { Token } from '../constants/tokens';
|
|
2
|
+
declare class RPCServices {
|
|
3
|
+
private rpcUrls;
|
|
4
|
+
setRPCs(rpcs: {
|
|
5
|
+
[chainId: number]: string;
|
|
6
|
+
}): void;
|
|
7
|
+
setRPCsFromTMS(env: {
|
|
8
|
+
[key: string]: string;
|
|
9
|
+
}): void;
|
|
10
|
+
getRPC(chainId: number): string;
|
|
11
|
+
getTokenDetails(chainId: number, contractAddress: string): Promise<Token>;
|
|
12
|
+
}
|
|
13
|
+
declare const rpcServices: RPCServices;
|
|
14
|
+
export default rpcServices;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|