fix: [asset-swapper] disable off-chain sampling for Balancer and CREAM (#41)

* fix: [asset-swapper] disable off-chain sampling for Balancer and CREAM

* Modify  Balancer off-chain test to expect only pools

* CHANGELOG
This commit is contained in:
Jacob Evans 2020-11-19 15:00:09 +10:00 committed by GitHub
parent 927fe2b58b
commit 048d8dee60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 61 additions and 67 deletions

View File

@ -9,6 +9,10 @@
{
"note": "Return the maker/taker token decimals from the sampler as part of the `SwapQuote`",
"pr": 34
},
{
"note": "Disable off-chain sampling for Balancer and CREAM",
"pr": 41
}
]
},

View File

@ -5,7 +5,7 @@ import * as _ from 'lodash';
import { ERC20BridgeSamplerContract } from '../../wrappers';
import { BalancerPoolsCache, computeBalancerBuyQuote, computeBalancerSellQuote } from './balancer_utils';
import { BalancerPoolsCache } from './balancer_utils';
import { BancorService } from './bancor_service';
import { LIQUIDITY_PROVIDER_REGISTRY, MAINNET_SUSHI_SWAP_ROUTER, MAX_UINT256, ZERO_AMOUNT } from './constants';
import { CreamPoolsCache } from './cream_utils';
@ -480,65 +480,74 @@ export class SamplerOperations {
public async getBalancerSellQuotesOffChainAsync(
makerToken: string,
takerToken: string,
takerFillAmounts: BigNumber[],
_takerFillAmounts: BigNumber[],
): Promise<Array<Array<DexSample<BalancerFillData>>>> {
const pools = await this.balancerPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return pools.map(pool =>
takerFillAmounts.map(amount => ({
source: ERC20BridgeSource.Balancer,
output: computeBalancerSellQuote(pool, amount),
input: amount,
fillData: { poolAddress: pool.id },
})),
);
// Prime the cache but do not sample off chain
await this.balancerPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return [];
// return pools.map(pool =>
// takerFillAmounts.map(amount => ({
// source: ERC20BridgeSource.Balancer,
// output: computeBalancerSellQuote(pool, amount),
// input: amount,
// fillData: { poolAddress: pool.id },
// })),
// );
}
public async getBalancerBuyQuotesOffChainAsync(
makerToken: string,
takerToken: string,
makerFillAmounts: BigNumber[],
_makerFillAmounts: BigNumber[],
): Promise<Array<Array<DexSample<BalancerFillData>>>> {
const pools = await this.balancerPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return pools.map(pool =>
makerFillAmounts.map(amount => ({
source: ERC20BridgeSource.Balancer,
output: computeBalancerBuyQuote(pool, amount),
input: amount,
fillData: { poolAddress: pool.id },
})),
);
// Prime the pools but do not sample off chain
// Prime the cache but do not sample off chain
await this.balancerPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return [];
// return pools.map(pool =>
// makerFillAmounts.map(amount => ({
// source: ERC20BridgeSource.Balancer,
// output: computeBalancerBuyQuote(pool, amount),
// input: amount,
// fillData: { poolAddress: pool.id },
// })),
// );
}
public async getCreamSellQuotesOffChainAsync(
makerToken: string,
takerToken: string,
takerFillAmounts: BigNumber[],
_takerFillAmounts: BigNumber[],
): Promise<Array<Array<DexSample<BalancerFillData>>>> {
const pools = await this.creamPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return pools.map(pool =>
takerFillAmounts.map(amount => ({
source: ERC20BridgeSource.Cream,
output: computeBalancerSellQuote(pool, amount),
input: amount,
fillData: { poolAddress: pool.id },
})),
);
// Prime the cache but do not sample off chain
await this.creamPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return [];
// return pools.map(pool =>
// takerFillAmounts.map(amount => ({
// source: ERC20BridgeSource.Cream,
// output: computeBalancerSellQuote(pool, amount),
// input: amount,
// fillData: { poolAddress: pool.id },
// })),
// );
}
public async getCreamBuyQuotesOffChainAsync(
makerToken: string,
takerToken: string,
makerFillAmounts: BigNumber[],
_makerFillAmounts: BigNumber[],
): Promise<Array<Array<DexSample<BalancerFillData>>>> {
const pools = await this.creamPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return pools.map(pool =>
makerFillAmounts.map(amount => ({
source: ERC20BridgeSource.Cream,
output: computeBalancerBuyQuote(pool, amount),
input: amount,
fillData: { poolAddress: pool.id },
})),
);
// Prime the cache but do not sample off chain
await this.creamPoolsCache.getPoolsForPairAsync(takerToken, makerToken);
return [];
// return pools.map(pool =>
// makerFillAmounts.map(amount => ({
// source: ERC20BridgeSource.Cream,
// output: computeBalancerBuyQuote(pool, amount),
// input: amount,
// fillData: { poolAddress: pool.id },
// })),
// );
}
public getMStableSellQuotes(

View File

@ -12,11 +12,7 @@ import { SignedOrder } from '@0x/types';
import { BigNumber, hexUtils } from '@0x/utils';
import * as _ from 'lodash';
import {
BalancerPool,
computeBalancerBuyQuote,
computeBalancerSellQuote,
} from '../src/utils/market_operation_utils/balancer_utils';
import { BalancerPool } from '../src/utils/market_operation_utils/balancer_utils';
import { DexOrderSampler, getSampleAmounts } from '../src/utils/market_operation_utils/sampler';
import { ERC20BridgeSource, TokenAdjacencyGraph } from '../src/utils/market_operation_utils/types';
@ -425,7 +421,10 @@ describe('DexSampler tests', () => {
expect(quotes).to.have.lengthOf(sources.length + additionalSourceCount);
expect(quotes).to.deep.eq(expectedQuotes.concat(uniswapV2ETHQuotes));
});
it('getSellQuotes() uses samples from Balancer', async () => {
it('getSellQuotes() fetches pools but not samples from Balancer', async () => {
// HACK
// We disabled the off-chain sampling due to incorrect data observed between
// on-chain and off-chain sampling
const expectedTakerToken = randomAddress();
const expectedMakerToken = randomAddress();
const expectedTakerFillAmounts = getSampleAmounts(new BigNumber(100e18), 3);
@ -448,16 +447,7 @@ describe('DexSampler tests', () => {
expectedTakerToken,
expectedTakerFillAmounts,
);
const expectedQuotes = pools.map(p =>
expectedTakerFillAmounts.map(a => ({
source: ERC20BridgeSource.Balancer,
input: a,
output: computeBalancerSellQuote(p, a),
fillData: { poolAddress: p.id },
})),
);
expect(quotes).to.have.lengthOf(2); // one array per pool
expect(quotes).to.deep.eq(expectedQuotes);
expect(quotes).to.have.lengthOf(0);
});
it('getSellQuotes() uses samples from Bancor', async () => {
const expectedTakerToken = randomAddress();
@ -593,16 +583,7 @@ describe('DexSampler tests', () => {
expectedTakerToken,
expectedMakerFillAmounts,
);
const expectedQuotes = pools.map(p =>
expectedMakerFillAmounts.map(a => ({
source: ERC20BridgeSource.Balancer,
input: a,
output: computeBalancerBuyQuote(p, a),
fillData: { poolAddress: p.id },
})),
);
expect(quotes).to.have.lengthOf(2); // one set per pool
expect(quotes).to.deep.eq(expectedQuotes);
expect(quotes).to.have.lengthOf(0);
});
});