Fix some bugs, tidy up
This commit is contained in:
parent
62bdea83ef
commit
a31167ae9c
|
@ -473,12 +473,11 @@ async function liquidateAccount(
|
||||||
);
|
);
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
await sleep(interval * 2);
|
|
||||||
}
|
|
||||||
await liqee.reload(connection, mangoGroup.dexProgramId);
|
await liqee.reload(connection, mangoGroup.dexProgramId);
|
||||||
if (!liqee.isLiquidatable(mangoGroup, cache)) {
|
if (!liqee.isLiquidatable(mangoGroup, cache)) {
|
||||||
throw new Error('Account no longer liquidatable');
|
throw new Error('Account no longer liquidatable');
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
for (let r = 0; r < 5 && liqee.hasAnySpotOrders(); r++) {
|
for (let r = 0; r < 5 && liqee.hasAnySpotOrders(); r++) {
|
||||||
for (let i = 0; i < mangoGroup.spotMarkets.length; i++) {
|
for (let i = 0; i < mangoGroup.spotMarkets.length; i++) {
|
||||||
|
@ -517,45 +516,30 @@ async function liquidateAccount(
|
||||||
healthComponents.quote,
|
healthComponents.quote,
|
||||||
'Maint',
|
'Maint',
|
||||||
);
|
);
|
||||||
const initHealths = liqee.getHealthsFromComponents(
|
|
||||||
mangoGroup,
|
|
||||||
cache,
|
|
||||||
healthComponents.spot,
|
|
||||||
healthComponents.perps,
|
|
||||||
healthComponents.quote,
|
|
||||||
'Init',
|
|
||||||
);
|
|
||||||
|
|
||||||
let shouldLiquidateSpot = false;
|
let shouldLiquidateSpot = false;
|
||||||
for (let i = 0; i < mangoGroup.tokens.length; i++) {
|
for (let i = 0; i < mangoGroup.tokens.length; i++) {
|
||||||
shouldLiquidateSpot = liqee.getNet(cache.rootBankCache[i], i).isNeg();
|
if (liqee.getNet(cache.rootBankCache[i], i).isNeg()) {
|
||||||
|
shouldLiquidateSpot = true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
const shouldLiquidatePerps =
|
|
||||||
maintHealths.perp.lt(ZERO_I80F48) ||
|
|
||||||
(initHealths.perp.lt(ZERO_I80F48) && liqee.beingLiquidated);
|
|
||||||
|
|
||||||
if (shouldLiquidateSpot) {
|
if (shouldLiquidateSpot) {
|
||||||
await liquidateSpot(
|
await liquidateSpot(
|
||||||
mangoGroup,
|
mangoGroup,
|
||||||
cache,
|
cache,
|
||||||
spotMarkets,
|
|
||||||
perpMarkets,
|
perpMarkets,
|
||||||
rootBanks,
|
rootBanks,
|
||||||
liqee,
|
liqee,
|
||||||
liqor,
|
liqor,
|
||||||
);
|
);
|
||||||
|
await liqee.reload(connection, mangoGroup.dexProgramId);
|
||||||
|
if (!liqee.isLiquidatable(mangoGroup, cache)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (shouldLiquidatePerps) {
|
await liquidatePerps(mangoGroup, cache, perpMarkets, rootBanks, liqee, liqor);
|
||||||
await liquidatePerps(
|
|
||||||
mangoGroup,
|
|
||||||
cache,
|
|
||||||
perpMarkets,
|
|
||||||
rootBanks,
|
|
||||||
liqee,
|
|
||||||
liqor,
|
|
||||||
);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!shouldLiquidateSpot &&
|
!shouldLiquidateSpot &&
|
||||||
|
@ -577,7 +561,6 @@ async function liquidateAccount(
|
||||||
async function liquidateSpot(
|
async function liquidateSpot(
|
||||||
mangoGroup: MangoGroup,
|
mangoGroup: MangoGroup,
|
||||||
cache: MangoCache,
|
cache: MangoCache,
|
||||||
spotMarkets: Market[],
|
|
||||||
perpMarkets: PerpMarket[],
|
perpMarkets: PerpMarket[],
|
||||||
rootBanks: (RootBank | undefined)[],
|
rootBanks: (RootBank | undefined)[],
|
||||||
liqee: MangoAccount,
|
liqee: MangoAccount,
|
||||||
|
@ -922,6 +905,40 @@ function getDiffsAndNet(
|
||||||
return { diffs, netValues };
|
return { diffs, netValues };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function balanceAccount(
|
||||||
|
mangoGroup: MangoGroup,
|
||||||
|
mangoAccount: MangoAccount,
|
||||||
|
mangoCache: MangoCache,
|
||||||
|
spotMarkets: Market[],
|
||||||
|
perpMarkets: PerpMarket[],
|
||||||
|
) {
|
||||||
|
const { diffs, netValues } = getDiffsAndNet(
|
||||||
|
mangoGroup,
|
||||||
|
mangoAccount,
|
||||||
|
mangoCache,
|
||||||
|
);
|
||||||
|
const tokensUnbalanced = netValues.some(
|
||||||
|
(nv) => Math.abs(diffs[nv[0]].toNumber()) > spotMarkets[nv[0]].minOrderSize,
|
||||||
|
);
|
||||||
|
const positionsUnbalanced = perpMarkets.some((pm) => {
|
||||||
|
const index = mangoGroup.getPerpMarketIndex(pm.publicKey);
|
||||||
|
const perpAccount = mangoAccount.perpAccounts[index];
|
||||||
|
const basePositionSize = Math.abs(
|
||||||
|
pm.baseLotsToNumber(perpAccount.basePosition),
|
||||||
|
);
|
||||||
|
|
||||||
|
return basePositionSize != 0 || perpAccount.quotePosition.gt(ZERO_I80F48);
|
||||||
|
});
|
||||||
|
|
||||||
|
if (tokensUnbalanced) {
|
||||||
|
await balanceTokens(mangoGroup, mangoAccount, spotMarkets);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (positionsUnbalanced) {
|
||||||
|
await closePositions(mangoGroup, mangoAccount, perpMarkets);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
async function balanceTokens(
|
async function balanceTokens(
|
||||||
mangoGroup: MangoGroup,
|
mangoGroup: MangoGroup,
|
||||||
mangoAccount: MangoAccount,
|
mangoAccount: MangoAccount,
|
||||||
|
@ -1061,40 +1078,6 @@ async function balanceTokens(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async function balanceAccount(
|
|
||||||
mangoGroup: MangoGroup,
|
|
||||||
mangoAccount: MangoAccount,
|
|
||||||
mangoCache: MangoCache,
|
|
||||||
spotMarkets: Market[],
|
|
||||||
perpMarkets: PerpMarket[],
|
|
||||||
) {
|
|
||||||
const { diffs, netValues } = getDiffsAndNet(
|
|
||||||
mangoGroup,
|
|
||||||
mangoAccount,
|
|
||||||
mangoCache,
|
|
||||||
);
|
|
||||||
const tokensUnbalanced = netValues.some(
|
|
||||||
(nv) => Math.abs(diffs[nv[0]].toNumber()) > spotMarkets[nv[0]].minOrderSize,
|
|
||||||
);
|
|
||||||
const positionsUnbalanced = perpMarkets.some((pm) => {
|
|
||||||
const index = mangoGroup.getPerpMarketIndex(pm.publicKey);
|
|
||||||
const perpAccount = mangoAccount.perpAccounts[index];
|
|
||||||
const basePositionSize = Math.abs(
|
|
||||||
pm.baseLotsToNumber(perpAccount.basePosition),
|
|
||||||
);
|
|
||||||
|
|
||||||
return basePositionSize != 0 || perpAccount.quotePosition.gt(ZERO_I80F48);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (tokensUnbalanced) {
|
|
||||||
await balanceTokens(mangoGroup, mangoAccount, spotMarkets);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (positionsUnbalanced) {
|
|
||||||
await closePositions(mangoGroup, mangoAccount, perpMarkets);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async function closePositions(
|
async function closePositions(
|
||||||
mangoGroup: MangoGroup,
|
mangoGroup: MangoGroup,
|
||||||
mangoAccount: MangoAccount,
|
mangoAccount: MangoAccount,
|
||||||
|
|
Loading…
Reference in New Issue