sampling trouble

This commit is contained in:
Noah Khamliche
2022-03-30 15:00:08 -04:00
parent 64c090c4b4
commit afe77a432c
23 changed files with 271 additions and 8 deletions

View File

@@ -27,6 +27,7 @@ import "./CompoundSampler.sol";
import "./CurveSampler.sol";
import "./DODOSampler.sol";
import "./DODOV2Sampler.sol";
import "./GMXSampler.sol";
import "./KyberSampler.sol";
import "./KyberDmmSampler.sol";
import "./LidoSampler.sol";
@@ -53,6 +54,7 @@ contract ERC20BridgeSampler is
CurveSampler,
DODOSampler,
DODOV2Sampler,
GMXSampler,
KyberSampler,
KyberDmmSampler,
LidoSampler,

View File

@@ -0,0 +1,40 @@
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./interfaces/IGMX.sol";
contract GMXSampler
{
/// @dev Gas limit for UniswapV2 calls.
function sampleSellsFromGMX(
address reader,
address vault,
address[] memory path,
uint256[] memory takerTokenAmounts
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
uint256 numSamples = takerTokenAmounts.length;
makerTokenAmounts = new uint256[](numSamples);
for (uint256 i = 0; i < numSamples; i++) {
try
IGMX(reader).getAmountOut(IVault(vault), path[0], path[1], takerTokenAmounts[i])
returns (uint256 amountAfterFees, uint256 feeAmount)
{
makerTokenAmounts[i] = amountAfterFees;
// Break early if there are 0 amounts
if (makerTokenAmounts[i] == 0) {
break;
}
} catch (bytes memory) {
// Swallow failures, leaving all results as zero.
break;
}
}
}
}

View File

@@ -0,0 +1,23 @@
pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
interface IGMX {
function getMaxAmountIn(IVault _vault, address _tokenIn, address _tokenOut)
external
view
returns (uint256);
function getAmountOut(IVault _vault, address _tokenIn, address _tokenOut, uint256 _amountIn)
external
view
returns (uint256, uint256);
}
interface IVault {
function getFeeBasisPoints(address _token, uint256 _usdgDelta, uint256 _feeBasisPoints, uint256 _taxBasisPoints, bool _increment) external view returns (uint256);
function stableSwapFeeBasisPoints() external view returns (uint256);
function stableTokens(address _token) external view returns (bool);
function tokenDecimals(address _token) external view returns (uint256);
function getMaxPrice(address _token) external view returns (uint256);
function getMinPrice(address _token) external view returns (uint256);
}

View File

@@ -39,7 +39,7 @@
"config": {
"publicInterfaceContracts": "ERC20BridgeSampler,BalanceChecker,FakeTaker",
"abis:comment": "This list is auto-generated by contracts-gen. Don't edit manually.",
"abis": "./test/generated-artifacts/@(ApproximateBuys|BalanceChecker|BalancerSampler|BalancerV2Sampler|BancorSampler|CompoundSampler|CurveSampler|DODOSampler|DODOV2Sampler|DummyLiquidityProvider|ERC20BridgeSampler|FakeTaker|IBalancer|IBancor|ICurve|IKyberNetwork|IMStable|IMooniswap|IMultiBridge|IShell|ISmoothy|IUniswapExchangeQuotes|IUniswapV2Router01|KyberDmmSampler|KyberSampler|LidoSampler|LiquidityProviderSampler|MStableSampler|MakerPSMSampler|MooniswapSampler|MultiBridgeSampler|NativeOrderSampler|SamplerUtils|ShellSampler|SmoothySampler|TestERC20BridgeSampler|TestNativeOrderSampler|TwoHopSampler|UniswapSampler|UniswapV2Sampler|UniswapV3Sampler|UtilitySampler).json",
"abis": "./test/generated-artifacts/@(ApproximateBuys|BalanceChecker|BalancerSampler|BalancerV2Sampler|BancorSampler|CompoundSampler|CurveSampler|DODOSampler|DODOV2Sampler|DummyLiquidityProvider|ERC20BridgeSampler|FakeTaker|GMXSampler|IBalancer|IBancor|ICurve|IGMX|IKyberNetwork|IMStable|IMooniswap|IMultiBridge|IShell|ISmoothy|IUniswapExchangeQuotes|IUniswapV2Router01|KyberDmmSampler|KyberSampler|LidoSampler|LiquidityProviderSampler|MStableSampler|MakerPSMSampler|MooniswapSampler|MultiBridgeSampler|NativeOrderSampler|SamplerUtils|ShellSampler|SmoothySampler|TestERC20BridgeSampler|TestNativeOrderSampler|TwoHopSampler|UniswapSampler|UniswapV2Sampler|UniswapV3Sampler|UtilitySampler).json",
"postpublish": {
"assets": []
}
@@ -128,5 +128,6 @@
"publishConfig": {
"access": "public"
},
"gitHead": "4f91bfd907996b2f4dd383778b50c479c2602b56"
"gitHead": "4f91bfd907996b2f4dd383778b50c479c2602b56",
"resolutions": {}
}

View File

@@ -24,6 +24,7 @@ import {
ELLIPSIS_BSC_INFOS,
FIREBIRDONESWAP_BSC_INFOS,
FIREBIRDONESWAP_POLYGON_INFOS,
GMX_ROUTER_BY_CHAIN_ID,
IRONSWAP_POLYGON_INFOS,
JETSWAP_ROUTER_BY_CHAIN_ID,
JULSWAP_ROUTER_BY_CHAIN_ID,

View File

@@ -19,6 +19,7 @@ import {
FillData,
FinalUniswapV3FillData,
GeistFillData,
GMXFillData,
GetMarketOrdersOpts,
isFinalUniswapV3FillData,
KyberSamplerOpts,
@@ -184,6 +185,7 @@ export const SELL_SOURCE_FILTER_BY_CHAIN_ID = valueByChainId<SourceFilters>(
ERC20BridgeSource.KyberDmm,
ERC20BridgeSource.AaveV2,
ERC20BridgeSource.Synapse,
ERC20BridgeSource.GMX,
]),
[ChainId.Fantom]: new SourceFilters([
ERC20BridgeSource.MultiHop,
@@ -331,6 +333,7 @@ export const BUY_SOURCE_FILTER_BY_CHAIN_ID = valueByChainId<SourceFilters>(
ERC20BridgeSource.KyberDmm,
ERC20BridgeSource.AaveV2,
ERC20BridgeSource.Synapse,
ERC20BridgeSource.GMX,
]),
[ChainId.Fantom]: new SourceFilters([
ERC20BridgeSource.MultiHop,
@@ -2344,6 +2347,26 @@ export const SPOOKYSWAP_ROUTER_BY_CHAIN_ID = valueByChainId<string>(
},
NULL_ADDRESS,
);
export const GMX_ROUTER_BY_CHAIN_ID = valueByChainId<string>(
{
[ChainId.Avalanche]: '0x5f719c2f1095f7b9fc68a68e35b51194f4b6abe8',
},
NULL_ADDRESS,
);
export const GMX_READER_BY_CHAIN_ID = valueByChainId<string>(
{
[ChainId.Avalanche]: '0x67b789d48c926006f5132bfce4e976f0a7a63d5d',
},
NULL_ADDRESS,
);
export const GMX_VAULT_BY_CHAIN_ID = valueByChainId<string>(
{
[ChainId.Avalanche]: '0x9ab2de34a33fb459b538c43f251eb825645e8595',
},
NULL_ADDRESS,
);
export const VIP_ERC20_BRIDGE_SOURCES_BY_CHAIN_ID = valueByChainId<ERC20BridgeSource[]>(
{
@@ -2548,6 +2571,7 @@ export const DEFAULT_GAS_SCHEDULE: Required<FeeSchedule> = {
//
[ERC20BridgeSource.Pangolin]: uniswapV2CloneGasSchedule,
[ERC20BridgeSource.TraderJoe]: uniswapV2CloneGasSchedule,
[ERC20BridgeSource.GMX]: () => 300e3,
//
// Celo

View File

@@ -20,6 +20,7 @@ import {
FinalUniswapV3FillData,
GeistFillData,
GenericRouterFillData,
GMXFillData,
KyberDmmFillData,
KyberFillData,
LidoFillData,
@@ -208,6 +209,8 @@ export function getErc20BridgeSourceToBridgeSource(source: ERC20BridgeSource): s
return encodeBridgeSourceId(BridgeProtocol.AaveV2, 'Geist');
case ERC20BridgeSource.MobiusMoney:
return encodeBridgeSourceId(BridgeProtocol.Nerve, 'MobiusMoney');
case ERC20BridgeSource.GMX:
return encodeBridgeSourceId(BridgeProtocol.GMX, 'GMX');
default:
throw new Error(AggregationError.NoBridgeForSource);
}
@@ -367,6 +370,10 @@ export function createBridgeDataForBridgeOrder(order: OptimizedMarketBridgeOrder
const geistFillData = (order as OptimizedMarketBridgeOrder<GeistFillData>).fillData;
bridgeData = encoder.encode([geistFillData.lendingPool, geistFillData.gToken]);
break;
case ERC20BridgeSource.GMX:
const gmxFillData = (order as OptimizedMarketBridgeOrder<GMXFillData>).fillData;
bridgeData = encoder.encode([gmxFillData.reader, gmxFillData.vault, gmxFillData.tokenAddressPath]);
break;
default:
throw new Error(AggregationError.NoBridgeForSource);
@@ -454,6 +461,7 @@ const balancerV2Encoder = AbiEncoder.create([
const routerAddressPathEncoder = AbiEncoder.create('(address,address[])');
const tokenAddressEncoder = AbiEncoder.create([{ name: 'tokenAddress', type: 'address' }]);
export const BRIDGE_ENCODERS: {
[key in Exclude<
ERC20BridgeSource,
@@ -505,6 +513,8 @@ export const BRIDGE_ENCODERS: {
[ERC20BridgeSource.SpiritSwap]: routerAddressPathEncoder,
[ERC20BridgeSource.SpookySwap]: routerAddressPathEncoder,
[ERC20BridgeSource.MorpheusSwap]: routerAddressPathEncoder,
// Avalanche
[ERC20BridgeSource.GMX] : AbiEncoder.create('(address,address,address[])'),
// Celo
[ERC20BridgeSource.UbeSwap]: routerAddressPathEncoder,
// BSC

View File

@@ -30,6 +30,9 @@ import {
COMPOUND_API_URL_BY_CHAIN_ID,
DODOV1_CONFIG_BY_CHAIN_ID,
DODOV2_FACTORIES_BY_CHAIN_ID,
GMX_READER_BY_CHAIN_ID,
GMX_ROUTER_BY_CHAIN_ID,
GMX_VAULT_BY_CHAIN_ID,
KYBER_CONFIG_BY_CHAIN_ID,
KYBER_DMM_ROUTER_BY_CHAIN_ID,
LIDO_INFO_BY_CHAIN,
@@ -71,6 +74,8 @@ import {
GeistFillData,
GeistInfo,
GenericRouterFillData,
GMXFillData,
GMXQuoteFillData,
HopInfo,
KyberDmmFillData,
KyberFillData,
@@ -1213,6 +1218,20 @@ export class SamplerOperations {
params: [cToken, takerToken, makerToken, makerFillAmounts],
});
}
public getGMXSellQuotes(
reader: string,
vault: string,
tokenAddressPath: string[],
takerFillAmounts: BigNumber[],
): SourceQuoteOperation<GMXFillData> {
return new SamplerContractOperation({
source: ERC20BridgeSource.GMX,
fillData: {reader, vault, tokenAddressPath },
contract: this._samplerContract,
function: this._samplerContract.sampleSellsFromGMX,
params: [reader, vault, tokenAddressPath, takerFillAmounts],
});
}
public getMedianSellRate(
sources: ERC20BridgeSource[],
@@ -1605,6 +1624,15 @@ export class SamplerOperations {
takerFillAmounts,
);
}
case ERC20BridgeSource.GMX: {
return this.getGMXSellQuotes(
GMX_READER_BY_CHAIN_ID[this.chainId],
GMX_VAULT_BY_CHAIN_ID[this.chainId],
[makerToken, takerToken],
takerFillAmounts,
);
}
default:
throw new Error(`Unsupported sell sample source: ${source}`);
}

View File

@@ -94,6 +94,7 @@ export enum ERC20BridgeSource {
// Avalanche
Pangolin = 'Pangolin',
TraderJoe = 'TraderJoe',
GMX = 'GMX',
// Celo only
UbeSwap = 'UbeSwap',
MobiusMoney = 'MobiusMoney',
@@ -222,6 +223,16 @@ export interface UniswapV2FillData extends FillData {
tokenAddressPath: string[];
router: string;
}
export interface GMXQuoteFillData extends FillData {
tokenAddressPath: string[];
reader: string;
vault: string;
}
export interface GMXFillData extends FillData {
reader: string;
vault: string;
tokenAddressPath: string[];
}
export interface ShellFillData extends FillData {
poolAddress: string;

View File

@@ -17,9 +17,11 @@ import * as DODOV2Sampler from '../test/generated-artifacts/DODOV2Sampler.json';
import * as DummyLiquidityProvider from '../test/generated-artifacts/DummyLiquidityProvider.json';
import * as ERC20BridgeSampler from '../test/generated-artifacts/ERC20BridgeSampler.json';
import * as FakeTaker from '../test/generated-artifacts/FakeTaker.json';
import * as GMXSampler from '../test/generated-artifacts/GMXSampler.json';
import * as IBalancer from '../test/generated-artifacts/IBalancer.json';
import * as IBancor from '../test/generated-artifacts/IBancor.json';
import * as ICurve from '../test/generated-artifacts/ICurve.json';
import * as IGMX from '../test/generated-artifacts/IGMX.json';
import * as IKyberNetwork from '../test/generated-artifacts/IKyberNetwork.json';
import * as IMooniswap from '../test/generated-artifacts/IMooniswap.json';
import * as IMStable from '../test/generated-artifacts/IMStable.json';
@@ -59,6 +61,7 @@ export const artifacts = {
DODOV2Sampler: DODOV2Sampler as ContractArtifact,
ERC20BridgeSampler: ERC20BridgeSampler as ContractArtifact,
FakeTaker: FakeTaker as ContractArtifact,
GMXSampler: GMXSampler as ContractArtifact,
KyberDmmSampler: KyberDmmSampler as ContractArtifact,
KyberSampler: KyberSampler as ContractArtifact,
LidoSampler: LidoSampler as ContractArtifact,
@@ -79,6 +82,7 @@ export const artifacts = {
IBalancer: IBalancer as ContractArtifact,
IBancor: IBancor as ContractArtifact,
ICurve: ICurve as ContractArtifact,
IGMX: IGMX as ContractArtifact,
IKyberNetwork: IKyberNetwork as ContractArtifact,
IMStable: IMStable as ContractArtifact,
IMooniswap: IMooniswap as ContractArtifact,

View File

@@ -15,6 +15,7 @@ export * from '../test/generated-wrappers/d_o_d_o_v2_sampler';
export * from '../test/generated-wrappers/dummy_liquidity_provider';
export * from '../test/generated-wrappers/erc20_bridge_sampler';
export * from '../test/generated-wrappers/fake_taker';
export * from '../test/generated-wrappers/g_m_x_sampler';
export * from '../test/generated-wrappers/i_balancer';
export * from '../test/generated-wrappers/i_bancor';
export * from '../test/generated-wrappers/i_curve';
@@ -26,6 +27,7 @@ export * from '../test/generated-wrappers/i_shell';
export * from '../test/generated-wrappers/i_smoothy';
export * from '../test/generated-wrappers/i_uniswap_exchange_quotes';
export * from '../test/generated-wrappers/i_uniswap_v2_router01';
export * from '../test/generated-wrappers/igmx';
export * from '../test/generated-wrappers/kyber_dmm_sampler';
export * from '../test/generated-wrappers/kyber_sampler';
export * from '../test/generated-wrappers/lido_sampler';

View File

@@ -18,9 +18,11 @@
"test/generated-artifacts/DummyLiquidityProvider.json",
"test/generated-artifacts/ERC20BridgeSampler.json",
"test/generated-artifacts/FakeTaker.json",
"test/generated-artifacts/GMXSampler.json",
"test/generated-artifacts/IBalancer.json",
"test/generated-artifacts/IBancor.json",
"test/generated-artifacts/ICurve.json",
"test/generated-artifacts/IGMX.json",
"test/generated-artifacts/IKyberNetwork.json",
"test/generated-artifacts/IMStable.json",
"test/generated-artifacts/IMooniswap.json",