@reyaxyz/common 0.175.0 → 0.175.1
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/README.md +1 -1
- package/dist/transactions/abis/CoreProxy.json +119 -0
- package/dist/transactions/abis/Errors.json +21 -0
- package/dist/transactions/buildMulticallTx.js +10 -1
- package/dist/transactions/buildMulticallTx.js.map +1 -1
- package/dist/transactions/executeTransaction.js +66 -8
- package/dist/transactions/executeTransaction.js.map +1 -1
- package/dist/types/transactions/buildMulticallTx.d.ts +1 -0
- package/dist/types/transactions/buildMulticallTx.d.ts.map +1 -1
- package/dist/types/transactions/executeTransaction.d.ts +6 -0
- package/dist/types/transactions/executeTransaction.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/transactions/abis/CoreProxy.json +120 -1
- package/src/transactions/abis/Errors.json +21 -0
- package/src/transactions/buildMulticallTx.ts +9 -0
- package/src/transactions/executeTransaction.ts +58 -0
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -5645,6 +5645,125 @@
|
|
|
5645
5645
|
],
|
|
5646
5646
|
"outputs": [],
|
|
5647
5647
|
"stateMutability": "nonpayable"
|
|
5648
|
+
},
|
|
5649
|
+
{
|
|
5650
|
+
"type": "function",
|
|
5651
|
+
"name": "tryAggregate",
|
|
5652
|
+
"inputs": [
|
|
5653
|
+
{
|
|
5654
|
+
"name": "requireSuccess",
|
|
5655
|
+
"type": "bool",
|
|
5656
|
+
"internalType": "bool"
|
|
5657
|
+
},
|
|
5658
|
+
{
|
|
5659
|
+
"name": "calls",
|
|
5660
|
+
"type": "bytes[]",
|
|
5661
|
+
"internalType": "bytes[]"
|
|
5662
|
+
}
|
|
5663
|
+
],
|
|
5664
|
+
"outputs": [
|
|
5665
|
+
{
|
|
5666
|
+
"name": "returnData",
|
|
5667
|
+
"type": "tuple[]",
|
|
5668
|
+
"internalType": "struct MulticallResult[]",
|
|
5669
|
+
"components": [
|
|
5670
|
+
{
|
|
5671
|
+
"name": "success",
|
|
5672
|
+
"type": "bool",
|
|
5673
|
+
"internalType": "bool"
|
|
5674
|
+
},
|
|
5675
|
+
{
|
|
5676
|
+
"name": "returnData",
|
|
5677
|
+
"type": "bytes",
|
|
5678
|
+
"internalType": "bytes"
|
|
5679
|
+
}
|
|
5680
|
+
]
|
|
5681
|
+
}
|
|
5682
|
+
],
|
|
5683
|
+
"stateMutability": "payable"
|
|
5684
|
+
},
|
|
5685
|
+
{
|
|
5686
|
+
"type": "error",
|
|
5687
|
+
"name": "MulticallFailed",
|
|
5688
|
+
"inputs": [
|
|
5689
|
+
{
|
|
5690
|
+
"name": "index",
|
|
5691
|
+
"type": "uint256",
|
|
5692
|
+
"internalType": "uint256"
|
|
5693
|
+
},
|
|
5694
|
+
{
|
|
5695
|
+
"name": "inputData",
|
|
5696
|
+
"type": "bytes",
|
|
5697
|
+
"internalType": "bytes"
|
|
5698
|
+
},
|
|
5699
|
+
{
|
|
5700
|
+
"name": "returnData",
|
|
5701
|
+
"type": "bytes",
|
|
5702
|
+
"internalType": "bytes"
|
|
5703
|
+
}
|
|
5704
|
+
]
|
|
5705
|
+
},
|
|
5706
|
+
{
|
|
5707
|
+
"type": "event",
|
|
5708
|
+
"name": "FailedCall",
|
|
5709
|
+
"inputs": [
|
|
5710
|
+
{
|
|
5711
|
+
"name": "index",
|
|
5712
|
+
"type": "uint256",
|
|
5713
|
+
"indexed": false,
|
|
5714
|
+
"internalType": "uint256"
|
|
5715
|
+
},
|
|
5716
|
+
{
|
|
5717
|
+
"name": "inputData",
|
|
5718
|
+
"type": "bytes",
|
|
5719
|
+
"indexed": false,
|
|
5720
|
+
"internalType": "bytes"
|
|
5721
|
+
},
|
|
5722
|
+
{
|
|
5723
|
+
"name": "returnData",
|
|
5724
|
+
"type": "bytes",
|
|
5725
|
+
"indexed": false,
|
|
5726
|
+
"internalType": "bytes"
|
|
5727
|
+
},
|
|
5728
|
+
{
|
|
5729
|
+
"name": "blockTimestamp",
|
|
5730
|
+
"type": "uint256",
|
|
5731
|
+
"indexed": false,
|
|
5732
|
+
"internalType": "uint256"
|
|
5733
|
+
}
|
|
5734
|
+
],
|
|
5735
|
+
"anonymous": false
|
|
5736
|
+
},
|
|
5737
|
+
{
|
|
5738
|
+
"type": "event",
|
|
5739
|
+
"name": "SuccessfulCall",
|
|
5740
|
+
"inputs": [
|
|
5741
|
+
{
|
|
5742
|
+
"name": "index",
|
|
5743
|
+
"type": "uint256",
|
|
5744
|
+
"indexed": false,
|
|
5745
|
+
"internalType": "uint256"
|
|
5746
|
+
},
|
|
5747
|
+
{
|
|
5748
|
+
"name": "inputData",
|
|
5749
|
+
"type": "bytes",
|
|
5750
|
+
"indexed": false,
|
|
5751
|
+
"internalType": "bytes"
|
|
5752
|
+
},
|
|
5753
|
+
{
|
|
5754
|
+
"name": "returnData",
|
|
5755
|
+
"type": "bytes",
|
|
5756
|
+
"indexed": false,
|
|
5757
|
+
"internalType": "bytes"
|
|
5758
|
+
},
|
|
5759
|
+
{
|
|
5760
|
+
"name": "blockTimestamp",
|
|
5761
|
+
"type": "uint256",
|
|
5762
|
+
"indexed": false,
|
|
5763
|
+
"internalType": "uint256"
|
|
5764
|
+
}
|
|
5765
|
+
],
|
|
5766
|
+
"anonymous": false
|
|
5648
5767
|
}
|
|
5649
5768
|
]
|
|
5650
5769
|
}
|
|
@@ -3586,6 +3586,27 @@
|
|
|
3586
3586
|
"type": "error",
|
|
3587
3587
|
"name": "MismatchedLength",
|
|
3588
3588
|
"inputs": []
|
|
3589
|
+
},
|
|
3590
|
+
{
|
|
3591
|
+
"type": "error",
|
|
3592
|
+
"name": "MulticallFailed",
|
|
3593
|
+
"inputs": [
|
|
3594
|
+
{
|
|
3595
|
+
"name": "index",
|
|
3596
|
+
"type": "uint256",
|
|
3597
|
+
"internalType": "uint256"
|
|
3598
|
+
},
|
|
3599
|
+
{
|
|
3600
|
+
"name": "inputData",
|
|
3601
|
+
"type": "bytes",
|
|
3602
|
+
"internalType": "bytes"
|
|
3603
|
+
},
|
|
3604
|
+
{
|
|
3605
|
+
"name": "returnData",
|
|
3606
|
+
"type": "bytes",
|
|
3607
|
+
"internalType": "bytes"
|
|
3608
|
+
}
|
|
3609
|
+
]
|
|
3589
3610
|
}
|
|
3590
3611
|
]
|
|
3591
3612
|
}
|
|
@@ -1,8 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.encodeOptionalMulticall = exports.encodeStrictMulticall = void 0;
|
|
3
|
+
exports.encodeOptionalMulticall = exports.encodeStrictMulticall = exports.encodeCoreMulticall = void 0;
|
|
4
4
|
var ethers_1 = require("ethers");
|
|
5
|
+
var CoreProxy_json_1 = require("./abis/CoreProxy.json");
|
|
5
6
|
var MulticallV2_json_1 = require("./abis/MulticallV2.json");
|
|
7
|
+
var encodeCoreMulticall = function (calls) {
|
|
8
|
+
var functionSignature = 'tryAggregate';
|
|
9
|
+
var parameters = [false, calls.map(function (c) { return c.callData; })];
|
|
10
|
+
var INTERFACE = new ethers_1.ethers.Interface(CoreProxy_json_1.abi);
|
|
11
|
+
var calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
12
|
+
return calldata;
|
|
13
|
+
};
|
|
14
|
+
exports.encodeCoreMulticall = encodeCoreMulticall;
|
|
6
15
|
var encodeMulticall = function (requireSuccess, calls) {
|
|
7
16
|
var functionSignature = 'tryAggregate';
|
|
8
17
|
var parameters = [requireSuccess, calls];
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildMulticallTx.js","sourceRoot":"/","sources":["transactions/buildMulticallTx.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,4DAA8D;
|
|
1
|
+
{"version":3,"file":"buildMulticallTx.js","sourceRoot":"/","sources":["transactions/buildMulticallTx.ts"],"names":[],"mappings":";;;AAAA,iCAAgC;AAChC,wDAAuD;AACvD,4DAA8D;AAOvD,IAAM,mBAAmB,GAAG,UAAC,KAAa;IAC/C,IAAM,iBAAiB,GAAG,cAAc,CAAC;IACzC,IAAM,UAAU,GAAwB,CAAC,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,UAAC,CAAC,IAAK,OAAA,CAAC,CAAC,QAAQ,EAAV,CAAU,CAAC,CAAC,CAAC;IAC9E,IAAM,SAAS,GAAG,IAAI,eAAM,CAAC,SAAS,CAAC,oBAAO,CAAC,CAAC;IAChD,IAAM,QAAQ,GAAG,SAAS,CAAC,kBAAkB,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;IAC7E,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AANW,QAAA,mBAAmB,uBAM9B;AAEF,IAAM,eAAe,GAAG,UAAC,cAAuB,EAAE,KAAa;IAC7D,IAAM,iBAAiB,GAAG,cAAc,CAAC;IACzC,IAAM,UAAU,GAAsB,CAAC,cAAc,EAAE,KAAK,CAAC,CAAC;IAC9D,IAAM,SAAS,GAAG,IAAI,eAAM,CAAC,SAAS,CAAC,sBAAY,CAAC,CAAC;IACrD,IAAM,QAAQ,GAAG,SAAS,CAAC,kBAAkB,CAAC,iBAAiB,EAAE,UAAU,CAAC,CAAC;IAC7E,OAAO,QAAQ,CAAC;AAClB,CAAC,CAAC;AAEK,IAAM,qBAAqB,GAAG,UAAC,KAAa;IACjD,OAAA,eAAe,CAAC,IAAI,EAAE,KAAK,CAAC;AAA5B,CAA4B,CAAC;AADlB,QAAA,qBAAqB,yBACH;AAExB,IAAM,uBAAuB,GAAG,UAAC,KAAa;IACnD,OAAA,eAAe,CAAC,KAAK,EAAE,KAAK,CAAC;AAA7B,CAA6B,CAAC;AADnB,QAAA,uBAAuB,2BACJ","sourcesContent":["import { ethers } from 'ethers';\nimport { abi as CoreAbi } from './abis/CoreProxy.json';\nimport { abi as MulticallAbi } from './abis/MulticallV2.json';\n\nexport type Call = {\n target: string;\n callData: string;\n};\n\nexport const encodeCoreMulticall = (calls: Call[]): string => {\n const functionSignature = 'tryAggregate';\n const parameters: [boolean, string[]] = [false, calls.map((c) => c.callData)];\n const INTERFACE = new ethers.Interface(CoreAbi);\n const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);\n return calldata;\n};\n\nconst encodeMulticall = (requireSuccess: boolean, calls: Call[]): string => {\n const functionSignature = 'tryAggregate';\n const parameters: [boolean, Call[]] = [requireSuccess, calls];\n const INTERFACE = new ethers.Interface(MulticallAbi);\n const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);\n return calldata;\n};\n\nexport const encodeStrictMulticall = (calls: Call[]) =>\n encodeMulticall(true, calls);\n\nexport const encodeOptionalMulticall = (calls: Call[]) =>\n encodeMulticall(false, calls);\n"]}
|
|
@@ -48,7 +48,7 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
48
48
|
};
|
|
49
49
|
var _a;
|
|
50
50
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
-
exports.executeTransaction = exports.estimateGasFromTxAndChainId = exports.estimateGas = void 0;
|
|
51
|
+
exports.decodeError = exports.executeTransaction = exports.estimateGasWithoutHardcoding = exports.estimateGasFromTxAndChainId = exports.estimateGas = void 0;
|
|
52
52
|
var ethers_1 = require("ethers");
|
|
53
53
|
var types_1 = require("../types");
|
|
54
54
|
var contractAddresses_1 = require("./contractAddresses");
|
|
@@ -136,9 +136,51 @@ function estimateGasFromTxAndChainId(chainId, tx) {
|
|
|
136
136
|
});
|
|
137
137
|
}
|
|
138
138
|
exports.estimateGasFromTxAndChainId = estimateGasFromTxAndChainId;
|
|
139
|
+
function estimateGasWithoutHardcoding(signer, data, value, chainId, targetContract) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
141
|
+
var accountAddress, contractAddress, tx, gasLimit, provider, estimation, error_3, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
142
|
+
return __generator(this, function (_a) {
|
|
143
|
+
switch (_a.label) {
|
|
144
|
+
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
145
|
+
case 1:
|
|
146
|
+
accountAddress = _a.sent();
|
|
147
|
+
contractAddress = Object.values(contractAddresses_1.ContractType).includes(targetContract)
|
|
148
|
+
? (0, contractAddresses_1.getAddress)(chainId, targetContract)
|
|
149
|
+
: targetContract;
|
|
150
|
+
tx = __assign({ from: accountAddress, to: contractAddress, data: data }, (value && value !== '0' ? { value: value } : {}));
|
|
151
|
+
gasLimit = BigInt('5000000');
|
|
152
|
+
_a.label = 2;
|
|
153
|
+
case 2:
|
|
154
|
+
_a.trys.push([2, 4, , 6]);
|
|
155
|
+
provider = ethers_1.ethers.getDefaultProvider(reyaChainIdRPCMapper[chainId]);
|
|
156
|
+
return [4 /*yield*/, provider.estimateGas(tx)];
|
|
157
|
+
case 3:
|
|
158
|
+
estimation = _a.sent();
|
|
159
|
+
gasLimit = addGasBuffer(estimation);
|
|
160
|
+
return [3 /*break*/, 6];
|
|
161
|
+
case 4:
|
|
162
|
+
error_3 = _a.sent();
|
|
163
|
+
return [4 /*yield*/, errorDecoder.decode(error_3)];
|
|
164
|
+
case 5:
|
|
165
|
+
decodedError = _a.sent();
|
|
166
|
+
reason = customReasonMapper(decodedError);
|
|
167
|
+
console.error("Error simulating transaction account address ".concat(accountAddress, " reason ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, " ").concat(decodedError, " ").concat(error_3));
|
|
168
|
+
throw new Error(reason);
|
|
169
|
+
case 6:
|
|
170
|
+
if (Object.values(types_1.ReyaChainId).includes(chainId)) {
|
|
171
|
+
maxPriorityFeePerGas = BigInt('0');
|
|
172
|
+
maxFeePerGas = BigInt('100000000');
|
|
173
|
+
return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit, maxPriorityFeePerGas: maxPriorityFeePerGas, maxFeePerGas: maxFeePerGas })];
|
|
174
|
+
}
|
|
175
|
+
return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit })];
|
|
176
|
+
}
|
|
177
|
+
});
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
exports.estimateGasWithoutHardcoding = estimateGasWithoutHardcoding;
|
|
139
181
|
function estimateGasExternalChains(signer, data, value, chainId, targetContract) {
|
|
140
182
|
return __awaiter(this, void 0, void 0, function () {
|
|
141
|
-
var accountAddress, contractAddress, tx, gasLimit, gasEstimate,
|
|
183
|
+
var accountAddress, contractAddress, tx, gasLimit, gasEstimate, error_4, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
142
184
|
return __generator(this, function (_a) {
|
|
143
185
|
switch (_a.label) {
|
|
144
186
|
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
@@ -158,12 +200,12 @@ function estimateGasExternalChains(signer, data, value, chainId, targetContract)
|
|
|
158
200
|
gasLimit = (0, txHelpers_1.getGasBuffer)(gasEstimate);
|
|
159
201
|
return [3 /*break*/, 6];
|
|
160
202
|
case 4:
|
|
161
|
-
|
|
162
|
-
return [4 /*yield*/, errorDecoder.decode(
|
|
203
|
+
error_4 = _a.sent();
|
|
204
|
+
return [4 /*yield*/, errorDecoder.decode(error_4)];
|
|
163
205
|
case 5:
|
|
164
206
|
decodedError = _a.sent();
|
|
165
207
|
reason = customReasonMapper(decodedError);
|
|
166
|
-
console.error("Error simulating external transaction account address ".concat(accountAddress, " reason ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, " ").concat(decodedError, " ").concat(
|
|
208
|
+
console.error("Error simulating external transaction account address ".concat(accountAddress, " reason ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, " ").concat(decodedError, " ").concat(error_4));
|
|
167
209
|
throw new Error(reason);
|
|
168
210
|
case 6:
|
|
169
211
|
if (Object.values(types_1.ReyaChainId).includes(chainId)) {
|
|
@@ -178,7 +220,7 @@ function estimateGasExternalChains(signer, data, value, chainId, targetContract)
|
|
|
178
220
|
}
|
|
179
221
|
function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
180
222
|
return __awaiter(this, void 0, void 0, function () {
|
|
181
|
-
var txData, txResponse, txReceipt,
|
|
223
|
+
var txData, txResponse, txReceipt, error_5;
|
|
182
224
|
return __generator(this, function (_a) {
|
|
183
225
|
switch (_a.label) {
|
|
184
226
|
case 0: return [4 /*yield*/, estimateGasExternalChains(signer, data, value, chainId, targetContract)];
|
|
@@ -195,8 +237,8 @@ function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
|
195
237
|
txReceipt = _a.sent();
|
|
196
238
|
return [2 /*return*/, txReceipt];
|
|
197
239
|
case 5:
|
|
198
|
-
|
|
199
|
-
console.warn(
|
|
240
|
+
error_5 = _a.sent();
|
|
241
|
+
console.warn(error_5);
|
|
200
242
|
throw new Error('Transaction Execution Error');
|
|
201
243
|
case 6: return [2 /*return*/];
|
|
202
244
|
}
|
|
@@ -225,4 +267,20 @@ var customReasonMapper = function (_a) {
|
|
|
225
267
|
function addGasBuffer(estimation) {
|
|
226
268
|
return (estimation * BigInt(120)) / BigInt(100);
|
|
227
269
|
}
|
|
270
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
271
|
+
function decodeError(returnData) {
|
|
272
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
273
|
+
var decodedError, reason;
|
|
274
|
+
return __generator(this, function (_a) {
|
|
275
|
+
switch (_a.label) {
|
|
276
|
+
case 0: return [4 /*yield*/, errorDecoder.decode(returnData)];
|
|
277
|
+
case 1:
|
|
278
|
+
decodedError = _a.sent();
|
|
279
|
+
reason = customReasonMapper(decodedError);
|
|
280
|
+
return [2 /*return*/, reason];
|
|
281
|
+
}
|
|
282
|
+
});
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
exports.decodeError = decodeError;
|
|
228
286
|
//# sourceMappingURL=executeTransaction.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executeTransaction.js","sourceRoot":"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,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC","sourcesContent":["import { ethers, JsonRpcSigner, Signer } from 'ethers';\nimport { MoneyInOutChainId, ReyaChainId } from '../types';\nimport { ContractType, getAddress } from './contractAddresses';\nimport { abi } from './abis/Errors.json';\nimport { ErrorDecoder } from 'ethers-decode-error';\nimport type { DecodedError } from 'ethers-decode-error';\nimport { getGasBuffer } from './txHelpers';\nimport { getPublicRpcUrlsOrThrow } from '../utils';\n\nconst reyaChainIdRPCMapper: { [key: number]: string } = {\n [ReyaChainId.reyaNetwork]: 'https://rpc.reya.network',\n [ReyaChainId.reyaCronos]: 'https://rpc.reya-cronos.gelato.digital',\n};\n\nexport type Transaction = {\n from: string;\n to: string;\n data: string;\n value?: string;\n};\nconst errorDecoder = ErrorDecoder.create([abi]);\n\nexport async function estimateGas(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n): Promise<\n Transaction & {\n gasLimit: bigint;\n maxPriorityFeePerGas?: bigint;\n maxFeePerGas?: bigint;\n }\n> {\n const accountAddress = await signer.getAddress();\n\n const contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n const tx = {\n from: accountAddress, // @todo Update with relayer address\n to: contractAddress,\n data,\n ...(value && value !== '0' ? { value: value } : {}),\n };\n\n const gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate\n\n try {\n const provider = ethers.getDefaultProvider(reyaChainIdRPCMapper[chainId]);\n await provider.call(tx);\n } catch (error) {\n const decodedError = await errorDecoder.decode(error);\n const reason = customReasonMapper(decodedError);\n console.error(\n `Error simulating transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,\n );\n throw new Error(reason);\n }\n\n if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {\n const maxPriorityFeePerGas: bigint = BigInt('0');\n const maxFeePerGas: bigint = BigInt('100000000');\n return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };\n }\n\n return { ...tx, gasLimit };\n}\n\nexport async function estimateGasFromTxAndChainId(\n chainId: ReyaChainId | MoneyInOutChainId,\n tx: ethers.TransactionRequest,\n): Promise<bigint> {\n const rpcUrls = getPublicRpcUrlsOrThrow(chainId);\n let lastError = null;\n\n for (const url of rpcUrls) {\n try {\n const provider = new ethers.JsonRpcProvider(url);\n const estimation = await provider.estimateGas(tx);\n return addGasBuffer(estimation);\n } catch (error) {\n lastError = error;\n }\n }\n\n throw new Error('All RPC URLs failed; last error: ' + lastError);\n}\n\nasync function estimateGasExternalChains(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n): Promise<\n Transaction & {\n gasLimit: bigint;\n maxPriorityFeePerGas?: bigint;\n maxFeePerGas?: bigint;\n }\n> {\n const accountAddress = await signer.getAddress();\n\n const contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n const tx = {\n from: accountAddress,\n to: contractAddress,\n data,\n ...(value && value !== '0' ? { value: value } : {}),\n };\n\n let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate\n\n try {\n const gasEstimate = await signer.estimateGas(tx);\n gasLimit = getGasBuffer(gasEstimate);\n } catch (error) {\n const decodedError = await errorDecoder.decode(error);\n const reason = customReasonMapper(decodedError);\n console.error(\n `Error simulating external transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,\n );\n throw new Error(reason);\n }\n\n if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {\n const maxPriorityFeePerGas: bigint = BigInt('0');\n const maxFeePerGas: bigint = BigInt('100000000');\n return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };\n }\n\n return { ...tx, gasLimit };\n}\nexport async function executeTransaction(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n) {\n const txData = await estimateGasExternalChains(\n signer,\n data,\n value,\n chainId,\n targetContract,\n );\n\n try {\n const txResponse = await signer.sendTransaction(txData);\n const txReceipt = await txResponse.wait();\n return txReceipt;\n } catch (error) {\n console.warn(error);\n throw new Error('Transaction Execution Error');\n }\n}\nconst customReasonMapper = ({ name, reason }: DecodedError): string => {\n switch (name) {\n case 'SignatureInvalid':\n return `Invalid signature. Please refresh page and try again`;\n case 'StalePriceDetected':\n return `Price not updated, please try again a bit later`;\n case 'SmallOrderSize':\n return `Order size below minimum`;\n case 'DustyOrderSize':\n return `Order amount is not divisible by trade size spacing`;\n case 'AccountBelowIM':\n return `Account below required margin`;\n default:\n // This handles the non-custom errors\n return reason ?? 'Error executing transaction';\n }\n};\n\nfunction addGasBuffer(estimation: bigint): bigint {\n return (estimation * BigInt(120)) / BigInt(100);\n}\n"]}
|
|
1
|
+
{"version":3,"file":"executeTransaction.js","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAuD;AACvD,kCAA0D;AAC1D,yDAA+D;AAC/D,kDAAyC;AACzC,2DAAmD;AAEnD,yCAA2C;AAC3C,kCAAmD;AAEnD,IAAM,oBAAoB;IACxB,GAAC,mBAAW,CAAC,WAAW,IAAG,0BAA0B;IACrD,GAAC,mBAAW,CAAC,UAAU,IAAG,wCAAwC;OACnE,CAAC;AAQF,IAAM,YAAY,GAAG,kCAAY,CAAC,MAAM,CAAC,CAAC,iBAAG,CAAC,CAAC,CAAC;AAEhD,SAAsB,WAAW,CAC/B,MAA8B,EAC9B,IAAY,EACZ,KAAa,EACb,OAAe,EACf,cAAqC;;;;;wBAQd,qBAAM,MAAM,CAAC,UAAU,EAAE,EAAA;;oBAA1C,cAAc,GAAG,SAAyB;oBAE1C,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,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sourcesContent":["import { ethers, JsonRpcSigner, Signer } from 'ethers';\nimport { MoneyInOutChainId, ReyaChainId } from '../types';\nimport { ContractType, getAddress } from './contractAddresses';\nimport { abi } from './abis/Errors.json';\nimport { ErrorDecoder } from 'ethers-decode-error';\nimport type { DecodedError } from 'ethers-decode-error';\nimport { getGasBuffer } from './txHelpers';\nimport { getPublicRpcUrlsOrThrow } from '../utils';\n\nconst reyaChainIdRPCMapper: { [key: number]: string } = {\n [ReyaChainId.reyaNetwork]: 'https://rpc.reya.network',\n [ReyaChainId.reyaCronos]: 'https://rpc.reya-cronos.gelato.digital',\n};\n\nexport type Transaction = {\n from: string;\n to: string;\n data: string;\n value?: string;\n};\nconst errorDecoder = ErrorDecoder.create([abi]);\n\nexport async function estimateGas(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n): Promise<\n Transaction & {\n gasLimit: bigint;\n maxPriorityFeePerGas?: bigint;\n maxFeePerGas?: bigint;\n }\n> {\n const accountAddress = await signer.getAddress();\n\n const contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n const tx = {\n from: accountAddress, // @todo Update with relayer address\n to: contractAddress,\n data,\n ...(value && value !== '0' ? { value: value } : {}),\n };\n\n const gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate\n\n try {\n const provider = ethers.getDefaultProvider(reyaChainIdRPCMapper[chainId]);\n await provider.call(tx);\n } catch (error) {\n const decodedError = await errorDecoder.decode(error);\n const reason = customReasonMapper(decodedError);\n console.error(\n `Error simulating transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,\n );\n throw new Error(reason);\n }\n\n if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {\n const maxPriorityFeePerGas: bigint = BigInt('0');\n const maxFeePerGas: bigint = BigInt('100000000');\n return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };\n }\n\n return { ...tx, gasLimit };\n}\n\nexport async function estimateGasFromTxAndChainId(\n chainId: ReyaChainId | MoneyInOutChainId,\n tx: ethers.TransactionRequest,\n): Promise<bigint> {\n const rpcUrls = getPublicRpcUrlsOrThrow(chainId);\n let lastError = null;\n\n for (const url of rpcUrls) {\n try {\n const provider = new ethers.JsonRpcProvider(url);\n const estimation = await provider.estimateGas(tx);\n return addGasBuffer(estimation);\n } catch (error) {\n lastError = error;\n }\n }\n\n throw new Error('All RPC URLs failed; last error: ' + lastError);\n}\n\nexport async function estimateGasWithoutHardcoding(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n): Promise<\n Transaction & {\n gasLimit: bigint;\n maxPriorityFeePerGas?: bigint;\n maxFeePerGas?: bigint;\n }\n> {\n const accountAddress = await signer.getAddress();\n\n const contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n const tx = {\n from: accountAddress, // @todo Update with relayer address\n to: contractAddress,\n data,\n ...(value && value !== '0' ? { value: value } : {}),\n };\n\n let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate\n\n try {\n const provider = ethers.getDefaultProvider(reyaChainIdRPCMapper[chainId]);\n const estimation = await provider.estimateGas(tx);\n gasLimit = addGasBuffer(estimation);\n } catch (error) {\n const decodedError = await errorDecoder.decode(error);\n const reason = customReasonMapper(decodedError);\n console.error(\n `Error simulating transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,\n );\n throw new Error(reason);\n }\n\n if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {\n const maxPriorityFeePerGas: bigint = BigInt('0');\n const maxFeePerGas: bigint = BigInt('100000000');\n return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };\n }\n\n return { ...tx, gasLimit };\n}\n\nasync function estimateGasExternalChains(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n): Promise<\n Transaction & {\n gasLimit: bigint;\n maxPriorityFeePerGas?: bigint;\n maxFeePerGas?: bigint;\n }\n> {\n const accountAddress = await signer.getAddress();\n\n const contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n const tx = {\n from: accountAddress,\n to: contractAddress,\n data,\n ...(value && value !== '0' ? { value: value } : {}),\n };\n\n let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate\n\n try {\n const gasEstimate = await signer.estimateGas(tx);\n gasLimit = getGasBuffer(gasEstimate);\n } catch (error) {\n const decodedError = await errorDecoder.decode(error);\n const reason = customReasonMapper(decodedError);\n console.error(\n `Error simulating external transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,\n );\n throw new Error(reason);\n }\n\n if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {\n const maxPriorityFeePerGas: bigint = BigInt('0');\n const maxFeePerGas: bigint = BigInt('100000000');\n return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };\n }\n\n return { ...tx, gasLimit };\n}\nexport async function executeTransaction(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n) {\n const txData = await estimateGasExternalChains(\n signer,\n data,\n value,\n chainId,\n targetContract,\n );\n\n try {\n const txResponse = await signer.sendTransaction(txData);\n const txReceipt = await txResponse.wait();\n return txReceipt;\n } catch (error) {\n console.warn(error);\n throw new Error('Transaction Execution Error');\n }\n}\nconst customReasonMapper = ({ name, reason }: DecodedError): string => {\n switch (name) {\n case 'SignatureInvalid':\n return `Invalid signature. Please refresh page and try again`;\n case 'StalePriceDetected':\n return `Price not updated, please try again a bit later`;\n case 'SmallOrderSize':\n return `Order size below minimum`;\n case 'DustyOrderSize':\n return `Order amount is not divisible by trade size spacing`;\n case 'AccountBelowIM':\n return `Account below required margin`;\n default:\n // This handles the non-custom errors\n return reason ?? 'Error executing transaction';\n }\n};\n\nfunction addGasBuffer(estimation: bigint): bigint {\n return (estimation * BigInt(120)) / BigInt(100);\n}\n\n// eslint-disable-next-line @typescript-eslint/no-explicit-any\nexport async function decodeError(returnData: any): Promise<string> {\n const decodedError = await errorDecoder.decode(returnData);\n const reason = customReasonMapper(decodedError);\n return reason;\n}\n"]}
|
|
@@ -2,6 +2,7 @@ export type Call = {
|
|
|
2
2
|
target: string;
|
|
3
3
|
callData: string;
|
|
4
4
|
};
|
|
5
|
+
export declare const encodeCoreMulticall: (calls: Call[]) => string;
|
|
5
6
|
export declare const encodeStrictMulticall: (calls: Call[]) => string;
|
|
6
7
|
export declare const encodeOptionalMulticall: (calls: Call[]) => string;
|
|
7
8
|
//# sourceMappingURL=buildMulticallTx.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"buildMulticallTx.d.ts","sourceRoot":"/","sources":["transactions/buildMulticallTx.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"buildMulticallTx.d.ts","sourceRoot":"/","sources":["transactions/buildMulticallTx.ts"],"names":[],"mappings":"AAIA,MAAM,MAAM,IAAI,GAAG;IACjB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF,eAAO,MAAM,mBAAmB,UAAW,IAAI,EAAE,KAAG,MAMnD,CAAC;AAUF,eAAO,MAAM,qBAAqB,UAAW,IAAI,EAAE,WACrB,CAAC;AAE/B,eAAO,MAAM,uBAAuB,UAAW,IAAI,EAAE,WACtB,CAAC"}
|
|
@@ -13,5 +13,11 @@ export declare function estimateGas(signer: Signer | JsonRpcSigner, data: string
|
|
|
13
13
|
maxFeePerGas?: bigint;
|
|
14
14
|
}>;
|
|
15
15
|
export declare function estimateGasFromTxAndChainId(chainId: ReyaChainId | MoneyInOutChainId, tx: ethers.TransactionRequest): Promise<bigint>;
|
|
16
|
+
export declare function estimateGasWithoutHardcoding(signer: Signer | JsonRpcSigner, data: string, value: string, chainId: number, targetContract: ContractType | string): Promise<Transaction & {
|
|
17
|
+
gasLimit: bigint;
|
|
18
|
+
maxPriorityFeePerGas?: bigint;
|
|
19
|
+
maxFeePerGas?: bigint;
|
|
20
|
+
}>;
|
|
16
21
|
export declare function executeTransaction(signer: Signer | JsonRpcSigner, data: string, value: string, chainId: number, targetContract: ContractType | string): Promise<ethers.TransactionReceipt | null>;
|
|
22
|
+
export declare function decodeError(returnData: any): Promise<string>;
|
|
17
23
|
//# sourceMappingURL=executeTransaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executeTransaction.d.ts","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,aAAa,EAAE,MAAM,EAAE,MAAM,QAAQ,CAAC;AACvD,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAc,MAAM,qBAAqB,CAAC;AAY/D,MAAM,MAAM,WAAW,GAAG;IACxB,IAAI,EAAE,MAAM,CAAC;IACb,EAAE,EAAE,MAAM,CAAC;IACX,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,CAAC,EAAE,MAAM,CAAC;CAChB,CAAC;AAGF,wBAAsB,WAAW,CAC/B,MAAM,EAAE,MAAM,GAAG,aAAa,EAC9B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EACf,cAAc,EAAE,YAAY,GAAG,MAAM,GACpC,OAAO,CACR,WAAW,GAAG;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CACF,CAoCA;AAED,wBAAsB,2BAA2B,CAC/C,OAAO,EAAE,WAAW,GAAG,iBAAiB,EACxC,EAAE,EAAE,MAAM,CAAC,kBAAkB,GA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E,MAAM,EACf,cAAc,EAAE,YAAY,GAAG,MAAM,GACpC,OAAO,CACR,WAAW,GAAG;IACZ,QAAQ,EAAE,MAAM,CAAC;IACjB,oBAAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CACF,CAqCA;AAmDD,wBAAsB,kBAAkB,CACtC,MAAM,EAAE,MAAM,GAAG,aAAa,EAC9B,IAAI,EAAE,MAAM,EACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,MAAM,EACf,cAAc,EAAE,YAAY,GAAG,MAAM,6CAkBtC;AAwBD,wBAAsB,WAAW,CAAC,UAAU,EAAE,GAAG,GAAG,OAAO,CAAC,MAAM,CAAC,CAIlE"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/common",
|
|
3
|
-
"version": "0.175.
|
|
3
|
+
"version": "0.175.1",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -43,5 +43,5 @@
|
|
|
43
43
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
44
44
|
},
|
|
45
45
|
"packageManager": "pnpm@8.3.1",
|
|
46
|
-
"gitHead": "
|
|
46
|
+
"gitHead": "f78f28e82279642fcdb08365ffe4716f05fc8d69"
|
|
47
47
|
}
|
|
@@ -5645,6 +5645,125 @@
|
|
|
5645
5645
|
],
|
|
5646
5646
|
"outputs": [],
|
|
5647
5647
|
"stateMutability": "nonpayable"
|
|
5648
|
-
}
|
|
5648
|
+
},
|
|
5649
|
+
{
|
|
5650
|
+
"type": "function",
|
|
5651
|
+
"name": "tryAggregate",
|
|
5652
|
+
"inputs": [
|
|
5653
|
+
{
|
|
5654
|
+
"name": "requireSuccess",
|
|
5655
|
+
"type": "bool",
|
|
5656
|
+
"internalType": "bool"
|
|
5657
|
+
},
|
|
5658
|
+
{
|
|
5659
|
+
"name": "calls",
|
|
5660
|
+
"type": "bytes[]",
|
|
5661
|
+
"internalType": "bytes[]"
|
|
5662
|
+
}
|
|
5663
|
+
],
|
|
5664
|
+
"outputs": [
|
|
5665
|
+
{
|
|
5666
|
+
"name": "returnData",
|
|
5667
|
+
"type": "tuple[]",
|
|
5668
|
+
"internalType": "struct MulticallResult[]",
|
|
5669
|
+
"components": [
|
|
5670
|
+
{
|
|
5671
|
+
"name": "success",
|
|
5672
|
+
"type": "bool",
|
|
5673
|
+
"internalType": "bool"
|
|
5674
|
+
},
|
|
5675
|
+
{
|
|
5676
|
+
"name": "returnData",
|
|
5677
|
+
"type": "bytes",
|
|
5678
|
+
"internalType": "bytes"
|
|
5679
|
+
}
|
|
5680
|
+
]
|
|
5681
|
+
}
|
|
5682
|
+
],
|
|
5683
|
+
"stateMutability": "payable"
|
|
5684
|
+
},
|
|
5685
|
+
{
|
|
5686
|
+
"type": "error",
|
|
5687
|
+
"name": "MulticallFailed",
|
|
5688
|
+
"inputs": [
|
|
5689
|
+
{
|
|
5690
|
+
"name": "index",
|
|
5691
|
+
"type": "uint256",
|
|
5692
|
+
"internalType": "uint256"
|
|
5693
|
+
},
|
|
5694
|
+
{
|
|
5695
|
+
"name": "inputData",
|
|
5696
|
+
"type": "bytes",
|
|
5697
|
+
"internalType": "bytes"
|
|
5698
|
+
},
|
|
5699
|
+
{
|
|
5700
|
+
"name": "returnData",
|
|
5701
|
+
"type": "bytes",
|
|
5702
|
+
"internalType": "bytes"
|
|
5703
|
+
}
|
|
5704
|
+
]
|
|
5705
|
+
},
|
|
5706
|
+
{
|
|
5707
|
+
"type": "event",
|
|
5708
|
+
"name": "FailedCall",
|
|
5709
|
+
"inputs": [
|
|
5710
|
+
{
|
|
5711
|
+
"name": "index",
|
|
5712
|
+
"type": "uint256",
|
|
5713
|
+
"indexed": false,
|
|
5714
|
+
"internalType": "uint256"
|
|
5715
|
+
},
|
|
5716
|
+
{
|
|
5717
|
+
"name": "inputData",
|
|
5718
|
+
"type": "bytes",
|
|
5719
|
+
"indexed": false,
|
|
5720
|
+
"internalType": "bytes"
|
|
5721
|
+
},
|
|
5722
|
+
{
|
|
5723
|
+
"name": "returnData",
|
|
5724
|
+
"type": "bytes",
|
|
5725
|
+
"indexed": false,
|
|
5726
|
+
"internalType": "bytes"
|
|
5727
|
+
},
|
|
5728
|
+
{
|
|
5729
|
+
"name": "blockTimestamp",
|
|
5730
|
+
"type": "uint256",
|
|
5731
|
+
"indexed": false,
|
|
5732
|
+
"internalType": "uint256"
|
|
5733
|
+
}
|
|
5734
|
+
],
|
|
5735
|
+
"anonymous": false
|
|
5736
|
+
},
|
|
5737
|
+
{
|
|
5738
|
+
"type": "event",
|
|
5739
|
+
"name": "SuccessfulCall",
|
|
5740
|
+
"inputs": [
|
|
5741
|
+
{
|
|
5742
|
+
"name": "index",
|
|
5743
|
+
"type": "uint256",
|
|
5744
|
+
"indexed": false,
|
|
5745
|
+
"internalType": "uint256"
|
|
5746
|
+
},
|
|
5747
|
+
{
|
|
5748
|
+
"name": "inputData",
|
|
5749
|
+
"type": "bytes",
|
|
5750
|
+
"indexed": false,
|
|
5751
|
+
"internalType": "bytes"
|
|
5752
|
+
},
|
|
5753
|
+
{
|
|
5754
|
+
"name": "returnData",
|
|
5755
|
+
"type": "bytes",
|
|
5756
|
+
"indexed": false,
|
|
5757
|
+
"internalType": "bytes"
|
|
5758
|
+
},
|
|
5759
|
+
{
|
|
5760
|
+
"name": "blockTimestamp",
|
|
5761
|
+
"type": "uint256",
|
|
5762
|
+
"indexed": false,
|
|
5763
|
+
"internalType": "uint256"
|
|
5764
|
+
}
|
|
5765
|
+
],
|
|
5766
|
+
"anonymous": false
|
|
5767
|
+
}
|
|
5649
5768
|
]
|
|
5650
5769
|
}
|
|
@@ -3586,6 +3586,27 @@
|
|
|
3586
3586
|
"type": "error",
|
|
3587
3587
|
"name": "MismatchedLength",
|
|
3588
3588
|
"inputs": []
|
|
3589
|
+
},
|
|
3590
|
+
{
|
|
3591
|
+
"type": "error",
|
|
3592
|
+
"name": "MulticallFailed",
|
|
3593
|
+
"inputs": [
|
|
3594
|
+
{
|
|
3595
|
+
"name": "index",
|
|
3596
|
+
"type": "uint256",
|
|
3597
|
+
"internalType": "uint256"
|
|
3598
|
+
},
|
|
3599
|
+
{
|
|
3600
|
+
"name": "inputData",
|
|
3601
|
+
"type": "bytes",
|
|
3602
|
+
"internalType": "bytes"
|
|
3603
|
+
},
|
|
3604
|
+
{
|
|
3605
|
+
"name": "returnData",
|
|
3606
|
+
"type": "bytes",
|
|
3607
|
+
"internalType": "bytes"
|
|
3608
|
+
}
|
|
3609
|
+
]
|
|
3589
3610
|
}
|
|
3590
3611
|
]
|
|
3591
3612
|
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { ethers } from 'ethers';
|
|
2
|
+
import { abi as CoreAbi } from './abis/CoreProxy.json';
|
|
2
3
|
import { abi as MulticallAbi } from './abis/MulticallV2.json';
|
|
3
4
|
|
|
4
5
|
export type Call = {
|
|
@@ -6,6 +7,14 @@ export type Call = {
|
|
|
6
7
|
callData: string;
|
|
7
8
|
};
|
|
8
9
|
|
|
10
|
+
export const encodeCoreMulticall = (calls: Call[]): string => {
|
|
11
|
+
const functionSignature = 'tryAggregate';
|
|
12
|
+
const parameters: [boolean, string[]] = [false, calls.map((c) => c.callData)];
|
|
13
|
+
const INTERFACE = new ethers.Interface(CoreAbi);
|
|
14
|
+
const calldata = INTERFACE.encodeFunctionData(functionSignature, parameters);
|
|
15
|
+
return calldata;
|
|
16
|
+
};
|
|
17
|
+
|
|
9
18
|
const encodeMulticall = (requireSuccess: boolean, calls: Call[]): string => {
|
|
10
19
|
const functionSignature = 'tryAggregate';
|
|
11
20
|
const parameters: [boolean, Call[]] = [requireSuccess, calls];
|
|
@@ -90,6 +90,57 @@ export async function estimateGasFromTxAndChainId(
|
|
|
90
90
|
throw new Error('All RPC URLs failed; last error: ' + lastError);
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
+
export async function estimateGasWithoutHardcoding(
|
|
94
|
+
signer: Signer | JsonRpcSigner,
|
|
95
|
+
data: string,
|
|
96
|
+
value: string,
|
|
97
|
+
chainId: number,
|
|
98
|
+
targetContract: ContractType | string,
|
|
99
|
+
): Promise<
|
|
100
|
+
Transaction & {
|
|
101
|
+
gasLimit: bigint;
|
|
102
|
+
maxPriorityFeePerGas?: bigint;
|
|
103
|
+
maxFeePerGas?: bigint;
|
|
104
|
+
}
|
|
105
|
+
> {
|
|
106
|
+
const accountAddress = await signer.getAddress();
|
|
107
|
+
|
|
108
|
+
const contractAddress = Object.values(ContractType).includes(
|
|
109
|
+
targetContract as ContractType,
|
|
110
|
+
)
|
|
111
|
+
? getAddress(chainId, targetContract as ContractType)
|
|
112
|
+
: targetContract;
|
|
113
|
+
const tx = {
|
|
114
|
+
from: accountAddress, // @todo Update with relayer address
|
|
115
|
+
to: contractAddress,
|
|
116
|
+
data,
|
|
117
|
+
...(value && value !== '0' ? { value: value } : {}),
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate
|
|
121
|
+
|
|
122
|
+
try {
|
|
123
|
+
const provider = ethers.getDefaultProvider(reyaChainIdRPCMapper[chainId]);
|
|
124
|
+
const estimation = await provider.estimateGas(tx);
|
|
125
|
+
gasLimit = addGasBuffer(estimation);
|
|
126
|
+
} catch (error) {
|
|
127
|
+
const decodedError = await errorDecoder.decode(error);
|
|
128
|
+
const reason = customReasonMapper(decodedError);
|
|
129
|
+
console.error(
|
|
130
|
+
`Error simulating transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,
|
|
131
|
+
);
|
|
132
|
+
throw new Error(reason);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {
|
|
136
|
+
const maxPriorityFeePerGas: bigint = BigInt('0');
|
|
137
|
+
const maxFeePerGas: bigint = BigInt('100000000');
|
|
138
|
+
return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
return { ...tx, gasLimit };
|
|
142
|
+
}
|
|
143
|
+
|
|
93
144
|
async function estimateGasExternalChains(
|
|
94
145
|
signer: Signer | JsonRpcSigner,
|
|
95
146
|
data: string,
|
|
@@ -184,3 +235,10 @@ const customReasonMapper = ({ name, reason }: DecodedError): string => {
|
|
|
184
235
|
function addGasBuffer(estimation: bigint): bigint {
|
|
185
236
|
return (estimation * BigInt(120)) / BigInt(100);
|
|
186
237
|
}
|
|
238
|
+
|
|
239
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
240
|
+
export async function decodeError(returnData: any): Promise<string> {
|
|
241
|
+
const decodedError = await errorDecoder.decode(returnData);
|
|
242
|
+
const reason = customReasonMapper(decodedError);
|
|
243
|
+
return reason;
|
|
244
|
+
}
|