@reyaxyz/common 0.137.0 → 0.139.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 CHANGED
@@ -6,5 +6,5 @@
6
6
 
7
7
  | Statements | Branches | Functions | Lines |
8
8
  | --------------------------- | ----------------------- | ------------------------- | ----------------- |
9
- | ![Statements](https://img.shields.io/badge/statements-15.41%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-13.09%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-9.09%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-15.98%25-red.svg?style=flat) |
9
+ | ![Statements](https://img.shields.io/badge/statements-15%25-red.svg?style=flat) | ![Branches](https://img.shields.io/badge/branches-12.57%25-red.svg?style=flat) | ![Functions](https://img.shields.io/badge/functions-9.02%25-red.svg?style=flat) | ![Lines](https://img.shields.io/badge/lines-15.54%25-red.svg?style=flat) |
10
10
 
@@ -5,19 +5,21 @@ exports.getNativeToken = void 0;
5
5
  var types_1 = require("../types");
6
6
  var nativeToken = (_a = {},
7
7
  // mainnet
8
+ _a[types_1.ReyaChainId.reyaNetwork] = 'ETH',
8
9
  _a[types_1.MoneyInOutChainId.ethereumMainnet] = 'ETH',
9
10
  _a[types_1.MoneyInOutChainId.opMainnet] = 'ETH',
10
11
  _a[types_1.MoneyInOutChainId.polygonMainnet] = 'MATIC',
11
12
  _a[types_1.MoneyInOutChainId.arbitrumOne] = 'ETH',
12
13
  _a[types_1.MoneyInOutChainId.baseMainnet] = 'ETH',
13
14
  // testnet
15
+ _a[types_1.ReyaChainId.reyaCronos] = 'ETH',
14
16
  _a[types_1.MoneyInOutChainId.polygonMumbai] = 'MATIC',
15
17
  _a[types_1.MoneyInOutChainId.arbitrumSepolia] = 'ETH',
16
18
  _a[types_1.MoneyInOutChainId.opSepolia] = 'ETH',
17
19
  _a[types_1.MoneyInOutChainId.ethereumSepolia] = 'ETH',
18
20
  _a);
19
- var getNativeToken = function (moneyInOutChainId) {
20
- return nativeToken[moneyInOutChainId];
21
+ var getNativeToken = function (chainId) {
22
+ return nativeToken[chainId];
21
23
  };
22
24
  exports.getNativeToken = getNativeToken;
23
25
  //# sourceMappingURL=getNativeToken.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"getNativeToken.js","sourceRoot":"/","sources":["services/getNativeToken.ts"],"names":[],"mappings":";;;;AAAA,kCAAwD;AAExD,IAAM,WAAW;IACf,UAAU;IACV,GAAC,yBAAiB,CAAC,eAAe,IAAG,KAAK;IAC1C,GAAC,yBAAiB,CAAC,SAAS,IAAG,KAAK;IACpC,GAAC,yBAAiB,CAAC,cAAc,IAAG,OAAO;IAC3C,GAAC,yBAAiB,CAAC,WAAW,IAAG,KAAK;IACtC,GAAC,yBAAiB,CAAC,WAAW,IAAG,KAAK;IACtC,UAAU;IACV,GAAC,yBAAiB,CAAC,aAAa,IAAG,OAAO;IAC1C,GAAC,yBAAiB,CAAC,eAAe,IAAG,KAAK;IAC1C,GAAC,yBAAiB,CAAC,SAAS,IAAG,KAAK;IACpC,GAAC,yBAAiB,CAAC,eAAe,IAAG,KAAK;OAC3C,CAAC;AAEK,IAAM,cAAc,GAAG,UAC5B,iBAAoC;IAEpC,OAAO,WAAW,CAAC,iBAAiB,CAAC,CAAC;AACxC,CAAC,CAAC;AAJW,QAAA,cAAc,kBAIzB","sourcesContent":["import { MoneyInOutChainId, TokenName } from '../types';\n\nconst nativeToken: Record<MoneyInOutChainId, TokenName> = {\n // mainnet\n [MoneyInOutChainId.ethereumMainnet]: 'ETH',\n [MoneyInOutChainId.opMainnet]: 'ETH',\n [MoneyInOutChainId.polygonMainnet]: 'MATIC',\n [MoneyInOutChainId.arbitrumOne]: 'ETH',\n [MoneyInOutChainId.baseMainnet]: 'ETH',\n // testnet\n [MoneyInOutChainId.polygonMumbai]: 'MATIC',\n [MoneyInOutChainId.arbitrumSepolia]: 'ETH',\n [MoneyInOutChainId.opSepolia]: 'ETH',\n [MoneyInOutChainId.ethereumSepolia]: 'ETH',\n};\n\nexport const getNativeToken = (\n moneyInOutChainId: MoneyInOutChainId,\n): TokenName => {\n return nativeToken[moneyInOutChainId];\n};\n"]}
1
+ {"version":3,"file":"getNativeToken.js","sourceRoot":"/","sources":["services/getNativeToken.ts"],"names":[],"mappings":";;;;AAAA,kCAAqE;AAErE,IAAM,WAAW;IACf,UAAU;IACV,GAAC,mBAAW,CAAC,WAAW,IAAG,KAAK;IAChC,GAAC,yBAAiB,CAAC,eAAe,IAAG,KAAK;IAC1C,GAAC,yBAAiB,CAAC,SAAS,IAAG,KAAK;IACpC,GAAC,yBAAiB,CAAC,cAAc,IAAG,OAAO;IAC3C,GAAC,yBAAiB,CAAC,WAAW,IAAG,KAAK;IACtC,GAAC,yBAAiB,CAAC,WAAW,IAAG,KAAK;IACtC,UAAU;IACV,GAAC,mBAAW,CAAC,UAAU,IAAG,KAAK;IAC/B,GAAC,yBAAiB,CAAC,aAAa,IAAG,OAAO;IAC1C,GAAC,yBAAiB,CAAC,eAAe,IAAG,KAAK;IAC1C,GAAC,yBAAiB,CAAC,SAAS,IAAG,KAAK;IACpC,GAAC,yBAAiB,CAAC,eAAe,IAAG,KAAK;OAC3C,CAAC;AAEK,IAAM,cAAc,GAAG,UAC5B,OAAwC;IAExC,OAAO,WAAW,CAAC,OAAO,CAAC,CAAC;AAC9B,CAAC,CAAC;AAJW,QAAA,cAAc,kBAIzB","sourcesContent":["import { MoneyInOutChainId, ReyaChainId, TokenName } from '../types';\n\nconst nativeToken: Record<MoneyInOutChainId | ReyaChainId, TokenName> = {\n // mainnet\n [ReyaChainId.reyaNetwork]: 'ETH',\n [MoneyInOutChainId.ethereumMainnet]: 'ETH',\n [MoneyInOutChainId.opMainnet]: 'ETH',\n [MoneyInOutChainId.polygonMainnet]: 'MATIC',\n [MoneyInOutChainId.arbitrumOne]: 'ETH',\n [MoneyInOutChainId.baseMainnet]: 'ETH',\n // testnet\n [ReyaChainId.reyaCronos]: 'ETH',\n [MoneyInOutChainId.polygonMumbai]: 'MATIC',\n [MoneyInOutChainId.arbitrumSepolia]: 'ETH',\n [MoneyInOutChainId.opSepolia]: 'ETH',\n [MoneyInOutChainId.ethereumSepolia]: 'ETH',\n};\n\nexport const getNativeToken = (\n chainId: MoneyInOutChainId | ReyaChainId,\n): TokenName => {\n return nativeToken[chainId];\n};\n"]}
@@ -54,6 +54,7 @@ var types_1 = require("../types");
54
54
  var contractAddresses_1 = require("./contractAddresses");
55
55
  var Errors_json_1 = require("./abis/Errors.json");
56
56
  var ethers_decode_error_1 = require("ethers-decode-error");
57
+ var txHelpers_1 = require("./txHelpers");
57
58
  var reyaChainIdRPCMapper = (_a = {},
58
59
  _a[types_1.ReyaChainId.reyaNetwork] = 'https://rpc.reya.network',
59
60
  _a[types_1.ReyaChainId.reyaCronos] = 'https://rpc.reya-cronos.gelato.digital',
@@ -100,12 +101,52 @@ function estimateGas(signer, data, value, chainId, targetContract) {
100
101
  });
101
102
  }
102
103
  exports.estimateGas = estimateGas;
104
+ function estimateGasExternalChains(signer, data, value, chainId, targetContract) {
105
+ return __awaiter(this, void 0, void 0, function () {
106
+ var accountAddress, contractAddress, tx, gasLimit, gasEstimate, error_2, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
107
+ return __generator(this, function (_a) {
108
+ switch (_a.label) {
109
+ case 0: return [4 /*yield*/, signer.getAddress()];
110
+ case 1:
111
+ accountAddress = _a.sent();
112
+ contractAddress = Object.values(contractAddresses_1.ContractType).includes(targetContract)
113
+ ? (0, contractAddresses_1.getAddress)(chainId, targetContract)
114
+ : targetContract;
115
+ tx = __assign({ from: accountAddress, to: contractAddress, data: data }, (value && value !== '0' ? { value: value } : {}));
116
+ gasLimit = BigInt('5000000');
117
+ _a.label = 2;
118
+ case 2:
119
+ _a.trys.push([2, 4, , 6]);
120
+ return [4 /*yield*/, signer.estimateGas(tx)];
121
+ case 3:
122
+ gasEstimate = _a.sent();
123
+ gasLimit = (0, txHelpers_1.getGasBuffer)(gasEstimate);
124
+ return [3 /*break*/, 6];
125
+ case 4:
126
+ error_2 = _a.sent();
127
+ return [4 /*yield*/, errorDecoder.decode(error_2)];
128
+ case 5:
129
+ decodedError = _a.sent();
130
+ reason = customReasonMapper(decodedError);
131
+ 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_2));
132
+ throw new Error(reason);
133
+ case 6:
134
+ if (Object.values(types_1.ReyaChainId).includes(chainId)) {
135
+ maxPriorityFeePerGas = BigInt('0');
136
+ maxFeePerGas = BigInt('100000000');
137
+ return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit, maxPriorityFeePerGas: maxPriorityFeePerGas, maxFeePerGas: maxFeePerGas })];
138
+ }
139
+ return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit })];
140
+ }
141
+ });
142
+ });
143
+ }
103
144
  function executeTransaction(signer, data, value, chainId, targetContract) {
104
145
  return __awaiter(this, void 0, void 0, function () {
105
- var txData, txResponse, txReceipt, error_2;
146
+ var txData, txResponse, txReceipt, error_3;
106
147
  return __generator(this, function (_a) {
107
148
  switch (_a.label) {
108
- case 0: return [4 /*yield*/, estimateGas(signer, data, value, chainId, targetContract)];
149
+ case 0: return [4 /*yield*/, estimateGasExternalChains(signer, data, value, chainId, targetContract)];
109
150
  case 1:
110
151
  txData = _a.sent();
111
152
  _a.label = 2;
@@ -119,8 +160,8 @@ function executeTransaction(signer, data, value, chainId, targetContract) {
119
160
  txReceipt = _a.sent();
120
161
  return [2 /*return*/, txReceipt];
121
162
  case 5:
122
- error_2 = _a.sent();
123
- console.warn(error_2);
163
+ error_3 = _a.sent();
164
+ console.warn(error_3);
124
165
  throw new Error('Transaction Execution Error');
125
166
  case 6: return [2 /*return*/];
126
167
  }
@@ -1 +1 @@
1
- {"version":3,"file":"executeTransaction.js","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":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AkB;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","sourcesContent":["import { ethers, JsonRpcSigner, Signer } from 'ethers';\nimport { 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';\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 executeTransaction(\n signer: Signer | JsonRpcSigner,\n data: string,\n value: string,\n chainId: number,\n targetContract: ContractType | string,\n) {\n const txData = await estimateGas(\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"]}
1
+ {"version":3,"file":"executeTransaction.js","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,iCAAuD;AACvD,kCAAuC;AACvC,yDAA+D;AAC/D,kDAAyC;AACzC,2DAAmD;AAEnD,yCAA2C;AAE3C,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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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","sourcesContent":["import { ethers, JsonRpcSigner, Signer } from 'ethers';\nimport { 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';\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\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"]}
@@ -1,3 +1,3 @@
1
- import { MoneyInOutChainId, TokenName } from '../types';
2
- export declare const getNativeToken: (moneyInOutChainId: MoneyInOutChainId) => TokenName;
1
+ import { MoneyInOutChainId, ReyaChainId, TokenName } from '../types';
2
+ export declare const getNativeToken: (chainId: MoneyInOutChainId | ReyaChainId) => TokenName;
3
3
  //# sourceMappingURL=getNativeToken.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"getNativeToken.d.ts","sourceRoot":"/","sources":["services/getNativeToken.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAgBxD,eAAO,MAAM,cAAc,sBACN,iBAAiB,KACnC,SAEF,CAAC"}
1
+ {"version":3,"file":"getNativeToken.d.ts","sourceRoot":"/","sources":["services/getNativeToken.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,WAAW,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAkBrE,eAAO,MAAM,cAAc,YAChB,iBAAiB,GAAG,WAAW,KACvC,SAEF,CAAC"}
@@ -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;AAEvD,OAAO,EAAE,YAAY,EAAc,MAAM,qBAAqB,CAAC;AAU/D,MAAM,MAAM,WAAW,GAAG;IAC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AAoB,CAAC,EAAE,MAAM,CAAC;IAC9B,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB,CACF,CAoCA;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"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@reyaxyz/common",
3
- "version": "0.137.0",
3
+ "version": "0.139.0",
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": "0af399e0a55cc36199d8a49deadf937faa6f780e"
46
+ "gitHead": "967ce93554cd961b7c0b67175cef7e5af409e380"
47
47
  }
@@ -1,13 +1,15 @@
1
- import { MoneyInOutChainId, TokenName } from '../types';
1
+ import { MoneyInOutChainId, ReyaChainId, TokenName } from '../types';
2
2
 
3
- const nativeToken: Record<MoneyInOutChainId, TokenName> = {
3
+ const nativeToken: Record<MoneyInOutChainId | ReyaChainId, TokenName> = {
4
4
  // mainnet
5
+ [ReyaChainId.reyaNetwork]: 'ETH',
5
6
  [MoneyInOutChainId.ethereumMainnet]: 'ETH',
6
7
  [MoneyInOutChainId.opMainnet]: 'ETH',
7
8
  [MoneyInOutChainId.polygonMainnet]: 'MATIC',
8
9
  [MoneyInOutChainId.arbitrumOne]: 'ETH',
9
10
  [MoneyInOutChainId.baseMainnet]: 'ETH',
10
11
  // testnet
12
+ [ReyaChainId.reyaCronos]: 'ETH',
11
13
  [MoneyInOutChainId.polygonMumbai]: 'MATIC',
12
14
  [MoneyInOutChainId.arbitrumSepolia]: 'ETH',
13
15
  [MoneyInOutChainId.opSepolia]: 'ETH',
@@ -15,7 +17,7 @@ const nativeToken: Record<MoneyInOutChainId, TokenName> = {
15
17
  };
16
18
 
17
19
  export const getNativeToken = (
18
- moneyInOutChainId: MoneyInOutChainId,
20
+ chainId: MoneyInOutChainId | ReyaChainId,
19
21
  ): TokenName => {
20
- return nativeToken[moneyInOutChainId];
22
+ return nativeToken[chainId];
21
23
  };
@@ -4,6 +4,7 @@ import { ContractType, getAddress } from './contractAddresses';
4
4
  import { abi } from './abis/Errors.json';
5
5
  import { ErrorDecoder } from 'ethers-decode-error';
6
6
  import type { DecodedError } from 'ethers-decode-error';
7
+ import { getGasBuffer } from './txHelpers';
7
8
 
8
9
  const reyaChainIdRPCMapper: { [key: number]: string } = {
9
10
  [ReyaChainId.reyaNetwork]: 'https://rpc.reya.network',
@@ -68,6 +69,55 @@ export async function estimateGas(
68
69
  return { ...tx, gasLimit };
69
70
  }
70
71
 
72
+ async function estimateGasExternalChains(
73
+ signer: Signer | JsonRpcSigner,
74
+ data: string,
75
+ value: string,
76
+ chainId: number,
77
+ targetContract: ContractType | string,
78
+ ): Promise<
79
+ Transaction & {
80
+ gasLimit: bigint;
81
+ maxPriorityFeePerGas?: bigint;
82
+ maxFeePerGas?: bigint;
83
+ }
84
+ > {
85
+ const accountAddress = await signer.getAddress();
86
+
87
+ const contractAddress = Object.values(ContractType).includes(
88
+ targetContract as ContractType,
89
+ )
90
+ ? getAddress(chainId, targetContract as ContractType)
91
+ : targetContract;
92
+ const tx = {
93
+ from: accountAddress,
94
+ to: contractAddress,
95
+ data,
96
+ ...(value && value !== '0' ? { value: value } : {}),
97
+ };
98
+
99
+ let gasLimit: bigint = BigInt('5000000'); // hardcode to 5m gas limit if fails to get estimate
100
+
101
+ try {
102
+ const gasEstimate = await signer.estimateGas(tx);
103
+ gasLimit = getGasBuffer(gasEstimate);
104
+ } catch (error) {
105
+ const decodedError = await errorDecoder.decode(error);
106
+ const reason = customReasonMapper(decodedError);
107
+ console.error(
108
+ `Error simulating external transaction account address ${accountAddress} reason ${decodedError?.name} ${decodedError} ${error}`,
109
+ );
110
+ throw new Error(reason);
111
+ }
112
+
113
+ if (Object.values(ReyaChainId).includes(chainId as ReyaChainId)) {
114
+ const maxPriorityFeePerGas: bigint = BigInt('0');
115
+ const maxFeePerGas: bigint = BigInt('100000000');
116
+ return { ...tx, gasLimit, maxPriorityFeePerGas, maxFeePerGas };
117
+ }
118
+
119
+ return { ...tx, gasLimit };
120
+ }
71
121
  export async function executeTransaction(
72
122
  signer: Signer | JsonRpcSigner,
73
123
  data: string,
@@ -75,7 +125,7 @@ export async function executeTransaction(
75
125
  chainId: number,
76
126
  targetContract: ContractType | string,
77
127
  ) {
78
- const txData = await estimateGas(
128
+ const txData = await estimateGasExternalChains(
79
129
  signer,
80
130
  data,
81
131
  value,