fix: 1 base unit output amount being scale down, rounding to 0 output (#422)

* fix: 1 base unit output amount being scale down, rounding to 0 output

* chore: add asset-swapper changelog entry

* fix: round scaled output to base units before clamping
This commit is contained in:
Kim Persson
2022-02-14 13:54:53 +01:00
committed by GitHub
parent f6edbd210c
commit 5d21af1a0a
2 changed files with 23 additions and 2 deletions

View File

@@ -1,4 +1,13 @@
[
{
"version": "16.49.5",
"changes": [
{
"note": "Fix scaling 1 base unit to 0, round output to base units",
"pr": 422
}
]
},
{
"version": "16.49.4",
"changes": [

View File

@@ -6,7 +6,7 @@ import * as _ from 'lodash';
import { performance } from 'perf_hooks';
import { MarketOperation, NativeOrderWithFillableAmounts } from '../../types';
import { VIP_ERC20_BRIDGE_SOURCES_BY_CHAIN_ID } from '../market_operation_utils/constants';
import { VIP_ERC20_BRIDGE_SOURCES_BY_CHAIN_ID, ZERO_AMOUNT } from '../market_operation_utils/constants';
import { dexSamplesToFills, ethToOutputAmount, nativeOrdersToFills } from './fills';
import { DEFAULT_PATH_PENALTY_OPTS, Path, PathPenaltyOpts } from './path';
@@ -274,7 +274,19 @@ function findRoutesAndCreateOptimalPath(
// TODO(kimpers): remove once we have solved the rounding/precision loss issues in the Rust router
const maxSampledOutput = BigNumber.max(...routeSamples.map(s => s.output));
const scaleOutput = (output: BigNumber) => BigNumber.min(output.times(scale), maxSampledOutput);
// Scale output by scale factor but never go above the largest sample (unknown liquidity) or below 1 base unit (unfillable)
const scaleOutput = (output: BigNumber) => {
// Don't try to scale 0 output as it will be clamped to 1
if (output.eq(ZERO_AMOUNT)) {
return output;
}
const scaled = output
.times(scale)
.decimalPlaces(0, side === MarketOperation.Sell ? BigNumber.ROUND_FLOOR : BigNumber.ROUND_CEIL);
return BigNumber.max(BigNumber.min(scaled, maxSampledOutput), 1);
};
adjustedFills.push({
...fill,