reduce max source swap targetError so that we dont error out

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-08-12 11:08:08 +02:00
parent a484aed42b
commit 261c856bb2
2 changed files with 7 additions and 4 deletions

View File

@ -695,7 +695,7 @@ impl HealthCache {
mut right_ratio: I80F48,
target_ratio: I80F48| {
let max_iterations = 20;
let target_error = I80F48::ONE;
let target_error = I80F48!(0.01);
require_msg!(
(left_ratio - target_ratio).signum() * (right_ratio - target_ratio).signum()
!= I80F48::ONE,

View File

@ -254,7 +254,8 @@ export class HealthCache {
targetRatio: I80F48,
) {
const maxIterations = 20;
const targetError = I80F48.fromString('0.000001'); // ONE_I80F48;
// TODO: make relative to health ratio decimals? Might be over engineering
const targetError = I80F48.fromString('0.001'); // ONE_I80F48;
if (
(leftRatio.sub(targetRatio).isPos() &&
@ -267,8 +268,9 @@ export class HealthCache {
);
}
let newAmount;
for (const key of Array(maxIterations).fill(0).keys()) {
const newAmount = left.add(right).mul(I80F48.fromString('0.5'));
newAmount = left.add(right).mul(I80F48.fromString('0.5'));
const newAmountRatio = healthRatioAfterSwap(newAmount);
const error = newAmountRatio.sub(targetRatio);
if (error.isPos() && error.lt(targetError)) {
@ -281,9 +283,10 @@ export class HealthCache {
rightRatio = newAmountRatio;
}
}
throw new Error(
console.error(
`Unable to get targetRatio within ${maxIterations} iterations`,
);
return newAmount;
}
let amount: I80F48;