@hyperlane-xyz/sdk 0.5.0-beta0
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 +5 -0
- package/dist/HyperlaneApp.d.ts +13 -0
- package/dist/HyperlaneApp.d.ts.map +1 -0
- package/dist/HyperlaneApp.js +25 -0
- package/dist/HyperlaneApp.js.map +1 -0
- package/dist/consts/chainConnectionConfigs.d.ts +28 -0
- package/dist/consts/chainConnectionConfigs.d.ts.map +1 -0
- package/dist/consts/chainConnectionConfigs.js +130 -0
- package/dist/consts/chainConnectionConfigs.js.map +1 -0
- package/dist/consts/chainMetadata.d.ts +43 -0
- package/dist/consts/chainMetadata.d.ts.map +1 -0
- package/dist/consts/chainMetadata.js +139 -0
- package/dist/consts/chainMetadata.js.map +1 -0
- package/dist/consts/chains.d.ts +28 -0
- package/dist/consts/chains.d.ts.map +1 -0
- package/dist/consts/chains.js +40 -0
- package/dist/consts/chains.js.map +1 -0
- package/dist/consts/environments/index.d.ts +1450 -0
- package/dist/consts/environments/index.d.ts.map +1 -0
- package/dist/consts/environments/index.js +15 -0
- package/dist/consts/environments/index.js.map +1 -0
- package/dist/consts/environments/mainnet.json +513 -0
- package/dist/consts/environments/test.json +113 -0
- package/dist/consts/environments/testnet2.json +821 -0
- package/dist/consts/metamask.d.ts +17 -0
- package/dist/consts/metamask.d.ts.map +1 -0
- package/dist/consts/metamask.js +50 -0
- package/dist/consts/metamask.js.map +1 -0
- package/dist/contracts.d.ts +17 -0
- package/dist/contracts.d.ts.map +1 -0
- package/dist/contracts.js +61 -0
- package/dist/contracts.js.map +1 -0
- package/dist/core/HyperlaneCore.d.ts +1491 -0
- package/dist/core/HyperlaneCore.d.ts.map +1 -0
- package/dist/core/HyperlaneCore.js +103 -0
- package/dist/core/HyperlaneCore.js.map +1 -0
- package/dist/core/TestCoreApp.d.ts +27 -0
- package/dist/core/TestCoreApp.d.ts.map +1 -0
- package/dist/core/TestCoreApp.js +63 -0
- package/dist/core/TestCoreApp.js.map +1 -0
- package/dist/core/TestCoreDeployer.d.ts +13 -0
- package/dist/core/TestCoreDeployer.d.ts.map +1 -0
- package/dist/core/TestCoreDeployer.js +85 -0
- package/dist/core/TestCoreDeployer.js.map +1 -0
- package/dist/core/contracts.d.ts +30 -0
- package/dist/core/contracts.d.ts.map +1 -0
- package/dist/core/contracts.js +14 -0
- package/dist/core/contracts.js.map +1 -0
- package/dist/core/events.d.ts +9 -0
- package/dist/core/events.d.ts.map +1 -0
- package/dist/core/events.js +3 -0
- package/dist/core/events.js.map +1 -0
- package/dist/core/message.d.ts +162 -0
- package/dist/core/message.d.ts.map +1 -0
- package/dist/core/message.js +311 -0
- package/dist/core/message.js.map +1 -0
- package/dist/core/testHyperlaneDeploy.hardhat-test.d.ts +3 -0
- package/dist/core/testHyperlaneDeploy.hardhat-test.d.ts.map +1 -0
- package/dist/core/testHyperlaneDeploy.hardhat-test.js +66 -0
- package/dist/core/testHyperlaneDeploy.hardhat-test.js.map +1 -0
- package/dist/deploy/HyperlaneAppChecker.d.ts +22 -0
- package/dist/deploy/HyperlaneAppChecker.d.ts.map +1 -0
- package/dist/deploy/HyperlaneAppChecker.js +76 -0
- package/dist/deploy/HyperlaneAppChecker.js.map +1 -0
- package/dist/deploy/HyperlaneDeployer.d.ts +40 -0
- package/dist/deploy/HyperlaneDeployer.d.ts.map +1 -0
- package/dist/deploy/HyperlaneDeployer.js +153 -0
- package/dist/deploy/HyperlaneDeployer.js.map +1 -0
- package/dist/deploy/core/HyperlaneCoreChecker.d.ts +17 -0
- package/dist/deploy/core/HyperlaneCoreChecker.d.ts.map +1 -0
- package/dist/deploy/core/HyperlaneCoreChecker.js +210 -0
- package/dist/deploy/core/HyperlaneCoreChecker.js.map +1 -0
- package/dist/deploy/core/HyperlaneCoreDeployer.d.ts +30 -0
- package/dist/deploy/core/HyperlaneCoreDeployer.d.ts.map +1 -0
- package/dist/deploy/core/HyperlaneCoreDeployer.js +134 -0
- package/dist/deploy/core/HyperlaneCoreDeployer.js.map +1 -0
- package/dist/deploy/core/types.d.ts +64 -0
- package/dist/deploy/core/types.d.ts.map +1 -0
- package/dist/deploy/core/types.js +23 -0
- package/dist/deploy/core/types.js.map +1 -0
- package/dist/deploy/proxy.d.ts +17 -0
- package/dist/deploy/proxy.d.ts.map +1 -0
- package/dist/deploy/proxy.js +35 -0
- package/dist/deploy/proxy.js.map +1 -0
- package/dist/deploy/router/HyperlaneRouterChecker.d.ts +13 -0
- package/dist/deploy/router/HyperlaneRouterChecker.d.ts.map +1 -0
- package/dist/deploy/router/HyperlaneRouterChecker.js +44 -0
- package/dist/deploy/router/HyperlaneRouterChecker.js.map +1 -0
- package/dist/deploy/router/HyperlaneRouterDeployer.d.ts +13 -0
- package/dist/deploy/router/HyperlaneRouterDeployer.d.ts.map +1 -0
- package/dist/deploy/router/HyperlaneRouterDeployer.js +103 -0
- package/dist/deploy/router/HyperlaneRouterDeployer.js.map +1 -0
- package/dist/deploy/router/types.d.ts +7 -0
- package/dist/deploy/router/types.d.ts.map +1 -0
- package/dist/deploy/router/types.js +3 -0
- package/dist/deploy/router/types.js.map +1 -0
- package/dist/deploy/types.d.ts +19 -0
- package/dist/deploy/types.d.ts.map +1 -0
- package/dist/deploy/types.js +8 -0
- package/dist/deploy/types.js.map +1 -0
- package/dist/deploy/utils.d.ts +10 -0
- package/dist/deploy/utils.d.ts.map +1 -0
- package/dist/deploy/utils.js +38 -0
- package/dist/deploy/utils.js.map +1 -0
- package/dist/deploy/verify/ContractVerifier.d.ts +19 -0
- package/dist/deploy/verify/ContractVerifier.d.ts.map +1 -0
- package/dist/deploy/verify/ContractVerifier.js +129 -0
- package/dist/deploy/verify/ContractVerifier.js.map +1 -0
- package/dist/deploy/verify/types.d.ts +14 -0
- package/dist/deploy/verify/types.d.ts.map +1 -0
- package/dist/deploy/verify/types.js +3 -0
- package/dist/deploy/verify/types.js.map +1 -0
- package/dist/deploy/verify/utils.d.ts +6 -0
- package/dist/deploy/verify/utils.d.ts.map +1 -0
- package/dist/deploy/verify/utils.js +25 -0
- package/dist/deploy/verify/utils.js.map +1 -0
- package/dist/domains.d.ts +4 -0
- package/dist/domains.d.ts.map +1 -0
- package/dist/domains.js +8 -0
- package/dist/domains.js.map +1 -0
- package/dist/events.d.ts +25 -0
- package/dist/events.d.ts.map +1 -0
- package/dist/events.js +161 -0
- package/dist/events.js.map +1 -0
- package/dist/gas/calculator.d.ts +134 -0
- package/dist/gas/calculator.d.ts.map +1 -0
- package/dist/gas/calculator.js +235 -0
- package/dist/gas/calculator.js.map +1 -0
- package/dist/gas/calculator.test.d.ts +2 -0
- package/dist/gas/calculator.test.d.ts.map +1 -0
- package/dist/gas/calculator.test.js +211 -0
- package/dist/gas/calculator.test.js.map +1 -0
- package/dist/gas/token-prices.d.ts +18 -0
- package/dist/gas/token-prices.d.ts.map +1 -0
- package/dist/gas/token-prices.js +63 -0
- package/dist/gas/token-prices.js.map +1 -0
- package/dist/gas/token-prices.test.d.ts +2 -0
- package/dist/gas/token-prices.test.d.ts.map +1 -0
- package/dist/gas/token-prices.test.js +40 -0
- package/dist/gas/token-prices.test.js.map +1 -0
- package/dist/index.d.ts +42 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +119 -0
- package/dist/index.js.map +1 -0
- package/dist/providers/ChainConnection.d.ts +22 -0
- package/dist/providers/ChainConnection.d.ts.map +1 -0
- package/dist/providers/ChainConnection.js +67 -0
- package/dist/providers/ChainConnection.js.map +1 -0
- package/dist/providers/MultiProvider.d.ts +55 -0
- package/dist/providers/MultiProvider.d.ts.map +1 -0
- package/dist/providers/MultiProvider.js +106 -0
- package/dist/providers/MultiProvider.js.map +1 -0
- package/dist/providers/RetryProvider.d.ts +18 -0
- package/dist/providers/RetryProvider.d.ts.map +1 -0
- package/dist/providers/RetryProvider.js +49 -0
- package/dist/providers/RetryProvider.js.map +1 -0
- package/dist/proxy.d.ts +23 -0
- package/dist/proxy.d.ts.map +1 -0
- package/dist/proxy.js +30 -0
- package/dist/proxy.js.map +1 -0
- package/dist/router.d.ts +19 -0
- package/dist/router.d.ts.map +1 -0
- package/dist/router.js +3 -0
- package/dist/router.js.map +1 -0
- package/dist/test/envSubsetDeployer/app.d.ts +36 -0
- package/dist/test/envSubsetDeployer/app.d.ts.map +1 -0
- package/dist/test/envSubsetDeployer/app.js +77 -0
- package/dist/test/envSubsetDeployer/app.js.map +1 -0
- package/dist/test/envSubsetDeployer/check-single-chain.d.ts +2 -0
- package/dist/test/envSubsetDeployer/check-single-chain.d.ts.map +1 -0
- package/dist/test/envSubsetDeployer/check-single-chain.js +49 -0
- package/dist/test/envSubsetDeployer/check-single-chain.js.map +1 -0
- package/dist/test/envSubsetDeployer/deploy-single-chain.d.ts +2 -0
- package/dist/test/envSubsetDeployer/deploy-single-chain.d.ts.map +1 -0
- package/dist/test/envSubsetDeployer/deploy-single-chain.js +42 -0
- package/dist/test/envSubsetDeployer/deploy-single-chain.js.map +1 -0
- package/dist/test/envSubsetDeployer/deploy.hardhat-test.d.ts +2 -0
- package/dist/test/envSubsetDeployer/deploy.hardhat-test.d.ts.map +1 -0
- package/dist/test/envSubsetDeployer/deploy.hardhat-test.js +80 -0
- package/dist/test/envSubsetDeployer/deploy.hardhat-test.js.map +1 -0
- package/dist/test/envSubsetDeployer/utils.d.ts +7 -0
- package/dist/test/envSubsetDeployer/utils.d.ts.map +1 -0
- package/dist/test/envSubsetDeployer/utils.js +25 -0
- package/dist/test/envSubsetDeployer/utils.js.map +1 -0
- package/dist/test/testUtils.d.ts +29 -0
- package/dist/test/testUtils.d.ts.map +1 -0
- package/dist/test/testUtils.js +98 -0
- package/dist/test/testUtils.js.map +1 -0
- package/dist/types.d.ts +20 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +3 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/MultiGeneric.d.ts +27 -0
- package/dist/utils/MultiGeneric.d.ts.map +1 -0
- package/dist/utils/MultiGeneric.js +70 -0
- package/dist/utils/MultiGeneric.js.map +1 -0
- package/dist/utils/ids.d.ts +20 -0
- package/dist/utils/ids.d.ts.map +1 -0
- package/dist/utils/ids.js +47 -0
- package/dist/utils/ids.js.map +1 -0
- package/dist/utils/number.d.ts +32 -0
- package/dist/utils/number.d.ts.map +1 -0
- package/dist/utils/number.js +59 -0
- package/dist/utils/number.js.map +1 -0
- package/dist/utils/objects.d.ts +7 -0
- package/dist/utils/objects.d.ts.map +1 -0
- package/dist/utils/objects.js +29 -0
- package/dist/utils/objects.js.map +1 -0
- package/dist/utils/time.d.ts +8 -0
- package/dist/utils/time.d.ts.map +1 -0
- package/dist/utils/time.js +15 -0
- package/dist/utils/time.js.map +1 -0
- package/dist/utils/utils.test.d.ts +2 -0
- package/dist/utils/utils.test.d.ts.map +1 -0
- package/dist/utils/utils.test.js +41 -0
- package/dist/utils/utils.test.js.map +1 -0
- package/package.json +55 -0
|
@@ -0,0 +1,235 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.InterchainGasCalculator = void 0;
|
|
16
|
+
const coingecko_api_1 = __importDefault(require("coingecko-api"));
|
|
17
|
+
const ethers_1 = require("ethers");
|
|
18
|
+
const utils_1 = require("@hyperlane-xyz/utils");
|
|
19
|
+
const chainMetadata_1 = require("../consts/chainMetadata");
|
|
20
|
+
const HyperlaneCore_1 = require("../core/HyperlaneCore");
|
|
21
|
+
const number_1 = require("../utils/number");
|
|
22
|
+
const token_prices_1 = require("./token-prices");
|
|
23
|
+
/**
|
|
24
|
+
* A note on arithmetic:
|
|
25
|
+
* The ethers.BigNumber implementation behaves very similar to Solidity's
|
|
26
|
+
* number handling by not supporting decimals. To avoid adding another big
|
|
27
|
+
* number implementation as a dependency, we use ethers.FixedNumber, a
|
|
28
|
+
* fixed point implementation intended to model how Solidity's half-supported
|
|
29
|
+
* fixed point numbers work, see https://docs.soliditylang.org/en/v0.8.13/types.html#fixed-point-numbers).
|
|
30
|
+
*
|
|
31
|
+
* Generally, ceiling is used rather than floor here to err on the side of over-
|
|
32
|
+
* estimating amounts.
|
|
33
|
+
*/
|
|
34
|
+
// If a chain doesn't specify how many decimals their native token has, 18 is used.
|
|
35
|
+
const DEFAULT_TOKEN_DECIMALS = 18;
|
|
36
|
+
// Intrinsic gas for a transaction. Does not consider calldata costs or differences in
|
|
37
|
+
// intrinsic gas for different chains.
|
|
38
|
+
const GAS_INTRINSIC = 21000;
|
|
39
|
+
// The gas used to process a message when the quorum size is zero.
|
|
40
|
+
// Includes intrinsic gas and all other gas that does not scale with the
|
|
41
|
+
// quorum size. Excludes the cost of calling `recipient.handle()`.
|
|
42
|
+
// Derived by observing the amount of gas consumed for a quorum of 1 (~103000 gas),
|
|
43
|
+
// and subtracting the gas overhead per signature.
|
|
44
|
+
const GAS_OVERHEAD_BASE = 95000;
|
|
45
|
+
// The amount of gas used for each signature when a signed checkpoint
|
|
46
|
+
// is submitted for verification.
|
|
47
|
+
// Really observed to be about 8568, but rounding up for safety.
|
|
48
|
+
const GAS_OVERHEAD_PER_SIGNATURE = 9000;
|
|
49
|
+
/**
|
|
50
|
+
* Calculates interchain gas payments.
|
|
51
|
+
*/
|
|
52
|
+
class InterchainGasCalculator {
|
|
53
|
+
constructor(multiProvider, core, config) {
|
|
54
|
+
var _a, _b;
|
|
55
|
+
this.multiProvider = multiProvider;
|
|
56
|
+
this.core = core;
|
|
57
|
+
if (config === null || config === void 0 ? void 0 : config.tokenPriceGetter) {
|
|
58
|
+
this.tokenPriceGetter = config.tokenPriceGetter;
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
const coinGecko = new coingecko_api_1.default();
|
|
62
|
+
this.tokenPriceGetter = new token_prices_1.CoinGeckoTokenPriceGetter(coinGecko);
|
|
63
|
+
}
|
|
64
|
+
this.paymentEstimateMultiplier = ethers_1.FixedNumber.from((_a = config === null || config === void 0 ? void 0 : config.paymentEstimateMultiplier) !== null && _a !== void 0 ? _a : '1.25');
|
|
65
|
+
this.messageGasEstimateBuffer = ethers_1.BigNumber.from((_b = config === null || config === void 0 ? void 0 : config.messageGasEstimateBuffer) !== null && _b !== void 0 ? _b : 50000);
|
|
66
|
+
}
|
|
67
|
+
static fromEnvironment(env, multiProvider, config) {
|
|
68
|
+
const core = HyperlaneCore_1.HyperlaneCore.fromEnvironment(env, multiProvider);
|
|
69
|
+
return new InterchainGasCalculator(multiProvider, core, config);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Given an amount of gas to consume on the destination chain, calculates the
|
|
73
|
+
* estimated payment denominated in the native token of the origin chain.
|
|
74
|
+
* Considers the exchange rate between the native tokens of the origin and
|
|
75
|
+
* destination chains and the suggested gas price of the destination chain.
|
|
76
|
+
* @param origin The name of the origin chain.
|
|
77
|
+
* @param destination The name of the destination chain.
|
|
78
|
+
* @param gas The amount of gas to pay for on the destination chain.
|
|
79
|
+
* @returns An estimated amount of origin chain tokens to cover gas costs on the
|
|
80
|
+
* destination chain.
|
|
81
|
+
*/
|
|
82
|
+
estimatePaymentForGas(origin, destination, gas) {
|
|
83
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
84
|
+
const destinationGasPrice = yield this.getGasPrice(destination);
|
|
85
|
+
const destinationGasCost = gas.mul(destinationGasPrice);
|
|
86
|
+
const originGasCost = yield this.convertBetweenTokens(destination, origin, destinationGasCost);
|
|
87
|
+
// Applies a multiplier
|
|
88
|
+
return (0, number_1.mulBigAndFixed)(originGasCost, this.paymentEstimateMultiplier, true);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
/**
|
|
92
|
+
* Given an amount of gas the message's recipient `handle` function is expected
|
|
93
|
+
* to use, calculates the estimated payment denominated in the native
|
|
94
|
+
* token of the origin chain. Considers the exchange rate between the native
|
|
95
|
+
* tokens of the origin and destination chains, the suggested gas price on
|
|
96
|
+
* the destination chain, gas costs incurred by a relayer when submitting a signed
|
|
97
|
+
* checkpoint to the destination chain, and the overhead gas cost in Inbox of processing
|
|
98
|
+
* a message.
|
|
99
|
+
* @param origin The name of the origin chain.
|
|
100
|
+
* @param destination The name of the destination chain.
|
|
101
|
+
* @param handleGas The amount of gas the recipient `handle` function
|
|
102
|
+
* is estimated to use.
|
|
103
|
+
* @returns An estimated amount of origin chain tokens to cover gas costs of the
|
|
104
|
+
* message on the destination chain.
|
|
105
|
+
*/
|
|
106
|
+
estimatePaymentForHandleGas(origin, destination, handleGas) {
|
|
107
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
108
|
+
const destinationGas = handleGas.add(yield this.estimateGasForProcess(origin, destination));
|
|
109
|
+
return this.estimatePaymentForGas(origin, destination, destinationGas);
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
/**
|
|
113
|
+
* Calculates the estimated payment to process the message on its destination chain,
|
|
114
|
+
* denominated in the native token of the origin chain. The gas used by the message's
|
|
115
|
+
* recipient handler function is estimated in an eth_estimateGas call to the
|
|
116
|
+
* destination chain, and is then used to calculate the payment using
|
|
117
|
+
* Currently made private as it does not work properly for Arbitrum.
|
|
118
|
+
* {@link estimatePaymentForHandleGasAmount}.
|
|
119
|
+
* @param message The parsed message to estimate payment for.
|
|
120
|
+
* @returns An estimated amount of origin chain tokens to cover gas costs of the
|
|
121
|
+
* message on the destination chain.
|
|
122
|
+
*/
|
|
123
|
+
estimatePaymentForMessage(message) {
|
|
124
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
125
|
+
const handleGas = yield this.estimateGasForHandle(message);
|
|
126
|
+
return this.estimatePaymentForHandleGas(message.origin, message.destination, handleGas);
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
/**
|
|
130
|
+
* Using the exchange rates provided by tokenPriceGetter, returns the amount of
|
|
131
|
+
* `toChain` native tokens equivalent in value to the provided `fromAmount` of
|
|
132
|
+
* `fromChain` native tokens. Accounts for differences in the decimals of the tokens.
|
|
133
|
+
* @param fromChain The chain whose native token is being converted from.
|
|
134
|
+
* @param toChain The chain whose native token is being converted into.
|
|
135
|
+
* @param fromAmount The amount of `fromChain` native tokens to convert from.
|
|
136
|
+
* @returns The amount of `toChain` native tokens whose value is equivalent to
|
|
137
|
+
* `fromAmount` of `fromChain` native tokens.
|
|
138
|
+
*/
|
|
139
|
+
convertBetweenTokens(fromChain, toChain, value) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
// Does not factor in differing token decimals.
|
|
142
|
+
const exchangeRate = yield this.tokenPriceGetter.getTokenExchangeRate(fromChain, toChain);
|
|
143
|
+
// 1/100th of a cent
|
|
144
|
+
const PRECISION = 1000;
|
|
145
|
+
return (0, number_1.convertDecimalValue)(value.mul(Math.round(exchangeRate * PRECISION)).div(PRECISION), this.tokenDecimals(fromChain), this.tokenDecimals(toChain));
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Gets a suggested gas price for a chain.
|
|
150
|
+
* @param chainName The name of the chain to get the gas price for
|
|
151
|
+
* @returns The suggested gas price in wei on the destination chain.
|
|
152
|
+
*/
|
|
153
|
+
getGasPrice(chain) {
|
|
154
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
155
|
+
const provider = this.multiProvider.getChainConnection(chain).provider;
|
|
156
|
+
if (provider == undefined) {
|
|
157
|
+
throw new Error(`Missing provider for ${chain}`);
|
|
158
|
+
}
|
|
159
|
+
return provider.getGasPrice();
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
/**
|
|
163
|
+
* Gets the number of decimals of the provided chain's native token.
|
|
164
|
+
* @param chain The chain.
|
|
165
|
+
* @returns The number of decimals of `chain`'s native token.
|
|
166
|
+
*/
|
|
167
|
+
tokenDecimals(chain) {
|
|
168
|
+
var _a;
|
|
169
|
+
return (_a = chainMetadata_1.chainMetadata[chain].nativeTokenDecimals) !== null && _a !== void 0 ? _a : DEFAULT_TOKEN_DECIMALS;
|
|
170
|
+
}
|
|
171
|
+
/**
|
|
172
|
+
* Estimates the amount of gas used by message's recipient `handle` function
|
|
173
|
+
* on its destination chain. This does not assume the Inbox of the destination
|
|
174
|
+
* chain has a checkpoint that the message is included in, and does not
|
|
175
|
+
* consider intrinsic gas or any "overhead" gas incurred by Inbox.process.
|
|
176
|
+
* The estimated gas returned is the sum of:
|
|
177
|
+
* 1. The estimated gas consumption of a direct call to the `handle`
|
|
178
|
+
* function of the recipient address using the correct parameters and
|
|
179
|
+
* setting the `from` address of the transaction to the address of the inbox.
|
|
180
|
+
* 2. A buffer to account for inaccuracies in the above estimation.
|
|
181
|
+
* @param message The message to estimate recipient `handle` gas usage for.
|
|
182
|
+
* @returns The estimated gas required by the message's recipient handle function
|
|
183
|
+
* on the destination chain.
|
|
184
|
+
*/
|
|
185
|
+
estimateGasForHandle(message) {
|
|
186
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
187
|
+
const provider = this.multiProvider.getChainConnection(message.destination)
|
|
188
|
+
.provider;
|
|
189
|
+
const { destinationInbox } = this.core.getMailboxPair(message.origin, message.destination);
|
|
190
|
+
const handlerInterface = new ethers_1.ethers.utils.Interface([
|
|
191
|
+
'function handle(uint32,bytes32,bytes)',
|
|
192
|
+
]);
|
|
193
|
+
// Estimates a direct call to the `handle` function of the recipient
|
|
194
|
+
// with the `from` address set to the inbox.
|
|
195
|
+
// This includes intrinsic gas.
|
|
196
|
+
const directHandleCallGas = yield provider.estimateGas({
|
|
197
|
+
to: utils_1.utils.bytes32ToAddress(message.recipient),
|
|
198
|
+
from: destinationInbox.address,
|
|
199
|
+
data: handlerInterface.encodeFunctionData('handle', [
|
|
200
|
+
chainMetadata_1.chainMetadata[message.origin].id,
|
|
201
|
+
utils_1.utils.addressToBytes32(message.sender),
|
|
202
|
+
message.body,
|
|
203
|
+
]),
|
|
204
|
+
});
|
|
205
|
+
// Subtract intrinsic gas, which is included in directHandleCallGas.
|
|
206
|
+
// Note the "real" intrinsic gas will always be higher than this.intrinsicGas
|
|
207
|
+
// due to calldata costs, but this is desired because subtracting the lower bound
|
|
208
|
+
// this.intrinsicGas will result in a more generous final estimate.
|
|
209
|
+
return directHandleCallGas
|
|
210
|
+
.add(this.messageGasEstimateBuffer)
|
|
211
|
+
.sub(this.intrinsicGas());
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
/**
|
|
215
|
+
* @returns A generous estimation of the gas consumption of all process
|
|
216
|
+
* operations within Inbox.sol, including intrinsic gas. Does not include any gas
|
|
217
|
+
* consumed within a message's recipient `handle` function.
|
|
218
|
+
*/
|
|
219
|
+
estimateGasForProcess(origin, destination) {
|
|
220
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
221
|
+
const inboxes = this.core.getContracts(destination).inboxes;
|
|
222
|
+
const threshold = yield inboxes[origin].inboxValidatorManager.threshold();
|
|
223
|
+
return threshold.mul(GAS_OVERHEAD_PER_SIGNATURE).add(GAS_OVERHEAD_BASE);
|
|
224
|
+
});
|
|
225
|
+
}
|
|
226
|
+
/**
|
|
227
|
+
* @returns The intrinsic gas of a basic transaction. Note this does not consider calldata
|
|
228
|
+
* costs or potentially different intrinsic gas costs for different chains.
|
|
229
|
+
*/
|
|
230
|
+
intrinsicGas() {
|
|
231
|
+
return ethers_1.BigNumber.from(GAS_INTRINSIC);
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
exports.InterchainGasCalculator = InterchainGasCalculator;
|
|
235
|
+
//# sourceMappingURL=calculator.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculator.js","sourceRoot":"","sources":["../../src/gas/calculator.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;AAAA,kEAAsC;AACtC,mCAAwD;AAExD,gDAA6C;AAE7C,2DAAwD;AACxD,yDAI+B;AAG/B,4CAAsE;AAEtE,iDAA6E;AAE7E;;;;;;;;;;GAUG;AAEH,mFAAmF;AACnF,MAAM,sBAAsB,GAAG,EAAE,CAAC;AAElC,sFAAsF;AACtF,sCAAsC;AACtC,MAAM,aAAa,GAAG,KAAM,CAAC;AAE7B,kEAAkE;AAClE,wEAAwE;AACxE,kEAAkE;AAClE,mFAAmF;AACnF,kDAAkD;AAClD,MAAM,iBAAiB,GAAG,KAAM,CAAC;AAEjC,qEAAqE;AACrE,iCAAiC;AACjC,gEAAgE;AAChE,MAAM,0BAA0B,GAAG,IAAK,CAAC;AAkCzC;;GAEG;AACH,MAAa,uBAAuB;IAkBlC,YACE,aAAmC,EACnC,IAA0B,EAC1B,MAAsC;;QAEtC,IAAI,CAAC,aAAa,GAAG,aAAa,CAAC;QACnC,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;QAEjB,IAAI,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,gBAAgB,EAAE;YAC5B,IAAI,CAAC,gBAAgB,GAAG,MAAM,CAAC,gBAAgB,CAAC;SACjD;aAAM;YACL,MAAM,SAAS,GAAG,IAAI,uBAAS,EAAE,CAAC;YAClC,IAAI,CAAC,gBAAgB,GAAG,IAAI,wCAAyB,CAAC,SAAS,CAAC,CAAC;SAClE;QAED,IAAI,CAAC,yBAAyB,GAAG,oBAAW,CAAC,IAAI,CAC/C,MAAA,MAAM,aAAN,MAAM,uBAAN,MAAM,CAAE,yBAAyB,mCAAI,MAAM,CAC5C,CAAC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AY;QACpB,OAAO,kBAAS,CAAC,IAAI,CAAC,aAAa,CAAC,CAAC;IACvC,CAAC;CACF;AApPD,0DAoPC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculator.test.d.ts","sourceRoot":"","sources":["../../src/gas/calculator.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,211 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const chai_1 = require("chai");
|
|
16
|
+
const ethers_1 = require("ethers");
|
|
17
|
+
const sinon_1 = __importDefault(require("sinon"));
|
|
18
|
+
const chains_1 = require("../consts/chains");
|
|
19
|
+
const HyperlaneCore_1 = require("../core/HyperlaneCore");
|
|
20
|
+
const MultiProvider_1 = require("../providers/MultiProvider");
|
|
21
|
+
const testUtils_1 = require("../test/testUtils");
|
|
22
|
+
const calculator_1 = require("./calculator");
|
|
23
|
+
const HANDLE_GAS = 100000;
|
|
24
|
+
const SUGGESTED_GAS_PRICE = 10;
|
|
25
|
+
const INBOX_PROCESS_OVERHEAD_GAS = 100000;
|
|
26
|
+
// Exposes protected methods so they can be stubbed.
|
|
27
|
+
class TestInterchainGasCalculator extends calculator_1.InterchainGasCalculator {
|
|
28
|
+
estimateGasForProcess(origin, destination) {
|
|
29
|
+
return super.estimateGasForProcess(origin, destination);
|
|
30
|
+
}
|
|
31
|
+
estimateGasForHandle(message) {
|
|
32
|
+
return super.estimateGasForHandle(message);
|
|
33
|
+
}
|
|
34
|
+
convertBetweenTokens(fromChain, toChain, fromAmount) {
|
|
35
|
+
return super.convertBetweenTokens(fromChain, toChain, fromAmount);
|
|
36
|
+
}
|
|
37
|
+
tokenDecimals(chain) {
|
|
38
|
+
return super.tokenDecimals(chain);
|
|
39
|
+
}
|
|
40
|
+
getGasPrice(chain) {
|
|
41
|
+
return super.getGasPrice(chain);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
describe('InterchainGasCalculator', () => {
|
|
45
|
+
const provider = new testUtils_1.MockProvider();
|
|
46
|
+
// TODO: fix types to not require the <any> here.
|
|
47
|
+
// This is because InterchainGasCalculator isn't very strongly typed,
|
|
48
|
+
// which is because ParsedMessage isn't very strongly typed. This results
|
|
49
|
+
// in InterchainGasCalculator expecting a multiprovider with providers for
|
|
50
|
+
// every chain.
|
|
51
|
+
const multiProvider = new MultiProvider_1.MultiProvider({
|
|
52
|
+
test1: { provider },
|
|
53
|
+
test2: { provider },
|
|
54
|
+
test3: { provider },
|
|
55
|
+
});
|
|
56
|
+
const core = HyperlaneCore_1.HyperlaneCore.fromEnvironment('test', multiProvider);
|
|
57
|
+
const origin = chains_1.Chains.test1;
|
|
58
|
+
const destination = chains_1.Chains.test2;
|
|
59
|
+
let tokenPriceGetter;
|
|
60
|
+
let calculator;
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
tokenPriceGetter = new testUtils_1.MockTokenPriceGetter();
|
|
63
|
+
tokenPriceGetter.setTokenPrice(origin, 9.0909);
|
|
64
|
+
tokenPriceGetter.setTokenPrice(destination, 5.5);
|
|
65
|
+
calculator = new TestInterchainGasCalculator(multiProvider, core, {
|
|
66
|
+
tokenPriceGetter,
|
|
67
|
+
// A multiplier of 1 makes testing easier to reason about
|
|
68
|
+
paymentEstimateMultiplier: '1',
|
|
69
|
+
});
|
|
70
|
+
});
|
|
71
|
+
afterEach(() => {
|
|
72
|
+
sinon_1.default.restore();
|
|
73
|
+
provider.clearMethodResolveValues();
|
|
74
|
+
});
|
|
75
|
+
describe('estimatePaymentForGas', () => {
|
|
76
|
+
it('estimates origin token payment from a specified destination gas amount', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
77
|
+
// Set destination gas price to 10 wei
|
|
78
|
+
provider.setMethodResolveValue('getGasPrice', ethers_1.BigNumber.from(SUGGESTED_GAS_PRICE));
|
|
79
|
+
const estimatedPayment = yield calculator.estimatePaymentForGas(origin, destination, ethers_1.BigNumber.from(HANDLE_GAS));
|
|
80
|
+
// 100k gas * 10 gas price * ($5.5 per destination token / $9.0909 per origin token)
|
|
81
|
+
(0, chai_1.expect)(estimatedPayment.toNumber()).to.equal(605000);
|
|
82
|
+
}));
|
|
83
|
+
});
|
|
84
|
+
describe('estimatePaymentForHandleGas', () => {
|
|
85
|
+
it('estimates origin token payment from a specified destination handle gas amount', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
86
|
+
// Set destination gas price to 10 wei
|
|
87
|
+
provider.setMethodResolveValue('getGasPrice', ethers_1.BigNumber.from(SUGGESTED_GAS_PRICE));
|
|
88
|
+
// Stub the inbox process overhead gas
|
|
89
|
+
sinon_1.default
|
|
90
|
+
.stub(calculator, 'estimateGasForProcess')
|
|
91
|
+
.returns(Promise.resolve(ethers_1.BigNumber.from(INBOX_PROCESS_OVERHEAD_GAS)));
|
|
92
|
+
const estimatedPayment = yield calculator.estimatePaymentForHandleGas(origin, destination, ethers_1.BigNumber.from(HANDLE_GAS));
|
|
93
|
+
// (100_000 dest handler gas + 100_000 process overhead gas)
|
|
94
|
+
// * 10 gas price * ($5.5 per destination token / $9.0909 per origin token)
|
|
95
|
+
(0, chai_1.expect)(estimatedPayment.toNumber()).to.equal(1210000);
|
|
96
|
+
}));
|
|
97
|
+
});
|
|
98
|
+
/*
|
|
99
|
+
describe('estimatePaymentForMessage', () => {
|
|
100
|
+
it('estimates origin token payment from a specified message', async () => {
|
|
101
|
+
// Set destination gas price to 10 wei
|
|
102
|
+
provider.setMethodResolveValue(
|
|
103
|
+
'getGasPrice',
|
|
104
|
+
BigNumber.from(SUGGESTED_GAS_PRICE),
|
|
105
|
+
);
|
|
106
|
+
// Set the estimated handle gas
|
|
107
|
+
sinon
|
|
108
|
+
.stub(calculator, 'estimateGasForHandle')
|
|
109
|
+
.returns(Promise.resolve(BigNumber.from(HANDLE_GAS)));
|
|
110
|
+
// Stub the inbox process overhead gas
|
|
111
|
+
sinon
|
|
112
|
+
.stub(calculator, 'estimateGasForProcess')
|
|
113
|
+
.returns(Promise.resolve(BigNumber.from(INBOX_PROCESS_OVERHEAD_GAS)));
|
|
114
|
+
|
|
115
|
+
const zeroAddressBytes32 = utils.addressToBytes32(
|
|
116
|
+
ethers.constants.AddressZero,
|
|
117
|
+
);
|
|
118
|
+
const message: ParsedMessage<TestChainNames, Chains.test2> = {
|
|
119
|
+
origin: origin,
|
|
120
|
+
sender: zeroAddressBytes32,
|
|
121
|
+
destination: destination,
|
|
122
|
+
recipient: zeroAddressBytes32,
|
|
123
|
+
body: '0x12345678',
|
|
124
|
+
};
|
|
125
|
+
|
|
126
|
+
const estimatedPayment = await calculator.estimatePaymentForMessage(
|
|
127
|
+
message,
|
|
128
|
+
);
|
|
129
|
+
|
|
130
|
+
// (100_000 dest handler gas + 100_000 process overhead gas)
|
|
131
|
+
// * 10 gas price * ($5.5 per destination token / $9.0909 per origin token)
|
|
132
|
+
expect(estimatedPayment.toNumber()).to.equal(1_210_000);
|
|
133
|
+
});
|
|
134
|
+
});
|
|
135
|
+
*/
|
|
136
|
+
describe('convertBetweenTokens', () => {
|
|
137
|
+
const destinationWei = ethers_1.BigNumber.from('1000');
|
|
138
|
+
it('converts using the USD value of origin and destination native tokens', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
139
|
+
const originWei = yield calculator.convertBetweenTokens(destination, origin, destinationWei);
|
|
140
|
+
// 1000 * (5.5 / 9.0909)
|
|
141
|
+
(0, chai_1.expect)(originWei.toNumber()).to.equal(605);
|
|
142
|
+
}));
|
|
143
|
+
it('considers when the origin token decimals > the destination token decimals', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
144
|
+
calculator.tokenDecimals = (chain) => {
|
|
145
|
+
if (chain === origin) {
|
|
146
|
+
return 20;
|
|
147
|
+
}
|
|
148
|
+
return 18;
|
|
149
|
+
};
|
|
150
|
+
const originWei = yield calculator.convertBetweenTokens(destination, origin, destinationWei);
|
|
151
|
+
// 1000 * (5.5 / 9.0909) * 100
|
|
152
|
+
(0, chai_1.expect)(originWei.toNumber()).to.equal(60500);
|
|
153
|
+
}));
|
|
154
|
+
it('considers when the origin token decimals < the destination token decimals', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
155
|
+
sinon_1.default
|
|
156
|
+
.stub(calculator, 'tokenDecimals')
|
|
157
|
+
.callsFake((chain) => {
|
|
158
|
+
if (chain === origin) {
|
|
159
|
+
return 16;
|
|
160
|
+
}
|
|
161
|
+
return 18;
|
|
162
|
+
});
|
|
163
|
+
const originWei = yield calculator.convertBetweenTokens(destination, origin, destinationWei);
|
|
164
|
+
// 1000 * (5.5 / 9.0909) / 100
|
|
165
|
+
(0, chai_1.expect)(originWei.toNumber()).to.equal(6);
|
|
166
|
+
}));
|
|
167
|
+
});
|
|
168
|
+
describe('getGasPrice', () => {
|
|
169
|
+
it('gets the gas price from the provider', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
170
|
+
provider.setMethodResolveValue('getGasPrice', ethers_1.BigNumber.from(SUGGESTED_GAS_PRICE));
|
|
171
|
+
(0, chai_1.expect)((yield calculator.getGasPrice(destination)).toNumber()).to.equal(SUGGESTED_GAS_PRICE);
|
|
172
|
+
}));
|
|
173
|
+
});
|
|
174
|
+
describe('estimateGasForProcess', () => {
|
|
175
|
+
let threshold;
|
|
176
|
+
// Mock the return value of InboxValidatorManager.threshold
|
|
177
|
+
// to return `threshold`. Because the mocking involves a closure,
|
|
178
|
+
// changing `threshold` will change the return value of InboxValidatorManager.threshold.
|
|
179
|
+
before(() => {
|
|
180
|
+
const getContractsStub = sinon_1.default.stub(core, 'getContracts');
|
|
181
|
+
let thresholdStub;
|
|
182
|
+
getContractsStub.callsFake((chain) => {
|
|
183
|
+
// Get the "real" return value of getContracts.
|
|
184
|
+
const contracts = getContractsStub.wrappedMethod.bind(core)(chain);
|
|
185
|
+
// Ethers contracts are frozen using Object.freeze, so we make a copy
|
|
186
|
+
// of the object so we can stub `threshold`.
|
|
187
|
+
const validatorManager = Object.assign({},
|
|
188
|
+
// @ts-ignore
|
|
189
|
+
contracts.inboxes[origin].inboxValidatorManager);
|
|
190
|
+
// Because we are stubbing vaidatorManager.threshold when core.getContracts gets called,
|
|
191
|
+
// we must ensure we don't try to stub more than once or sinon will complain.
|
|
192
|
+
if (!thresholdStub) {
|
|
193
|
+
thresholdStub = sinon_1.default
|
|
194
|
+
.stub(validatorManager, 'threshold')
|
|
195
|
+
.callsFake(() => Promise.resolve(ethers_1.BigNumber.from(threshold)));
|
|
196
|
+
// @ts-ignore
|
|
197
|
+
contracts.inboxes[origin].inboxValidatorManager = validatorManager;
|
|
198
|
+
}
|
|
199
|
+
return contracts;
|
|
200
|
+
});
|
|
201
|
+
});
|
|
202
|
+
it('scales the gas cost with the quorum threshold', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
203
|
+
threshold = 2;
|
|
204
|
+
const gasWithThresholdLow = yield calculator.estimateGasForProcess(origin, destination);
|
|
205
|
+
threshold = 3;
|
|
206
|
+
const gasWithThresholdHigh = yield calculator.estimateGasForProcess(origin, destination);
|
|
207
|
+
(0, chai_1.expect)(gasWithThresholdHigh.gt(gasWithThresholdLow)).to.be.true;
|
|
208
|
+
}));
|
|
209
|
+
});
|
|
210
|
+
});
|
|
211
|
+
//# sourceMappingURL=calculator.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculator.test.js","sourceRoot":"","sources":["../../src/gas/calculator.test.ts"],"names":[],"mappings":";;;;;;;;;;;;;;AAAA,+BAA8B;AAC9B,mCAAmC;AACnC,kDAA0B;AAE1B,6CAA0C;AAC1C,yDAAsD;AAEtD,8DAA2D;AAC3D,iDAAuE;AAGvE,6CAAsE;AAEtE,MAAM,UAAU,GAAG,MAAO,CAAC;AAC3B,MAAM,mBAAmB,GAAG,EAAE,CAAC;AAC/B,MAAM,0BAA0B,GAAG,MAAO,CAAC;AAE3C,oDAAoD;AACpD,MAAM,2BAEJ,SAAQ,oCAA8B;IACtC,qBAAqB,CACnB,MAAmC,EACnC,WAAwB;QAExB,OAAO,KAAK,CAAC,qBAAqB,CAAC,MAAM,EAAE,WAAW,CAAC,CAAC;IAC1D,CAAC;IACD,oBAAoB,CAClB,OAAyC;QAEzC,OAAO,KAAK,CAAC,oBAAoB,CAAC,OAAO,CAAC,CAAC;IAC7C,CAAC;IACD,oBAAoB,CAClB,SAAgB,EAChB,OAAc,EACd,UAAqB;QAErB,OAAO,KAAK,CAAC,oBAAoB,CAAC,SAAS,EAAE,OAAO,EAAE,UAAU,CAAC,CAAC;IACpE,CAAC;IACD,aAAa,CAAC,KAAY;QACxB,OAAO,KAAK,CAAC,aAAa,CAAC,KAAK,CAAC,CAAC;IACpC,CAAC;IACD,WAAW,CAAC,KAAY;QACtB,OAAO,KAAK,CAAC,WAAW,CAAC,KAAK,CAAC,CAAC;IAClC,CAAC;CACF;AAED,QAAQ,CAAC,yBAAyB,EAAE,GAAG,EAAE;IACvC,MAAM,QAAQ,GAAG,IAAI,wBAAY,EAAE,CAAC;IACpC,iDAAiD;IACjD,qEAAqE;IACrE,yEAAyE;IACzE,0EAA0E;IAC1E,eAAe;IACf,MAAM,aAAa,GAAG,IAAI,6BAAa,CAAC;QACtC,KAAK,EAAE,EAAE,QAAQ,EAA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,SAAS,GAAG,CAAC,CAAC;YACd,MAAM,mBAAmB,GAAG,MAAM,UAAU,CAAC,qBAAqB,CAChE,MAAM,EACN,WAAW,CACZ,CAAC;YAEF,SAAS,GAAG,CAAC,CAAC;YACd,MAAM,oBAAoB,GAAG,MAAM,UAAU,CAAC,qBAAqB,CACjE,MAAM,EACN,WAAW,CACZ,CAAC;YAEF,IAAA,aAAM,EAAC,oBAAoB,CAAC,EAAE,CAAC,mBAAmB,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,CAAC,IAAI,CAAC;QAClE,CAAC,CAAA,CAAC,CAAC;IACL,CAAC,CAAC,CAAC;AACL,CAAC,CAAC,CAAC"}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import CoinGecko from 'coingecko-api';
|
|
2
|
+
import { ChainName } from '../types';
|
|
3
|
+
export interface TokenPriceGetter {
|
|
4
|
+
getTokenPrice(chain: ChainName): Promise<number>;
|
|
5
|
+
getTokenExchangeRate(base: ChainName, quote: ChainName): Promise<number>;
|
|
6
|
+
}
|
|
7
|
+
export declare type CoinGeckoInterface = Pick<CoinGecko, 'simple'>;
|
|
8
|
+
export declare type CoinGeckoSimpleInterface = CoinGecko['simple'];
|
|
9
|
+
export declare type CoinGeckoSimplePriceParams = Parameters<CoinGeckoSimpleInterface['price']>[0];
|
|
10
|
+
export declare type CoinGeckoResponse = ReturnType<CoinGeckoSimpleInterface['price']>;
|
|
11
|
+
export declare class CoinGeckoTokenPriceGetter implements TokenPriceGetter {
|
|
12
|
+
protected coinGecko: CoinGeckoInterface;
|
|
13
|
+
constructor(coinGecko: CoinGeckoInterface);
|
|
14
|
+
getTokenPrice(chain: ChainName): Promise<number>;
|
|
15
|
+
getTokenExchangeRate(base: ChainName, quote: ChainName): Promise<number>;
|
|
16
|
+
private getTokenPrices;
|
|
17
|
+
}
|
|
18
|
+
//# sourceMappingURL=token-prices.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-prices.d.ts","sourceRoot":"","sources":["../../src/gas/token-prices.ts"],"names":[],"mappings":"AAAA,OAAO,SAAS,MAAM,eAAe,CAAC;AAItC,OAAO,EAAE,SAAS,EAAE,MAAM,UAAU,CAAC;AAErC,MAAM,WAAW,gBAAgB;IAC/B,aAAa,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;IACjD,oBAAoB,CAAC,IAAI,EAAE,SAAS,EAAE,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC,CAAC;CAC1E;AAED,oBAAY,kBAAkB,GAAG,IAAI,CAAC,SAAS,EAAE,QAAQ,CAAC,CAAC;AAC3D,oBAAY,wBAAwB,GAAG,SAAS,CAAC,QAAQ,CAAC,CAAC;AAC3D,oBAAY,0BAA0B,GAAG,UAAU,CACjD,wBAAwB,CAAC,OAAO,CAAC,CAClC,CAAC,CAAC,CAAC,CAAC;AACL,oBAAY,iBAAiB,GAAG,UAAU,CAAC,wBAAwB,CAAC,OAAO,CAAC,CAAC,CAAC;AAG9E,qBAAa,yBAA0B,YAAW,gBAAgB;IAChE,SAAS,CAAC,SAAS,EAAE,kBAAkB,CAAC;gBAE5B,SAAS,EAAE,kBAAkB;IAInC,aAAa,CAAC,KAAK,EAAE,SAAS,GAAG,OAAO,CAAC,MAAM,CAAC;IAKhD,oBAAoB,CACxB,IAAI,EAAE,SAAS,EACf,KAAK,EAAE,SAAS,GACf,OAAO,CAAC,MAAM,CAAC;YAKJ,cAAc;CAoC7B"}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
exports.CoinGeckoTokenPriceGetter = void 0;
|
|
13
|
+
const chainMetadata_1 = require("../consts/chainMetadata");
|
|
14
|
+
const chains_1 = require("../consts/chains");
|
|
15
|
+
// TODO: Consider caching to avoid exceeding CoinGecko's 50 requests / min limit
|
|
16
|
+
class CoinGeckoTokenPriceGetter {
|
|
17
|
+
constructor(coinGecko) {
|
|
18
|
+
this.coinGecko = coinGecko;
|
|
19
|
+
}
|
|
20
|
+
getTokenPrice(chain) {
|
|
21
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
22
|
+
const [price] = yield this.getTokenPrices([chain]);
|
|
23
|
+
return price;
|
|
24
|
+
});
|
|
25
|
+
}
|
|
26
|
+
getTokenExchangeRate(base, quote) {
|
|
27
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
28
|
+
const [basePrice, quotePrice] = yield this.getTokenPrices([base, quote]);
|
|
29
|
+
return basePrice / quotePrice;
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
getTokenPrices(chains) {
|
|
33
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
34
|
+
const isMainnet = chains.map((c) => chains_1.Mainnets.includes(c));
|
|
35
|
+
const allMainnets = isMainnet.every((v) => v === true);
|
|
36
|
+
const allTestnets = isMainnet.every((v) => v === false);
|
|
37
|
+
if (allTestnets) {
|
|
38
|
+
// Testnet tokens are all artificially priced at 1.0 USD.
|
|
39
|
+
return chains.map(() => 1);
|
|
40
|
+
}
|
|
41
|
+
if (!allMainnets) {
|
|
42
|
+
throw new Error('Cannot mix testnets and mainnets when fetching token prices');
|
|
43
|
+
}
|
|
44
|
+
const currency = 'usd';
|
|
45
|
+
// The CoinGecko API expects, in some cases, IDs that do not match
|
|
46
|
+
// ChainNames.
|
|
47
|
+
const ids = chains.map((chain) => chainMetadata_1.chainMetadata[chain].gasCurrencyCoinGeckoId || chain);
|
|
48
|
+
const response = yield this.coinGecko.simple.price({
|
|
49
|
+
ids,
|
|
50
|
+
vs_currencies: [currency],
|
|
51
|
+
});
|
|
52
|
+
try {
|
|
53
|
+
const prices = ids.map((id) => response.data[id][currency]);
|
|
54
|
+
return prices;
|
|
55
|
+
}
|
|
56
|
+
catch (e) {
|
|
57
|
+
throw new Error(`Unable to fetch prices for ${chains}, received ${JSON.stringify(response)}, got error ${e}`);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
exports.CoinGeckoTokenPriceGetter = CoinGeckoTokenPriceGetter;
|
|
63
|
+
//# sourceMappingURL=token-prices.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-prices.js","sourceRoot":"","sources":["../../src/gas/token-prices.ts"],"names":[],"mappings":";;;;;;;;;;;;AAEA,2DAAwD;AACxD,6CAA4C;AAe5C,gFAAgF;AAChF,MAAa,yBAAyB;IAGpC,YAAY,SAA6B;QACvC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC;IAC7B,CAAC;IAEK,aAAa,CAAC,KAAgB;;YAClC,MAAM,CAAC,KAAK,CAAC,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,CAAC,KAAK,CAAC,CAAC,CAAC;YACnD,OAAO,KAAK,CAAC;QACf,CAAC;KAAA;IAEK,oBAAoB,CACxB,IAAe,EACf,KAAgB;;YAEhB,MAAM,CAAC,SAAS,EAAE,UAAU,CAAC,GAAG,MAAM,IAAI,CAAC,cAAc,CAAC,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,CAAC;YACzE,OAAO,SAAS,GAAG,UAAU,CAAC;QAChC,CAAC;KAAA;IAEa,cAAc,CAAC,MAAmB;;YAC9C,MAAM,SAAS,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AAA;CACF;AAxDD,8DAwDC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-prices.test.d.ts","sourceRoot":"","sources":["../../src/gas/token-prices.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const chai_1 = require("chai");
|
|
13
|
+
const chains_1 = require("../consts/chains");
|
|
14
|
+
const testUtils_1 = require("../test/testUtils");
|
|
15
|
+
const token_prices_1 = require("./token-prices");
|
|
16
|
+
describe('TokenPriceGetter', () => {
|
|
17
|
+
let tokenPriceGetter;
|
|
18
|
+
const chainA = chains_1.Chains.ethereum, chainB = chains_1.Chains.polygon, priceA = 10, priceB = 5.5;
|
|
19
|
+
beforeEach(() => __awaiter(void 0, void 0, void 0, function* () {
|
|
20
|
+
const mockCoinGecko = new testUtils_1.MockCoinGecko();
|
|
21
|
+
// Origin token
|
|
22
|
+
mockCoinGecko.setTokenPrice(chainA, priceA);
|
|
23
|
+
// Destination token
|
|
24
|
+
mockCoinGecko.setTokenPrice(chainB, priceB);
|
|
25
|
+
tokenPriceGetter = new token_prices_1.CoinGeckoTokenPriceGetter(mockCoinGecko);
|
|
26
|
+
}));
|
|
27
|
+
describe('getTokenPrice', () => {
|
|
28
|
+
it('returns a token price', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
29
|
+
(0, chai_1.expect)(yield tokenPriceGetter.getTokenPrice(chainA)).to.equal(priceA);
|
|
30
|
+
}));
|
|
31
|
+
});
|
|
32
|
+
describe('getTokenExchangeRate', () => {
|
|
33
|
+
it('returns a value consistent with getTokenPrice()', () => __awaiter(void 0, void 0, void 0, function* () {
|
|
34
|
+
const exchangeRate = yield tokenPriceGetter.getTokenExchangeRate(chainA, chainB);
|
|
35
|
+
const expectedExchangeRate = priceA / priceB;
|
|
36
|
+
(0, chai_1.expect)(exchangeRate).to.equal(expectedExchangeRate);
|
|
37
|
+
}));
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
//# sourceMappingURL=token-prices.test.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"token-prices.test.js","sourceRoot":"","sources":["../../src/gas/token-prices.test.ts"],"names":[],"mappings":";;;;;;;;;;;AAAA,+BAA8B;AAE9B,6CAA0C;AAC1C,iDAAkD;AAElD,iDAA2D;AAE3D,QAAQ,CAAC,kBAAkB,EAAE,GAAG,EAAE;IAChC,IAAI,gBAA2C,CAAC;IAChD,MAAM,MAAM,GAAG,eAAM,CAAC,QAAQ,EAC5B,MAAM,GAAG,eAAM,CAAC,OAAO,EACvB,MAAM,GAAG,EAAE,EACX,MAAM,GAAG,GAAG,CAAC;IACf,UAAU,CAAC,GAAS,EAAE;QACpB,MAAM,aAAa,GAAG,IAAI,yBAAa,EAAE,CAAC;QAC1C,eAAe;QACf,aAAa,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC5C,oBAAoB;QACpB,aAAa,CAAC,aAAa,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;QAC5C,gBAAgB,GAAG,IAAI,wCAAyB,CAAC,aAAa,CAAC,CAAC;IAClE,CAAC,CAAA,CAAC,CAAC;IAEH,QAAQ,CAAC,eAAe,EAAE,GAAG,EAAE;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