diff --git a/package.json b/package.json index af55181e8..c00a16359 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "scripts": { "build": "tsc", "clean": "rm -rf dist", - "example": "ts-node ts/example.ts", + "example0": "ts-node ts/example0.ts", + "example1": "ts-node ts/example1.ts", "format": "prettier --check .", "lint": "eslint . --ext ts --ext tsx --ext js --quiet", "type-check": "tsc --pretty --noEmit" diff --git a/ts/accounts/types/group.ts b/ts/accounts/types/group.ts index 37521cbf2..0e12621ef 100644 --- a/ts/accounts/types/group.ts +++ b/ts/accounts/types/group.ts @@ -3,6 +3,7 @@ import { Transaction, TransactionInstruction, } from '@solana/web3.js'; +import { assert } from 'console'; import { MangoClient } from '../../client'; export class Group { @@ -39,8 +40,8 @@ export async function createGroupIx( export async function getGroupForAdmin( client: MangoClient, adminPk: PublicKey, -): Promise { - return ( +): Promise { + const groups = ( await client.program.account.group.all([ { memcmp: { @@ -50,4 +51,8 @@ export async function getGroupForAdmin( }, ]) ).map((tuple) => Group.from(tuple.publicKey, tuple.account)); + + assert(groups.length == 1); + + return groups[0]; } diff --git a/ts/client.ts b/ts/client.ts index 18cd027f6..e23c86c38 100644 --- a/ts/client.ts +++ b/ts/client.ts @@ -1,5 +1,6 @@ import { Program, Provider } from '@project-serum/anchor'; import { PublicKey } from '@solana/web3.js'; +import { getGroupForAdmin, Group } from './accounts/types/group'; import { IDL, MangoV4 } from './mango_v4'; export const MANGO_V4_ID = new PublicKey( @@ -47,4 +48,10 @@ export class MangoClient { devnet, ); } + + public getGroup(adminPk: PublicKey): Promise { + return getGroupForAdmin(this, adminPk); + } + + } diff --git a/ts/example1.ts b/ts/example1.ts new file mode 100644 index 000000000..c1a461fe1 --- /dev/null +++ b/ts/example1.ts @@ -0,0 +1,32 @@ +import { Provider, Wallet } from '@project-serum/anchor'; +import { Connection, Keypair, PublicKey } from '@solana/web3.js'; +import fs from 'fs'; +import { MangoClient } from './client'; + +// +// An example based on high level api i.e. the client +// +async function main() { + const options = Provider.defaultOptions(); + const connection = new Connection( + 'https://mango.devnet.rpcpool.com', + options, + ); + + const user = Keypair.fromSecretKey( + Buffer.from( + JSON.parse(fs.readFileSync(process.env.USER_KEYPAIR!, 'utf-8')), + ), + ); + const userWallet = new Wallet(user); + const userProvider = new Provider(connection, userWallet, options); + const client = await MangoClient.connect(userProvider, true); + console.log(`User ${userWallet.publicKey.toBase58()}`); + + const group = await client.getGroup( + new PublicKey('6ACH752p6FsdLzuociVkmDwc3wJW8pcCoxZKfXJKfKcD'), + ); + console.log(group); +} + +main();