Merge branch 'main' into dev
This commit is contained in:
commit
dfcb9117b6
|
@ -1,6 +1,6 @@
|
|||
{
|
||||
"name": "@blockworks-foundation/mango-v4",
|
||||
"version": "0.0.1-beta.2",
|
||||
"version": "0.0.1-beta.3",
|
||||
"description": "Typescript Client for mango-v4 program.",
|
||||
"repository": "https://github.com/blockworks-foundation/mango-v4",
|
||||
"author": {
|
||||
|
|
|
@ -455,6 +455,16 @@ export class Group {
|
|||
return serum3Market;
|
||||
}
|
||||
|
||||
public getSerum3MarketByPk(pk: PublicKey): Serum3Market | undefined {
|
||||
const serum3Market = Array.from(
|
||||
this.serum3MarketsMapByExternal.values(),
|
||||
).find((serum3Market) => serum3Market.serumMarketExternal.equals(pk));
|
||||
if (!serum3Market) {
|
||||
throw new Error(`No serum3Market found by public key ${pk}!`);
|
||||
}
|
||||
return serum3Market;
|
||||
}
|
||||
|
||||
public getSerum3MarketByExternalMarket(
|
||||
externalMarketPk: PublicKey,
|
||||
): Serum3Market {
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import { BN } from '@project-serum/anchor';
|
||||
import { utf8 } from '@project-serum/anchor/dist/cjs/utils/bytes';
|
||||
import { OpenOrders, Order, Orderbook } from '@project-serum/serum/lib/market';
|
||||
import { PublicKey } from '@solana/web3.js';
|
||||
import { AccountInfo, PublicKey } from '@solana/web3.js';
|
||||
import { MangoClient } from '../client';
|
||||
import { SERUM3_PROGRAM_ID } from '../constants';
|
||||
import { I80F48, I80F48Dto, ONE_I80F48, ZERO_I80F48 } from '../numbers/I80F48';
|
||||
|
@ -481,6 +481,26 @@ export class MangoAccount {
|
|||
.toNumber();
|
||||
}
|
||||
|
||||
public async loadSerum3OpenOrdersAccounts(
|
||||
client: MangoClient,
|
||||
): Promise<OpenOrders[]> {
|
||||
const response =
|
||||
await client.program.provider.connection.getMultipleAccountsInfo(
|
||||
this.serum3.map((s) => s.openOrders),
|
||||
);
|
||||
const accounts = response.filter((a): a is AccountInfo<Buffer> =>
|
||||
Boolean(a),
|
||||
);
|
||||
|
||||
return accounts.map((acc, index) => {
|
||||
return OpenOrders.fromAccountInfo(
|
||||
this.serum3[index].openOrders,
|
||||
acc,
|
||||
SERUM3_PROGRAM_ID[client.cluster],
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
public async loadSerum3OpenOrdersForMarket(
|
||||
client: MangoClient,
|
||||
group: Group,
|
||||
|
|
|
@ -624,12 +624,14 @@ export class MangoClient {
|
|||
);
|
||||
}
|
||||
|
||||
public async getMangoAccount(
|
||||
mangoAccount: MangoAccount,
|
||||
): Promise<MangoAccount> {
|
||||
public async getMangoAccount(mangoAccount: MangoAccount | PublicKey) {
|
||||
const mangoAccountPk =
|
||||
mangoAccount instanceof MangoAccount
|
||||
? mangoAccount.publicKey
|
||||
: mangoAccount;
|
||||
return MangoAccount.from(
|
||||
mangoAccount.publicKey,
|
||||
await this.program.account.mangoAccount.fetch(mangoAccount.publicKey),
|
||||
mangoAccountPk,
|
||||
await this.program.account.mangoAccount.fetch(mangoAccountPk),
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ async function main() {
|
|||
for (const serum3Account of mangoAccount.serum3Active()) {
|
||||
let orders = await client.getSerum3Orders(
|
||||
group,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
for (const order of orders) {
|
||||
console.log(
|
||||
|
@ -80,12 +80,12 @@ async function main() {
|
|||
await client.serum3SettleFunds(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
await client.serum3CloseOpenOrders(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -61,7 +61,7 @@ async function main() {
|
|||
for (const serum3Account of mangoAccount.serum3Active()) {
|
||||
let orders = await client.getSerum3Orders(
|
||||
group,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
for (const order of orders) {
|
||||
console.log(
|
||||
|
@ -80,12 +80,12 @@ async function main() {
|
|||
await client.serum3SettleFunds(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
await client.serum3CloseOpenOrders(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -49,7 +49,7 @@ async function closeUserAccount(userKeypairFile: string) {
|
|||
for (const serum3Account of mangoAccount.serum3Active()) {
|
||||
let orders = await client.getSerum3Orders(
|
||||
group,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
for (const order of orders) {
|
||||
console.log(
|
||||
|
@ -69,12 +69,12 @@ async function closeUserAccount(userKeypairFile: string) {
|
|||
await client.serum3SettleFunds(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
await client.serum3CloseOpenOrders(
|
||||
group,
|
||||
mangoAccount,
|
||||
group.findSerum3Market(serum3Account.marketIndex)!.name,
|
||||
group.getSerum3MarketByIndex(serum3Account.marketIndex)!.name,
|
||||
);
|
||||
}
|
||||
|
||||
|
|
|
@ -139,7 +139,7 @@ async function main() {
|
|||
`...created mangoAccount ${mangoAccount.publicKey} for ${name}`,
|
||||
);
|
||||
|
||||
const market = group.findSerum3MarketByName('SOL/USDC')!;
|
||||
const market = group.getSerum3MarketByIndexByName('SOL/USDC')!;
|
||||
const sellMint = new PublicKey(MAINNET_MINTS.get('USDC')!);
|
||||
const buyMint = new PublicKey(MAINNET_MINTS.get('SOL')!);
|
||||
|
||||
|
|
|
@ -49,7 +49,9 @@ async function main() {
|
|||
let accounts = await client.getMangoAccountsForOwner(group, admin.publicKey);
|
||||
for (let account of accounts) {
|
||||
for (let serumOrders of account.serum3Active()) {
|
||||
const serumMarket = group.findSerum3Market(serumOrders.marketIndex)!;
|
||||
const serumMarket = group.getSerum3MarketByIndex(
|
||||
serumOrders.marketIndex,
|
||||
)!;
|
||||
const serumExternal = serumMarket.serumMarketExternal;
|
||||
console.log(
|
||||
`closing serum orders on: ${account} for market ${serumMarket.name}`,
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
import { AnchorProvider, Wallet } from '@project-serum/anchor';
|
||||
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
||||
import fs from 'fs';
|
||||
import { Serum3Side } from '../accounts/serum3';
|
||||
import { MangoClient } from '../client';
|
||||
import { MANGO_V4_ID } from '../constants';
|
||||
|
||||
//
|
||||
// (untested?) script which closes a mango account cleanly, first closes all positions, withdraws all tokens and then closes it
|
||||
//
|
||||
async function viewUnownedAccount(userKeypairFile: string) {
|
||||
const options = AnchorProvider.defaultOptions();
|
||||
const connection = new Connection(
|
||||
'https://mango.rpcpool.com/0f9acc0d45173b51bf7d7e09c1e5',
|
||||
options,
|
||||
);
|
||||
|
||||
// user
|
||||
const userWallet = new Wallet(Keypair.generate());
|
||||
const userProvider = new AnchorProvider(connection, userWallet, options);
|
||||
const client = await MangoClient.connect(
|
||||
userProvider,
|
||||
'mainnet-beta',
|
||||
MANGO_V4_ID['mainnet-beta'],
|
||||
);
|
||||
console.log(`User ${userWallet.publicKey.toBase58()}`);
|
||||
|
||||
// admin
|
||||
const admin = Keypair.fromSecretKey(
|
||||
Buffer.from(
|
||||
JSON.parse(fs.readFileSync(process.env.MB_PAYER_KEYPAIR || '', 'utf-8')),
|
||||
),
|
||||
);
|
||||
console.log(`Admin ${admin.publicKey.toBase58()}`);
|
||||
|
||||
// fetch group
|
||||
const group = await client.getGroupForCreator(admin.publicKey, 2);
|
||||
console.log(`Found group ${group.publicKey.toBase58()}`);
|
||||
|
||||
const x = await client.getMangoAccount(
|
||||
new PublicKey('6cTqJrSzQZWGEeHHePqFuJV4Kf54YDVfSamdCrT3agw6'),
|
||||
);
|
||||
const y = await x.reloadAccountData(client, group);
|
||||
|
||||
process.exit();
|
||||
}
|
||||
|
||||
async function main() {
|
||||
await viewUnownedAccount(process.env.MB_USER2_KEYPAIR || '');
|
||||
}
|
||||
|
||||
main();
|
Loading…
Reference in New Issue