Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-08-23 11:43:25 +02:00
parent 7321df31be
commit 363d7a306c
3 changed files with 39 additions and 16 deletions

View File

@ -174,15 +174,21 @@ export class HealthCache {
private static logHealthCache(debug: string, healthCache: HealthCache) { private static logHealthCache(debug: string, healthCache: HealthCache) {
console.log(debug); console.log(debug);
for (const token of healthCache.tokenInfos.sort( for (const token of healthCache.tokenInfos) {
(a, b) => a.tokenIndex - b.tokenIndex,
)) {
console.log(`${token.toString()}`); console.log(`${token.toString()}`);
} }
console.log( console.log(
`assets ${healthCache.assets(HealthType.init)}, liabs ${healthCache.liabs( ` assets ${healthCache.assets(
HealthType.init, HealthType.init,
)}, `, )}, liabs ${healthCache.liabs(HealthType.init)}, `,
);
console.log(
` health(HealthType.init) ${healthCache.health(HealthType.init)}`,
);
console.log(
` healthRatio(HealthType.init) ${healthCache.healthRatio(
HealthType.init,
)}`,
); );
} }
@ -249,7 +255,6 @@ export class HealthCache {
const healthCacheClone: HealthCache = _.cloneDeep(this); const healthCacheClone: HealthCache = _.cloneDeep(this);
const sourceIndex = healthCacheClone.getOrCreateTokenInfoIndex(sourceBank); const sourceIndex = healthCacheClone.getOrCreateTokenInfoIndex(sourceBank);
const targetIndex = healthCacheClone.getOrCreateTokenInfoIndex(targetBank); const targetIndex = healthCacheClone.getOrCreateTokenInfoIndex(targetBank);
const source = healthCacheClone.tokenInfos[sourceIndex]; const source = healthCacheClone.tokenInfos[sourceIndex];
const target = healthCacheClone.tokenInfos[targetIndex]; const target = healthCacheClone.tokenInfos[targetIndex];
@ -261,10 +266,12 @@ export class HealthCache {
function cacheAfterSwap(amount: I80F48) { function cacheAfterSwap(amount: I80F48) {
const adjustedCache: HealthCache = _.cloneDeep(healthCacheClone); const adjustedCache: HealthCache = _.cloneDeep(healthCacheClone);
// HealthCache.logHealthCache('beforeSwap', adjustedCache);
adjustedCache.tokenInfos[sourceIndex].balance = adjustedCache.tokenInfos[sourceIndex].balance =
adjustedCache.tokenInfos[sourceIndex].balance.sub(amount); adjustedCache.tokenInfos[sourceIndex].balance.sub(amount);
adjustedCache.tokenInfos[targetIndex].balance = adjustedCache.tokenInfos[targetIndex].balance =
adjustedCache.tokenInfos[targetIndex].balance.add(amount); adjustedCache.tokenInfos[targetIndex].balance.add(amount);
// HealthCache.logHealthCache('afterSwap', adjustedCache);
return adjustedCache; return adjustedCache;
} }
@ -278,10 +285,12 @@ export class HealthCache {
const point1Amount = source.balance const point1Amount = source.balance
.max(target.balance.neg()) .max(target.balance.neg())
.max(ZERO_I80F48); .max(ZERO_I80F48);
const point0Ratio = healthRatioAfterSwap(point0Amount); const cache0 = cacheAfterSwap(point0Amount);
const cache = cacheAfterSwap(point1Amount); const point0Ratio = cache0.healthRatio(HealthType.init);
const point1Ratio = cache.healthRatio(HealthType.init); const point0Health = cache0.health(HealthType.init);
const point1Health = cache.health(HealthType.init); const cache1 = cacheAfterSwap(point1Amount);
const point1Ratio = cache1.healthRatio(HealthType.init);
const point1Health = cache1.health(HealthType.init);
function binaryApproximationSearch( function binaryApproximationSearch(
left: I80F48, left: I80F48,
@ -292,7 +301,7 @@ export class HealthCache {
) { ) {
const maxIterations = 20; const maxIterations = 20;
// TODO: make relative to health ratio decimals? Might be over engineering // TODO: make relative to health ratio decimals? Might be over engineering
const targetError = I80F48.fromString('0.001'); // ONE_I80F48; const targetError = I80F48.fromString('0.001');
if ( if (
(leftRatio.sub(targetRatio).isPos() && (leftRatio.sub(targetRatio).isPos() &&
@ -356,7 +365,15 @@ export class HealthCache {
const zeroHealthAmount = point1Amount.add( const zeroHealthAmount = point1Amount.add(
point1Health.div(source.initLiabWeight.sub(target.initAssetWeight)), point1Health.div(source.initLiabWeight.sub(target.initAssetWeight)),
); );
// console.log(`point1Amount ${point1Amount}`);
// console.log(`point1Health ${point1Health}`);
// console.log(`point1Ratio ${point1Ratio}`);
// console.log(`point0Amount ${point0Amount}`);
// console.log(`point0Health ${point0Health}`);
// console.log(`point0Ratio ${point0Ratio}`);
// console.log(`zeroHealthAmount ${zeroHealthAmount}`);
const zeroHealthRatio = healthRatioAfterSwap(zeroHealthAmount); const zeroHealthRatio = healthRatioAfterSwap(zeroHealthAmount);
// console.log(`zeroHealthRatio ${zeroHealthRatio}`);
amount = binaryApproximationSearch( amount = binaryApproximationSearch(
point1Amount, point1Amount,
point1Ratio, point1Ratio,

View File

@ -317,7 +317,7 @@ export class MangoAccount {
group, group,
sourceMintPk, sourceMintPk,
targetMintPk, targetMintPk,
ONE_I80F48, // target 1% health ZERO_I80F48, // target 1% health
) )
.mul(I80F48.fromNumber(slippageAndFeesFactor)); .mul(I80F48.fromNumber(slippageAndFeesFactor));
} }

View File

@ -139,6 +139,7 @@ async function debugUser(
}); });
function getMaxSourceForTokenSwapWrapper(src, tgt) { function getMaxSourceForTokenSwapWrapper(src, tgt) {
// console.log();
console.log( console.log(
`getMaxSourceForTokenSwap ${src.padEnd(4)} ${tgt.padEnd(4)} ` + `getMaxSourceForTokenSwap ${src.padEnd(4)} ${tgt.padEnd(4)} ` +
mangoAccount mangoAccount
@ -146,7 +147,7 @@ async function debugUser(
group, group,
group.banksMapByName.get(src)[0].mint, group.banksMapByName.get(src)[0].mint,
group.banksMapByName.get(tgt)[0].mint, group.banksMapByName.get(tgt)[0].mint,
0.9, 1,
) )
.div( .div(
I80F48.fromNumber( I80F48.fromNumber(
@ -156,8 +157,13 @@ async function debugUser(
.toNumber(), .toNumber(),
); );
} }
getMaxSourceForTokenSwapWrapper('SOL', 'BTC'); for (const srcToken of Array.from(group.banksMapByName.keys())) {
getMaxSourceForTokenSwapWrapper('USDC', 'USDC'); for (const tgtToken of Array.from(group.banksMapByName.keys())) {
// if (srcToken === 'SOL')
// if (tgtToken === 'MSOL')
getMaxSourceForTokenSwapWrapper(srcToken, tgtToken);
}
}
} }
async function main() { async function main() {