From 48b5b81a09017ac8693a3f6346a61173e30dde30 Mon Sep 17 00:00:00 2001 From: tjs Date: Mon, 19 Dec 2022 20:30:47 -0500 Subject: [PATCH 1/3] script to add spot market --- ts/client/src/scripts/mb-add-spot-market.ts | 58 +++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 ts/client/src/scripts/mb-add-spot-market.ts diff --git a/ts/client/src/scripts/mb-add-spot-market.ts b/ts/client/src/scripts/mb-add-spot-market.ts new file mode 100644 index 000000000..822c1f813 --- /dev/null +++ b/ts/client/src/scripts/mb-add-spot-market.ts @@ -0,0 +1,58 @@ +import { AnchorProvider, Wallet } from '@project-serum/anchor'; +import { Connection, Keypair, PublicKey } from '@solana/web3.js'; +import * as dotenv from 'dotenv'; +import fs from 'fs'; +import { MangoClient } from '../client'; +import { MANGO_V4_ID } from '../constants'; +dotenv.config(); + +// +// (untested?) script which closes a mango account cleanly, first closes all positions, withdraws all tokens and then closes it +// +async function addSpotMarket() { + const options = AnchorProvider.defaultOptions(); + const connection = new Connection(process.env.MB_CLUSTER_URL!, options); + + // admin + const admin = Keypair.fromSecretKey( + Buffer.from( + JSON.parse(fs.readFileSync(process.env.MB_PAYER_KEYPAIR!, 'utf-8')), + ), + ); + const adminWallet = new Wallet(admin); + const adminProvider = new AnchorProvider(connection, adminWallet, options); + const client = await MangoClient.connect( + adminProvider, + 'mainnet-beta', + MANGO_V4_ID['mainnet-beta'], + ); + console.log(`Admin ${admin.publicKey.toBase58()}`); + + // fetch group + const groupPk = '78b8f4cGCwmZ9ysPFMWLaLTkkaYnUjwMJYStWe5RTSSX'; + const group = await client.getGroup(new PublicKey(groupPk)); + console.log(`Found group ${group.publicKey.toBase58()}`); + + const eth_openbook_mkt = 'FZxi3yWkE5mMjyaZj6utmYL54QQYfMCKMcLaQZq4UwnA'; + const eth_mint = '7vfCXTUXx5WJV5JADk17DUJ4ksgau7utNKj4b963voxs'; + const usdc_mint = 'EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v'; + + const signature = await client.serum3RegisterMarket( + group, + new PublicKey(eth_openbook_mkt), + group.getFirstBankByMint(new PublicKey(eth_mint)), + group.getFirstBankByMint(new PublicKey(usdc_mint)), + 1, // market index + 'ETH/USDC', + ); + + console.log('Tx Successful:', signature); + + process.exit(); +} + +async function main() { + await addSpotMarket(); +} + +main(); From ebeb2328b912cad89dd4957afbf32b9164aa3925 Mon Sep 17 00:00:00 2001 From: microwavedcola1 Date: Tue, 20 Dec 2022 10:32:33 +0100 Subject: [PATCH 2/3] force close mango account script Signed-off-by: microwavedcola1 --- ts/client/src/debug-scripts/debug-user.ts | 6 +-- .../src/scripts/mb-force-close-account.ts | 54 +++++++++++++++++++ 2 files changed, 57 insertions(+), 3 deletions(-) create mode 100644 ts/client/src/scripts/mb-force-close-account.ts diff --git a/ts/client/src/debug-scripts/debug-user.ts b/ts/client/src/debug-scripts/debug-user.ts index 37a2a542b..13c1990dc 100644 --- a/ts/client/src/debug-scripts/debug-user.ts +++ b/ts/client/src/debug-scripts/debug-user.ts @@ -17,7 +17,7 @@ const PAYER_KEYPAIR = const USER_KEYPAIR = process.env.USER_KEYPAIR_OVERRIDE || process.env.MB_PAYER_KEYPAIR; const GROUP_NUM = Number(process.env.GROUP_NUM || 0); -const MANGO_ACCOUNT = process.env.MANGO_ACCOUNT_PK; +const MANGO_ACCOUNT_PK = process.env.MANGO_ACCOUNT_PK; const CLUSTER: Cluster = (process.env.CLUSTER_OVERRIDE as Cluster) || 'mainnet-beta'; @@ -257,8 +257,8 @@ async function main(): Promise { for (const mangoAccount of mangoAccounts) { if ( - !MANGO_ACCOUNT || - mangoAccount.publicKey.equals(new PublicKey(MANGO_ACCOUNT)) + !MANGO_ACCOUNT_PK || + mangoAccount.publicKey.equals(new PublicKey(MANGO_ACCOUNT_PK)) ) { console.log(); console.log(`MangoAccount ${mangoAccount.publicKey}`); diff --git a/ts/client/src/scripts/mb-force-close-account.ts b/ts/client/src/scripts/mb-force-close-account.ts new file mode 100644 index 000000000..5e38413b2 --- /dev/null +++ b/ts/client/src/scripts/mb-force-close-account.ts @@ -0,0 +1,54 @@ +import { AnchorProvider, Wallet } from '@project-serum/anchor'; +import { Connection, Keypair, PublicKey } from '@solana/web3.js'; +import fs from 'fs'; +import { Group } from '../accounts/group'; +import { MangoClient } from '../client'; +import { MANGO_V4_ID } from '../constants'; + +const GROUP_NUM = Number(process.env.GROUP_NUM || 0); +const MANGO_ACCOUNT_PK = process.env.MANGO_ACCOUNT_PK; +const { MB_CLUSTER_URL, MB_PAYER_KEYPAIR, MB_USER_KEYPAIR, MB_USER4_KEYPAIR } = + process.env; + +async function buildUserClient( + userKeypair: string, +): Promise<[MangoClient, Group, Keypair]> { + const options = AnchorProvider.defaultOptions(); + const connection = new Connection(MB_CLUSTER_URL!, options); + + const user = Keypair.fromSecretKey( + Buffer.from(JSON.parse(fs.readFileSync(userKeypair, 'utf-8'))), + ); + const userWallet = new Wallet(user); + const userProvider = new AnchorProvider(connection, userWallet, options); + + const client = await MangoClient.connect( + userProvider, + 'mainnet-beta', + MANGO_V4_ID['mainnet-beta'], + ); + + const admin = Keypair.fromSecretKey( + Buffer.from(JSON.parse(fs.readFileSync(MB_PAYER_KEYPAIR!, 'utf-8'))), + ); + console.log(`Admin ${admin.publicKey.toBase58()}`); + const group = await client.getGroupForCreator(admin.publicKey, GROUP_NUM); + return [client, group, user]; +} + +async function forceCloseUserAccount() { + const result = await buildUserClient(MB_PAYER_KEYPAIR!); + const client = result[0]; + const group = result[1]; + const mangoAccount = await client.getMangoAccount( + new PublicKey(MANGO_ACCOUNT_PK!), + ); + await client.closeMangoAccount(group, mangoAccount, true); + process.exit(); +} + +async function main() { + await forceCloseUserAccount(); +} + +main(); From 5def04f6eefc94fd802a235eb9375593f18ed942 Mon Sep 17 00:00:00 2001 From: microwavedcola1 <89031858+microwavedcola1@users.noreply.github.com> Date: Tue, 20 Dec 2022 19:19:18 +0100 Subject: [PATCH 3/3] add price band helper (#349) Signed-off-by: microwavedcola1 Signed-off-by: microwavedcola1 --- ts/client/src/accounts/perp.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/ts/client/src/accounts/perp.ts b/ts/client/src/accounts/perp.ts index ef4d0fa35..6636e8707 100644 --- a/ts/client/src/accounts/perp.ts +++ b/ts/client/src/accounts/perp.ts @@ -5,7 +5,7 @@ import Big from 'big.js'; import { MangoClient } from '../client'; import { I80F48, I80F48Dto, ZERO_I80F48 } from '../numbers/I80F48'; import { Modify } from '../types'; -import { As, toNative, toUiDecimals, U64_MAX_BN } from '../utils'; +import { As, U64_MAX_BN, toNative, toUiDecimals } from '../utils'; import { OracleConfig, OracleConfigDto, @@ -242,6 +242,15 @@ export class PerpMarket { return this.priceLotsToUiConverter; } + insidePriceLimit(side: PerpOrderSide, orderPrice: number): boolean { + return ( + (side === PerpOrderSide.bid && + orderPrice <= this.maintLiabWeight.toNumber() * this.uiPrice) || + (side === PerpOrderSide.ask && + orderPrice >= this.maintAssetWeight.toNumber() * this.uiPrice) + ); + } + public async loadAsks( client: MangoClient, forceReload = false,