wip: exampl1 based on higher level api in client
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
ed312e71d5
commit
70e39f6333
|
@ -3,6 +3,7 @@ import {
|
||||||
SYSVAR_RENT_PUBKEY,
|
SYSVAR_RENT_PUBKEY,
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionInstruction,
|
TransactionInstruction,
|
||||||
|
TransactionSignature,
|
||||||
} from '@solana/web3.js';
|
} from '@solana/web3.js';
|
||||||
import bs58 from 'bs58';
|
import bs58 from 'bs58';
|
||||||
import { MangoClient } from '../../client';
|
import { MangoClient } from '../../client';
|
||||||
|
@ -88,7 +89,7 @@ export async function registerToken(
|
||||||
mintPk: PublicKey,
|
mintPk: PublicKey,
|
||||||
oraclePk: PublicKey,
|
oraclePk: PublicKey,
|
||||||
tokenIndex: number,
|
tokenIndex: number,
|
||||||
): Promise<void> {
|
): Promise<TransactionSignature> {
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
const ix = await registerTokenIx(
|
const ix = await registerTokenIx(
|
||||||
client,
|
client,
|
||||||
|
@ -99,7 +100,7 @@ export async function registerToken(
|
||||||
tokenIndex,
|
tokenIndex,
|
||||||
);
|
);
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function registerTokenIx(
|
export async function registerTokenIx(
|
||||||
|
|
|
@ -2,6 +2,7 @@ import {
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionInstruction,
|
TransactionInstruction,
|
||||||
|
TransactionSignature,
|
||||||
} from '@solana/web3.js';
|
} from '@solana/web3.js';
|
||||||
import { assert } from 'console';
|
import { assert } from 'console';
|
||||||
import { MangoClient } from '../../client';
|
import { MangoClient } from '../../client';
|
||||||
|
@ -17,11 +18,11 @@ export class Group {
|
||||||
export async function createGroup(
|
export async function createGroup(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
adminPk: PublicKey,
|
adminPk: PublicKey,
|
||||||
): Promise<void> {
|
): Promise<TransactionSignature> {
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
const ix = await createGroupIx(client, adminPk);
|
const ix = await createGroupIx(client, adminPk);
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createGroupIx(
|
export async function createGroupIx(
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
import { BN } from '@project-serum/anchor';
|
import { BN } from '@project-serum/anchor';
|
||||||
import {
|
import {
|
||||||
AccountMeta,
|
AccountMeta,
|
||||||
Keypair,
|
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionInstruction,
|
TransactionInstruction,
|
||||||
|
TransactionSignature,
|
||||||
} from '@solana/web3.js';
|
} from '@solana/web3.js';
|
||||||
import { MangoClient } from '../../client';
|
import { MangoClient } from '../../client';
|
||||||
import { Bank } from './bank';
|
import { Bank } from './bank';
|
||||||
|
@ -137,24 +137,31 @@ export async function createMangoAccount(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
ownerPk: PublicKey,
|
ownerPk: PublicKey,
|
||||||
): Promise<void> {
|
accountNumber: number,
|
||||||
|
): Promise<TransactionSignature> {
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
const ix = await createMangoAccountIx(client, groupPk, ownerPk);
|
const ix = await createMangoAccountIx(
|
||||||
|
client,
|
||||||
|
groupPk,
|
||||||
|
ownerPk,
|
||||||
|
accountNumber,
|
||||||
|
);
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function createMangoAccountIx(
|
export async function createMangoAccountIx(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
ownerPk: PublicKey,
|
ownerPk: PublicKey,
|
||||||
|
accountNumber: number,
|
||||||
): Promise<TransactionInstruction> {
|
): Promise<TransactionInstruction> {
|
||||||
return await client.program.methods
|
return await client.program.methods
|
||||||
.createAccount(11)
|
.createAccount(accountNumber)
|
||||||
.accounts({
|
.accounts({
|
||||||
group: groupPk,
|
group: groupPk,
|
||||||
owner: ownerPk,
|
owner: ownerPk,
|
||||||
payer: ownerPk
|
payer: ownerPk,
|
||||||
})
|
})
|
||||||
.signers()
|
.signers()
|
||||||
.instruction();
|
.instruction();
|
||||||
|
@ -164,11 +171,11 @@ export async function closeMangoAccount(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
accountPk: PublicKey,
|
accountPk: PublicKey,
|
||||||
ownerPk: PublicKey,
|
ownerPk: PublicKey,
|
||||||
) {
|
): Promise<TransactionSignature> {
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
const ix = await closeMangoAccountIx(client, accountPk, ownerPk);
|
const ix = await closeMangoAccountIx(client, accountPk, ownerPk);
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function closeMangoAccountIx(
|
export async function closeMangoAccountIx(
|
||||||
|
@ -247,7 +254,7 @@ export async function deposit(
|
||||||
ownerPk: PublicKey,
|
ownerPk: PublicKey,
|
||||||
healthRemainingAccounts: PublicKey[],
|
healthRemainingAccounts: PublicKey[],
|
||||||
amount: number,
|
amount: number,
|
||||||
): Promise<void> {
|
): Promise<TransactionSignature> {
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
const ix = await depositIx(
|
const ix = await depositIx(
|
||||||
client,
|
client,
|
||||||
|
@ -261,7 +268,7 @@ export async function deposit(
|
||||||
amount,
|
amount,
|
||||||
);
|
);
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function depositIx(
|
export async function depositIx(
|
||||||
|
@ -305,7 +312,7 @@ export async function withdraw(
|
||||||
healthRemainingAccounts: PublicKey[],
|
healthRemainingAccounts: PublicKey[],
|
||||||
amount: number,
|
amount: number,
|
||||||
allowBorrow: boolean,
|
allowBorrow: boolean,
|
||||||
): Promise<void> {
|
): Promise<TransactionSignature> {
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
const ix = await withdrawIx(
|
const ix = await withdrawIx(
|
||||||
client,
|
client,
|
||||||
|
@ -320,7 +327,7 @@ export async function withdraw(
|
||||||
allowBorrow,
|
allowBorrow,
|
||||||
);
|
);
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function withdrawIx(
|
export async function withdrawIx(
|
||||||
|
|
|
@ -3,6 +3,7 @@ import {
|
||||||
PublicKey,
|
PublicKey,
|
||||||
Transaction,
|
Transaction,
|
||||||
TransactionInstruction,
|
TransactionInstruction,
|
||||||
|
TransactionSignature,
|
||||||
} from '@solana/web3.js';
|
} from '@solana/web3.js';
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import * as bs58 from 'bs58';
|
import * as bs58 from 'bs58';
|
||||||
|
@ -53,7 +54,7 @@ export async function serum3RegisterMarket(
|
||||||
quoteBankPk: PublicKey,
|
quoteBankPk: PublicKey,
|
||||||
baseBankPk: PublicKey,
|
baseBankPk: PublicKey,
|
||||||
marketIndex: number,
|
marketIndex: number,
|
||||||
): Promise<void> {
|
): Promise<TransactionSignature> {
|
||||||
const tx = new Transaction();
|
const tx = new Transaction();
|
||||||
const ix = await serum3RegisterMarketIx(
|
const ix = await serum3RegisterMarketIx(
|
||||||
client,
|
client,
|
||||||
|
@ -66,7 +67,7 @@ export async function serum3RegisterMarket(
|
||||||
marketIndex,
|
marketIndex,
|
||||||
);
|
);
|
||||||
tx.add(ix);
|
tx.add(ix);
|
||||||
await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function serum3RegisterMarketIx(
|
export async function serum3RegisterMarketIx(
|
||||||
|
|
47
ts/client.ts
47
ts/client.ts
|
@ -1,6 +1,12 @@
|
||||||
import { Program, Provider } from '@project-serum/anchor';
|
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 { getGroupForAdmin, Group } from './accounts/types/group';
|
||||||
|
import {
|
||||||
|
createMangoAccount,
|
||||||
|
getMangoAccountsForGroupAndOwner,
|
||||||
|
MangoAccount,
|
||||||
|
} from './accounts/types/mangoAccount';
|
||||||
import { IDL, MangoV4 } from './mango_v4';
|
import { IDL, MangoV4 } from './mango_v4';
|
||||||
|
|
||||||
export const MANGO_V4_ID = new PublicKey(
|
export const MANGO_V4_ID = new PublicKey(
|
||||||
|
@ -49,9 +55,42 @@ export class MangoClient {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public getGroup(adminPk: PublicKey): Promise<Group> {
|
public async getGroup(adminPk: PublicKey): Promise<Group> {
|
||||||
return getGroupForAdmin(this, adminPk);
|
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) {
|
||||||
|
// return await deposit(
|
||||||
|
// this,
|
||||||
|
// group.publicKey,
|
||||||
|
// mangoAccount.publicKey,
|
||||||
|
// bank.publicKey,
|
||||||
|
// bank.vault,
|
||||||
|
// tokenAccountPk,
|
||||||
|
// ownerPk,
|
||||||
|
// );
|
||||||
|
// }
|
||||||
}
|
}
|
||||||
|
|
|
@ -162,7 +162,7 @@ async function main() {
|
||||||
getMangoAccountsForGroupAndOwner,
|
getMangoAccountsForGroupAndOwner,
|
||||||
[userClient, group.publicKey, user.publicKey],
|
[userClient, group.publicKey, user.publicKey],
|
||||||
createMangoAccount,
|
createMangoAccount,
|
||||||
[userClient, group.publicKey, user.publicKey],
|
[userClient, group.publicKey, user.publicKey, 0],
|
||||||
);
|
);
|
||||||
console.log(`MangoAccount ${mangoAccount.publicKey}`);
|
console.log(`MangoAccount ${mangoAccount.publicKey}`);
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,7 @@
|
||||||
import { Provider, Wallet } from '@project-serum/anchor';
|
import { Provider, Wallet } from '@project-serum/anchor';
|
||||||
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
||||||
import fs from 'fs';
|
import fs from 'fs';
|
||||||
|
import { MangoAccount } from './accounts/types/mangoAccount';
|
||||||
import { MangoClient } from './client';
|
import { MangoClient } from './client';
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -26,7 +27,24 @@ async function main() {
|
||||||
const group = await client.getGroup(
|
const group = await client.getGroup(
|
||||||
new PublicKey('6ACH752p6FsdLzuociVkmDwc3wJW8pcCoxZKfXJKfKcD'),
|
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();
|
main();
|
||||||
|
|
Loading…
Reference in New Issue