@tonappchain/sdk 0.7.0-rc24-test-8 ā 0.7.0-rc24-test-10
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/artifacts/dev/tac/artifacts.js +2 -2
- package/dist/artifacts/dev/tac/wrappers.d.ts +4 -4
- package/dist/artifacts/mainnet/tac/artifacts.js +2 -2
- package/dist/artifacts/mainnet/tac/wrappers.d.ts +4 -4
- package/dist/artifacts/tacTypes.d.ts +1 -1
- package/dist/artifacts/testnet/tac/artifacts.js +2 -2
- package/dist/artifacts/testnet/tac/wrappers.d.ts +4 -4
- package/dist/artifacts/tonTypes.d.ts +1 -1
- package/dist/src/adapters/contractOpener.js +3 -1
- package/dist/src/agnosticSdk/AbiHandler.d.ts +23 -0
- package/dist/src/agnosticSdk/AbiHandler.js +105 -0
- package/dist/src/agnosticSdk/AgnosticSdk.d.ts +47 -217
- package/dist/src/agnosticSdk/AgnosticSdk.js +60 -595
- package/dist/src/agnosticSdk/AgnosticStructs.d.ts +108 -0
- package/dist/src/agnosticSdk/AgnosticStructs.js +23 -0
- package/dist/src/agnosticSdk/DebugHelpers.d.ts +88 -0
- package/dist/src/agnosticSdk/DebugHelpers.js +274 -0
- package/dist/src/agnosticSdk/HooksHandler.d.ts +43 -0
- package/dist/src/agnosticSdk/HooksHandler.js +102 -0
- package/dist/src/agnosticSdk/ReplacementHelper.d.ts +95 -0
- package/dist/src/agnosticSdk/ReplacementHelper.js +233 -0
- package/dist/src/assets/NFT.js +3 -3
- package/dist/src/errors/index.d.ts +1 -1
- package/dist/src/index.d.ts +2 -1
- package/dist/src/index.js +26 -2
- package/dist/src/sdk/Configuration.js +5 -5
- package/dist/src/sdk/OperationTracker.js +37 -14
- package/dist/src/sdk/TONTransactionManager.js +3 -2
- package/dist/src/sdk/TacSdk.js +4 -37
- package/dist/src/structs/InternalStruct.d.ts +1 -1
- package/dist/src/wrappers/ContentUtils.js +0 -1
- package/package.json +1 -1
|
@@ -1,37 +1,20 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.AgnosticProxySDK =
|
|
4
|
-
const ethers_1 = require("ethers");
|
|
3
|
+
exports.AgnosticProxySDK = void 0;
|
|
5
4
|
const mainnet_1 = require("../../artifacts/mainnet");
|
|
6
5
|
const testnet_1 = require("../../artifacts/testnet");
|
|
7
6
|
const Struct_1 = require("../structs/Struct");
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
* @param FullBalanceTransfer - The full balance transfer hook
|
|
13
|
-
*/
|
|
14
|
-
var HookType;
|
|
15
|
-
(function (HookType) {
|
|
16
|
-
HookType[HookType["Custom"] = 0] = "Custom";
|
|
17
|
-
HookType[HookType["FullBalanceApprove"] = 1] = "FullBalanceApprove";
|
|
18
|
-
HookType[HookType["FullBalanceTransfer"] = 2] = "FullBalanceTransfer";
|
|
19
|
-
})(HookType || (exports.HookType = HookType = {}));
|
|
20
|
-
/**
|
|
21
|
-
* ReplacementType is an enum that contains the type of the replacement
|
|
22
|
-
* @param Amount - The amount replacement
|
|
23
|
-
*/
|
|
24
|
-
var ReplacementType;
|
|
25
|
-
(function (ReplacementType) {
|
|
26
|
-
ReplacementType[ReplacementType["Amount"] = 0] = "Amount";
|
|
27
|
-
})(ReplacementType || (exports.ReplacementType = ReplacementType = {}));
|
|
7
|
+
const AbiHandler_1 = require("./AbiHandler");
|
|
8
|
+
const DebugHelpers_1 = require("./DebugHelpers");
|
|
9
|
+
const HooksHandler_1 = require("./HooksHandler");
|
|
10
|
+
const ReplacementHelper_1 = require("./ReplacementHelper");
|
|
28
11
|
/**
|
|
29
12
|
* SDK for building AgnosticProxy Zap calls with efficient hook encoding
|
|
13
|
+
* @param network - The network to use (MAINNET || TESTNET || DEV)
|
|
30
14
|
* @param agnosticProxyAddress - The address of the agnostic proxy(optional)
|
|
31
15
|
*/
|
|
32
16
|
class AgnosticProxySDK {
|
|
33
17
|
constructor(network, agnosticProxyAddress) {
|
|
34
|
-
this.contractInterfaces = new Map();
|
|
35
18
|
switch (network) {
|
|
36
19
|
case Struct_1.Network.MAINNET:
|
|
37
20
|
this.agnosticProxyAddress = agnosticProxyAddress ?? mainnet_1.AGNOSTIC_PROXY_ADDRESS;
|
|
@@ -41,103 +24,20 @@ class AgnosticProxySDK {
|
|
|
41
24
|
break;
|
|
42
25
|
case Struct_1.Network.DEV:
|
|
43
26
|
if (!agnosticProxyAddress) {
|
|
44
|
-
throw new Error(
|
|
27
|
+
throw new Error('Agnostic proxy address is required for dev network');
|
|
45
28
|
}
|
|
46
29
|
this.agnosticProxyAddress = agnosticProxyAddress;
|
|
47
30
|
break;
|
|
48
31
|
}
|
|
32
|
+
this.abiHandler = new AbiHandler_1.AbiHandler();
|
|
33
|
+
this.replacementHelper = new ReplacementHelper_1.ReplacementHelper();
|
|
34
|
+
this.hooksHandler = new HooksHandler_1.HooksHandler();
|
|
35
|
+
this.debugHelpers = new DebugHelpers_1.DebugHelpers();
|
|
49
36
|
}
|
|
50
|
-
/**
|
|
51
|
-
* Add a contract interface for encoding function calls
|
|
52
|
-
* Supports both human-readable ABI strings and generated ABI JSON objects
|
|
53
|
-
* @param address - The address of the contract to add interface for
|
|
54
|
-
* @param abi - The abi of the contract to add interface for
|
|
55
|
-
* @returns The sdk instance
|
|
56
|
-
*/
|
|
57
37
|
addContractInterface(address, abi) {
|
|
58
|
-
|
|
59
|
-
this.contractInterfaces.set(address.toLowerCase(), new ethers_1.Interface(parsedAbi));
|
|
38
|
+
this.abiHandler.addContractInterface(address, abi);
|
|
60
39
|
return this;
|
|
61
40
|
}
|
|
62
|
-
/**
|
|
63
|
-
* Parse ABI - handles both human-readable strings and generated ABI JSON objects
|
|
64
|
-
* Extracts only function definitions from JSON ABI
|
|
65
|
-
*/
|
|
66
|
-
_parseAbi(abi) {
|
|
67
|
-
const humanReadableAbi = [];
|
|
68
|
-
for (const item of abi) {
|
|
69
|
-
// If it's already a string (human-readable format), keep it
|
|
70
|
-
if (typeof item === 'string') {
|
|
71
|
-
humanReadableAbi.push(item);
|
|
72
|
-
continue;
|
|
73
|
-
}
|
|
74
|
-
// If it's a JSON ABI object, parse it
|
|
75
|
-
if (typeof item === 'object' && item.type) {
|
|
76
|
-
// Only process functions
|
|
77
|
-
if (item.type === 'function') {
|
|
78
|
-
const signature = this._buildFunctionSignature(item);
|
|
79
|
-
if (signature) {
|
|
80
|
-
humanReadableAbi.push(signature);
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
// Skip events, errors, constructor, etc.
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
return humanReadableAbi;
|
|
87
|
-
}
|
|
88
|
-
/**
|
|
89
|
-
* Build human-readable function signature from JSON ABI function object
|
|
90
|
-
* @param func - The function to build signature for
|
|
91
|
-
* @returns The signature of the function
|
|
92
|
-
*/
|
|
93
|
-
_buildFunctionSignature(func) {
|
|
94
|
-
if (!func.name || !Array.isArray(func.inputs)) {
|
|
95
|
-
return null;
|
|
96
|
-
}
|
|
97
|
-
// Build parameter list
|
|
98
|
-
const params = func.inputs.map((input) => {
|
|
99
|
-
let paramType = input.type;
|
|
100
|
-
// Handle array types
|
|
101
|
-
if (input.type.includes('[]')) {
|
|
102
|
-
paramType = input.type;
|
|
103
|
-
}
|
|
104
|
-
// Add parameter name if available
|
|
105
|
-
if (input.name) {
|
|
106
|
-
return `${paramType} ${input.name}`;
|
|
107
|
-
}
|
|
108
|
-
return paramType;
|
|
109
|
-
}).join(', ');
|
|
110
|
-
// Build return types if available
|
|
111
|
-
let returnTypes = '';
|
|
112
|
-
if (func.outputs && func.outputs.length > 0) {
|
|
113
|
-
const outputs = func.outputs.map((output) => {
|
|
114
|
-
const outputType = output.type;
|
|
115
|
-
if (output.name) {
|
|
116
|
-
return `${outputType} ${output.name}`;
|
|
117
|
-
}
|
|
118
|
-
return outputType;
|
|
119
|
-
}).join(', ');
|
|
120
|
-
if (func.outputs.length === 1) {
|
|
121
|
-
returnTypes = ` returns (${outputs})`;
|
|
122
|
-
}
|
|
123
|
-
else {
|
|
124
|
-
returnTypes = ` returns (${outputs})`;
|
|
125
|
-
}
|
|
126
|
-
}
|
|
127
|
-
// Build full signature
|
|
128
|
-
const stateMutability = func.stateMutability || 'nonpayable';
|
|
129
|
-
let mutabilityKeyword = '';
|
|
130
|
-
if (stateMutability === 'view') {
|
|
131
|
-
mutabilityKeyword = ' view';
|
|
132
|
-
}
|
|
133
|
-
else if (stateMutability === 'pure') {
|
|
134
|
-
mutabilityKeyword = ' pure';
|
|
135
|
-
}
|
|
136
|
-
else if (stateMutability === 'payable') {
|
|
137
|
-
mutabilityKeyword = ' payable';
|
|
138
|
-
}
|
|
139
|
-
return `function ${func.name}(${params})${mutabilityKeyword} external${returnTypes}`;
|
|
140
|
-
}
|
|
141
41
|
/**
|
|
142
42
|
* Create a custom hook with optional dynamic value replacement
|
|
143
43
|
* @param contractAddress - The address of the contract to call
|
|
@@ -147,40 +47,7 @@ class AgnosticProxySDK {
|
|
|
147
47
|
* @returns The custom hook
|
|
148
48
|
*/
|
|
149
49
|
createCustomHook(contractAddress, functionName, params, options = {}) {
|
|
150
|
-
|
|
151
|
-
const contractInterface = this.contractInterfaces.get(contractAddress.toLowerCase());
|
|
152
|
-
if (!contractInterface) {
|
|
153
|
-
throw new Error(`Contract interface not found for address: ${contractAddress}`);
|
|
154
|
-
}
|
|
155
|
-
const data = contractInterface.encodeFunctionData(functionName, params);
|
|
156
|
-
let improvedMissionInfo = "0x";
|
|
157
|
-
// If dynamic replacements are specified, encode them
|
|
158
|
-
if (dynamicReplacements && dynamicReplacements.length > 0) {
|
|
159
|
-
// For now, support single replacement (can be extended)
|
|
160
|
-
const replacement = dynamicReplacements[0];
|
|
161
|
-
const replacementData = ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(["tuple(uint16,uint16,address,address)"], [[replacement.position, replacement.len, replacement.token, replacement.balanceAddress]]);
|
|
162
|
-
// Encode as ReplacementType.Amount with the replacement data
|
|
163
|
-
improvedMissionInfo = ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(["uint8", "bytes"], [ReplacementType.Amount, replacementData]);
|
|
164
|
-
}
|
|
165
|
-
const customHookData = {
|
|
166
|
-
isFromSAPerspective,
|
|
167
|
-
contractAddress,
|
|
168
|
-
value,
|
|
169
|
-
data,
|
|
170
|
-
improvedMissionInfo
|
|
171
|
-
};
|
|
172
|
-
// Encode only the CustomHookData
|
|
173
|
-
const encodedHookData = ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(["tuple(bool,address,uint256,bytes,bytes)"], [[
|
|
174
|
-
customHookData.isFromSAPerspective,
|
|
175
|
-
customHookData.contractAddress,
|
|
176
|
-
customHookData.value,
|
|
177
|
-
customHookData.data,
|
|
178
|
-
customHookData.improvedMissionInfo
|
|
179
|
-
]]);
|
|
180
|
-
return {
|
|
181
|
-
hookType: HookType.Custom,
|
|
182
|
-
hookData: encodedHookData
|
|
183
|
-
};
|
|
50
|
+
return this.hooksHandler.createCustomHook(contractAddress, functionName, params, this.abiHandler.contractInterfaces, options);
|
|
184
51
|
}
|
|
185
52
|
/**
|
|
186
53
|
* Create a full balance approve hook
|
|
@@ -190,17 +57,7 @@ class AgnosticProxySDK {
|
|
|
190
57
|
* @returns The full balance approve hook
|
|
191
58
|
*/
|
|
192
59
|
createFullBalanceApproveHook(token, to, isFromSAPerspective = true) {
|
|
193
|
-
|
|
194
|
-
token,
|
|
195
|
-
to,
|
|
196
|
-
isFromSAPerspective
|
|
197
|
-
};
|
|
198
|
-
// Encode only the ApproveHookData
|
|
199
|
-
const encodedHookData = ethers_1.ethers.AbiCoder.defaultAbiCoder().encode(["tuple(address,address,bool)"], [[approveHookData.token, approveHookData.to, approveHookData.isFromSAPerspective]]);
|
|
200
|
-
return {
|
|
201
|
-
hookType: HookType.FullBalanceApprove,
|
|
202
|
-
hookData: encodedHookData
|
|
203
|
-
};
|
|
60
|
+
return this.hooksHandler.createFullBalanceApproveHook(token, to, isFromSAPerspective);
|
|
204
61
|
}
|
|
205
62
|
/**
|
|
206
63
|
* Create a full balance transfer hook
|
|
@@ -210,17 +67,23 @@ class AgnosticProxySDK {
|
|
|
210
67
|
* @returns The full balance transfer hook
|
|
211
68
|
*/
|
|
212
69
|
createFullBalanceTransferHook(token, to, isFromSAPerspective = true) {
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
70
|
+
return this.hooksHandler.createFullBalanceTransferHook(token, to, isFromSAPerspective);
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Utility: Create multiple approve hooks at once
|
|
74
|
+
* @param approvals - The approvals to create
|
|
75
|
+
* @returns The multiple approve hooks
|
|
76
|
+
*/
|
|
77
|
+
createMultipleApproves(approvals) {
|
|
78
|
+
return this.hooksHandler.createMultipleApproves(approvals);
|
|
79
|
+
}
|
|
80
|
+
/**
|
|
81
|
+
* Utility: Create a sequence of custom hooks
|
|
82
|
+
* @param calls - The calls to create
|
|
83
|
+
* @returns The hook sequence
|
|
84
|
+
*/
|
|
85
|
+
createHookSequence(calls) {
|
|
86
|
+
return calls.map((call) => this.createCustomHook(call.contract, call.functionName, call.params, call.options));
|
|
224
87
|
}
|
|
225
88
|
/**
|
|
226
89
|
* Helper to create dynamic amount replacement for a specific parameter
|
|
@@ -230,14 +93,7 @@ class AgnosticProxySDK {
|
|
|
230
93
|
* @returns The amount replacement
|
|
231
94
|
*/
|
|
232
95
|
createAmountReplacement(paramIndex, token, balanceAddress) {
|
|
233
|
-
|
|
234
|
-
const position = 4 + (paramIndex * 32);
|
|
235
|
-
return {
|
|
236
|
-
position,
|
|
237
|
-
len: 32, // uint256 is 32 bytes
|
|
238
|
-
token,
|
|
239
|
-
balanceAddress
|
|
240
|
-
};
|
|
96
|
+
return this.replacementHelper.createAmountReplacement(paramIndex, token, balanceAddress);
|
|
241
97
|
}
|
|
242
98
|
/**
|
|
243
99
|
* Advanced replacement calculator - calculates position and length for any parameter type
|
|
@@ -251,35 +107,7 @@ class AgnosticProxySDK {
|
|
|
251
107
|
* @returns The replacement data
|
|
252
108
|
*/
|
|
253
109
|
calculateReplacementData(contractAddress, functionName, parameterName, token, balanceAddress) {
|
|
254
|
-
|
|
255
|
-
if (!contractInterface) {
|
|
256
|
-
throw new Error(`Contract interface not found for address: ${contractAddress}. Please add it first using addContractInterface().`);
|
|
257
|
-
}
|
|
258
|
-
let functionFragment;
|
|
259
|
-
try {
|
|
260
|
-
functionFragment = contractInterface.getFunction(functionName);
|
|
261
|
-
}
|
|
262
|
-
catch {
|
|
263
|
-
throw new Error(`Function '${functionName}' not found in contract interface for ${contractAddress}`);
|
|
264
|
-
}
|
|
265
|
-
if (!functionFragment) {
|
|
266
|
-
throw new Error(`Function '${functionName}' not found in contract interface for ${contractAddress}`);
|
|
267
|
-
}
|
|
268
|
-
// Find the parameter by name
|
|
269
|
-
const paramIndex = functionFragment.inputs.findIndex(input => input.name === parameterName);
|
|
270
|
-
if (paramIndex === -1) {
|
|
271
|
-
const availableParams = functionFragment.inputs.map(input => `${input.name} (${input.type})`).join(', ');
|
|
272
|
-
throw new Error(`Parameter '${parameterName}' not found in function '${functionName}'. Available parameters: ${availableParams}`);
|
|
273
|
-
}
|
|
274
|
-
const param = functionFragment.inputs[paramIndex];
|
|
275
|
-
// Calculate position and length based on parameter type
|
|
276
|
-
const { position, len } = this._calculateParamPositionAndLength(functionFragment.inputs, paramIndex);
|
|
277
|
-
return {
|
|
278
|
-
position,
|
|
279
|
-
len,
|
|
280
|
-
token,
|
|
281
|
-
balanceAddress
|
|
282
|
-
};
|
|
110
|
+
return this.replacementHelper.calculateReplacementData(contractAddress, functionName, parameterName, token, balanceAddress, this.abiHandler.contractInterfaces);
|
|
283
111
|
}
|
|
284
112
|
/**
|
|
285
113
|
* Get replacement helper - shows available functions and parameters for a contract
|
|
@@ -289,33 +117,7 @@ class AgnosticProxySDK {
|
|
|
289
117
|
* @returns The replacement helper
|
|
290
118
|
*/
|
|
291
119
|
getReplacementHelper(contractAddress) {
|
|
292
|
-
|
|
293
|
-
if (!contractInterface) {
|
|
294
|
-
throw new Error(`Contract interface not found for address: ${contractAddress}. Please add it first using addContractInterface().`);
|
|
295
|
-
}
|
|
296
|
-
const functions = contractInterface.fragments
|
|
297
|
-
.filter(fragment => fragment.type === 'function')
|
|
298
|
-
.map(fragment => {
|
|
299
|
-
const func = fragment;
|
|
300
|
-
return {
|
|
301
|
-
name: func.name,
|
|
302
|
-
signature: func.format('full'),
|
|
303
|
-
parameters: func.inputs.map((input, index) => {
|
|
304
|
-
const canReplace = this._canParameterBeReplaced(input.type);
|
|
305
|
-
return {
|
|
306
|
-
name: input.name || `param${index}`,
|
|
307
|
-
type: input.type,
|
|
308
|
-
index,
|
|
309
|
-
canReplace: canReplace.canReplace,
|
|
310
|
-
reason: canReplace.reason
|
|
311
|
-
};
|
|
312
|
-
})
|
|
313
|
-
};
|
|
314
|
-
});
|
|
315
|
-
return {
|
|
316
|
-
contractAddress,
|
|
317
|
-
functions
|
|
318
|
-
};
|
|
120
|
+
return this.replacementHelper.getReplacementHelper(contractAddress, this.abiHandler.contractInterfaces);
|
|
319
121
|
}
|
|
320
122
|
/**
|
|
321
123
|
* Interactive replacement builder - helps build replacement step by step
|
|
@@ -328,177 +130,7 @@ class AgnosticProxySDK {
|
|
|
328
130
|
* @returns The interactive replacement builder
|
|
329
131
|
*/
|
|
330
132
|
buildReplacementInteractive(contractAddress, functionName, parameterName, token, balanceAddress, options = {}) {
|
|
331
|
-
|
|
332
|
-
// Get the replacement data
|
|
333
|
-
const replacement = this.calculateReplacementData(contractAddress, functionName, parameterName, token, balanceAddress);
|
|
334
|
-
// Get function info for calculation details
|
|
335
|
-
const contractInterface = this.contractInterfaces.get(contractAddress.toLowerCase());
|
|
336
|
-
if (!contractInterface) {
|
|
337
|
-
throw new Error(`Contract interface not found for address: ${contractAddress}`);
|
|
338
|
-
}
|
|
339
|
-
const functionFragment = contractInterface.getFunction(functionName);
|
|
340
|
-
if (!functionFragment) {
|
|
341
|
-
throw new Error(`Function '${functionName}' not found in contract interface for ${contractAddress}`);
|
|
342
|
-
}
|
|
343
|
-
const paramIndex = functionFragment.inputs.findIndex(input => input.name === parameterName);
|
|
344
|
-
const param = functionFragment.inputs[paramIndex];
|
|
345
|
-
const calculation = {
|
|
346
|
-
functionSignature: functionFragment.format('full'),
|
|
347
|
-
parameterInfo: {
|
|
348
|
-
name: parameterName,
|
|
349
|
-
type: param.type,
|
|
350
|
-
index: paramIndex,
|
|
351
|
-
position: replacement.position,
|
|
352
|
-
length: replacement.len
|
|
353
|
-
},
|
|
354
|
-
positionCalculation: `Position = 4 bytes (selector) + ${paramIndex} * 32 bytes = ${replacement.position} bytes`
|
|
355
|
-
};
|
|
356
|
-
// Validation
|
|
357
|
-
const validation = {
|
|
358
|
-
isValid: true,
|
|
359
|
-
warnings: [],
|
|
360
|
-
suggestions: []
|
|
361
|
-
};
|
|
362
|
-
if (validate) {
|
|
363
|
-
// Check if parameter type is suitable for replacement
|
|
364
|
-
const typeCheck = this._canParameterBeReplaced(param.type);
|
|
365
|
-
if (!typeCheck.canReplace) {
|
|
366
|
-
validation.isValid = false;
|
|
367
|
-
validation.warnings.push(`Parameter type '${param.type}' ${typeCheck.reason}`);
|
|
368
|
-
}
|
|
369
|
-
// Check if it's a reasonable parameter to replace
|
|
370
|
-
if (param.name.toLowerCase().includes('amount') || param.name.toLowerCase().includes('value')) {
|
|
371
|
-
validation.suggestions.push(`ā
Parameter '${param.name}' looks suitable for dynamic replacement`);
|
|
372
|
-
}
|
|
373
|
-
else {
|
|
374
|
-
validation.warnings.push(`ā ļø Parameter '${param.name}' might not be intended for amount replacement`);
|
|
375
|
-
}
|
|
376
|
-
// Check token address
|
|
377
|
-
if (!ethers_1.ethers.isAddress(token)) {
|
|
378
|
-
validation.isValid = false;
|
|
379
|
-
validation.warnings.push(`Invalid token address: ${token}`);
|
|
380
|
-
}
|
|
381
|
-
if (!ethers_1.ethers.isAddress(balanceAddress)) {
|
|
382
|
-
validation.isValid = false;
|
|
383
|
-
validation.warnings.push(`Invalid balance address: ${balanceAddress}`);
|
|
384
|
-
}
|
|
385
|
-
}
|
|
386
|
-
return {
|
|
387
|
-
replacement,
|
|
388
|
-
calculation,
|
|
389
|
-
validation
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
/**
|
|
393
|
-
* Private helper to calculate position and length for complex parameter types
|
|
394
|
-
* @param inputs - The inputs of the function
|
|
395
|
-
* @param targetIndex - The index of the parameter to calculate the position and length for
|
|
396
|
-
* @returns The position and length of the parameter
|
|
397
|
-
*/
|
|
398
|
-
_calculateParamPositionAndLength(inputs, targetIndex) {
|
|
399
|
-
// For now, we support simple types. Complex types (arrays, structs) would need more sophisticated calculation
|
|
400
|
-
// This is a simplified version that works for basic types like uint256, address, etc.
|
|
401
|
-
let position = 4; // Start after function selector
|
|
402
|
-
for (let i = 0; i < targetIndex; i++) {
|
|
403
|
-
const paramType = inputs[i].type;
|
|
404
|
-
position += this._getTypeSize(paramType);
|
|
405
|
-
}
|
|
406
|
-
const targetType = inputs[targetIndex].type;
|
|
407
|
-
const len = this._getTypeSize(targetType);
|
|
408
|
-
return { position, len };
|
|
409
|
-
}
|
|
410
|
-
/**
|
|
411
|
-
* Get the size in bytes for a parameter type
|
|
412
|
-
* @param type - The type of the parameter
|
|
413
|
-
* @returns The size in bytes of the parameter
|
|
414
|
-
*/
|
|
415
|
-
_getTypeSize(type) {
|
|
416
|
-
// Basic types are all 32 bytes in calldata (due to ABI encoding)
|
|
417
|
-
if (type.startsWith('uint') || type.startsWith('int') || type === 'address' || type === 'bool' || type.startsWith('bytes32')) {
|
|
418
|
-
return 32;
|
|
419
|
-
}
|
|
420
|
-
// Dynamic types (bytes, string) are also 32 bytes for the offset
|
|
421
|
-
if (type === 'bytes' || type === 'string') {
|
|
422
|
-
return 32;
|
|
423
|
-
}
|
|
424
|
-
// Arrays are 32 bytes for the offset
|
|
425
|
-
if (type.includes('[]')) {
|
|
426
|
-
return 32;
|
|
427
|
-
}
|
|
428
|
-
// Default to 32 bytes for unknown types
|
|
429
|
-
return 32;
|
|
430
|
-
}
|
|
431
|
-
/**
|
|
432
|
-
* Check if a parameter type can be replaced with a balance
|
|
433
|
-
* @param type - The type of the parameter
|
|
434
|
-
* @returns The can replace and reason of the parameter
|
|
435
|
-
*/
|
|
436
|
-
_canParameterBeReplaced(type) {
|
|
437
|
-
if (type.startsWith('uint') && !type.includes('[]')) {
|
|
438
|
-
return { canReplace: true };
|
|
439
|
-
}
|
|
440
|
-
if (type.startsWith('int') && !type.includes('[]')) {
|
|
441
|
-
return { canReplace: true, reason: 'but be careful with signed integers' };
|
|
442
|
-
}
|
|
443
|
-
return {
|
|
444
|
-
canReplace: false,
|
|
445
|
-
reason: `is not suitable for balance replacement. Only uint/int types are supported.`
|
|
446
|
-
};
|
|
447
|
-
}
|
|
448
|
-
/**
|
|
449
|
-
* Build a complete ZapCall
|
|
450
|
-
* @param hooks - The hooks of the zap call
|
|
451
|
-
* @param bridgeTokens - The tokens to bridge
|
|
452
|
-
* @param bridgeNFTs - The nfts to bridge
|
|
453
|
-
* @returns The zap call
|
|
454
|
-
*/
|
|
455
|
-
buildZapCall(hooks, bridgeTokens = [], bridgeNFTs = []) {
|
|
456
|
-
return {
|
|
457
|
-
hooks,
|
|
458
|
-
bridgeData: {
|
|
459
|
-
tokens: bridgeTokens,
|
|
460
|
-
nfts: bridgeNFTs,
|
|
461
|
-
isRequired: (bridgeTokens.length > 0 || bridgeNFTs.length > 0) ? true : false
|
|
462
|
-
}
|
|
463
|
-
};
|
|
464
|
-
}
|
|
465
|
-
/**
|
|
466
|
-
* Encode ZapCall for transaction - Much more efficient now!
|
|
467
|
-
* @param zapCall - The zap call to encode
|
|
468
|
-
* @returns The encoded zap call that can be used as calldata in tac sdk
|
|
469
|
-
*/
|
|
470
|
-
encodeZapCall(zapCall) {
|
|
471
|
-
return ethers_1.ethers.AbiCoder.defaultAbiCoder().encode([
|
|
472
|
-
"tuple(" +
|
|
473
|
-
"tuple(uint8,bytes)[]," + // hooks: only hookType and hookData
|
|
474
|
-
"tuple(address[],tuple(address,uint256,uint256)[],bool)" + // bridgeData
|
|
475
|
-
")"
|
|
476
|
-
], [
|
|
477
|
-
[
|
|
478
|
-
zapCall.hooks.map(hook => [hook.hookType, hook.hookData]),
|
|
479
|
-
[
|
|
480
|
-
zapCall.bridgeData.tokens,
|
|
481
|
-
zapCall.bridgeData.nfts.map(nft => [nft.nft, nft.id, nft.amount]),
|
|
482
|
-
zapCall.bridgeData.isRequired
|
|
483
|
-
]
|
|
484
|
-
]
|
|
485
|
-
]);
|
|
486
|
-
}
|
|
487
|
-
/**
|
|
488
|
-
* Utility: Create multiple approve hooks at once
|
|
489
|
-
* @param approvals - The approvals to create
|
|
490
|
-
* @returns The multiple approve hooks
|
|
491
|
-
*/
|
|
492
|
-
createMultipleApproves(approvals) {
|
|
493
|
-
return approvals.map(approval => this.createFullBalanceApproveHook(approval.token, approval.spender, approval.isFromSA ?? true));
|
|
494
|
-
}
|
|
495
|
-
/**
|
|
496
|
-
* Utility: Create a sequence of custom hooks
|
|
497
|
-
* @param calls - The calls to create
|
|
498
|
-
* @returns The hook sequence
|
|
499
|
-
*/
|
|
500
|
-
createHookSequence(calls) {
|
|
501
|
-
return calls.map(call => this.createCustomHook(call.contract, call.functionName, call.params, call.options));
|
|
133
|
+
return this.replacementHelper.buildReplacementInteractive(contractAddress, functionName, parameterName, token, balanceAddress, this.abiHandler.contractInterfaces, options);
|
|
502
134
|
}
|
|
503
135
|
/**
|
|
504
136
|
* Debug helper: Decode hook data back to readable format
|
|
@@ -506,21 +138,7 @@ class AgnosticProxySDK {
|
|
|
506
138
|
* @returns The decoded hook
|
|
507
139
|
*/
|
|
508
140
|
decodeHookData(hook) {
|
|
509
|
-
|
|
510
|
-
switch (hook.hookType) {
|
|
511
|
-
case HookType.Custom:
|
|
512
|
-
return ethers_1.ethers.AbiCoder.defaultAbiCoder().decode(["tuple(bool,address,uint256,bytes,bytes)"], hook.hookData)[0];
|
|
513
|
-
case HookType.FullBalanceApprove:
|
|
514
|
-
return ethers_1.ethers.AbiCoder.defaultAbiCoder().decode(["tuple(address,address,bool)"], hook.hookData)[0];
|
|
515
|
-
case HookType.FullBalanceTransfer:
|
|
516
|
-
return ethers_1.ethers.AbiCoder.defaultAbiCoder().decode(["tuple(address,address,bool)"], hook.hookData)[0];
|
|
517
|
-
default:
|
|
518
|
-
throw new Error(`Unknown hook type: ${hook.hookType}`);
|
|
519
|
-
}
|
|
520
|
-
}
|
|
521
|
-
catch (error) {
|
|
522
|
-
throw new Error(`Failed to decode hook data: ${error}`);
|
|
523
|
-
}
|
|
141
|
+
return this.debugHelpers.decodeHookData(hook);
|
|
524
142
|
}
|
|
525
143
|
/**
|
|
526
144
|
* Debug helper: Get estimated gas for a ZapCall
|
|
@@ -528,160 +146,14 @@ class AgnosticProxySDK {
|
|
|
528
146
|
* @returns The estimated gas usage
|
|
529
147
|
*/
|
|
530
148
|
estimateGasUsage(zapCall) {
|
|
531
|
-
|
|
532
|
-
let gasEstimate = 50000; // Base gas
|
|
533
|
-
zapCall.hooks.forEach(hook => {
|
|
534
|
-
switch (hook.hookType) {
|
|
535
|
-
case HookType.Custom:
|
|
536
|
-
gasEstimate += 100000; // Custom calls can vary widely
|
|
537
|
-
break;
|
|
538
|
-
case HookType.FullBalanceApprove:
|
|
539
|
-
gasEstimate += 50000; // ERC20 approve
|
|
540
|
-
break;
|
|
541
|
-
case HookType.FullBalanceTransfer:
|
|
542
|
-
gasEstimate += 65000; // ERC20 transfer
|
|
543
|
-
break;
|
|
544
|
-
}
|
|
545
|
-
});
|
|
546
|
-
if (zapCall.bridgeData.isRequired) {
|
|
547
|
-
gasEstimate += 200000; // Bridge operations
|
|
548
|
-
}
|
|
549
|
-
return gasEstimate;
|
|
149
|
+
return this.debugHelpers.estimateGasUsage(zapCall);
|
|
550
150
|
}
|
|
551
151
|
/**
|
|
552
152
|
* Visualize ZapCall chain - Human readable description of all operations
|
|
553
153
|
* @param zapCall - The zap call to visualize
|
|
554
154
|
*/
|
|
555
155
|
visualizeZapCall(zapCall) {
|
|
556
|
-
|
|
557
|
-
console.log("================================");
|
|
558
|
-
if (zapCall.hooks.length === 0) {
|
|
559
|
-
console.log("ā No hooks in this ZapCall");
|
|
560
|
-
return;
|
|
561
|
-
}
|
|
562
|
-
zapCall.hooks.forEach((hook, index) => {
|
|
563
|
-
const stepNumber = (index + 1).toString().padStart(2, " ");
|
|
564
|
-
console.log(`\n${stepNumber}. ${this._describeHook(hook)}`);
|
|
565
|
-
});
|
|
566
|
-
// Bridge information
|
|
567
|
-
if (zapCall.bridgeData.isRequired) {
|
|
568
|
-
console.log("\nš Bridge Operations:");
|
|
569
|
-
if (zapCall.bridgeData.tokens.length > 0) {
|
|
570
|
-
console.log(` š¤ Bridge tokens: ${zapCall.bridgeData.tokens.map(t => this._formatAddress(t)).join(", ")}`);
|
|
571
|
-
}
|
|
572
|
-
if (zapCall.bridgeData.nfts.length > 0) {
|
|
573
|
-
console.log(` š¼ļø Bridge NFTs: ${zapCall.bridgeData.nfts.length} NFT(s)`);
|
|
574
|
-
zapCall.bridgeData.nfts.forEach((nft, i) => {
|
|
575
|
-
console.log(` ${i + 1}. ${this._formatAddress(nft.nft)} #${nft.id} (amount: ${nft.amount})`);
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
}
|
|
579
|
-
else {
|
|
580
|
-
console.log("\nš« No bridge operations required");
|
|
581
|
-
}
|
|
582
|
-
// Summary
|
|
583
|
-
console.log("\nš Summary:");
|
|
584
|
-
console.log(` Total hooks: ${zapCall.hooks.length}`);
|
|
585
|
-
console.log(` Estimated gas: ${this.estimateGasUsage(zapCall).toLocaleString()}`);
|
|
586
|
-
console.log(` Bridge required: ${zapCall.bridgeData.isRequired ? "Yes" : "No"}`);
|
|
587
|
-
console.log("================================");
|
|
588
|
-
}
|
|
589
|
-
/**
|
|
590
|
-
* Private helper to describe individual hooks
|
|
591
|
-
* @param hook - The hook to describe
|
|
592
|
-
* @returns The description of the hook
|
|
593
|
-
*/
|
|
594
|
-
_describeHook(hook) {
|
|
595
|
-
try {
|
|
596
|
-
switch (hook.hookType) {
|
|
597
|
-
case HookType.Custom:
|
|
598
|
-
return this._describeCustomHook(hook);
|
|
599
|
-
case HookType.FullBalanceApprove:
|
|
600
|
-
return this._describeApproveHook(hook);
|
|
601
|
-
case HookType.FullBalanceTransfer:
|
|
602
|
-
return this._describeTransferHook(hook);
|
|
603
|
-
default:
|
|
604
|
-
return `ā Unknown hook type: ${hook.hookType}`;
|
|
605
|
-
}
|
|
606
|
-
}
|
|
607
|
-
catch (error) {
|
|
608
|
-
return `ā Error describing hook: ${error}`;
|
|
609
|
-
}
|
|
610
|
-
}
|
|
611
|
-
/**
|
|
612
|
-
* Describe custom hook with function details
|
|
613
|
-
* @param hook - The hook to describe
|
|
614
|
-
* @returns The description of the hook
|
|
615
|
-
*/
|
|
616
|
-
_describeCustomHook(hook) {
|
|
617
|
-
const decoded = this.decodeHookData(hook);
|
|
618
|
-
const [isFromSA, contractAddress, value, data, improvedMissionInfo] = decoded;
|
|
619
|
-
// Try to decode function name from data
|
|
620
|
-
let functionDescription = "unknown function";
|
|
621
|
-
let hasReplacements = false;
|
|
622
|
-
if (data && data.length >= 10) { // At least 4 bytes for selector + some data
|
|
623
|
-
const selector = data.slice(0, 10); // "0x" + 8 hex chars
|
|
624
|
-
// Try to find function name from registered interfaces
|
|
625
|
-
for (const [address, contractInterface] of this.contractInterfaces) {
|
|
626
|
-
if (address === contractAddress.toLowerCase()) {
|
|
627
|
-
try {
|
|
628
|
-
const fragment = contractInterface.getFunction(selector);
|
|
629
|
-
if (fragment) {
|
|
630
|
-
functionDescription = `${fragment.name}(${fragment.inputs.map(input => input.type).join(", ")})`;
|
|
631
|
-
break;
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
catch {
|
|
635
|
-
// Function not found in this interface
|
|
636
|
-
}
|
|
637
|
-
}
|
|
638
|
-
}
|
|
639
|
-
// If not found in registered interfaces, just show selector
|
|
640
|
-
if (functionDescription === "unknown function") {
|
|
641
|
-
functionDescription = `function with selector ${selector}`;
|
|
642
|
-
}
|
|
643
|
-
}
|
|
644
|
-
// Check for dynamic replacements
|
|
645
|
-
if (improvedMissionInfo && improvedMissionInfo !== "0x" && improvedMissionInfo.length > 2) {
|
|
646
|
-
hasReplacements = true;
|
|
647
|
-
}
|
|
648
|
-
const perspective = isFromSA ? "Smart Account" : "Proxy Contract";
|
|
649
|
-
const valueStr = value > 0n ? ` (sending ${ethers_1.ethers.formatEther(value)} ETH)` : "";
|
|
650
|
-
const replacementStr = hasReplacements ? " š [with dynamic value replacement]" : "";
|
|
651
|
-
return `š Custom call to ${this._formatAddress(contractAddress)} from ${perspective}${valueStr}
|
|
652
|
-
Function: ${functionDescription}${replacementStr}`;
|
|
653
|
-
}
|
|
654
|
-
/**
|
|
655
|
-
* Describe approve hook
|
|
656
|
-
* @param hook - The hook to describe
|
|
657
|
-
* @returns The description of the hook
|
|
658
|
-
*/
|
|
659
|
-
_describeApproveHook(hook) {
|
|
660
|
-
const decoded = this.decodeHookData(hook);
|
|
661
|
-
const [token, to, isFromSA] = decoded;
|
|
662
|
-
const perspective = isFromSA ? "Smart Account" : "Proxy Contract";
|
|
663
|
-
return `ā
Approve full balance of ${this._formatAddress(token)} to ${this._formatAddress(to)} from ${perspective}`;
|
|
664
|
-
}
|
|
665
|
-
/**
|
|
666
|
-
* Describe transfer hook
|
|
667
|
-
* @param hook - The hook to describe
|
|
668
|
-
* @returns The description of the hook
|
|
669
|
-
*/
|
|
670
|
-
_describeTransferHook(hook) {
|
|
671
|
-
const decoded = this.decodeHookData(hook);
|
|
672
|
-
const [token, to, isFromSA] = decoded;
|
|
673
|
-
const perspective = isFromSA ? "Smart Account" : "Proxy Contract";
|
|
674
|
-
return `šø Transfer full balance of ${this._formatAddress(token)} to ${this._formatAddress(to)} from ${perspective}`;
|
|
675
|
-
}
|
|
676
|
-
/**
|
|
677
|
-
* Format address for display (show first 6 and last 4 characters)
|
|
678
|
-
* @param address - The address to format
|
|
679
|
-
* @returns The formatted address
|
|
680
|
-
*/
|
|
681
|
-
_formatAddress(address) {
|
|
682
|
-
if (!address || address.length < 10)
|
|
683
|
-
return address;
|
|
684
|
-
return `${address.slice(0, 6)}...${address.slice(-4)}`;
|
|
156
|
+
return this.debugHelpers.visualizeZapCall(zapCall, this.abiHandler.contractInterfaces);
|
|
685
157
|
}
|
|
686
158
|
/**
|
|
687
159
|
* Get a detailed breakdown of a ZapCall for logging
|
|
@@ -689,21 +161,7 @@ class AgnosticProxySDK {
|
|
|
689
161
|
* @returns The breakdown of the zap call
|
|
690
162
|
*/
|
|
691
163
|
getZapCallBreakdown(zapCall) {
|
|
692
|
-
|
|
693
|
-
const hookDescriptions = [];
|
|
694
|
-
zapCall.hooks.forEach((hook, index) => {
|
|
695
|
-
const typeName = HookType[hook.hookType];
|
|
696
|
-
hookTypes[typeName] = (hookTypes[typeName] || 0) + 1;
|
|
697
|
-
hookDescriptions.push(`${index + 1}. ${this._describeHook(hook)}`);
|
|
698
|
-
});
|
|
699
|
-
return {
|
|
700
|
-
totalHooks: zapCall.hooks.length,
|
|
701
|
-
hookTypes,
|
|
702
|
-
gasEstimate: this.estimateGasUsage(zapCall),
|
|
703
|
-
encodedSize: this.encodeZapCall(zapCall).length / 2, // bytes
|
|
704
|
-
bridgeRequired: zapCall.bridgeData.isRequired,
|
|
705
|
-
hookDescriptions
|
|
706
|
-
};
|
|
164
|
+
return this.debugHelpers.getZapCallBreakdown(zapCall, this.abiHandler.contractInterfaces);
|
|
707
165
|
}
|
|
708
166
|
/**
|
|
709
167
|
* Compare two ZapCalls and show differences
|
|
@@ -712,25 +170,32 @@ class AgnosticProxySDK {
|
|
|
712
170
|
* @param label1 - The label of the first zap call
|
|
713
171
|
* @param label2 - The label of the second zap call
|
|
714
172
|
*/
|
|
715
|
-
compareZapCalls(zapCall1, zapCall2, label1 =
|
|
716
|
-
|
|
717
|
-
console.log("=".repeat(50));
|
|
718
|
-
const breakdown1 = this.getZapCallBreakdown(zapCall1);
|
|
719
|
-
const breakdown2 = this.getZapCallBreakdown(zapCall2);
|
|
720
|
-
console.log(`š ${label1}:`);
|
|
721
|
-
console.log(` Hooks: ${breakdown1.totalHooks}, Gas: ${breakdown1.gasEstimate.toLocaleString()}, Size: ${breakdown1.encodedSize} bytes`);
|
|
722
|
-
console.log(`š ${label2}:`);
|
|
723
|
-
console.log(` Hooks: ${breakdown2.totalHooks}, Gas: ${breakdown2.gasEstimate.toLocaleString()}, Size: ${breakdown2.encodedSize} bytes`);
|
|
724
|
-
console.log("\nš Differences:");
|
|
725
|
-
console.log(` Hooks: ${breakdown2.totalHooks - breakdown1.totalHooks > 0 ? "+" : ""}${breakdown2.totalHooks - breakdown1.totalHooks}`);
|
|
726
|
-
console.log(` Gas: ${breakdown2.gasEstimate - breakdown1.gasEstimate > 0 ? "+" : ""}${(breakdown2.gasEstimate - breakdown1.gasEstimate).toLocaleString()}`);
|
|
727
|
-
console.log(` Size: ${breakdown2.encodedSize - breakdown1.encodedSize > 0 ? "+" : ""}${breakdown2.encodedSize - breakdown1.encodedSize} bytes`);
|
|
173
|
+
compareZapCalls(zapCall1, zapCall2, label1 = 'ZapCall 1', label2 = 'ZapCall 2') {
|
|
174
|
+
return this.debugHelpers.compareZapCalls(zapCall1, zapCall2, label1, label2, this.abiHandler.contractInterfaces);
|
|
728
175
|
}
|
|
729
176
|
getAgnosticCallParams() {
|
|
730
177
|
return {
|
|
731
178
|
evmTargetAddress: this.agnosticProxyAddress,
|
|
732
|
-
methodName:
|
|
179
|
+
methodName: 'Zap(bytes,bytes)',
|
|
733
180
|
};
|
|
734
181
|
}
|
|
182
|
+
/**
|
|
183
|
+
* Build a complete ZapCall
|
|
184
|
+
* @param hooks - The hooks of the zap call
|
|
185
|
+
* @param bridgeTokens - The tokens to bridge
|
|
186
|
+
* @param bridgeNFTs - The nfts to bridge
|
|
187
|
+
* @returns The zap call
|
|
188
|
+
*/
|
|
189
|
+
buildZapCall(hooks, bridgeTokens = [], bridgeNFTs = []) {
|
|
190
|
+
return this.debugHelpers.buildZapCall(hooks, bridgeTokens, bridgeNFTs);
|
|
191
|
+
}
|
|
192
|
+
/**
|
|
193
|
+
* Encode ZapCall for transaction
|
|
194
|
+
* @param zapCall - The zap call to encode
|
|
195
|
+
* @returns The encoded zap call that can be used as calldata in tac sdk
|
|
196
|
+
*/
|
|
197
|
+
encodeZapCall(zapCall) {
|
|
198
|
+
return this.debugHelpers.encodeZapCall(zapCall);
|
|
199
|
+
}
|
|
735
200
|
}
|
|
736
201
|
exports.AgnosticProxySDK = AgnosticProxySDK;
|