Merge pull request #3 from blockworks-foundation/fix-mem-leak

Fix memory leak, use latest mango-client
This commit is contained in:
Riordan Panayides 2021-11-17 22:21:04 +00:00 committed by GitHub
commit 0c5ecd22ad
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 53 additions and 17 deletions

View File

@ -37,7 +37,7 @@
"trailingComma": "all" "trailingComma": "all"
}, },
"dependencies": { "dependencies": {
"@blockworks-foundation/mango-client": "^3.2.5", "@blockworks-foundation/mango-client": "git+https://github.com/blockworks-foundation/mango-client-v3.git",
"@project-serum/anchor": "^0.16.2", "@project-serum/anchor": "^0.16.2",
"@project-serum/serum": "0.13.55", "@project-serum/serum": "0.13.55",
"@project-serum/sol-wallet-adapter": "^0.2.0", "@project-serum/sol-wallet-adapter": "^0.2.0",

View File

@ -75,8 +75,6 @@ const connection = new Connection(
); );
const client = new MangoClient(connection, mangoProgramId); const client = new MangoClient(connection, mangoProgramId);
let mangoAccounts: MangoAccount[] = [];
let mangoSubscriptionId = -1; let mangoSubscriptionId = -1;
let dexSubscriptionId = -1; let dexSubscriptionId = -1;
@ -162,9 +160,9 @@ async function main() {
} }
console.log(`Liqor Public Key: ${liqorMangoAccount.publicKey.toBase58()}`); console.log(`Liqor Public Key: ${liqorMangoAccount.publicKey.toBase58()}`);
let mangoAccounts: MangoAccount[] = [];
await refreshAccounts(mangoGroup); await refreshAccounts(mangoGroup, mangoAccounts);
watchAccounts(groupIds.mangoProgramId, mangoGroup); watchAccounts(groupIds.mangoProgramId, mangoGroup, mangoAccounts);
const perpMarkets = await Promise.all( const perpMarkets = await Promise.all(
groupIds.perpMarkets.map((perpMarket) => { groupIds.perpMarkets.map((perpMarket) => {
return mangoGroup.loadPerpMarket( return mangoGroup.loadPerpMarket(
@ -310,7 +308,11 @@ async function main() {
} }
} }
function watchAccounts(mangoProgramId: PublicKey, mangoGroup: MangoGroup) { function watchAccounts(
mangoProgramId: PublicKey,
mangoGroup: MangoGroup,
mangoAccounts: MangoAccount[],
) {
try { try {
console.log('Watching accounts...'); console.log('Watching accounts...');
const openOrdersAccountSpan = OpenOrders.getLayout( const openOrdersAccountSpan = OpenOrders.getLayout(
@ -403,25 +405,34 @@ function watchAccounts(mangoProgramId: PublicKey, mangoGroup: MangoGroup) {
refreshWebsocketInterval, refreshWebsocketInterval,
mangoProgramId, mangoProgramId,
mangoGroup, mangoGroup,
mangoAccounts,
); );
} }
} }
async function refreshAccounts(mangoGroup: MangoGroup) { async function refreshAccounts(
mangoGroup: MangoGroup,
mangoAccounts: MangoAccount[],
) {
try { try {
console.log('Refreshing accounts...'); console.log('Refreshing accounts...');
console.time('getAllMangoAccounts'); console.time('getAllMangoAccounts');
mangoAccounts = await client.getAllMangoAccounts( mangoAccounts.splice(0, mangoAccounts.length, ...(await client.getAllMangoAccounts(
mangoGroup, mangoGroup,
undefined, undefined,
true, true,
); )));
console.timeEnd('getAllMangoAccounts'); console.timeEnd('getAllMangoAccounts');
console.log(`Fetched ${mangoAccounts.length} accounts`); console.log(`Fetched ${mangoAccounts.length} accounts`);
} catch (err) { } catch (err) {
console.error('Error reloading accounts', err); console.error('Error reloading accounts', err);
} finally { } finally {
setTimeout(refreshAccounts, refreshAccountsInterval, mangoGroup); setTimeout(
refreshAccounts,
refreshAccountsInterval,
mangoGroup,
mangoAccounts,
);
} }
} }
@ -508,7 +519,9 @@ async function liquidateAccount(
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(); shouldLiquidateSpot = liqee.getNet(cache.rootBankCache[i], i).isNeg();
} }
const shouldLiquidatePerps = maintHealths.perp.lt(ZERO_I80F48) || (initHealths.perp.lt(ZERO_I80F48) && liqee.beingLiquidated); const shouldLiquidatePerps =
maintHealths.perp.lt(ZERO_I80F48) ||
(initHealths.perp.lt(ZERO_I80F48) && liqee.beingLiquidated);
if (shouldLiquidateSpot) { if (shouldLiquidateSpot) {
await liquidateSpot( await liquidateSpot(
@ -533,7 +546,11 @@ async function liquidateAccount(
); );
} }
if (!shouldLiquidateSpot && !maintHealths.perp.isNeg() && liqee.beingLiquidated) { if (
!shouldLiquidateSpot &&
!maintHealths.perp.isNeg() &&
liqee.beingLiquidated
) {
// Send a ForceCancelPerp to reset the being_liquidated flag // Send a ForceCancelPerp to reset the being_liquidated flag
await client.forceCancelAllPerpOrdersInMarket( await client.forceCancelAllPerpOrdersInMarket(
mangoGroup, mangoGroup,

View File

@ -30,10 +30,9 @@
dependencies: dependencies:
regenerator-runtime "^0.13.4" regenerator-runtime "^0.13.4"
"@blockworks-foundation/mango-client@^3.2.5": "@blockworks-foundation/mango-client@git+https://github.com/blockworks-foundation/mango-client-v3.git":
version "3.2.5" version "3.2.5"
resolved "https://registry.yarnpkg.com/@blockworks-foundation/mango-client/-/mango-client-3.2.5.tgz#a1da40078754eb1ee3ec5aa897d3a1cb548d0253" resolved "git+https://github.com/blockworks-foundation/mango-client-v3.git#834505a8a7ffb68c56035d6602f3d12b1c953cf0"
integrity sha512-3mXYFDaDiIdDXfBf/McEZIpYMT5dpYAT4h6CRVA4GR62Vk6bY+ARzr5Ei2xc6tVyxvEU7fnutJ07cSxW4U8Qow==
dependencies: dependencies:
"@project-serum/anchor" "^0.16.2" "@project-serum/anchor" "^0.16.2"
"@project-serum/serum" "0.13.55" "@project-serum/serum" "0.13.55"
@ -217,7 +216,27 @@
buffer-layout "^1.2.0" buffer-layout "^1.2.0"
dotenv "10.0.0" dotenv "10.0.0"
"@solana/web3.js@1.21.0", "@solana/web3.js@^1.17.0", "@solana/web3.js@^1.21.0": "@solana/web3.js@1.21.0":
version "1.21.0"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.21.0.tgz#4f98edea38d4cb3ae4d2ea49a050b0ec09508023"
integrity sha512-x1NXlF92tEjxuTxS0u4n9JV17UKk0Dn2L+qSWGvKOb4iWhzApDj6wicJsrGdSbGdxnZ7eciQ/SNn3zB4ydUllA==
dependencies:
"@babel/runtime" "^7.12.5"
"@solana/buffer-layout" "^3.0.0"
bn.js "^5.0.0"
borsh "^0.4.0"
bs58 "^4.0.1"
buffer "6.0.1"
crypto-hash "^1.2.2"
jayson "^3.4.4"
js-sha3 "^0.8.0"
node-fetch "^2.6.1"
rpc-websockets "^7.4.2"
secp256k1 "^4.0.2"
superstruct "^0.14.2"
tweetnacl "^1.0.0"
"@solana/web3.js@^1.17.0", "@solana/web3.js@^1.21.0":
version "1.30.2" version "1.30.2"
resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.30.2.tgz#e85da75e0825dc64f53eb64a1ff0115b27bec135" resolved "https://registry.yarnpkg.com/@solana/web3.js/-/web3.js-1.30.2.tgz#e85da75e0825dc64f53eb64a1ff0115b27bec135"
integrity sha512-hznCj+rkfvM5taRP3Z+l5lumB7IQnDrB4l55Wpsg4kDU9Zds8pE5YOH5Z9bbF/pUzZJKQjyBjnY/6kScBm3Ugg== integrity sha512-hznCj+rkfvM5taRP3Z+l5lumB7IQnDrB4l55Wpsg4kDU9Zds8pE5YOH5Z9bbF/pUzZJKQjyBjnY/6kScBm3Ugg==