@reyaxyz/common 0.272.0 → 0.273.0
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/executeTransaction.js +14 -52
- package/dist/transactions/executeTransaction.js.map +1 -1
- package/dist/types/transactions/executeTransaction.d.ts +0 -5
- package/dist/types/transactions/executeTransaction.d.ts.map +1 -1
- package/package.json +2 -2
- package/src/transactions/executeTransaction.ts +17 -53
package/README.md
CHANGED
|
@@ -6,5 +6,5 @@
|
|
|
6
6
|
|
|
7
7
|
| Statements | Branches | Functions | Lines |
|
|
8
8
|
| --------------------------- | ----------------------- | ------------------------- | ----------------- |
|
|
9
|
-
|  |  |  |  |
|
|
10
10
|
|
|
@@ -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.decodeErrorBytes = exports.executeTransaction = exports.
|
|
51
|
+
exports.decodeErrorBytes = exports.executeTransaction = exports.estimateGasFromTxAndChainId = exports.estimateGasGivenAddress = exports.estimateGas = exports.reyaChainIdRPCMapper = void 0;
|
|
52
52
|
var ethers_1 = require("ethers");
|
|
53
53
|
var types_1 = require("../types");
|
|
54
54
|
var contractAddresses_1 = require("./contractAddresses");
|
|
@@ -78,7 +78,7 @@ function estimateGas(signer, data, value, chainId, targetContract) {
|
|
|
78
78
|
exports.estimateGas = estimateGas;
|
|
79
79
|
function estimateGasGivenAddress(accountAddress, data, value, chainId, targetContract) {
|
|
80
80
|
return __awaiter(this, void 0, void 0, function () {
|
|
81
|
-
var contractAddress, tx, gasLimit, provider, error_1, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
81
|
+
var contractAddress, tx, gasLimit, network, provider, error_1, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
82
82
|
return __generator(this, function (_a) {
|
|
83
83
|
switch (_a.label) {
|
|
84
84
|
case 0:
|
|
@@ -90,7 +90,11 @@ function estimateGasGivenAddress(accountAddress, data, value, chainId, targetCon
|
|
|
90
90
|
_a.label = 1;
|
|
91
91
|
case 1:
|
|
92
92
|
_a.trys.push([1, 3, , 5]);
|
|
93
|
-
|
|
93
|
+
network = new ethers_1.Network('Reya', chainId);
|
|
94
|
+
provider = new ethers_1.JsonRpcProvider(exports.reyaChainIdRPCMapper[chainId], network, {
|
|
95
|
+
staticNetwork: network,
|
|
96
|
+
pollingInterval: 50,
|
|
97
|
+
});
|
|
94
98
|
return [4 /*yield*/, provider.call(tx)];
|
|
95
99
|
case 2:
|
|
96
100
|
_a.sent();
|
|
@@ -149,51 +153,9 @@ function estimateGasFromTxAndChainId(chainId, tx) {
|
|
|
149
153
|
});
|
|
150
154
|
}
|
|
151
155
|
exports.estimateGasFromTxAndChainId = estimateGasFromTxAndChainId;
|
|
152
|
-
function estimateGasWithoutHardcoding(signer, data, value, chainId, targetContract) {
|
|
153
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
154
|
-
var accountAddress, contractAddress, tx, gasLimit, provider, estimation, error_3, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
155
|
-
return __generator(this, function (_a) {
|
|
156
|
-
switch (_a.label) {
|
|
157
|
-
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
158
|
-
case 1:
|
|
159
|
-
accountAddress = _a.sent();
|
|
160
|
-
contractAddress = Object.values(contractAddresses_1.ContractType).includes(targetContract)
|
|
161
|
-
? (0, contractAddresses_1.getAddress)(chainId, targetContract)
|
|
162
|
-
: targetContract;
|
|
163
|
-
tx = __assign({ from: accountAddress, to: contractAddress, data: data }, (value && value !== '0' ? { value: value } : {}));
|
|
164
|
-
gasLimit = BigInt('5000000');
|
|
165
|
-
_a.label = 2;
|
|
166
|
-
case 2:
|
|
167
|
-
_a.trys.push([2, 4, , 6]);
|
|
168
|
-
provider = ethers_1.ethers.getDefaultProvider(exports.reyaChainIdRPCMapper[chainId]);
|
|
169
|
-
return [4 /*yield*/, provider.estimateGas(tx)];
|
|
170
|
-
case 3:
|
|
171
|
-
estimation = _a.sent();
|
|
172
|
-
gasLimit = addGasBuffer(estimation);
|
|
173
|
-
return [3 /*break*/, 6];
|
|
174
|
-
case 4:
|
|
175
|
-
error_3 = _a.sent();
|
|
176
|
-
return [4 /*yield*/, errorDecoder.decode(error_3)];
|
|
177
|
-
case 5:
|
|
178
|
-
decodedError = _a.sent();
|
|
179
|
-
reason = customReasonMapper(decodedError);
|
|
180
|
-
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));
|
|
181
|
-
throw new Error(reason);
|
|
182
|
-
case 6:
|
|
183
|
-
if (Object.values(types_1.ReyaChainId).includes(chainId)) {
|
|
184
|
-
maxPriorityFeePerGas = BigInt('0');
|
|
185
|
-
maxFeePerGas = BigInt('100000000');
|
|
186
|
-
return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit, maxPriorityFeePerGas: maxPriorityFeePerGas, maxFeePerGas: maxFeePerGas })];
|
|
187
|
-
}
|
|
188
|
-
return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit })];
|
|
189
|
-
}
|
|
190
|
-
});
|
|
191
|
-
});
|
|
192
|
-
}
|
|
193
|
-
exports.estimateGasWithoutHardcoding = estimateGasWithoutHardcoding;
|
|
194
156
|
function estimateGasExternalChains(signer, data, value, chainId, targetContract) {
|
|
195
157
|
return __awaiter(this, void 0, void 0, function () {
|
|
196
|
-
var accountAddress, contractAddress, tx, gasLimit, gasEstimate,
|
|
158
|
+
var accountAddress, contractAddress, tx, gasLimit, gasEstimate, error_3, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
197
159
|
return __generator(this, function (_a) {
|
|
198
160
|
switch (_a.label) {
|
|
199
161
|
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
@@ -213,12 +175,12 @@ function estimateGasExternalChains(signer, data, value, chainId, targetContract)
|
|
|
213
175
|
gasLimit = (0, txHelpers_1.getGasBuffer)(gasEstimate);
|
|
214
176
|
return [3 /*break*/, 6];
|
|
215
177
|
case 4:
|
|
216
|
-
|
|
217
|
-
return [4 /*yield*/, errorDecoder.decode(
|
|
178
|
+
error_3 = _a.sent();
|
|
179
|
+
return [4 /*yield*/, errorDecoder.decode(error_3)];
|
|
218
180
|
case 5:
|
|
219
181
|
decodedError = _a.sent();
|
|
220
182
|
reason = customReasonMapper(decodedError);
|
|
221
|
-
console.error("Error simulating external transaction account address ".concat(accountAddress, " reason ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, " ").concat(decodedError, " ").concat(
|
|
183
|
+
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_3));
|
|
222
184
|
throw new Error(reason);
|
|
223
185
|
case 6:
|
|
224
186
|
if (Object.values(types_1.ReyaChainId).includes(chainId)) {
|
|
@@ -233,7 +195,7 @@ function estimateGasExternalChains(signer, data, value, chainId, targetContract)
|
|
|
233
195
|
}
|
|
234
196
|
function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
235
197
|
return __awaiter(this, void 0, void 0, function () {
|
|
236
|
-
var txData, txResponse, txReceipt,
|
|
198
|
+
var txData, txResponse, txReceipt, error_4;
|
|
237
199
|
return __generator(this, function (_a) {
|
|
238
200
|
switch (_a.label) {
|
|
239
201
|
case 0: return [4 /*yield*/, estimateGasExternalChains(signer, data, value, chainId, targetContract)];
|
|
@@ -250,8 +212,8 @@ function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
|
250
212
|
txReceipt = _a.sent();
|
|
251
213
|
return [2 /*return*/, txReceipt];
|
|
252
214
|
case 5:
|
|
253
|
-
|
|
254
|
-
console.warn(
|
|
215
|
+
error_4 = _a.sent();
|
|
216
|
+
console.warn(error_4);
|
|
255
217
|
throw new Error('Transaction Execution Error');
|
|
256
218
|
case 6: return [2 /*return*/];
|
|
257
219
|
}
|
|
@@ -1 +1 @@
|
|
|
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;AACnD,iCAAmC;AAEtB,QAAA,oBAAoB;IAC/B,GAAC,mBAAW,CAAC,WAAW,IACtB,2DAA2D;IAC7D,GAAC,mBAAW,CAAC,UAAU,IACrB,yEAAyE;QAC3E;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;oBAEhD,sBAAO,uBAAuB,CAC5B,cAAc,EACd,IAAI,EACJ,KAAK,EACL,OAAO,EACP,cAAc,CACf,EAAC;;;;CACH;AAtBD,kCAsBC;AAED,SAAsB,uBAAuB,CAC3C,cAAsB,EACtB,IAAY,EACZ,KAAa,EACb,OAAe,EACf,cAAqC;;;;;;oBAQ/B,eAAe,GAAG,MAAM,CAAC,MAAM,CAAC,gCAAY,CAAC,CAAC,QAAQ,CAC1D,cAA8B,CAC/B;wBACC,CAAC,CAAC,IAAA,8BAAU,EAAC,OAAO,EAAE,cAA8B,CAAC;wBACrD,CAAC,CAAC,cAAc,CAAC;oBAEb,EAAE,cACN,IAAI,EAAE,cAAc,EACpB,EAAE,EAAE,eAAe,EACnB,IAAI,MAAA,IACD,CAAC,KAAK,IAAI,KAAK,KAAK,GAAG,CAA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S,EAAC;;;oBAEjB,OAAO,CAAC,IAAI,CAAC,OAAK,CAAC,CAAC;oBACpB,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;;;;;CAElD;AAvBD,gDAuBC;AACD,IAAM,kBAAkB,GAAG,UAAC,EAA8B;QAA5B,IAAI,UAAA,EAAE,MAAM,YAAA;IACxC,QAAQ,IAAI,EAAE,CAAC;QACb,KAAK,kBAAkB;YACrB,OAAO,sDAAsD,CAAC;QAChE,KAAK,oBAAoB;YACvB,OAAO,iDAAiD,CAAC;QAC3D,KAAK,gBAAgB;YACnB,OAAO,0BAA0B,CAAC;QACpC,KAAK,gBAAgB;YACnB,OAAO,qDAAqD,CAAC;QAC/D,KAAK,gBAAgB;YACnB,OAAO,+BAA+B,CAAC;QACzC;YACE,qCAAqC;YACrC,OAAO,MAAM,aAAN,MAAM,cAAN,MAAM,GAAI,6BAA6B,CAAC;IACnD,CAAC;AACH,CAAC,CAAC;AAEF,SAAS,YAAY,CAAC,UAAkB;IACtC,OAAO,CAAC,UAAU,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;AAClD,CAAC;AAED,SAAsB,gBAAgB,CAAC,UAAkB;;;;YACjD,SAAS,GAAG,IAAI,kBAAS,CAAC,iBAAG,CAAC,CAAC;YAC/B,YAAY,GAAG,SAAS,CAAC,UAAU,CAAC,UAAU,CAAC,CAAC;YACtD,OAAO,CAAC,GAAG,CAAC,YAAY,CAAC,CAAC;YAC1B,sBAAO,UAAG,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,IAAI,eAAK,YAAY,aAAZ,YAAY,uBAAZ,YAAY,CAAE,IAAI,CAAE,IAAI,iBAAiB,EAAC;;;CAC5E;AALD,4CAKC","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';\nimport { Interface } from 'ethers';\n\nexport const reyaChainIdRPCMapper: { [key: number]: string } = {\n [ReyaChainId.reyaNetwork]:\n 'https://rpc.reya.network/263e76b89da84d478fd2b6e8783a1cfe',\n [ReyaChainId.reyaCronos]:\n 'https://rpc.reya-cronos.gelato.digital/263e76b89da84d478fd2b6e8783a1cfe',\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 return estimateGasGivenAddress(\n accountAddress,\n data,\n value,\n chainId,\n targetContract,\n );\n}\n\nexport async function estimateGasGivenAddress(\n accountAddress: string,\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 contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n\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\nexport async function decodeErrorBytes(returnData: string): Promise<string> {\n const INTERFACE = new Interface(abi);\n const decodedError = INTERFACE.parseError(returnData);\n console.log(decodedError);\n return `${decodedError?.name}: ${decodedError?.args}` || 'Error not found';\n}\n"]}
|
|
1
|
+
{"version":3,"file":"executeTransaction.js","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAMgB;AAChB,kCAA0D;AAC1D,yDAA+D;AAC/D,kDAAyC;AACzC,2DAAmD;AAEnD,yCAA2C;AAC3C,kCAAmD;AACnD,iCAAmC;AAEtB,QAAA,oBAAoB;IAC/B,GAAC,mBAAW,CAAC,WAAW,IACtB,2DAA2D;IAC7D,GAAC,mBAAW,CAAC,UAAU,IACrB,yEAAyE;QAC3E;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;oBAEhD,sBAAO,uBAAuB,CAC5B,cAAc,EACd,IAAI,EACJ,KAAK,EACL,OAAO,EACP,cAAc,CACf,EAAC;;;;CACH;AAtBD,kCAsBC;AAED,SAAsB,uBAAuB,CAC3C,cAAsB,EACtB,IAAY,EACZ,KAAa,EACb,OAAe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iB,EAAC;;;CAC5E;AALD,4CAKC","sourcesContent":["import {\n ethers,\n JsonRpcProvider,\n JsonRpcSigner,\n Network,\n Signer,\n} 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';\nimport { Interface } from 'ethers';\n\nexport const reyaChainIdRPCMapper: { [key: number]: string } = {\n [ReyaChainId.reyaNetwork]:\n 'https://rpc.reya.network/263e76b89da84d478fd2b6e8783a1cfe',\n [ReyaChainId.reyaCronos]:\n 'https://rpc.reya-cronos.gelato.digital/263e76b89da84d478fd2b6e8783a1cfe',\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 return estimateGasGivenAddress(\n accountAddress,\n data,\n value,\n chainId,\n targetContract,\n );\n}\n\nexport async function estimateGasGivenAddress(\n accountAddress: string,\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 contractAddress = Object.values(ContractType).includes(\n targetContract as ContractType,\n )\n ? getAddress(chainId, targetContract as ContractType)\n : targetContract;\n\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 network = new Network('Reya', chainId);\n const provider = new JsonRpcProvider(\n reyaChainIdRPCMapper[chainId],\n network,\n {\n staticNetwork: network,\n pollingInterval: 50,\n },\n );\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}\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\nexport async function decodeErrorBytes(returnData: string): Promise<string> {\n const INTERFACE = new Interface(abi);\n const decodedError = INTERFACE.parseError(returnData);\n console.log(decodedError);\n return `${decodedError?.name}: ${decodedError?.args}` || 'Error not found';\n}\n"]}
|
|
@@ -21,11 +21,6 @@ export declare function estimateGasGivenAddress(accountAddress: string, data: st
|
|
|
21
21
|
maxFeePerGas?: bigint;
|
|
22
22
|
}>;
|
|
23
23
|
export declare function estimateGasFromTxAndChainId(chainId: ReyaChainId | MoneyInOutChainId, tx: ethers.TransactionRequest): Promise<bigint>;
|
|
24
|
-
export declare function estimateGasWithoutHardcoding(signer: Signer | JsonRpcSigner, data: string, value: string, chainId: number, targetContract: ContractType | string): Promise<Transaction & {
|
|
25
|
-
gasLimit: bigint;
|
|
26
|
-
maxPriorityFeePerGas?: bigint;
|
|
27
|
-
maxFeePerGas?: bigint;
|
|
28
|
-
}>;
|
|
29
24
|
export declare function executeTransaction(signer: Signer | JsonRpcSigner, data: string, value: string, chainId: number, targetContract: ContractType | string): Promise<ethers.TransactionReceipt | null>;
|
|
30
25
|
export declare function decodeErrorBytes(returnData: string): Promise<string>;
|
|
31
26
|
//# sourceMappingURL=executeTransaction.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"executeTransaction.d.ts","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,
|
|
1
|
+
{"version":3,"file":"executeTransaction.d.ts","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":"AAAA,OAAO,EACL,MAAM,EAEN,aAAa,EAEb,MAAM,EACP,MAAM,QAAQ,CAAC;AAChB,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,MAAM,UAAU,CAAC;AAC1D,OAAO,EAAE,YAAY,EAAc,MAAM,qBAAqB,CAAC;AAQ/D,eAAO,MAAM,oBAAoB,EAAE;IAAE,CAAC,GAAG,EAAE,MAAM,GAAG,MAAM,CAAA;CAKzD,CAAC;AAEF,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,CAUA;AAED,wBAAsB,uBAAuB,CAC3C,cAAc,EAAE,MAAM,EACtB,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,CA2CA;AAED,wBAAsB,2BAA2B,CAC/C,OAAO,EAAE,WAAW,GAAG,iBAAiB,EACxC,EAAE,EAAE,MAAM,CAAC,kBAAkB,GAC5B,OAAO,CAAC,MAAM,CAAC,CAejB;AAoDD,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;AAuBD,wBAAsB,gBAAgB,CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC,CAK1E"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@reyaxyz/common",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.273.0",
|
|
4
4
|
"publishConfig": {
|
|
5
5
|
"access": "public",
|
|
6
6
|
"registry": "https://registry.npmjs.org"
|
|
@@ -44,5 +44,5 @@
|
|
|
44
44
|
"generate:coverage-badges": "npx istanbul-badges-readme --silent"
|
|
45
45
|
},
|
|
46
46
|
"packageManager": "pnpm@8.3.1",
|
|
47
|
-
"gitHead": "
|
|
47
|
+
"gitHead": "ad492b9166a0dc0b46a43ca6f1e4cf33c8dcabef"
|
|
48
48
|
}
|
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import {
|
|
2
|
+
ethers,
|
|
3
|
+
JsonRpcProvider,
|
|
4
|
+
JsonRpcSigner,
|
|
5
|
+
Network,
|
|
6
|
+
Signer,
|
|
7
|
+
} from 'ethers';
|
|
2
8
|
import { MoneyInOutChainId, ReyaChainId } from '../types';
|
|
3
9
|
import { ContractType, getAddress } from './contractAddresses';
|
|
4
10
|
import { abi } from './abis/Errors.json';
|
|
@@ -76,7 +82,15 @@ export async function estimateGasGivenAddress(
|
|
|
76
82
|
const gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate
|
|
77
83
|
|
|
78
84
|
try {
|
|
79
|
-
const
|
|
85
|
+
const network = new Network('Reya', chainId);
|
|
86
|
+
const provider = new JsonRpcProvider(
|
|
87
|
+
reyaChainIdRPCMapper[chainId],
|
|
88
|
+
network,
|
|
89
|
+
{
|
|
90
|
+
staticNetwork: network,
|
|
91
|
+
pollingInterval: 50,
|
|
92
|
+
},
|
|
93
|
+
);
|
|
80
94
|
await provider.call(tx);
|
|
81
95
|
} catch (error) {
|
|
82
96
|
const decodedError = await errorDecoder.decode(error);
|
|
@@ -116,57 +130,6 @@ export async function estimateGasFromTxAndChainId(
|
|
|
116
130
|
throw new Error('All RPC URLs failed; last error: ' + lastError);
|
|
117
131
|
}
|
|
118
132
|
|
|
119
|
-
export async function estimateGasWithoutHardcoding(
|
|
120
|
-
signer: Signer | JsonRpcSigner,
|
|
121
|
-
data: string,
|
|
122
|
-
value: string,
|
|
123
|
-
chainId: number,
|
|
124
|
-
targetContract: ContractType | string,
|
|
125
|
-
): Promise<
|
|
126
|
-
Transaction & {
|
|
127
|
-
gasLimit: bigint;
|
|
128
|
-
maxPriorityFeePerGas?: bigint;
|
|
129
|
-
maxFeePerGas?: bigint;
|
|
130
|
-
}
|
|
131
|
-
> {
|
|
132
|
-
const accountAddress = await signer.getAddress();
|
|
133
|
-
|
|
134
|
-
const contractAddress = Object.values(ContractType).includes(
|
|
135
|
-
targetContract as ContractType,
|
|
136
|
-
)
|
|
137
|
-
? getAddress(chainId, targetContract as ContractType)
|
|
138
|
-
: targetContract;
|
|
139
|
-
const tx = {
|
|
140
|
-
from: accountAddress, // @todo Update with relayer address
|
|
141
|
-
to: contractAddress,
|
|
142
|
-
data,
|
|
143
|
-
...(value && value !== '0' ? { value: value } : {}),
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate
|
|
147
|
-
|
|
148
|
-
try {
|
|
149
|
-
const provider = ethers.getDefaultProvider(reyaChainIdRPCMapper[chainId]);
|
|
150
|
-
const estimation = await provider.estimateGas(tx);
|
|
151
|
-
gasLimit = addGasBuffer(estimation);
|
|
152
|
-
} catch (error) {
|
|
153
|
-
const decodedError = await errorDecoder.decode(error);
|
|
154
|
-
const reason = customReasonMapper(decodedError);
|
|
155
|
-
console.error(
|
|
156
|
-
`Error simulating transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,
|
|
157
|
-
);
|
|
158
|
-
throw new Error(reason);
|
|
159
|
-
}
|
|
160
|
-
|
|
161
|
-
if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {
|
|
162
|
-
const maxPriorityFeePerGas: bigint = BigInt('0');
|
|
163
|
-
const maxFeePerGas: bigint = BigInt('100000000');
|
|
164
|
-
return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
return { ...tx, gasLimit };
|
|
168
|
-
}
|
|
169
|
-
|
|
170
133
|
async function estimateGasExternalChains(
|
|
171
134
|
signer: Signer | JsonRpcSigner,
|
|
172
135
|
data: string,
|
|
@@ -216,6 +179,7 @@ async function estimateGasExternalChains(
|
|
|
216
179
|
|
|
217
180
|
return { ...tx, gasLimit };
|
|
218
181
|
}
|
|
182
|
+
|
|
219
183
|
export async function executeTransaction(
|
|
220
184
|
signer: Signer | JsonRpcSigner,
|
|
221
185
|
data: string,
|