diff --git a/ts/accounts/types/bank.ts b/ts/accounts/types/bank.ts index 3bc8ff15d..d67f6b14a 100644 --- a/ts/accounts/types/bank.ts +++ b/ts/accounts/types/bank.ts @@ -82,6 +82,9 @@ export class Bank { } } +/** + * @deprecated + */ export async function registerToken( client: MangoClient, groupPk: PublicKey, @@ -103,6 +106,9 @@ export async function registerToken( return await client.program.provider.send(tx); } +/** + * @deprecated + */ export async function registerTokenIx( client: MangoClient, groupPk: PublicKey, @@ -124,6 +130,9 @@ export async function registerTokenIx( .instruction(); } +/** + * @deprecated + */ export async function getBank( client: MangoClient, address: PublicKey, @@ -131,6 +140,9 @@ export async function getBank( return Bank.from(address, await client.program.account.bank.fetch(address)); } +/** + * @deprecated + */ export async function getBanksForGroup( client: MangoClient, groupPk: PublicKey, diff --git a/ts/accounts/types/group.ts b/ts/accounts/types/group.ts index e419fbd07..7965db33d 100644 --- a/ts/accounts/types/group.ts +++ b/ts/accounts/types/group.ts @@ -14,6 +14,9 @@ export class Group { constructor(public publicKey: PublicKey, public admin: PublicKey) {} } +/** + * @deprecated + */ export async function createGroup( client: MangoClient, adminPk: PublicKey, @@ -24,6 +27,9 @@ export async function createGroup( return await client.program.provider.send(tx); } +/** + * @deprecated + */ export async function createGroupIx( client: MangoClient, adminPk: PublicKey, @@ -37,6 +43,9 @@ export async function createGroupIx( .instruction(); } +/** + * @deprecated + */ export async function getGroupForAdmin( client: MangoClient, adminPk: PublicKey, diff --git a/ts/accounts/types/mangoAccount.ts b/ts/accounts/types/mangoAccount.ts index 7303389a1..a7918dd9e 100644 --- a/ts/accounts/types/mangoAccount.ts +++ b/ts/accounts/types/mangoAccount.ts @@ -150,6 +150,10 @@ export async function createMangoAccount( return await client.program.provider.send(tx); } +/** + * + * @deprecated + */ export async function createMangoAccountIx( client: MangoClient, groupPk: PublicKey, @@ -219,6 +223,9 @@ export async function getMangoAccountsForGroup( ).map((pa) => MangoAccount.from(pa.publicKey, pa.account)); } +/** + * @deprecated + */ export async function getMangoAccountsForGroupAndOwner( client: MangoClient, groupPk: PublicKey, @@ -244,6 +251,9 @@ export async function getMangoAccountsForGroupAndOwner( }); } +/** + * @deprecated + */ export async function deposit( client: MangoClient, groupPk: PublicKey, @@ -271,6 +281,9 @@ export async function deposit( return await client.program.provider.send(tx); } +/** + * @deprecated + */ export async function depositIx( client: MangoClient, groupPk: PublicKey, @@ -301,6 +314,9 @@ export async function depositIx( .instruction(); } +/** + * @deprecated + */ export async function withdraw( client: MangoClient, groupPk: PublicKey, @@ -330,6 +346,9 @@ export async function withdraw( return await client.program.provider.send(tx); } +/** + * @deprecated + */ export async function withdrawIx( client: MangoClient, groupPk: PublicKey, diff --git a/ts/accounts/types/oracle.ts b/ts/accounts/types/oracle.ts index 866d57b57..dafa43472 100644 --- a/ts/accounts/types/oracle.ts +++ b/ts/accounts/types/oracle.ts @@ -38,6 +38,9 @@ export class StubOracle { } } +/** + * @deprecated + */ export async function createStubOracle( client: MangoClient, groupPk: PublicKey, @@ -56,6 +59,9 @@ export async function createStubOracle( .rpc(); } +/** + * @deprecated + */ export async function setStubOracle( client: MangoClient, groupPk: PublicKey, @@ -74,6 +80,9 @@ export async function setStubOracle( .rpc(); } +/** + * @deprecated + */ export async function getStubOracleForGroupAndMint( client: MangoClient, groupPk: PublicKey, diff --git a/ts/client.ts b/ts/client.ts index e53b568fb..fe3bcb6ba 100644 --- a/ts/client.ts +++ b/ts/client.ts @@ -1,26 +1,16 @@ -import { Program, Provider } from '@project-serum/anchor'; +import { BN, Program, Provider } from '@project-serum/anchor'; import * as spl from '@solana/spl-token'; -import { PublicKey, TransactionSignature } from '@solana/web3.js'; import { - Bank, - getBanksForGroup, - getMintInfoForTokenIndex, - registerToken, -} from './accounts/types/bank'; -import { createGroup, getGroupForAdmin, Group } from './accounts/types/group'; -import { - createMangoAccount, - deposit, - getMangoAccountsForGroupAndOwner, - MangoAccount, - withdraw, -} from './accounts/types/mangoAccount'; -import { - createStubOracle, - getStubOracleForGroupAndMint, - setStubOracle, - StubOracle, -} from './accounts/types/oracle'; + AccountMeta, + PublicKey, + SYSVAR_RENT_PUBKEY, + TransactionSignature, +} from '@solana/web3.js'; +import { Bank, getMintInfoForTokenIndex } from './accounts/types/bank'; +import { Group } from './accounts/types/group'; +import { I80F48 } from './accounts/types/I80F48'; +import { MangoAccount } from './accounts/types/mangoAccount'; +import { StubOracle } from './accounts/types/oracle'; import { IDL, MangoV4 } from './mango_v4'; export const MANGO_V4_ID = new PublicKey( @@ -34,12 +24,29 @@ export class MangoClient { // Group - public async createGroup() { - return await createGroup(this, this.program.provider.wallet.publicKey); + public async createGroup(): Promise { + const adminPk = this.program.provider.wallet.publicKey; + return await this.program.methods + .createGroup() + .accounts({ + admin: adminPk, + payer: adminPk, + }) + .rpc(); } public async getGroup(adminPk: PublicKey): Promise { - return await getGroupForAdmin(this, adminPk); + const groups = ( + await this.program.account.group.all([ + { + memcmp: { + bytes: adminPk.toBase58(), + offset: 8, + }, + }, + ]) + ).map((tuple) => Group.from(tuple.publicKey, tuple.account)); + return groups[0]; } // Tokens/Banks @@ -50,18 +57,37 @@ export class MangoClient { oraclePk: PublicKey, tokenIndex: number, ): Promise { - return await registerToken( - this, - group.publicKey, - this.program.provider.wallet.publicKey, - mintPk, - oraclePk, - tokenIndex, - ); + return await this.program.methods + .registerToken( + tokenIndex, + 0.8, + 0.6, + 1.2, + 1.4, + 0.02 /*TODO expose as args*/, + ) + .accounts({ + group: group.publicKey, + admin: this.program.provider.wallet.publicKey, + mint: mintPk, + oracle: oraclePk, + payer: this.program.provider.wallet.publicKey, + rent: SYSVAR_RENT_PUBKEY, + }) + .rpc(); } public async getBanksForGroup(group: Group): Promise { - return await getBanksForGroup(this, group.publicKey); + return ( + await this.program.account.bank.all([ + { + memcmp: { + bytes: group.publicKey.toBase58(), + offset: 8, + }, + }, + ]) + ).map((tuple) => Bank.from(tuple.publicKey, tuple.account)); } // Stub Oracle @@ -71,13 +97,15 @@ export class MangoClient { mintPk: PublicKey, price: number, ): Promise { - return await createStubOracle( - this, - group.publicKey, - this.program.provider.wallet.publicKey, - mintPk, - price, - ); + return await this.program.methods + .createStubOracle({ val: I80F48.fromNumber(price).getData() }) + .accounts({ + group: group.publicKey, + admin: this.program.provider.wallet.publicKey, + tokenMint: mintPk, + payer: this.program.provider.wallet.publicKey, + }) + .rpc(); } public async setStubOracle( @@ -85,20 +113,38 @@ export class MangoClient { mintPk: PublicKey, price: number, ): Promise { - return await setStubOracle( - this, - group.publicKey, - this.program.provider.wallet.publicKey, - mintPk, - price, - ); + return await this.program.methods + .setStubOracle({ val: I80F48.fromNumber(price).getData() }) + .accounts({ + group: group.publicKey, + admin: this.program.provider.wallet.publicKey, + tokenMint: mintPk, + payer: this.program.provider.wallet.publicKey, + }) + .rpc(); } public async getStubOracle( group: Group, mintPk: PublicKey, ): Promise { - return await getStubOracleForGroupAndMint(this, group.publicKey, mintPk); + const stubOracles = ( + await this.program.account.stubOracle.all([ + { + memcmp: { + bytes: group.publicKey.toBase58(), + offset: 8, + }, + }, + { + memcmp: { + bytes: mintPk.toBase58(), + offset: 40, + }, + }, + ]) + ).map((pa) => StubOracle.from(pa.publicKey, pa.account)); + return stubOracles[0]; } // MangoAccount @@ -107,23 +153,38 @@ export class MangoClient { group: Group, accountNumber: number, ): Promise { - return createMangoAccount( - this, - group.publicKey, - this.program.provider.wallet.publicKey, - accountNumber, - ); + return await this.program.methods + .createAccount(accountNumber) + .accounts({ + group: group.publicKey, + owner: this.program.provider.wallet.publicKey, + payer: this.program.provider.wallet.publicKey, + }) + .rpc(); } public async getMangoAccount( group: Group, ownerPk: PublicKey, ): Promise { - return await getMangoAccountsForGroupAndOwner( - this, - group.publicKey, - ownerPk, - ); + return ( + await this.program.account.mangoAccount.all([ + { + memcmp: { + bytes: group.publicKey.toBase58(), + offset: 8, + }, + }, + { + memcmp: { + bytes: ownerPk.toBase58(), + offset: 40, + }, + }, + ]) + ).map((pa) => { + return MangoAccount.from(pa.publicKey, pa.account); + }); } public async deposit( @@ -140,17 +201,23 @@ export class MangoClient { const healthRemainingAccounts: PublicKey[] = await this.buildHealthRemainingAccounts(group, mangoAccount, bank); - return await deposit( - this, - group.publicKey, - mangoAccount.publicKey, - bank.publicKey, - bank.vault, - tokenAccountPk, - mangoAccount.owner, - healthRemainingAccounts, - amount, - ); + return await this.program.methods + .deposit(new BN(amount)) + .accounts({ + group: group.publicKey, + account: mangoAccount.publicKey, + bank: bank.publicKey, + vault: bank.vault, + tokenAccount: tokenAccountPk, + tokenAuthority: this.program.provider.wallet.publicKey, + }) + .remainingAccounts( + healthRemainingAccounts.map( + (pk) => + ({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta), + ), + ) + .rpc(); } public async withdraw( @@ -168,18 +235,23 @@ export class MangoClient { const healthRemainingAccounts: PublicKey[] = await this.buildHealthRemainingAccounts(group, mangoAccount, bank); - return await withdraw( - this, - group.publicKey, - mangoAccount.publicKey, - bank.publicKey, - bank.vault, - tokenAccountPk, - mangoAccount.owner, - healthRemainingAccounts, - amount, - allowBorrow, - ); + return await this.program.methods + .withdraw(new BN(amount), allowBorrow) + .accounts({ + group: group.publicKey, + account: mangoAccount.publicKey, + bank: bank.publicKey, + vault: bank.vault, + tokenAccount: tokenAccountPk, + tokenAuthority: this.program.provider.wallet.publicKey, + }) + .remainingAccounts( + healthRemainingAccounts.map( + (pk) => + ({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta), + ), + ) + .rpc(); } /// static diff --git a/ts/example1-admin.ts b/ts/example1-admin.ts index 00ef0a945..48a8385d2 100644 --- a/ts/example1-admin.ts +++ b/ts/example1-admin.ts @@ -54,7 +54,7 @@ async function main() { group, usdcDevnetMint, usdcDevnetOracle.publicKey, - 0, + 1, ); } catch (error) {}