update to internal SAMPLE_VALUES

This commit is contained in:
Jacob Evans
2022-01-25 08:27:02 +10:00
parent 7ac97533d0
commit ee5197d474
23 changed files with 140 additions and 52 deletions

View File

@@ -24,7 +24,9 @@ import "./interfaces/IBalancer.sol";
import "./SamplerBase.sol";
contract BalancerSampler {
contract BalancerSampler is
SamplerBase
{
/// @dev Base gas limit for Balancer calls.
uint256 constant private BALANCER_CALL_GAS = 300e3; // 300k
@@ -62,7 +64,7 @@ contract BalancerSampler {
poolAddress,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -54,7 +54,10 @@ interface IAsset {
// solhint-disable-previous-line no-empty-blocks
}
contract BalancerV2Sampler is SamplerUtils {
contract BalancerV2Sampler is
SamplerBase,
SamplerUtils
{
struct BalancerV2PoolInfo {
bytes32 poolId;
@@ -79,7 +82,7 @@ contract BalancerV2Sampler is SamplerUtils {
poolInfo,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -25,7 +25,10 @@ import "./SamplerBase.sol";
contract CompilerHack {}
contract BancorSampler is CompilerHack {
contract BancorSampler is
SamplerBase,
CompilerHack
{
/// @dev Base gas limit for Bancor calls.
uint256 constant private BANCOR_CALL_GAS = 300e3; // 300k
@@ -56,7 +59,7 @@ contract BancorSampler is CompilerHack {
opts,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -33,7 +33,10 @@ interface ICToken {
function decimals() external view returns (uint8);
}
contract CompoundSampler is SamplerUtils {
contract CompoundSampler is
SamplerBase,
SamplerUtils
{
uint256 constant private EXCHANGE_RATE_SCALE = 1e10;
function sampleSellsFromCompoundGlobal(
@@ -49,7 +52,7 @@ contract CompoundSampler is SamplerUtils {
cToken,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -27,6 +27,7 @@ import "./SamplerBase.sol";
contract CurveSampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -60,7 +61,7 @@ contract CurveSampler is
curveInfo,
fromTokenIdx,
toTokenIdx,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -39,6 +39,7 @@ interface IDODO {
}
contract DODOSampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -71,7 +72,7 @@ contract DODOSampler is
opts,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -44,6 +44,7 @@ interface IDODOV2Pool {
}
contract DODOV2Sampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -75,7 +76,7 @@ contract DODOV2Sampler is
offset,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -38,7 +38,6 @@ import "./MooniswapSampler.sol";
import "./NativeOrderSampler.sol";
import "./ShellSampler.sol";
import "./SmoothySampler.sol";
import "./SamplerBase.sol";
import "./TwoHopSampler.sol";
import "./UniswapSampler.sol";
import "./UniswapV2Sampler.sol";
@@ -63,7 +62,6 @@ contract ERC20BridgeSampler is
MooniswapSampler,
MultiBridgeSampler,
NativeOrderSampler,
SamplerBase,
ShellSampler,
SmoothySampler,
TwoHopSampler,

View File

@@ -55,7 +55,8 @@ interface IKyberDmmRouter {
contract KyberDmmSampler
contract KyberDmmSampler is
SamplerBase
{
/// @dev Gas limit for KyberDmm calls.
uint256 constant private KYBER_DMM_CALL_GAS = 150e3; // 150k
@@ -77,7 +78,7 @@ contract KyberDmmSampler
(pools, makerTokenAmounts) = this.sampleSellsFromKyberDmm(
router,
path,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}
/// @dev Sample sell quotes from KyberDmm.

View File

@@ -27,6 +27,7 @@ import "./SamplerBase.sol";
contract KyberSampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -63,7 +64,7 @@ contract KyberSampler is
opts,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -21,13 +21,40 @@ pragma solidity ^0.6;
pragma experimental ABIEncoderV2;
import "./SamplerUtils.sol";
import "./SamplerBase.sol";
contract LidoSampler is SamplerUtils {
contract LidoSampler is
SamplerBase,
SamplerUtils
{
struct LidoInfo {
address stEthToken;
address wethToken;
}
/// @dev Sample sell quotes from Lido
/// @param lidoInfo Info regarding a specific Lido deployment
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromLidoGlobal(
LidoInfo memory lidoInfo,
address takerToken,
address makerToken
)
public
returns (uint256[] memory makerTokenAmounts)
{
makerTokenAmounts = this.sampleSellsFromLido(
lidoInfo,
takerToken,
makerToken,
SAMPLE_VALUES
);
}
/// @dev Sample sell quotes from Lido
/// @param lidoInfo Info regarding a specific Lido deployment
/// @param takerToken Address of the taker token (what to sell).

View File

@@ -24,15 +24,40 @@ import "@0x/contracts-utils/contracts/src/v06/LibBytesV06.sol";
import "@0x/contracts-zero-ex/contracts/src/vendor/ILiquidityProvider.sol";
import "./ApproximateBuys.sol";
import "./SamplerUtils.sol";
import "./SamplerBase.sol";
contract LiquidityProviderSampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
/// @dev Default gas limit for liquidity provider calls.
uint256 constant private DEFAULT_CALL_GAS = 400e3; // 400k
/// @dev Sample sell quotes from an arbitrary on-chain liquidity provider.
/// @param providerAddress Address of the liquidity provider.
/// @param takerToken Address of the taker token (what to sell).
/// @param makerToken Address of the maker token (what to buy).
/// @return makerTokenAmounts Maker amounts bought at each taker token
/// amount.
function sampleSellsFromLiquidityProviderGlobal(
address providerAddress,
address takerToken,
address makerToken
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
makerTokenAmounts = this.sampleSellsFromLiquidityProvider(
providerAddress,
takerToken,
makerToken,
SAMPLE_VALUES
);
}
/// @dev Sample sell quotes from an arbitrary on-chain liquidity provider.
/// @param providerAddress Address of the liquidity provider.
/// @param takerToken Address of the taker token (what to sell).

View File

@@ -27,6 +27,7 @@ import "./SamplerBase.sol";
contract MStableSampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -52,7 +53,7 @@ contract MStableSampler is
router,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}
/// @dev Sample sell quotes from the mStable contract

View File

@@ -22,6 +22,7 @@ pragma experimental ABIEncoderV2;
import "./SamplerUtils.sol";
import "@0x/contracts-utils/contracts/src/v06/LibMathV06.sol";
import "./SamplerBase.sol";
interface IPSM {
// @dev Get the fee for selling USDC to DAI in PSM
@@ -80,6 +81,7 @@ interface IVAT {
}
contract MakerPSMSampler is
SamplerBase,
SamplerUtils
{
using LibSafeMathV06 for uint256;
@@ -104,6 +106,24 @@ contract MakerPSMSampler is
uint256 constant private RAD = 10 ** 45;
// See https://github.com/makerdao/dss/blob/master/DEVELOPING.m
/// @dev Sample sell quotes from Maker PSM
function sampleSellsFromMakerPsmGlobal(
MakerPsmInfo memory psmInfo,
address takerToken,
address makerToken
)
public
view
returns (uint256[] memory makerTokenAmounts)
{
makerTokenAmounts = this.sampleSellsFromMakerPsm(
psmInfo,
takerToken,
makerToken,
SAMPLE_VALUES
);
}
/// @dev Sample sell quotes from Maker PSM
function sampleSellsFromMakerPsm(
MakerPsmInfo memory psmInfo,

View File

@@ -27,6 +27,7 @@ import "./SamplerBase.sol";
contract MooniswapSampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -53,7 +54,7 @@ contract MooniswapSampler is
registry,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -23,15 +23,7 @@ pragma experimental ABIEncoderV2;
contract SamplerBase
{
/// @dev Stored Sample values for each Sampler to pull
uint256[] private SAMPLE_VALUES = new uint256[](0);
function getSampleValues()
public
view
returns (uint256[] memory sampleValues)
{
sampleValues = SAMPLE_VALUES;
}
uint256[] internal SAMPLE_VALUES = new uint256[](0);
function setSampleValues(uint256[] memory sampleValues)
public

View File

@@ -27,6 +27,7 @@ import "./SamplerBase.sol";
contract ShellSampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -57,7 +58,7 @@ contract ShellSampler is
pool,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -27,6 +27,7 @@ import "./interfaces/ISmoothy.sol";
import "./SamplerBase.sol";
contract SmoothySampler is
SamplerBase,
SamplerUtils,
ApproximateBuys
{
@@ -59,7 +60,7 @@ contract SmoothySampler is
smoothyInfo,
fromTokenIdx,
toTokenIdx,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -37,6 +37,7 @@ interface IUniswapExchangeFactory {
contract UniswapSampler is
SamplerBase,
SamplerUtils
{
/// @dev Gas limit for Uniswap calls.
@@ -61,7 +62,7 @@ contract UniswapSampler is
router,
takerToken,
makerToken,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}
/// @dev Sample sell quotes from Uniswap.

View File

@@ -24,7 +24,8 @@ import "./interfaces/IUniswapV2Router01.sol";
import "./SamplerBase.sol";
contract UniswapV2Sampler
contract UniswapV2Sampler is
SamplerBase
{
/// @dev Gas limit for UniswapV2 calls.
uint256 constant private UNISWAPV2_CALL_GAS = 150e3; // 150k
@@ -45,7 +46,7 @@ contract UniswapV2Sampler
makerTokenAmounts = this.sampleSellsFromUniswapV2(
router,
path,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}

View File

@@ -49,7 +49,8 @@ interface IUniswapV3Pool {
function fee() external view returns (uint24);
}
contract UniswapV3Sampler
contract UniswapV3Sampler is
SamplerBase
{
/// @dev Gas limit for UniswapV3 calls. This is 100% a guess.
uint256 constant private QUOTE_GAS = 600e3;
@@ -73,7 +74,7 @@ contract UniswapV3Sampler
(uniswapPaths, makerTokenAmounts) = this.sampleSellsFromUniswapV3(
quoter,
path,
SamplerBase(address(this)).getSampleValues()
SAMPLE_VALUES
);
}
/// @dev Sample sell quotes from UniswapV3.

View File

@@ -1,7 +1,6 @@
import { FillQuoteTransformerOrderType, RfqOrder } from '@0x/protocol-utils';
import { BigNumber, NULL_ADDRESS } from '@0x/utils';
import * as _ from 'lodash';
import { sample } from 'lodash';
import { DEFAULT_INFO_LOGGER, INVALID_SIGNATURE } from '../../constants';
import {
@@ -165,6 +164,7 @@ export class MarketOperationUtils {
// Get native order fillable amounts.
this._sampler.getLimitOrderFillableTakerAmounts(nativeOrders, this.contractAddresses.exchangeProxy),
// Get ETH -> maker token price.
// Set the global sample value to discover the network fee token in maker token
this._sampler.setSampleValues([this._nativeFeeTokenAmount]),
this._sampler.getMedianSellRate(
feeSourceFilters.sources,
@@ -173,6 +173,7 @@ export class MarketOperationUtils {
this._nativeFeeTokenAmount,
),
// Get ETH -> taker token price.
// Set the global sample value to discover the network fee token in taker token
this._sampler.getMedianSellRate(
feeSourceFilters.sources,
takerToken,
@@ -180,8 +181,10 @@ export class MarketOperationUtils {
this._nativeFeeTokenAmount,
),
// Get sell quotes for taker -> maker.
// Set the global sample values to the increasing sized samples up to takerAmount
this._sampler.setSampleValues(sampleAmounts),
this._sampler.getSellQuotes(quoteSourceFilters.sources, makerToken, takerToken, sampleAmounts),
// Set the global sample values to just the entire amount for MultiHop
this._sampler.setSampleValues([takerAmount]),
this._sampler.getTwoHopSellQuotes(
quoteSourceFilters.isAllowed(ERC20BridgeSource.MultiHop) ? quoteSourceFilters.sources : [],
@@ -202,12 +205,12 @@ export class MarketOperationUtils {
gasBefore,
tokenDecimals,
orderFillableTakerAmounts,
_setSample1,
_setFeeTokenSample,
outputAmountPerEth,
inputAmountPerEth,
_setSample2,
_setSampleValues,
dexQuotes,
_setSample3,
_setMultiHopSample,
rawTwoHopQuotes,
isTxOriginContract,
gasAfter,

View File

@@ -188,7 +188,7 @@ export class SamplerOperations {
handleCallResults: (_callResults: string) => true,
handleRevert: () => {
/* should never happen */
throw new Error('Invalid address for isAddressContract');
throw new Error('Invalid result for setting sample values');
},
};
}
@@ -421,7 +421,7 @@ export class SamplerOperations {
providerAddress: string,
makerToken: string,
takerToken: string,
takerFillAmounts: BigNumber[],
_takerFillAmounts: BigNumber[],
gasCost: number,
source: ERC20BridgeSource = ERC20BridgeSource.LiquidityProvider,
): SourceQuoteOperation<LiquidityProviderFillData> {
@@ -432,8 +432,8 @@ export class SamplerOperations {
gasCost,
},
contract: this._samplerContract,
function: this._samplerContract.sampleSellsFromLiquidityProvider,
params: [providerAddress, takerToken, makerToken, takerFillAmounts],
function: this._samplerContract.sampleSellsFromLiquidityProviderGlobal,
params: [providerAddress, takerToken, makerToken],
});
}
@@ -936,15 +936,15 @@ export class SamplerOperations {
poolAddress: string,
makerToken: string,
takerToken: string,
takerFillAmounts: BigNumber[],
_takerFillAmounts: BigNumber[],
source: ERC20BridgeSource = ERC20BridgeSource.Shell,
): SourceQuoteOperation<ShellFillData> {
return new SamplerContractOperation({
source,
fillData: { poolAddress },
contract: this._samplerContract,
function: this._samplerContract.sampleSellsFromShell,
params: [poolAddress, takerToken, makerToken, takerFillAmounts],
function: this._samplerContract.sampleSellsFromShellGlobal,
params: [poolAddress, takerToken, makerToken],
});
}
@@ -1060,7 +1060,7 @@ export class SamplerOperations {
psmInfo: PsmInfo,
makerToken: string,
takerToken: string,
takerFillAmounts: BigNumber[],
_takerFillAmounts: BigNumber[],
): SourceQuoteOperation<MakerPsmFillData> {
return new SamplerContractOperation({
source: ERC20BridgeSource.MakerPsm,
@@ -1071,8 +1071,8 @@ export class SamplerOperations {
...psmInfo,
},
contract: this._samplerContract,
function: this._samplerContract.sampleSellsFromMakerPsm,
params: [psmInfo, takerToken, makerToken, takerFillAmounts],
function: this._samplerContract.sampleSellsFromMakerPsmGlobal,
params: [psmInfo, takerToken, makerToken],
});
}
@@ -1100,7 +1100,7 @@ export class SamplerOperations {
lidoInfo: LidoInfo,
makerToken: string,
takerToken: string,
takerFillAmounts: BigNumber[],
_takerFillAmounts: BigNumber[],
): SourceQuoteOperation<LidoFillData> {
return new SamplerContractOperation({
source: ERC20BridgeSource.Lido,
@@ -1109,8 +1109,8 @@ export class SamplerOperations {
stEthTokenAddress: lidoInfo.stEthToken,
},
contract: this._samplerContract,
function: this._samplerContract.sampleSellsFromLido,
params: [lidoInfo, takerToken, makerToken, takerFillAmounts],
function: this._samplerContract.sampleSellsFromLidoGlobal,
params: [lidoInfo, takerToken, makerToken],
});
}