diff --git a/ts/accounts/types/bank.ts b/ts/accounts/types/bank.ts index 67f0de905..e7ebeea9e 100644 --- a/ts/accounts/types/bank.ts +++ b/ts/accounts/types/bank.ts @@ -3,6 +3,7 @@ import { SYSVAR_RENT_PUBKEY, Transaction, TransactionInstruction, + TransactionSignature, } from '@solana/web3.js'; import bs58 from 'bs58'; import { MangoClient } from '../../client'; @@ -88,7 +89,7 @@ export async function registerToken( mintPk: PublicKey, oraclePk: PublicKey, tokenIndex: number, -): Promise { +): Promise { const tx = new Transaction(); const ix = await registerTokenIx( client, @@ -99,7 +100,7 @@ export async function registerToken( tokenIndex, ); tx.add(ix); - await client.program.provider.send(tx); + return await client.program.provider.send(tx); } export async function registerTokenIx( diff --git a/ts/accounts/types/group.ts b/ts/accounts/types/group.ts index 0e12621ef..c042afeaa 100644 --- a/ts/accounts/types/group.ts +++ b/ts/accounts/types/group.ts @@ -2,6 +2,7 @@ import { PublicKey, Transaction, TransactionInstruction, + TransactionSignature, } from '@solana/web3.js'; import { assert } from 'console'; import { MangoClient } from '../../client'; @@ -17,11 +18,11 @@ export class Group { export async function createGroup( client: MangoClient, adminPk: PublicKey, -): Promise { +): Promise { const tx = new Transaction(); const ix = await createGroupIx(client, adminPk); tx.add(ix); - await client.program.provider.send(tx); + return await client.program.provider.send(tx); } export async function createGroupIx( diff --git a/ts/accounts/types/mangoAccount.ts b/ts/accounts/types/mangoAccount.ts index f1438eefa..27756ce28 100644 --- a/ts/accounts/types/mangoAccount.ts +++ b/ts/accounts/types/mangoAccount.ts @@ -1,10 +1,10 @@ import { BN } from '@project-serum/anchor'; import { AccountMeta, - Keypair, PublicKey, Transaction, TransactionInstruction, + TransactionSignature, } from '@solana/web3.js'; import { MangoClient } from '../../client'; import { Bank } from './bank'; @@ -137,24 +137,31 @@ export async function createMangoAccount( client: MangoClient, groupPk: PublicKey, ownerPk: PublicKey, -): Promise { + accountNumber: number, +): Promise { const tx = new Transaction(); - const ix = await createMangoAccountIx(client, groupPk, ownerPk); + const ix = await createMangoAccountIx( + client, + groupPk, + ownerPk, + accountNumber, + ); tx.add(ix); - await client.program.provider.send(tx); + return await client.program.provider.send(tx); } export async function createMangoAccountIx( client: MangoClient, groupPk: PublicKey, ownerPk: PublicKey, + accountNumber: number, ): Promise { return await client.program.methods - .createAccount(11) + .createAccount(accountNumber) .accounts({ group: groupPk, owner: ownerPk, - payer: ownerPk + payer: ownerPk, }) .signers() .instruction(); @@ -164,11 +171,11 @@ export async function closeMangoAccount( client: MangoClient, accountPk: PublicKey, ownerPk: PublicKey, -) { +): Promise { const tx = new Transaction(); const ix = await closeMangoAccountIx(client, accountPk, ownerPk); tx.add(ix); - await client.program.provider.send(tx); + return await client.program.provider.send(tx); } export async function closeMangoAccountIx( @@ -247,7 +254,7 @@ export async function deposit( ownerPk: PublicKey, healthRemainingAccounts: PublicKey[], amount: number, -): Promise { +): Promise { const tx = new Transaction(); const ix = await depositIx( client, @@ -261,7 +268,7 @@ export async function deposit( amount, ); tx.add(ix); - await client.program.provider.send(tx); + return await client.program.provider.send(tx); } export async function depositIx( @@ -305,7 +312,7 @@ export async function withdraw( healthRemainingAccounts: PublicKey[], amount: number, allowBorrow: boolean, -): Promise { +): Promise { const tx = new Transaction(); const ix = await withdrawIx( client, @@ -320,7 +327,7 @@ export async function withdraw( allowBorrow, ); tx.add(ix); - await client.program.provider.send(tx); + return await client.program.provider.send(tx); } export async function withdrawIx( diff --git a/ts/accounts/types/serum3.ts b/ts/accounts/types/serum3.ts index c6c6e5eee..f89e10ac2 100644 --- a/ts/accounts/types/serum3.ts +++ b/ts/accounts/types/serum3.ts @@ -3,6 +3,7 @@ import { PublicKey, Transaction, TransactionInstruction, + TransactionSignature, } from '@solana/web3.js'; import BN from 'bn.js'; import * as bs58 from 'bs58'; @@ -53,7 +54,7 @@ export async function serum3RegisterMarket( quoteBankPk: PublicKey, baseBankPk: PublicKey, marketIndex: number, -): Promise { +): Promise { const tx = new Transaction(); const ix = await serum3RegisterMarketIx( client, @@ -66,7 +67,7 @@ export async function serum3RegisterMarket( marketIndex, ); tx.add(ix); - await client.program.provider.send(tx); + return await client.program.provider.send(tx); } export async function serum3RegisterMarketIx( diff --git a/ts/client.ts b/ts/client.ts index e23c86c38..80b6122ff 100644 --- a/ts/client.ts +++ b/ts/client.ts @@ -1,6 +1,12 @@ import { Program, Provider } from '@project-serum/anchor'; -import { PublicKey } from '@solana/web3.js'; +import { PublicKey, TransactionSignature } from '@solana/web3.js'; +import { Bank, getBanksForGroup } from './accounts/types/bank'; import { getGroupForAdmin, Group } from './accounts/types/group'; +import { + createMangoAccount, + getMangoAccountsForGroupAndOwner, + MangoAccount, +} from './accounts/types/mangoAccount'; import { IDL, MangoV4 } from './mango_v4'; export const MANGO_V4_ID = new PublicKey( @@ -49,9 +55,42 @@ export class MangoClient { ); } - public getGroup(adminPk: PublicKey): Promise { - return getGroupForAdmin(this, adminPk); + public async getGroup(adminPk: PublicKey): Promise { + return await getGroupForAdmin(this, adminPk); } - + public async getBanksForGroup(group: Group): Promise { + return await getBanksForGroup(this, group.publicKey); + } + + public async createMangoAccount( + group: Group, + ownerPk: PublicKey, + accountNumber: number, + ): Promise { + return createMangoAccount(this, group.publicKey, ownerPk, accountNumber); + } + + public async getMangoAccount( + group: Group, + ownerPk: PublicKey, + ): Promise { + return await getMangoAccountsForGroupAndOwner( + this, + group.publicKey, + ownerPk, + ); + } + + // public async deposit(group: Group, mangoAccount: MangoAccount, bank: Bank) { + // return await deposit( + // this, + // group.publicKey, + // mangoAccount.publicKey, + // bank.publicKey, + // bank.vault, + // tokenAccountPk, + // ownerPk, + // ); + // } } diff --git a/ts/example0.ts b/ts/example0.ts index 85804c2f3..ea63479f1 100644 --- a/ts/example0.ts +++ b/ts/example0.ts @@ -162,7 +162,7 @@ async function main() { getMangoAccountsForGroupAndOwner, [userClient, group.publicKey, user.publicKey], createMangoAccount, - [userClient, group.publicKey, user.publicKey], + [userClient, group.publicKey, user.publicKey, 0], ); console.log(`MangoAccount ${mangoAccount.publicKey}`); diff --git a/ts/example1.ts b/ts/example1.ts index c1a461fe1..ee8add72e 100644 --- a/ts/example1.ts +++ b/ts/example1.ts @@ -1,6 +1,7 @@ import { Provider, Wallet } from '@project-serum/anchor'; import { Connection, Keypair, PublicKey } from '@solana/web3.js'; import fs from 'fs'; +import { MangoAccount } from './accounts/types/mangoAccount'; import { MangoClient } from './client'; // @@ -26,7 +27,24 @@ async function main() { const group = await client.getGroup( new PublicKey('6ACH752p6FsdLzuociVkmDwc3wJW8pcCoxZKfXJKfKcD'), ); - console.log(group); + console.log(`Group ${group.publicKey}`); + + const banks = await client.getBanksForGroup(group); + for (const bank of banks) { + console.log(`Bank ${bank.tokenIndex} ${bank.publicKey}`); + } + + let mangoAccounts: MangoAccount[] = []; + let mangoAccount: MangoAccount; + mangoAccounts = await client.getMangoAccount(group, user.publicKey); + if (mangoAccounts.length === 0) { + await client.createMangoAccount(group, user.publicKey, 0); + mangoAccounts = await client.getMangoAccount(group, user.publicKey); + } + mangoAccount = mangoAccounts[0]; + console.log(`MangoAccount ${mangoAccount.publicKey}`); + + process.exit(0); } main();