From 3943f57c6f6165b0fcb52b45caac9c5813b61c1f Mon Sep 17 00:00:00 2001 From: Shresth Agrawal Date: Tue, 3 May 2022 20:19:39 +0200 Subject: [PATCH] Improve integration test to check on chain prices --- src/abi/gmx/reader.json | 337 ++++++++++++++++++++++++++++ src/dex/gmx/gmx-integration.test.ts | 39 +++- 2 files changed, 375 insertions(+), 1 deletion(-) create mode 100644 src/abi/gmx/reader.json diff --git a/src/abi/gmx/reader.json b/src/abi/gmx/reader.json new file mode 100644 index 000000000..1f15b08b3 --- /dev/null +++ b/src/abi/gmx/reader.json @@ -0,0 +1,337 @@ +[ + { + "inputs": [], + "name": "BASIS_POINTS_DIVISOR", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "POSITION_PROPS_LENGTH", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "PRICE_PRECISION", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "USDG_DECIMALS", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract IVault", + "name": "_vault", + "type": "address" + }, + { "internalType": "address", "name": "_tokenIn", "type": "address" }, + { "internalType": "address", "name": "_tokenOut", "type": "address" }, + { "internalType": "uint256", "name": "_amountIn", "type": "uint256" } + ], + "name": "getAmountOut", + "outputs": [ + { "internalType": "uint256", "name": "", "type": "uint256" }, + { "internalType": "uint256", "name": "", "type": "uint256" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract IVault", + "name": "_vault", + "type": "address" + }, + { "internalType": "address", "name": "_tokenIn", "type": "address" }, + { "internalType": "address", "name": "_tokenOut", "type": "address" }, + { "internalType": "uint256", "name": "_amountIn", "type": "uint256" } + ], + "name": "getFeeBasisPoints", + "outputs": [ + { "internalType": "uint256", "name": "", "type": "uint256" }, + { "internalType": "uint256", "name": "", "type": "uint256" }, + { "internalType": "uint256", "name": "", "type": "uint256" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_vault", "type": "address" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getFees", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_vault", "type": "address" }, + { "internalType": "address", "name": "_weth", "type": "address" }, + { "internalType": "uint256", "name": "_usdgAmount", "type": "uint256" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getFullVaultTokenInfo", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_vault", "type": "address" }, + { "internalType": "address", "name": "_weth", "type": "address" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getFundingRates", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract IVault", + "name": "_vault", + "type": "address" + }, + { "internalType": "address", "name": "_tokenIn", "type": "address" }, + { "internalType": "address", "name": "_tokenOut", "type": "address" } + ], + "name": "getMaxAmountIn", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_factory", "type": "address" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getPairInfo", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_vault", "type": "address" }, + { "internalType": "address", "name": "_account", "type": "address" }, + { + "internalType": "address[]", + "name": "_collateralTokens", + "type": "address[]" + }, + { + "internalType": "address[]", + "name": "_indexTokens", + "type": "address[]" + }, + { "internalType": "bool[]", "name": "_isLong", "type": "bool[]" } + ], + "name": "getPositions", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract IVaultPriceFeed", + "name": "_priceFeed", + "type": "address" + }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getPrices", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_account", "type": "address" }, + { + "internalType": "address[]", + "name": "_yieldTrackers", + "type": "address[]" + } + ], + "name": "getStakingInfo", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_account", "type": "address" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getTokenBalances", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_account", "type": "address" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getTokenBalancesWithSupplies", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract IERC20", + "name": "_token", + "type": "address" + }, + { + "internalType": "address[]", + "name": "_excludedAccounts", + "type": "address[]" + } + ], + "name": "getTokenSupply", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "contract IERC20", + "name": "_token", + "type": "address" + }, + { "internalType": "address[]", "name": "_accounts", "type": "address[]" } + ], + "name": "getTotalBalance", + "outputs": [{ "internalType": "uint256", "name": "", "type": "uint256" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "address[]", + "name": "_yieldTokens", + "type": "address[]" + } + ], + "name": "getTotalStaked", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_vault", "type": "address" }, + { "internalType": "address", "name": "_weth", "type": "address" }, + { "internalType": "uint256", "name": "_usdgAmount", "type": "uint256" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getVaultTokenInfo", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_vault", "type": "address" }, + { "internalType": "address", "name": "_weth", "type": "address" }, + { "internalType": "uint256", "name": "_usdgAmount", "type": "uint256" }, + { "internalType": "address[]", "name": "_tokens", "type": "address[]" } + ], + "name": "getVaultTokenInfoV2", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_account", "type": "address" }, + { "internalType": "address[]", "name": "_vesters", "type": "address[]" } + ], + "name": "getVestingInfo", + "outputs": [ + { "internalType": "uint256[]", "name": "", "type": "uint256[]" } + ], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "gov", + "outputs": [{ "internalType": "address", "name": "", "type": "address" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [], + "name": "hasMaxGlobalShortSizes", + "outputs": [{ "internalType": "bool", "name": "", "type": "bool" }], + "stateMutability": "view", + "type": "function" + }, + { + "inputs": [ + { + "internalType": "bool", + "name": "_hasMaxGlobalShortSizes", + "type": "bool" + } + ], + "name": "setConfig", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + }, + { + "inputs": [ + { "internalType": "address", "name": "_gov", "type": "address" } + ], + "name": "setGov", + "outputs": [], + "stateMutability": "nonpayable", + "type": "function" + } +] diff --git a/src/dex/gmx/gmx-integration.test.ts b/src/dex/gmx/gmx-integration.test.ts index dc45d6848..ef097f41c 100644 --- a/src/dex/gmx/gmx-integration.test.ts +++ b/src/dex/gmx/gmx-integration.test.ts @@ -1,16 +1,19 @@ import dotenv from 'dotenv'; dotenv.config(); +import { Interface } from '@ethersproject/abi'; import { DummyDexHelper } from '../../dex-helper/index'; import { Network, SwapSide } from '../../constants'; import { BI_POWS } from '../../bigint-constants'; import { GMX } from './gmx'; +import { GMXConfig } from './config'; import { checkPoolPrices, checkPoolsLiquidity, checkConstantPoolPrices, } from '../../../tests/utils'; import { Tokens } from '../../../tests/constants-e2e'; +import ReaderABI from '../../abi/gmx/reader.json'; const network = Network.AVALANCHE; const TokenASymbol = 'USDCe'; @@ -19,9 +22,19 @@ const TokenA = Tokens[network][TokenASymbol]; const TokenBSymbol = 'WAVAX'; const TokenB = Tokens[network][TokenBSymbol]; -const amounts = [0n, 1000000000n, 2000000000n]; +const amounts = [ + 0n, + 1000000000n, + 2000000000n, + 3000000000n, + 4000000000n, + 5000000000n, +]; const dexKey = 'GMX'; +const params = GMXConfig[dexKey][network]; +const readerInterface = new Interface(ReaderABI); +const readerAddress = '0x67b789D48c926006F5132BFCe4e976F0A7A63d5D'; describe('GMX', function () { it('getPoolIdentifiers and getPricesVolume SELL', async function () { @@ -57,6 +70,30 @@ describe('GMX', function () { } else { checkPoolPrices(poolPrices!, amounts, SwapSide.SELL, dexKey); } + + // Do on chain pricing based on reader to compare + const readerCallData = amounts.map(a => ({ + target: readerAddress, + callData: readerInterface.encodeFunctionData('getAmountOut', [ + params.vault, + TokenA.address, + TokenB.address, + a.toString(), + ]), + })); + + const readerResult = ( + await dexHelper.multiContract.methods + .aggregate(readerCallData) + .call({}, blocknumber) + ).returnData; + const expectedPrices = readerResult.map((p: any) => + BigInt( + readerInterface.decodeFunctionResult('getAmountOut', p)[0].toString(), + ), + ); + + expect(poolPrices![0].prices).toEqual(expectedPrices); }); it('getTopPoolsForToken', async function () {