@reyaxyz/common 0.344.0 → 0.344.1-perpob.110
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/commands/exposure/command.js +8 -22
- package/dist/commands/exposure/command.js.map +1 -1
- package/dist/incentives-api-types.js.map +1 -1
- package/dist/rest/helpers/constants.js +12 -0
- package/dist/rest/helpers/constants.js.map +1 -1
- package/dist/services/getNativeToken.js +1 -0
- package/dist/services/getNativeToken.js.map +1 -1
- package/dist/services/getSocketAddresses.js +1 -0
- package/dist/services/getSocketAddresses.js.map +1 -1
- package/dist/trading-api-types.js.map +1 -1
- package/dist/transactions/abis/Errors.json +1283 -34
- package/dist/transactions/abis/OrdersGateway.json +729 -123
- package/dist/transactions/abis/PassivePerpProxy.json +542 -0
- package/dist/transactions/buildMulticallTx.js.map +1 -1
- package/dist/transactions/consts.js +1 -2
- package/dist/transactions/consts.js.map +1 -1
- package/dist/transactions/contractAddresses.js +46 -5
- package/dist/transactions/contractAddresses.js.map +1 -1
- package/dist/transactions/executeTransaction.js +8 -123
- package/dist/transactions/executeTransaction.js.map +1 -1
- package/dist/transactions/sign.js +285 -79
- package/dist/transactions/sign.js.map +1 -1
- package/dist/types/commands/exposure/command.d.ts +8 -7
- package/dist/types/commands/exposure/command.d.ts.map +1 -1
- package/dist/types/incentives-api-types.d.ts +40 -17
- package/dist/types/incentives-api-types.d.ts.map +1 -1
- package/dist/types/rest/helpers/constants.d.ts +1 -1
- package/dist/types/rest/helpers/constants.d.ts.map +1 -1
- package/dist/types/services/getNativeToken.d.ts.map +1 -1
- package/dist/types/services/getSocketAddresses.d.ts.map +1 -1
- package/dist/types/trading-api-types.d.ts +144 -94
- package/dist/types/trading-api-types.d.ts.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/consts.d.ts +0 -1
- package/dist/types/transactions/consts.d.ts.map +1 -1
- package/dist/types/transactions/contractAddresses.d.ts +1 -1
- package/dist/types/transactions/contractAddresses.d.ts.map +1 -1
- package/dist/types/transactions/executeTransaction.d.ts +2 -15
- package/dist/types/transactions/executeTransaction.d.ts.map +1 -1
- package/dist/types/transactions/sign.d.ts +117 -6
- package/dist/types/transactions/sign.d.ts.map +1 -1
- package/dist/types/types.d.ts +52 -64
- package/dist/types/types.d.ts.map +1 -1
- package/dist/types/utils/calculate.d.ts +14 -2
- package/dist/types/utils/calculate.d.ts.map +1 -1
- package/dist/types/utils/compute-trade-pnl.d.ts +39 -0
- package/dist/types/utils/compute-trade-pnl.d.ts.map +1 -0
- package/dist/types/utils/consts.d.ts +2 -2
- package/dist/types/utils/consts.d.ts.map +1 -1
- package/dist/types/utils/error-sanitizer.d.ts +15 -0
- package/dist/types/utils/error-sanitizer.d.ts.map +1 -0
- package/dist/types/utils/index.d.ts +3 -0
- package/dist/types/utils/index.d.ts.map +1 -1
- package/dist/types/utils/layerzero.d.ts +22 -0
- package/dist/types/utils/layerzero.d.ts.map +1 -0
- package/dist/types/utils/network.d.ts.map +1 -1
- package/dist/types/utils/throttled-error.d.ts +42 -0
- package/dist/types/utils/throttled-error.d.ts.map +1 -0
- package/dist/types/utils/token/batch-token-getters.d.ts +0 -5
- package/dist/types/utils/token/batch-token-getters.d.ts.map +1 -1
- package/dist/types/utils/token/normalize.d.ts +2 -1
- package/dist/types/utils/token/normalize.d.ts.map +1 -1
- package/dist/types/utils/token/token-getters.d.ts +0 -2
- package/dist/types/utils/token/token-getters.d.ts.map +1 -1
- package/dist/types/utils/token/token-info-getters.d.ts.map +1 -1
- package/dist/types/utils/token/token-info.d.ts.map +1 -1
- package/dist/types.js +41 -11
- package/dist/types.js.map +1 -1
- package/dist/utils/calculate.js +137 -16
- package/dist/utils/calculate.js.map +1 -1
- package/dist/utils/compute-trade-pnl.js +60 -0
- package/dist/utils/compute-trade-pnl.js.map +1 -0
- package/dist/utils/consts.js +8 -13
- package/dist/utils/consts.js.map +1 -1
- package/dist/utils/error-sanitizer.js +135 -0
- package/dist/utils/error-sanitizer.js.map +1 -0
- package/dist/utils/index.js +3 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/layerzero.js +31 -0
- package/dist/utils/layerzero.js.map +1 -0
- package/dist/utils/network.js +4 -0
- package/dist/utils/network.js.map +1 -1
- package/dist/utils/throttled-error.js +60 -0
- package/dist/utils/throttled-error.js.map +1 -0
- package/dist/utils/token/batch-token-getters.js +13 -64
- package/dist/utils/token/batch-token-getters.js.map +1 -1
- package/dist/utils/token/normalize.js +2 -2
- package/dist/utils/token/normalize.js.map +1 -1
- package/dist/utils/token/token-getters.js +1 -42
- package/dist/utils/token/token-getters.js.map +1 -1
- package/dist/utils/token/token-info-getters.js +45 -4
- package/dist/utils/token/token-info-getters.js.map +1 -1
- package/dist/utils/token/token-info.js +6 -0
- package/dist/utils/token/token-info.js.map +1 -1
- package/package.json +3 -3
- package/src/commands/exposure/command.ts +8 -38
- package/src/incentives-api-types.ts +66 -33
- package/src/rest/helpers/constants.ts +22 -0
- package/src/services/getNativeToken.ts +1 -0
- package/src/services/getSocketAddresses.ts +1 -0
- package/src/trading-api-types.ts +175 -127
- package/src/transactions/abis/Errors.json +5133 -3884
- package/src/transactions/abis/OrdersGateway.json +730 -124
- package/src/transactions/abis/PassivePerpProxy.json +542 -0
- package/src/transactions/buildMulticallTx.ts +4 -0
- package/src/transactions/consts.ts +0 -1
- package/src/transactions/contractAddresses.ts +43 -6
- package/src/transactions/executeTransaction.ts +2 -130
- package/src/transactions/sign.ts +347 -93
- package/src/types.ts +90 -74
- package/src/utils/calculate.ts +181 -21
- package/src/utils/compute-trade-pnl.ts +110 -0
- package/src/utils/consts.ts +10 -22
- package/src/utils/error-sanitizer.ts +137 -0
- package/src/utils/index.ts +3 -0
- package/src/utils/layerzero.ts +66 -0
- package/src/utils/network.ts +4 -0
- package/src/utils/throttled-error.ts +85 -0
- package/src/utils/token/batch-token-getters.ts +0 -28
- package/src/utils/token/normalize.ts +6 -1
- package/src/utils/token/token-getters.ts +0 -29
- package/src/utils/token/token-info-getters.ts +54 -4
- package/src/utils/token/token-info.ts +35 -0
- package/dist/types/utils/layerzero/index.d.ts +0 -3
- package/dist/types/utils/layerzero/index.d.ts.map +0 -1
- package/dist/types/utils/layerzero/oft-config.d.ts +0 -4
- package/dist/types/utils/layerzero/oft-config.d.ts.map +0 -1
- package/dist/types/utils/layerzero/types.d.ts +0 -23
- package/dist/types/utils/layerzero/types.d.ts.map +0 -1
- package/dist/utils/layerzero/index.js +0 -19
- package/dist/utils/layerzero/index.js.map +0 -1
- package/dist/utils/layerzero/oft-config.js +0 -29
- package/dist/utils/layerzero/oft-config.js.map +0 -1
- package/dist/utils/layerzero/types.js +0 -3
- package/dist/utils/layerzero/types.js.map +0 -1
- package/src/utils/layerzero/index.ts +0 -2
- package/src/utils/layerzero/oft-config.ts +0 -32
- package/src/utils/layerzero/types.ts +0 -25
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"contractAddresses.js","sourceRoot":"/","sources":["transactions/contractAddresses.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"contractAddresses.js","sourceRoot":"/","sources":["transactions/contractAddresses.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,kCAAgD;AAEhD,IAAY,YAWX;AAXD,WAAY,YAAY;IACtB,yCAAyB,CAAA;IACzB,yDAAyC,CAAA;IACzC,mDAAmC,CAAA;IACnC,qEAAqD,CAAA;IACrD,6DAA6C,CAAA;IAC7C,yDAAyC,CAAA;IACzC,+DAA+C,CAAA;IAC/C,uCAAuB,CAAA;IACvB,uCAAuB,CAAA;IACvB,+DAA+C,CAAA;AACjD,CAAC,EAXW,YAAY,4BAAZ,YAAY,QAWvB;AAID,IAAM,eAAe,GAAG,CAAA;IACtB,GAAC,mBAAW,CAAC,UAAU;QACrB,GAAC,YAAY,CAAC,UAAU,IAAG,4CAA4C;QACvE,GAAC,YAAY,C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XY = 'periphery_proxy',\n CONDITIONAL_ORDERS_PROXY = 'conditional_orders_proxy',\n ORDERS_GATEWAY_PROXY = 'orders_gateway_proxy',\n PASSIVE_PERP_PROXY = 'passive_perp_proxy',\n ORACLE_ADAPTERS_PROXY = 'oracle_adapters_proxy',\n MULTICALL = 'multicall',\n REYA_SBTS = 'reya_sbts',\n REYA_TGE_DISTRIBUTION = 'reya_tge_distribution',\n}\n\ntype RemoteReyaChainId = Exclude<ReyaChainId, ReyaChainId.reyaLocal>;\n\nconst remoteAddresses = {\n [ReyaChainId.reyaCronos]: {\n [ContractType.CORE_PROXY]: '0xc6fb022962e1426f4e0ec9d2f8861c57926e9f72',\n [ContractType.PASSIVE_POOL_PROXY]:\n '0x9a3a664987b88790a6fdc1632e3b607813fd94ff',\n [ContractType.PERIPHERY_PROXY]:\n '0x94ccae812f1647696754412082dd6684c2366a7f',\n [ContractType.CONDITIONAL_ORDERS_PROXY]:\n '0x5a0ac2f89e0bdeafc5c549e354842210a3e87ca5',\n [ContractType.ORDERS_GATEWAY_PROXY]:\n '0x5a0ac2f89e0bdeafc5c549e354842210a3e87ca5',\n [ContractType.PASSIVE_PERP_PROXY]:\n '0x9ec177fed042ef2307928be2f5cdbf663b20244b',\n [ContractType.ORACLE_ADAPTERS_PROXY]:\n '0xc501a2356703cd351703d68963c6f4136120f7cf',\n [ContractType.MULTICALL]: '0x5abde4f0af8eaf3c9967f7fa126e59a103357b5c',\n [ContractType.REYA_SBTS]: '0xc0092ea9179ace93b0cd8910958e335575562722',\n [ContractType.REYA_TGE_DISTRIBUTION]:\n '0x16917248669fa64340e6517538e419588e9684c3',\n },\n [ReyaChainId.reyaNetwork]: {\n [ContractType.CORE_PROXY]: '0xa763b6a5e09378434406c003dae6487fbbdc1a80',\n [ContractType.PASSIVE_POOL_PROXY]:\n '0xb4b77d6180cc14472a9a7bdff01cc2459368d413',\n [ContractType.PERIPHERY_PROXY]:\n '0xcd2869d1eb1bc8991bc55de9e9b779e912faf736',\n [ContractType.CONDITIONAL_ORDERS_PROXY]:\n '0xfc8c96be87da63cecddbf54abfa7b13ee8044739',\n [ContractType.ORDERS_GATEWAY_PROXY]:\n '0xfc8c96be87da63cecddbf54abfa7b13ee8044739',\n [ContractType.PASSIVE_PERP_PROXY]:\n '0x27e5cb712334e101b3c232eb0be198baaa595f5f',\n [ContractType.ORACLE_ADAPTERS_PROXY]:\n '0x32edabc058c1207fe0ec5f8557643c28e4ff379e',\n [ContractType.MULTICALL]: '0xed28d27dfca47ad2513c9f2e2d3c098c2ea5a47f',\n [ContractType.REYA_SBTS]: '0x14d7c1efc024e118df70b241afbd2447d37f1ed6',\n [ContractType.REYA_TGE_DISTRIBUTION]:\n '0x0000000000000000000000000000000000000000',\n },\n} satisfies Record<RemoteReyaChainId, Record<ContractType, Address>>;\n\nconst addresses: Record<ReyaChainId, Partial<Record<ContractType, Address>>> = {\n ...remoteAddresses,\n [ReyaChainId.reyaLocal]: {},\n};\n// Map stores the resolved env-var VALUES (not the var names) so that\n// webpack / Next.js `DefinePlugin` can statically inline them into\n// browser bundles.\n//\n// DefinePlugin only inlines literal `process.env.NAME` access — a\n// dynamic `process.env[varName]` lookup is opaque to its source-text\n// pass and stays `undefined` in the browser. Keeping the four\n// `process.env.*` accesses as static literals in this object literal\n// is what makes the override work in both Node (real `process.env` at\n// runtime) and browser (DefinePlugin-substituted constants at build\n// time). See PR #2660 / Daniel's comment for the discovery context.\n//\n// New entries must use the same static-literal pattern; do NOT\n// introduce a dynamic `process.env[someVar]` lookup downstream.\nconst CONTRACT_ADDRESS_ENV_OVERRIDES: Partial<Record<ContractType, string>> = {\n [ContractType.CORE_PROXY]: process.env.CORE_PROXY_ADDRESS,\n [ContractType.PASSIVE_PERP_PROXY]: process.env.PASSIVE_PERP_PROXY_ADDRESS,\n [ContractType.ORDERS_GATEWAY_PROXY]: process.env.ORDERS_GATEWAY_PROXY_ADDRESS,\n [ContractType.PERIPHERY_PROXY]: process.env.PERIPHERY_PROXY_ADDRESS,\n};\n\nexport const getAddress = (\n chainId: number,\n contractName: ContractType,\n): Address => {\n // Normalize env-overridden addresses to lowercase. The hardcoded fallback\n // map below stores lowercased values, and downstream comparisons (e.g.\n // transaction-whitelist.ts uses `convertToAddress(target) === OG_ADDRESS`,\n // which lowercases the runtime target) assume that invariant. Without\n // this, a checksummed env value silently fails every comparison and the\n // OG-aware settlement path gets bypassed.\n const override = CONTRACT_ADDRESS_ENV_OVERRIDES[contractName];\n if (override) return override.toLowerCase() as Address;\n\n const keyChainId = chainId.toString();\n\n if (!Object.keys(addresses).includes(keyChainId)) {\n throw new Error(`Unspecified addresses for chain id ${keyChainId}`);\n }\n\n const networkAddresses = addresses[chainId as ReyaChainId];\n if (!Object.keys(networkAddresses).includes(contractName)) {\n throw new Error(`Unspecified address for ${contractName} contract`);\n }\n\n return networkAddresses[contractName]!;\n};\n"]}
|
|
@@ -46,117 +46,17 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
46
46
|
if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
|
|
47
47
|
}
|
|
48
48
|
};
|
|
49
|
-
var _a;
|
|
50
49
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
51
|
-
exports.
|
|
52
|
-
var ethers_1 = require("ethers");
|
|
50
|
+
exports.executeTransaction = void 0;
|
|
53
51
|
var types_1 = require("../types");
|
|
54
52
|
var contractAddresses_1 = require("./contractAddresses");
|
|
55
53
|
var Errors_json_1 = require("./abis/Errors.json");
|
|
56
54
|
var ethers_decode_error_1 = require("ethers-decode-error");
|
|
57
55
|
var txHelpers_1 = require("./txHelpers");
|
|
58
|
-
var utils_1 = require("../utils");
|
|
59
|
-
var ethers_2 = require("ethers");
|
|
60
|
-
var reyaChainIdRPCMapper = (_a = {},
|
|
61
|
-
_a[types_1.ReyaChainId.reyaNetwork] = 'https://rpc.reya.network/263e76b89da84d478fd2b6e8783a1cfe',
|
|
62
|
-
_a[types_1.ReyaChainId.reyaCronos] = 'https://rpc.reya-cronos.gelato.digital/263e76b89da84d478fd2b6e8783a1cfe',
|
|
63
|
-
_a);
|
|
64
56
|
var errorDecoder = ethers_decode_error_1.ErrorDecoder.create([Errors_json_1.abi]);
|
|
65
|
-
function estimateGas(signer, data, value, chainId, targetContract) {
|
|
66
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
67
|
-
var accountAddress;
|
|
68
|
-
return __generator(this, function (_a) {
|
|
69
|
-
switch (_a.label) {
|
|
70
|
-
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
71
|
-
case 1:
|
|
72
|
-
accountAddress = _a.sent();
|
|
73
|
-
return [2 /*return*/, estimateGasGivenAddress(accountAddress, data, value, chainId, targetContract)];
|
|
74
|
-
}
|
|
75
|
-
});
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
exports.estimateGas = estimateGas;
|
|
79
|
-
// TODO: deprecate this when /executeGelato is gone
|
|
80
|
-
function estimateGasGivenAddress(accountAddress, data, value, chainId, targetContract) {
|
|
81
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
82
|
-
var contractAddress, tx, gasLimit, network, provider, error_1, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
83
|
-
return __generator(this, function (_a) {
|
|
84
|
-
switch (_a.label) {
|
|
85
|
-
case 0:
|
|
86
|
-
contractAddress = Object.values(contractAddresses_1.ContractType).includes(targetContract)
|
|
87
|
-
? (0, contractAddresses_1.getAddress)(chainId, targetContract)
|
|
88
|
-
: targetContract;
|
|
89
|
-
tx = __assign({ from: accountAddress, to: contractAddress, data: data }, (value && value !== '0' ? { value: value } : {}));
|
|
90
|
-
gasLimit = BigInt('5000000');
|
|
91
|
-
_a.label = 1;
|
|
92
|
-
case 1:
|
|
93
|
-
_a.trys.push([1, 3, , 5]);
|
|
94
|
-
network = new ethers_1.Network('Reya', chainId);
|
|
95
|
-
provider = new ethers_1.JsonRpcProvider(reyaChainIdRPCMapper[chainId], network, {
|
|
96
|
-
staticNetwork: network,
|
|
97
|
-
pollingInterval: 50,
|
|
98
|
-
});
|
|
99
|
-
return [4 /*yield*/, provider.call(tx)];
|
|
100
|
-
case 2:
|
|
101
|
-
_a.sent();
|
|
102
|
-
return [3 /*break*/, 5];
|
|
103
|
-
case 3:
|
|
104
|
-
error_1 = _a.sent();
|
|
105
|
-
return [4 /*yield*/, errorDecoder.decode(error_1)];
|
|
106
|
-
case 4:
|
|
107
|
-
decodedError = _a.sent();
|
|
108
|
-
reason = customReasonMapper(decodedError);
|
|
109
|
-
console.error("Error simulating transaction account address ".concat(accountAddress, " reason ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, " ").concat(decodedError, " ").concat(error_1));
|
|
110
|
-
throw new Error(reason);
|
|
111
|
-
case 5:
|
|
112
|
-
if (Object.values(types_1.ReyaChainId).includes(chainId)) {
|
|
113
|
-
maxPriorityFeePerGas = BigInt('0');
|
|
114
|
-
maxFeePerGas = BigInt('100000000');
|
|
115
|
-
return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit, maxPriorityFeePerGas: maxPriorityFeePerGas, maxFeePerGas: maxFeePerGas })];
|
|
116
|
-
}
|
|
117
|
-
return [2 /*return*/, __assign(__assign({}, tx), { gasLimit: gasLimit })];
|
|
118
|
-
}
|
|
119
|
-
});
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
exports.estimateGasGivenAddress = estimateGasGivenAddress;
|
|
123
|
-
function estimateGasFromTxAndChainId(chainId, tx) {
|
|
124
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
125
|
-
var rpcUrls, lastError, _i, rpcUrls_1, url, provider, estimation, error_2;
|
|
126
|
-
return __generator(this, function (_a) {
|
|
127
|
-
switch (_a.label) {
|
|
128
|
-
case 0:
|
|
129
|
-
rpcUrls = (0, utils_1.getPublicRpcUrlsOrThrow)(chainId);
|
|
130
|
-
lastError = null;
|
|
131
|
-
_i = 0, rpcUrls_1 = rpcUrls;
|
|
132
|
-
_a.label = 1;
|
|
133
|
-
case 1:
|
|
134
|
-
if (!(_i < rpcUrls_1.length)) return [3 /*break*/, 6];
|
|
135
|
-
url = rpcUrls_1[_i];
|
|
136
|
-
_a.label = 2;
|
|
137
|
-
case 2:
|
|
138
|
-
_a.trys.push([2, 4, , 5]);
|
|
139
|
-
provider = new ethers_1.ethers.JsonRpcProvider(url);
|
|
140
|
-
return [4 /*yield*/, provider.estimateGas(tx)];
|
|
141
|
-
case 3:
|
|
142
|
-
estimation = _a.sent();
|
|
143
|
-
return [2 /*return*/, addGasBuffer(estimation)];
|
|
144
|
-
case 4:
|
|
145
|
-
error_2 = _a.sent();
|
|
146
|
-
lastError = error_2;
|
|
147
|
-
return [3 /*break*/, 5];
|
|
148
|
-
case 5:
|
|
149
|
-
_i++;
|
|
150
|
-
return [3 /*break*/, 1];
|
|
151
|
-
case 6: throw new Error('All RPC URLs failed; last error: ' + lastError);
|
|
152
|
-
}
|
|
153
|
-
});
|
|
154
|
-
});
|
|
155
|
-
}
|
|
156
|
-
exports.estimateGasFromTxAndChainId = estimateGasFromTxAndChainId;
|
|
157
57
|
function estimateGasExternalChains(signer, data, value, chainId, targetContract) {
|
|
158
58
|
return __awaiter(this, void 0, void 0, function () {
|
|
159
|
-
var accountAddress, contractAddress, tx, gasLimit, gasEstimate,
|
|
59
|
+
var accountAddress, contractAddress, tx, gasLimit, gasEstimate, error_1, decodedError, reason, maxPriorityFeePerGas, maxFeePerGas;
|
|
160
60
|
return __generator(this, function (_a) {
|
|
161
61
|
switch (_a.label) {
|
|
162
62
|
case 0: return [4 /*yield*/, signer.getAddress()];
|
|
@@ -176,12 +76,12 @@ function estimateGasExternalChains(signer, data, value, chainId, targetContract)
|
|
|
176
76
|
gasLimit = (0, txHelpers_1.getGasBuffer)(gasEstimate);
|
|
177
77
|
return [3 /*break*/, 6];
|
|
178
78
|
case 4:
|
|
179
|
-
|
|
180
|
-
return [4 /*yield*/, errorDecoder.decode(
|
|
79
|
+
error_1 = _a.sent();
|
|
80
|
+
return [4 /*yield*/, errorDecoder.decode(error_1)];
|
|
181
81
|
case 5:
|
|
182
82
|
decodedError = _a.sent();
|
|
183
83
|
reason = customReasonMapper(decodedError);
|
|
184
|
-
console.error("Error simulating external transaction with value ".concat(value, ", account address ").concat(accountAddress, ", reason ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, " ").concat(JSON.stringify(decodedError), " ").concat(
|
|
84
|
+
console.error("Error simulating external transaction with value ".concat(value, ", account address ").concat(accountAddress, ", reason ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, " ").concat(JSON.stringify(decodedError), " ").concat(error_1));
|
|
185
85
|
throw new Error(reason);
|
|
186
86
|
case 6:
|
|
187
87
|
if (Object.values(types_1.ReyaChainId).includes(chainId)) {
|
|
@@ -196,7 +96,7 @@ function estimateGasExternalChains(signer, data, value, chainId, targetContract)
|
|
|
196
96
|
}
|
|
197
97
|
function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
198
98
|
return __awaiter(this, void 0, void 0, function () {
|
|
199
|
-
var txData, txResponse, txReceipt,
|
|
99
|
+
var txData, txResponse, txReceipt, error_2;
|
|
200
100
|
return __generator(this, function (_a) {
|
|
201
101
|
switch (_a.label) {
|
|
202
102
|
case 0: return [4 /*yield*/, estimateGasExternalChains(signer, data, value, chainId, targetContract)];
|
|
@@ -213,8 +113,8 @@ function executeTransaction(signer, data, value, chainId, targetContract) {
|
|
|
213
113
|
txReceipt = _a.sent();
|
|
214
114
|
return [2 /*return*/, txReceipt];
|
|
215
115
|
case 5:
|
|
216
|
-
|
|
217
|
-
console.warn(
|
|
116
|
+
error_2 = _a.sent();
|
|
117
|
+
console.warn(error_2);
|
|
218
118
|
throw new Error('Transaction Execution Error');
|
|
219
119
|
case 6: return [2 /*return*/];
|
|
220
120
|
}
|
|
@@ -240,19 +140,4 @@ var customReasonMapper = function (_a) {
|
|
|
240
140
|
return reason !== null && reason !== void 0 ? reason : 'Error executing transaction';
|
|
241
141
|
}
|
|
242
142
|
};
|
|
243
|
-
function addGasBuffer(estimation) {
|
|
244
|
-
return (estimation * BigInt(120)) / BigInt(100);
|
|
245
|
-
}
|
|
246
|
-
function decodeErrorBytes(returnData) {
|
|
247
|
-
return __awaiter(this, void 0, void 0, function () {
|
|
248
|
-
var INTERFACE, decodedError;
|
|
249
|
-
return __generator(this, function (_a) {
|
|
250
|
-
INTERFACE = new ethers_2.Interface(Errors_json_1.abi);
|
|
251
|
-
decodedError = INTERFACE.parseError(returnData);
|
|
252
|
-
console.log(decodedError);
|
|
253
|
-
return [2 /*return*/, "".concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.name, ": ").concat(decodedError === null || decodedError === void 0 ? void 0 : decodedError.args) || 'Error not found'];
|
|
254
|
-
});
|
|
255
|
-
});
|
|
256
|
-
}
|
|
257
|
-
exports.decodeErrorBytes = decodeErrorBytes;
|
|
258
143
|
//# sourceMappingURL=executeTransaction.js.map
|
|
@@ -1 +1 @@
|
|
|
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;AAEnC,IAAM,oBAAoB;IACxB,GAAC,mBAAW,CAAC,WAAW,IACtB,2DAA2D;IAC7D,GAAC,mBAAW,CAAC,UAAU,IACrB,yEAAyE;OAC5E,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;oBAEhD,sBAAO,uBAA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AS,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 {\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\nconst 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\n// TODO: deprecate this when /executeGelato is gone\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 with value ${value}, account address ${accountAddress}, reason ${decodedError?.name} ${JSON.stringify(\n decodedError,\n )} ${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"]}
|
|
1
|
+
{"version":3,"file":"executeTransaction.js","sourceRoot":"/","sources":["transactions/executeTransaction.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AACA,kCAAuC;AACvC,yDAA+D;AAC/D,kDAAyC;AACzC,2DAAmD;AAEnD,yCAA2C;AAQ3C,IAAM,YAAY,GAAG,kCAAY,CAAC,MAAM,CAAC,CAAC,iBAAG,CAAC,CAAC,CAAC;AAEhD,SAAe,yBAAyB,CACtC,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,gCAAY,CAAC,CAAC,QAAQ,CAC1D,cAA8B,CAC/B;wBACC,CAAC,CAAC,IAAA,8BAAU,EAAC,OAAO,EAAE,cAA8B,CAAC;wBACrD,CAAC,CAAC,cAAc,CAAC;oBACb,EAAE,cACN,IAAI,EAAE,cAAc,EACpB,EAAE,EAAE,eAAe,EACnB,IAAI,MAAA,IACD,CAAC,KAAK,IAAI,KAAK,KAAK,GAAG,CAAC,CAAC,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC,CACpD,CAAC;oBAEE,QAAQ,GAAW,MAAM,CAAC,SAAS,CAAC,CAAC;;;;oBAGnB,qBAAM,MAAM,CAAC,WAAW,CAAC,EAAE,CAAC,EAAA;;oBAA1C,WAAW,GAAG,SAA4B;oBAChD,QAAQ,GAAG,IAAA,wBAAY,EAAC,WAAW,CAAC,CAAC;;;;oBAEhB,qBAAM,YAAY,CAAC,MAAM,CAAC,OAAK,CAAC,EAAA;;oBAA/C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AAI,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","sourcesContent":["import { 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\nexport type Transaction = {\n from: string;\n to: string;\n data: string;\n value?: string;\n};\nconst errorDecoder = ErrorDecoder.create([abi]);\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 with value ${value}, account address ${accountAddress}, reason ${decodedError?.name} ${JSON.stringify(\n decodedError,\n )} ${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"]}
|