add support for market margin available; update client

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2021-12-14 07:45:00 +01:00
parent dd3d9647cf
commit 3c08f79058
3 changed files with 62 additions and 69 deletions

View File

@ -12,7 +12,7 @@
"author": "microwavedcola1",
"license": "MIT",
"dependencies": {
"@blockworks-foundation/mango-client": "^3.2.14",
"@blockworks-foundation/mango-client": "^3.2.16",
"ansi-regex": "^6.0.1",
"big.js": "^6.1.1",
"body-parser": "^1.19.0",

View File

@ -5,6 +5,8 @@ import { patchInternalMarketName } from "./utils";
import {
getAllMarkets,
MarketConfig,
nativeI80F48ToUi,
QUOTE_INDEX,
} from "@blockworks-foundation/mango-client";
/**
@ -28,20 +30,55 @@ export class AccountController implements Controller {
response: Response,
next: NextFunction
) => {
const accountInternalDto = this.fetchAccountInternal();
const accountInternalDto = await this.fetchAccountInternal();
response.send({
success: true,
result: accountInternalDto,
} as AccountDto);
};
private fetchAccountInternal(): AccountInternalDto {
private async fetchAccountInternal(): Promise<AccountInternalDto> {
let allMarketConfigs = getAllMarkets(
this.mangoSimpleClient.mangoGroupConfig
);
const marketMarginAvailableListDtos = await this.getMarketMarginAvailable(
allMarketConfigs
);
const spotOpenOrdersAccountDtos =
this.getSpotOpenOrdersAccount(allMarketConfigs);
return {
spotOpenOrdersAccounts: spotOpenOrdersAccountDtos,
marketMarginAvailable: marketMarginAvailableListDtos,
} as AccountInternalDto;
}
private async getMarketMarginAvailable(
allMarketConfigs: MarketConfig[]
): Promise<MarketMarginAvailableListDto[]> {
const mangoCache = await this.mangoSimpleClient.mangoGroup.loadCache(
this.mangoSimpleClient.connection
);
const marketMarginAvailableDtos: MarketMarginAvailableListDto[] = [];
for (let marketConfig of allMarketConfigs) {
marketMarginAvailableDtos.push({
name: patchInternalMarketName(marketConfig.name),
marginAvailable: nativeI80F48ToUi(
this.mangoSimpleClient.mangoAccount.getMarketMarginAvailable(
this.mangoSimpleClient.mangoGroup,
mangoCache,
marketConfig.marketIndex,
marketConfig.kind
),
this.mangoSimpleClient.mangoGroup.tokens[QUOTE_INDEX].decimals
).toNumber(),
} as MarketMarginAvailableListDto);
}
return marketMarginAvailableDtos;
}
private getSpotOpenOrdersAccount(allMarketConfigs: MarketConfig[]) {
const spotOpenOrdersAccountDtos = allMarketConfigs
// filter only spot markets
.filter((marketConfig) => !marketConfig.name.includes("PERP"))
.map((spotMarketConfig) =>
this.getSpotOpenOrdersAccountForMarket(spotMarketConfig)
@ -50,9 +87,7 @@ export class AccountController implements Controller {
.filter(
(spotOpenOrdersAccount) => spotOpenOrdersAccount.publicKey != null
);
return {
spotOpenOrdersAccounts: spotOpenOrdersAccountDtos,
} as AccountInternalDto;
return spotOpenOrdersAccountDtos;
}
private getSpotOpenOrdersAccountForMarket(
@ -78,8 +113,16 @@ export class AccountController implements Controller {
{
"name": "MNGO-SPOT",
"publicKey": "..."
}
]
},
...
],
"marketMarginAvailable": [
{
"name": "MNGO-SPOT",
"marginAvailable": ...
},
...
],
}
}
*/
@ -90,9 +133,14 @@ interface AccountDto {
interface AccountInternalDto {
spotOpenOrdersAccounts: SpotOpenOrdersAccountDto[];
marketMarginAvailable: MarketMarginAvailableListDto[];
}
interface SpotOpenOrdersAccountDto {
name: string;
publicKey: string;
}
interface MarketMarginAvailableListDto {
name: string;
marginAvailable: number;
}

View File

@ -9,25 +9,20 @@
dependencies:
regenerator-runtime "^0.13.4"
"@blockworks-foundation/mango-client@^3.2.14":
version "3.2.14"
resolved "https://registry.yarnpkg.com/@blockworks-foundation/mango-client/-/mango-client-3.2.14.tgz#64854e60d8bf21a571858c0385d8c699af6f639b"
integrity sha512-EBO39zx3wtX4p/BmkEznVOPvuKI8dm33zxDYRvDVLoVGDSnNMBMpRao3bgHiQpGAUp962wX9ScYmLfiZ7qdoEQ==
"@blockworks-foundation/mango-client@^3.2.16":
version "3.2.16"
resolved "https://registry.yarnpkg.com/@blockworks-foundation/mango-client/-/mango-client-3.2.16.tgz#8379193841589db74642368aa04938ed06f02bc3"
integrity sha512-ZI3Tt/k77azt4iBrPiWsm9rS6EPEbGX+IsWKzCTCXLIfwOxpF09FarL+1YL/hBvoRkd8X/jAyQ1/KWpkb3zvUg==
dependencies:
"@project-serum/anchor" "^0.16.2"
"@project-serum/serum" "0.13.55"
"@project-serum/sol-wallet-adapter" "^0.2.0"
"@solana/spl-token" "^0.1.6"
"@solana/web3.js" "1.21.0"
axios "^0.21.1"
big.js "^6.1.1"
bigint-buffer "^1.1.5"
bn.js "^5.2.0"
buffer-layout "^1.2.1"
cross-fetch "^3.1.4"
dotenv "^10.0.0"
dotenv-expand "^5.1.0"
encoding "^0.1.13"
yargs "^17.0.1"
"@cspotcode/source-map-consumer@0.8.0":
@ -423,13 +418,6 @@ atomic-sleep@^1.0.0:
resolved "https://registry.yarnpkg.com/atomic-sleep/-/atomic-sleep-1.0.0.tgz#eb85b77a601fc932cfe432c5acd364a9e2c9075b"
integrity sha512-kNOjDqAh7px0XWNI+4QbzoiR/nTkHAWNud2uvnJquD1/x5a7EQZMJT0AczqK0Qn67oY/TTQ1LbUKajZpp3I9tQ==
axios@^0.21.1:
version "0.21.4"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.21.4.tgz#c67b90dc0568e5c1cf2b0b858c43ba28e2eda575"
integrity sha512-ut5vewkiu8jjGBdqpM44XxjuCjq9LAKeHVmoVfHVzy8eHgxxq8SbAVQNovDA8mVi05kP0Ea/n/UzcSHcTJQfNg==
dependencies:
follow-redirects "^1.14.0"
balanced-match@^1.0.0:
version "1.0.2"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.2.tgz#e83e3a7e3f300b34cb9d87f615fa0cbf357690ee"
@ -452,25 +440,11 @@ big.js@^6.1.1:
resolved "https://registry.yarnpkg.com/big.js/-/big.js-6.1.1.tgz#63b35b19dc9775c94991ee5db7694880655d5537"
integrity sha512-1vObw81a8ylZO5ePrtMay0n018TcftpTA5HFKDaSuiUDBo8biRBtjIobw60OpwuvrGk+FsxKamqN4cnmj/eXdg==
bigint-buffer@^1.1.5:
version "1.1.5"
resolved "https://registry.yarnpkg.com/bigint-buffer/-/bigint-buffer-1.1.5.tgz#d038f31c8e4534c1f8d0015209bf34b4fa6dd442"
integrity sha512-trfYco6AoZ+rKhKnxA0hgX0HAbVP/s808/EuDSe2JDzUnCp/xAsli35Orvk67UrTEcwuxZqYZDmfA2RXJgxVvA==
dependencies:
bindings "^1.3.0"
binary-extensions@^2.0.0:
version "2.2.0"
resolved "https://registry.yarnpkg.com/binary-extensions/-/binary-extensions-2.2.0.tgz#75f502eeaf9ffde42fc98829645be4ea76bd9e2d"
integrity sha512-jDctJ/IVQbZoJykoeHbhXpOlNBqGNcwXJKJog42E5HDPUwQTSdjCHdihjj0DlnheQ7blbT6dHOafNAiS8ooQKA==
bindings@^1.3.0:
version "1.5.0"
resolved "https://registry.yarnpkg.com/bindings/-/bindings-1.5.0.tgz#10353c9e945334bc0511a6d90b38fbc7c9c504df"
integrity sha512-p2q/t/mhvuOj/UeLlV6566GD/guowlr0hHxClI0W9m7MWYkL1F0hLo+0Aexs9HSPCtR1SXQ0TD3MMKrXZajbiQ==
dependencies:
file-uri-to-path "1.0.0"
bn.js@^4.11.9:
version "4.12.0"
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.12.0.tgz#775b3f278efbb9718eec7361f483fb36fbbfea88"
@ -778,11 +752,6 @@ dot-case@^3.0.4:
no-case "^3.0.4"
tslib "^2.0.3"
dotenv-expand@^5.1.0:
version "5.1.0"
resolved "https://registry.yarnpkg.com/dotenv-expand/-/dotenv-expand-5.1.0.tgz#3fbaf020bfd794884072ea26b1e9791d45a629f0"
integrity sha512-YXQl1DSa4/PQyRfgrv6aoNjhasp/p4qs9FjJ4q4cQk+8m4r6k4ZSiEyytKG8f8W9gi8WsQtIObNmKd+tMzNTmA==
dotenv@10.0.0, dotenv@^10.0.0:
version "10.0.0"
resolved "https://registry.yarnpkg.com/dotenv/-/dotenv-10.0.0.tgz#3d4227b8fb95f81096cdd2b66653fb2c7085ba81"
@ -816,13 +785,6 @@ encodeurl@~1.0.2:
resolved "https://registry.yarnpkg.com/encodeurl/-/encodeurl-1.0.2.tgz#ad3ff4c86ec2d029322f5a02c3a9a606c95b3f59"
integrity sha1-rT/0yG7C0CkyL1oCw6mmBslbP1k=
encoding@^0.1.13:
version "0.1.13"
resolved "https://registry.yarnpkg.com/encoding/-/encoding-0.1.13.tgz#56574afdd791f54a8e9b2785c0582a2d26210fa9"
integrity sha512-ETBauow1T35Y/WZMkio9jiM0Z5xjHHmJ4XmjZOq1l/dXz3lr2sRn87nJy20RupqSh1F2m3HHPSp8ShIPQJrJ3A==
dependencies:
iconv-lite "^0.6.2"
end-of-stream@^1.1.0:
version "1.4.4"
resolved "https://registry.yarnpkg.com/end-of-stream/-/end-of-stream-1.4.4.tgz#5ae64a5f45057baf3626ec14da0ca5e4b2431eb0"
@ -936,11 +898,6 @@ fastify-warning@^0.2.0:
resolved "https://registry.yarnpkg.com/fastify-warning/-/fastify-warning-0.2.0.tgz#e717776026a4493dc9a2befa44db6d17f618008f"
integrity sha512-s1EQguBw/9qtc1p/WTY4eq9WMRIACkj+HTcOIK1in4MV5aFaQC9ZCIt0dJ7pr5bIf4lPpHvAtP2ywpTNgs7hqw==
file-uri-to-path@1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/file-uri-to-path/-/file-uri-to-path-1.0.0.tgz#553a7b8446ff6f684359c445f1e37a05dacc33dd"
integrity sha512-0Zt+s3L7Vf1biwWZ29aARiVYLx7iMGnEUl9x33fbB/j3jR81u/O2LbqK+Bm1CDSNDKVtJ/YjwY7TUd5SkeLQLw==
fill-range@^7.0.1:
version "7.0.1"
resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-7.0.1.tgz#1919a6a7c75fe38b2c7c77e5198535da9acdda40"
@ -986,11 +943,6 @@ flatstr@^1.0.12:
resolved "https://registry.yarnpkg.com/flatstr/-/flatstr-1.0.12.tgz#c2ba6a08173edbb6c9640e3055b95e287ceb5931"
integrity sha512-4zPxDyhCyiN2wIAtSLI6gc82/EjqZc1onI4Mz/l0pWrAlsSfYH/2ZIcU+e3oA2wDwbzIWNKwa23F8rh6+DRWkw==
follow-redirects@^1.14.0:
version "1.14.4"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.14.4.tgz#838fdf48a8bbdd79e52ee51fb1c94e3ed98b9379"
integrity sha512-zwGkiSXC1MUJG/qmeIFH2HBJx9u0V46QGUe3YR1fXG8bXQxq7fLj0RjLZQ5nubr9qNJUZrH+xUcwXEoXNpfS+g==
forwarded@0.2.0:
version "0.2.0"
resolved "https://registry.yarnpkg.com/forwarded/-/forwarded-0.2.0.tgz#2269936428aad4c15c7ebe9779a84bf0b2a81811"
@ -1101,13 +1053,6 @@ iconv-lite@0.4.24:
dependencies:
safer-buffer ">= 2.1.2 < 3"
iconv-lite@^0.6.2:
version "0.6.3"
resolved "https://registry.yarnpkg.com/iconv-lite/-/iconv-lite-0.6.3.tgz#a52f80bf38da1952eb5c681790719871a1a72501"
integrity sha512-4fCk79wshMdzMp2rH06qWrJE4iolqLhCUH+OiuIgU++RB0+94NlDL81atO7GX55uUKueo0txHNtvEyI6D7WdMw==
dependencies:
safer-buffer ">= 2.1.2 < 3.0.0"
ieee754@^1.2.1:
version "1.2.1"
resolved "https://registry.yarnpkg.com/ieee754/-/ieee754-1.2.1.tgz#8eb7a10a63fff25d15a57b001586d177d1b0d352"
@ -1652,7 +1597,7 @@ safe-buffer@^5.0.1, safe-buffer@^5.1.0, safe-buffer@~5.2.0:
resolved "https://registry.yarnpkg.com/safe-buffer/-/safe-buffer-5.2.1.tgz#1eaf9fa9bdb1fdd4ec75f58f9cdb4e6b7827eec6"
integrity sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==
"safer-buffer@>= 2.1.2 < 3", "safer-buffer@>= 2.1.2 < 3.0.0":
"safer-buffer@>= 2.1.2 < 3":
version "2.1.2"
resolved "https://registry.yarnpkg.com/safer-buffer/-/safer-buffer-2.1.2.tgz#44fa161b0187b9549dd84bb91802f9bd8385cd6a"
integrity sha512-YZo3K82SD7Riyi0E1EQPojLz7kpepnSQI9IyPbHHg1XXXevb5dJI7tpyN2ADxGcQbHG7vcyRHk0cbwqcQriUtg==