diff --git a/ts/client.ts b/ts/client.ts index 03cd6cdb8..84fb7cffd 100644 --- a/ts/client.ts +++ b/ts/client.ts @@ -45,7 +45,16 @@ export class MangoClient { .rpc(); } - public async getGroup(adminPk: PublicKey): Promise { + public async getGroup(groupPk: PublicKey): Promise { + const group = Group.from( + groupPk, + await this.program.account.group.fetch(groupPk), + ); + await group.reload(this); + return group; + } + + public async getGroupForAdmin(adminPk: PublicKey): Promise { const groups = ( await this.program.account.group.all([ { @@ -168,8 +177,8 @@ export class MangoClient { let mangoAccounts = await this.getMangoAccountForOwner(group, ownerPk); if (mangoAccounts.length === 0) { await this.createMangoAccount(group, accountNumber ?? 0); + mangoAccounts = await this.getMangoAccountForOwner(group, ownerPk); } - mangoAccounts = await this.getMangoAccountForOwner(group, ownerPk); return mangoAccounts[0]; } @@ -489,7 +498,58 @@ export class MangoClient { .rpc(); } - async cancelSerumOrder( + async serum3SettleFunds( + group: Group, + mangoAccount: MangoAccount, + serum3ProgramId: PublicKey, + serum3MarketName: string, + ): Promise { + const serum3Market = group.serum3MarketsMap.get(serum3MarketName)!; + + const serum3MarketExternal = await Market.load( + this.program.provider.connection, + serum3Market.serumMarketExternal, + { commitment: this.program.provider.connection.commitment }, + serum3ProgramId, + ); + + const serum3MarketExternalVaultSigner = + // TODO: put into a helper method, and remove copy pasta + await PublicKey.createProgramAddress( + [ + serum3Market.serumMarketExternal.toBuffer(), + serum3MarketExternal.decoded.vaultSignerNonce.toArrayLike( + Buffer, + 'le', + 8, + ), + ], + serum3ProgramId, + ); + + return await this.program.methods + .serum3SettleFunds() + .accounts({ + group: group.publicKey, + account: mangoAccount.publicKey, + owner: this.program.provider.wallet.publicKey, + openOrders: mangoAccount.findSerum3Account(serum3Market.marketIndex) + ?.openOrders, + serumMarket: serum3Market.publicKey, + serumProgram: serum3ProgramId, + serumMarketExternal: serum3Market.serumMarketExternal, + marketBaseVault: serum3MarketExternal.decoded.baseVault, + marketQuoteVault: serum3MarketExternal.decoded.quoteVault, + marketVaultSigner: serum3MarketExternalVaultSigner, + quoteBank: group.findBank(serum3Market.quoteTokenIndex)?.publicKey, + quoteVault: group.findBank(serum3Market.quoteTokenIndex)?.vault, + baseBank: group.findBank(serum3Market.baseTokenIndex)?.publicKey, + baseVault: group.findBank(serum3Market.baseTokenIndex)?.vault, + }) + .rpc(); + } + + async serum3CancelOrder( group: Group, mangoAccount: MangoAccount, serum3ProgramId: PublicKey, diff --git a/ts/constants.ts b/ts/constants.ts index 7d46974ca..a5b3f4243 100644 --- a/ts/constants.ts +++ b/ts/constants.ts @@ -1,6 +1,6 @@ import { PublicKey } from '@solana/web3.js'; -export const DEVNET_GROUP = '6ACH752p6FsdLzuociVkmDwc3wJW8pcCoxZKfXJKfKcD'; +export const DEVNET_GROUP = 'Fwhv1B7yysktAqmoKbV6f1p25ap26Q81NKvd4jApmiB2'; export const DEVNET_MINTS = new Map([ ['USDC', '8FRFC6MoGGkMFQwngccyu69VnYbzykGeez7ignHVAFSN'], diff --git a/ts/example1-admin.ts b/ts/example1-admin.ts index a0f3676c6..587a09121 100644 --- a/ts/example1-admin.ts +++ b/ts/example1-admin.ts @@ -30,13 +30,15 @@ async function main() { const client = await MangoClient.connect(adminProvider, true); // group + console.log(`Group...`); try { await client.createGroup(); } catch (error) {} - const group = await client.getGroup(admin.publicKey); + const group = await client.getGroupForAdmin(admin.publicKey); console.log(`Group ${group.publicKey}`); // register token 0 + console.log(`Token 0...`); const btcDevnetMint = new PublicKey(DEVNET_MINTS.get('BTC')!); const btcDevnetOracle = new PublicKey(DEVNET_ORACLES.get('BTC')!); try { @@ -44,7 +46,8 @@ async function main() { } catch (error) {} // stub oracle + register token 1 - const usdcDevnetMint = new PublicKey(DEVNET_MINTS.get('BTC')!); + console.log(`Token 1...`); + const usdcDevnetMint = new PublicKey(DEVNET_MINTS.get('USDC')!); try { await client.createStubOracle(group, usdcDevnetMint, 1.0); } catch (error) {} @@ -67,11 +70,11 @@ async function main() { } // register serum market + console.log(`Serum3 market...`); const serumMarketExternalPk = new PublicKey( DEVNET_SERUM3_MARKETS.get('BTC/USDC')!, ); try { - } catch (error) { await client.serum3RegisterMarket( group, DEVNET_SERUM3_PROGRAM_ID, @@ -80,11 +83,13 @@ async function main() { banks[1], 0, ); - } + } catch (error) {} const markets = await client.serum3GetMarket( group, - banks[0].tokenIndex, - banks[1].tokenIndex, + banks.find((bank) => bank.mint.toBase58() === DEVNET_MINTS.get('BTC')) + ?.tokenIndex, + banks.find((bank) => bank.mint.toBase58() === DEVNET_MINTS.get('USDC')) + ?.tokenIndex, ); console.log(`Serum3 market ${markets[0].publicKey}`); diff --git a/ts/example1-user.ts b/ts/example1-user.ts index 17e931c52..47d52fddd 100644 --- a/ts/example1-user.ts +++ b/ts/example1-user.ts @@ -42,10 +42,11 @@ async function main() { console.log(`MangoAccount ${mangoAccount.publicKey}`); // deposit and withdraw - console.log(`Depositing...1000000`); - await client.deposit(group, mangoAccount, 'USDC', 1000000); - console.log(`Withdrawing...500000`); - await client.withdraw(group, mangoAccount, 'USDC', 500000, false); + console.log(`Depositing...5000000`); + await client.deposit(group, mangoAccount, 'USDC', 5000000); + await client.deposit(group, mangoAccount, 'BTC', 5000000); + console.log(`Withdrawing...1000000`); + await client.withdraw(group, mangoAccount, 'USDC', 1000000, false); // serum3 console.log( @@ -57,7 +58,7 @@ async function main() { DEVNET_SERUM3_PROGRAM_ID, 'BTC/USDC', Serum3Side.bid, - 20000, + 20, 0.0001, Serum3SelfTradeBehavior.decrementTake, Serum3OrderType.limit, @@ -104,7 +105,7 @@ async function main() { `Order orderId ${order.orderId}, ${order.side}, ${order.price}, ${order.size}`, ); console.log(`Cancelling order with ${order.orderId}`); - await client.cancelSerumOrder( + await client.serum3CancelOrder( group, mangoAccount, DEVNET_SERUM3_PROGRAM_ID, @@ -127,6 +128,14 @@ async function main() { // console.log(`Close mango account...`); // await client.closeMangoAccount(mangoAccount); + console.log(`Settling funds...`); + await client.serum3SettleFunds( + group, + mangoAccount, + DEVNET_SERUM3_PROGRAM_ID, + 'BTC/USDC', + ); + process.exit(); }