add account info
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
23c82b747d
commit
8d65d476e5
|
@ -1,3 +1,5 @@
|
||||||
|
.idea
|
||||||
|
__pycache__/
|
||||||
mango-service-v3/node_modules
|
mango-service-v3/node_modules
|
||||||
mango-service-v3/dist
|
mango-service-v3/dist
|
||||||
mango-service-v3/run-dev-server.sh
|
mango-service-v3/run-dev-server.sh
|
||||||
|
|
File diff suppressed because one or more lines are too long
|
@ -0,0 +1,85 @@
|
||||||
|
import Controller from "./controller.interface";
|
||||||
|
import { NextFunction, Request, Response, Router } from "express";
|
||||||
|
import MangoSimpleClient from "./mango.simple.client";
|
||||||
|
import { patchInternalMarketName } from "./utils";
|
||||||
|
import {
|
||||||
|
getAllMarkets,
|
||||||
|
MarketConfig,
|
||||||
|
} from "@blockworks-foundation/mango-client";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Houses every non-ftx style, mango specific information
|
||||||
|
*/
|
||||||
|
export class AccountController implements Controller {
|
||||||
|
public path = "/api/mango";
|
||||||
|
public router = Router();
|
||||||
|
|
||||||
|
constructor(public mangoSimpleClient: MangoSimpleClient) {
|
||||||
|
this.initializeRoutes();
|
||||||
|
}
|
||||||
|
|
||||||
|
private initializeRoutes() {
|
||||||
|
// GET /account
|
||||||
|
this.router.get(`${this.path}/account`, this.fetchMangoAccount);
|
||||||
|
}
|
||||||
|
|
||||||
|
private fetchMangoAccount = async (
|
||||||
|
request: Request,
|
||||||
|
response: Response,
|
||||||
|
next: NextFunction
|
||||||
|
) => {
|
||||||
|
const accountInternalDto = this.fetchAccountInternal();
|
||||||
|
response.send({
|
||||||
|
success: true,
|
||||||
|
result: accountInternalDto,
|
||||||
|
} as AccountDto);
|
||||||
|
};
|
||||||
|
|
||||||
|
private fetchAccountInternal(): AccountInternalDto {
|
||||||
|
let allMarketConfigs = getAllMarkets(
|
||||||
|
this.mangoSimpleClient.mangoGroupConfig
|
||||||
|
);
|
||||||
|
|
||||||
|
const spotOpenOrdersAccountDtos = allMarketConfigs
|
||||||
|
// filter only spot markets
|
||||||
|
.filter((marketConfig) => !marketConfig.name.includes("PERP"))
|
||||||
|
.map((spotMarketConfig) =>
|
||||||
|
this.getSpotOpenOrdersAccountForMarket(spotMarketConfig)
|
||||||
|
)
|
||||||
|
// filter markets where a spotOpenOrdersAccount exists
|
||||||
|
.filter(
|
||||||
|
(spotOpenOrdersAccount) => spotOpenOrdersAccount.publicKey != null
|
||||||
|
);
|
||||||
|
return {
|
||||||
|
spotOpenOrdersAccounts: spotOpenOrdersAccountDtos,
|
||||||
|
} as AccountInternalDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
private getSpotOpenOrdersAccountForMarket(
|
||||||
|
marketConfig: MarketConfig
|
||||||
|
): SpotOpenOrdersAccountDto {
|
||||||
|
const spotOpenOrdersAccount =
|
||||||
|
this.mangoSimpleClient.getSpotOpenOrdersAccount(marketConfig);
|
||||||
|
|
||||||
|
return {
|
||||||
|
name: patchInternalMarketName(marketConfig.name),
|
||||||
|
publicKey: spotOpenOrdersAccount
|
||||||
|
? spotOpenOrdersAccount.toBase58()
|
||||||
|
: null,
|
||||||
|
} as SpotOpenOrdersAccountDto;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AccountDto {
|
||||||
|
success: boolean;
|
||||||
|
result: AccountInternalDto;
|
||||||
|
}
|
||||||
|
|
||||||
|
interface AccountInternalDto {
|
||||||
|
spotOpenOrdersAccounts: SpotOpenOrdersAccountDto[];
|
||||||
|
}
|
||||||
|
|
||||||
|
interface SpotOpenOrdersAccountDto {
|
||||||
|
name: string;
|
||||||
|
publicKey: string;
|
||||||
|
}
|
|
@ -8,6 +8,7 @@ import MangoSimpleClient from "./mango.simple.client";
|
||||||
import MarketsController from "./markets.controller";
|
import MarketsController from "./markets.controller";
|
||||||
import OrdersController from "./orders.controller";
|
import OrdersController from "./orders.controller";
|
||||||
import WalletController from "./wallet.controller";
|
import WalletController from "./wallet.controller";
|
||||||
|
import { AccountController } from "./account.controller";
|
||||||
|
|
||||||
class App {
|
class App {
|
||||||
public app: express.Application;
|
public app: express.Application;
|
||||||
|
@ -26,6 +27,7 @@ class App {
|
||||||
new OrdersController(this.mangoSimpleClient),
|
new OrdersController(this.mangoSimpleClient),
|
||||||
new MarketsController(this.mangoSimpleClient),
|
new MarketsController(this.mangoSimpleClient),
|
||||||
new PositionsController(this.mangoSimpleClient),
|
new PositionsController(this.mangoSimpleClient),
|
||||||
|
new AccountController(this.mangoSimpleClient),
|
||||||
]);
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -253,6 +253,19 @@ class MangoSimpleClient {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public getSpotOpenOrdersAccount(
|
||||||
|
marketConfig: MarketConfig
|
||||||
|
): PublicKey | null {
|
||||||
|
// not loaded by default, load explicitly
|
||||||
|
this.mangoAccount.loadOpenOrders(
|
||||||
|
this.connection,
|
||||||
|
new PublicKey(this.mangoGroupConfig.serumProgramId)
|
||||||
|
);
|
||||||
|
const spotOpenOrdersAccount =
|
||||||
|
this.mangoAccount.spotOpenOrdersAccounts[marketConfig.marketIndex];
|
||||||
|
return spotOpenOrdersAccount ? spotOpenOrdersAccount.publicKey : null;
|
||||||
|
}
|
||||||
|
|
||||||
public async fetchAllSpotFills(): Promise<any[]> {
|
public async fetchAllSpotFills(): Promise<any[]> {
|
||||||
const allMarketConfigs = getAllMarkets(this.mangoGroupConfig);
|
const allMarketConfigs = getAllMarkets(this.mangoGroupConfig);
|
||||||
const allMarkets = await this.fetchAllMarkets();
|
const allMarkets = await this.fetchAllMarkets();
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
[virtualenvs]
|
||||||
|
in-project = true
|
Loading…
Reference in New Issue