deposit and withdraw examples

Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
microwavedcola1 2022-04-07 18:58:42 +02:00
parent 70e39f6333
commit dc26190a81
5 changed files with 143 additions and 42 deletions

View File

@ -55,8 +55,8 @@ export class Bank {
constructor( constructor(
public publicKey: PublicKey, public publicKey: PublicKey,
group: PublicKey, public group: PublicKey,
mint: PublicKey, public mint: PublicKey,
public vault: PublicKey, public vault: PublicKey,
public oracle: PublicKey, public oracle: PublicKey,
depositIndex: I80F48Dto, depositIndex: I80F48Dto,

View File

@ -48,9 +48,9 @@ export class MangoAccount {
constructor( constructor(
public publicKey: PublicKey, public publicKey: PublicKey,
group: PublicKey, public group: PublicKey,
owner: PublicKey, public owner: PublicKey,
delegate: PublicKey, public delegate: PublicKey,
tokens: { values: TokenAccountDto[] }, tokens: { values: TokenAccountDto[] },
serum3: { values: Serum3AccountDto[] }, serum3: { values: Serum3AccountDto[] },
perps: unknown, perps: unknown,

View File

@ -1,11 +1,18 @@
import { Program, Provider } from '@project-serum/anchor'; import { Program, Provider } from '@project-serum/anchor';
import * as spl from '@solana/spl-token';
import { PublicKey, TransactionSignature } from '@solana/web3.js'; import { PublicKey, TransactionSignature } from '@solana/web3.js';
import { Bank, getBanksForGroup } from './accounts/types/bank'; import {
Bank,
getBanksForGroup,
getMintInfoForTokenIndex,
} from './accounts/types/bank';
import { getGroupForAdmin, Group } from './accounts/types/group'; import { getGroupForAdmin, Group } from './accounts/types/group';
import { import {
createMangoAccount, createMangoAccount,
deposit,
getMangoAccountsForGroupAndOwner, getMangoAccountsForGroupAndOwner,
MangoAccount, MangoAccount,
withdraw,
} from './accounts/types/mangoAccount'; } from './accounts/types/mangoAccount';
import { IDL, MangoV4 } from './mango_v4'; import { IDL, MangoV4 } from './mango_v4';
@ -16,6 +23,93 @@ export const MANGO_V4_ID = new PublicKey(
export class MangoClient { export class MangoClient {
constructor(public program: Program<MangoV4>, public devnet?: boolean) {} constructor(public program: Program<MangoV4>, public devnet?: boolean) {}
/// public
public async getGroup(adminPk: PublicKey): Promise<Group> {
return await getGroupForAdmin(this, adminPk);
}
public async getBanksForGroup(group: Group): Promise<Bank[]> {
return await getBanksForGroup(this, group.publicKey);
}
public async createMangoAccount(
group: Group,
ownerPk: PublicKey,
accountNumber: number,
): Promise<TransactionSignature> {
return createMangoAccount(this, group.publicKey, ownerPk, accountNumber);
}
public async getMangoAccount(
group: Group,
ownerPk: PublicKey,
): Promise<MangoAccount[]> {
return await getMangoAccountsForGroupAndOwner(
this,
group.publicKey,
ownerPk,
);
}
public async deposit(
group: Group,
mangoAccount: MangoAccount,
bank: Bank,
amount: number,
) {
const tokenAccountPk = await spl.getAssociatedTokenAddress(
bank.mint,
mangoAccount.owner,
);
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,
);
}
public async withdraw(
group: Group,
mangoAccount: MangoAccount,
bank: Bank,
amount: number,
allowBorrow: boolean,
) {
const tokenAccountPk = await spl.getAssociatedTokenAddress(
bank.mint,
mangoAccount.owner,
);
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,
);
}
/// static
static async connect( static async connect(
provider: Provider, provider: Provider,
devnet?: boolean, devnet?: boolean,
@ -55,42 +149,45 @@ export class MangoClient {
); );
} }
public async getGroup(adminPk: PublicKey): Promise<Group> { /// private
return await getGroupForAdmin(this, adminPk);
}
public async getBanksForGroup(group: Group): Promise<Bank[]> { private async buildHealthRemainingAccounts(
return await getBanksForGroup(this, group.publicKey);
}
public async createMangoAccount(
group: Group, group: Group,
ownerPk: PublicKey, mangoAccount: MangoAccount,
accountNumber: number, bank: Bank,
): Promise<TransactionSignature> { ) {
return createMangoAccount(this, group.publicKey, ownerPk, accountNumber); const healthRemainingAccounts: PublicKey[] = [];
} {
const tokenIndices = mangoAccount.tokens
.filter((token) => token.tokenIndex !== 65535)
.map((token) => token.tokenIndex);
tokenIndices.push(bank.tokenIndex);
public async getMangoAccount( const mintInfos = await Promise.all(
group: Group, [...new Set(tokenIndices)].map(async (tokenIndex) =>
ownerPk: PublicKey, getMintInfoForTokenIndex(this, group.publicKey, tokenIndex),
): Promise<MangoAccount[]> { ),
return await getMangoAccountsForGroupAndOwner( );
this, healthRemainingAccounts.push(
group.publicKey, ...mintInfos.flatMap((mintinfos) => {
ownerPk, return mintinfos.flatMap((mintinfo) => {
return mintinfo.bank;
});
}),
);
healthRemainingAccounts.push(
...mintInfos.flatMap((mintinfos) => {
return mintinfos.flatMap((mintinfo) => {
return mintinfo.oracle;
});
}),
);
healthRemainingAccounts.push(
...mangoAccount.serum3
.filter((serum3Account) => serum3Account.marketIndex !== 65535)
.map((serum3Account) => serum3Account.openOrders),
); );
} }
return healthRemainingAccounts;
// public async deposit(group: Group, mangoAccount: MangoAccount, bank: Bank) { }
// return await deposit(
// this,
// group.publicKey,
// mangoAccount.publicKey,
// bank.publicKey,
// bank.vault,
// tokenAccountPk,
// ownerPk,
// );
// }
} }

View File

@ -280,7 +280,7 @@ async function main() {
1000, 1000,
); );
console.log(`Witdrawing...500`); console.log(`Withdrawing...500`);
await withdraw( await withdraw(
userClient, userClient,
group.publicKey, group.publicKey,

View File

@ -44,7 +44,11 @@ async function main() {
mangoAccount = mangoAccounts[0]; mangoAccount = mangoAccounts[0];
console.log(`MangoAccount ${mangoAccount.publicKey}`); console.log(`MangoAccount ${mangoAccount.publicKey}`);
process.exit(0); console.log(`Depositing...1000`);
await client.deposit(group, mangoAccount, banks[0], 1000);
console.log(`Withdrawing...500`);
await client.withdraw(group, mangoAccount, banks[0], 500, false);
} }
main(); main();