deprecate low level api, move usage to high level api in client
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
9a49164110
commit
c3bcbf948b
|
@ -82,6 +82,9 @@ export class Bank {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function registerToken(
|
export async function registerToken(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -103,6 +106,9 @@ export async function registerToken(
|
||||||
return await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function registerTokenIx(
|
export async function registerTokenIx(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -124,6 +130,9 @@ export async function registerTokenIx(
|
||||||
.instruction();
|
.instruction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function getBank(
|
export async function getBank(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
address: PublicKey,
|
address: PublicKey,
|
||||||
|
@ -131,6 +140,9 @@ export async function getBank(
|
||||||
return Bank.from(address, await client.program.account.bank.fetch(address));
|
return Bank.from(address, await client.program.account.bank.fetch(address));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function getBanksForGroup(
|
export async function getBanksForGroup(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
|
|
@ -14,6 +14,9 @@ export class Group {
|
||||||
constructor(public publicKey: PublicKey, public admin: PublicKey) {}
|
constructor(public publicKey: PublicKey, public admin: PublicKey) {}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function createGroup(
|
export async function createGroup(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
adminPk: PublicKey,
|
adminPk: PublicKey,
|
||||||
|
@ -24,6 +27,9 @@ export async function createGroup(
|
||||||
return await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function createGroupIx(
|
export async function createGroupIx(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
adminPk: PublicKey,
|
adminPk: PublicKey,
|
||||||
|
@ -37,6 +43,9 @@ export async function createGroupIx(
|
||||||
.instruction();
|
.instruction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function getGroupForAdmin(
|
export async function getGroupForAdmin(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
adminPk: PublicKey,
|
adminPk: PublicKey,
|
||||||
|
|
|
@ -150,6 +150,10 @@ export async function createMangoAccount(
|
||||||
return await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function createMangoAccountIx(
|
export async function createMangoAccountIx(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -219,6 +223,9 @@ export async function getMangoAccountsForGroup(
|
||||||
).map((pa) => MangoAccount.from(pa.publicKey, pa.account));
|
).map((pa) => MangoAccount.from(pa.publicKey, pa.account));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function getMangoAccountsForGroupAndOwner(
|
export async function getMangoAccountsForGroupAndOwner(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -244,6 +251,9 @@ export async function getMangoAccountsForGroupAndOwner(
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function deposit(
|
export async function deposit(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -271,6 +281,9 @@ export async function deposit(
|
||||||
return await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function depositIx(
|
export async function depositIx(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -301,6 +314,9 @@ export async function depositIx(
|
||||||
.instruction();
|
.instruction();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function withdraw(
|
export async function withdraw(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -330,6 +346,9 @@ export async function withdraw(
|
||||||
return await client.program.provider.send(tx);
|
return await client.program.provider.send(tx);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function withdrawIx(
|
export async function withdrawIx(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
|
|
@ -38,6 +38,9 @@ export class StubOracle {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function createStubOracle(
|
export async function createStubOracle(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -56,6 +59,9 @@ export async function createStubOracle(
|
||||||
.rpc();
|
.rpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function setStubOracle(
|
export async function setStubOracle(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
@ -74,6 +80,9 @@ export async function setStubOracle(
|
||||||
.rpc();
|
.rpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @deprecated
|
||||||
|
*/
|
||||||
export async function getStubOracleForGroupAndMint(
|
export async function getStubOracleForGroupAndMint(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
|
|
236
ts/client.ts
236
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 * as spl from '@solana/spl-token';
|
||||||
import { PublicKey, TransactionSignature } from '@solana/web3.js';
|
|
||||||
import {
|
import {
|
||||||
Bank,
|
AccountMeta,
|
||||||
getBanksForGroup,
|
PublicKey,
|
||||||
getMintInfoForTokenIndex,
|
SYSVAR_RENT_PUBKEY,
|
||||||
registerToken,
|
TransactionSignature,
|
||||||
} from './accounts/types/bank';
|
} from '@solana/web3.js';
|
||||||
import { createGroup, getGroupForAdmin, Group } from './accounts/types/group';
|
import { Bank, getMintInfoForTokenIndex } from './accounts/types/bank';
|
||||||
import {
|
import { Group } from './accounts/types/group';
|
||||||
createMangoAccount,
|
import { I80F48 } from './accounts/types/I80F48';
|
||||||
deposit,
|
import { MangoAccount } from './accounts/types/mangoAccount';
|
||||||
getMangoAccountsForGroupAndOwner,
|
import { StubOracle } from './accounts/types/oracle';
|
||||||
MangoAccount,
|
|
||||||
withdraw,
|
|
||||||
} from './accounts/types/mangoAccount';
|
|
||||||
import {
|
|
||||||
createStubOracle,
|
|
||||||
getStubOracleForGroupAndMint,
|
|
||||||
setStubOracle,
|
|
||||||
StubOracle,
|
|
||||||
} from './accounts/types/oracle';
|
|
||||||
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(
|
||||||
|
@ -34,12 +24,29 @@ export class MangoClient {
|
||||||
|
|
||||||
// Group
|
// Group
|
||||||
|
|
||||||
public async createGroup() {
|
public async createGroup(): Promise<TransactionSignature> {
|
||||||
return await createGroup(this, this.program.provider.wallet.publicKey);
|
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<Group> {
|
public async getGroup(adminPk: PublicKey): Promise<Group> {
|
||||||
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
|
// Tokens/Banks
|
||||||
|
@ -50,18 +57,37 @@ export class MangoClient {
|
||||||
oraclePk: PublicKey,
|
oraclePk: PublicKey,
|
||||||
tokenIndex: number,
|
tokenIndex: number,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await registerToken(
|
return await this.program.methods
|
||||||
this,
|
.registerToken(
|
||||||
group.publicKey,
|
tokenIndex,
|
||||||
this.program.provider.wallet.publicKey,
|
0.8,
|
||||||
mintPk,
|
0.6,
|
||||||
oraclePk,
|
1.2,
|
||||||
tokenIndex,
|
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<Bank[]> {
|
public async getBanksForGroup(group: Group): Promise<Bank[]> {
|
||||||
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
|
// Stub Oracle
|
||||||
|
@ -71,13 +97,15 @@ export class MangoClient {
|
||||||
mintPk: PublicKey,
|
mintPk: PublicKey,
|
||||||
price: number,
|
price: number,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await createStubOracle(
|
return await this.program.methods
|
||||||
this,
|
.createStubOracle({ val: I80F48.fromNumber(price).getData() })
|
||||||
group.publicKey,
|
.accounts({
|
||||||
this.program.provider.wallet.publicKey,
|
group: group.publicKey,
|
||||||
mintPk,
|
admin: this.program.provider.wallet.publicKey,
|
||||||
price,
|
tokenMint: mintPk,
|
||||||
);
|
payer: this.program.provider.wallet.publicKey,
|
||||||
|
})
|
||||||
|
.rpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async setStubOracle(
|
public async setStubOracle(
|
||||||
|
@ -85,20 +113,38 @@ export class MangoClient {
|
||||||
mintPk: PublicKey,
|
mintPk: PublicKey,
|
||||||
price: number,
|
price: number,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return await setStubOracle(
|
return await this.program.methods
|
||||||
this,
|
.setStubOracle({ val: I80F48.fromNumber(price).getData() })
|
||||||
group.publicKey,
|
.accounts({
|
||||||
this.program.provider.wallet.publicKey,
|
group: group.publicKey,
|
||||||
mintPk,
|
admin: this.program.provider.wallet.publicKey,
|
||||||
price,
|
tokenMint: mintPk,
|
||||||
);
|
payer: this.program.provider.wallet.publicKey,
|
||||||
|
})
|
||||||
|
.rpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getStubOracle(
|
public async getStubOracle(
|
||||||
group: Group,
|
group: Group,
|
||||||
mintPk: PublicKey,
|
mintPk: PublicKey,
|
||||||
): Promise<StubOracle> {
|
): Promise<StubOracle> {
|
||||||
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
|
// MangoAccount
|
||||||
|
@ -107,23 +153,38 @@ export class MangoClient {
|
||||||
group: Group,
|
group: Group,
|
||||||
accountNumber: number,
|
accountNumber: number,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return createMangoAccount(
|
return await this.program.methods
|
||||||
this,
|
.createAccount(accountNumber)
|
||||||
group.publicKey,
|
.accounts({
|
||||||
this.program.provider.wallet.publicKey,
|
group: group.publicKey,
|
||||||
accountNumber,
|
owner: this.program.provider.wallet.publicKey,
|
||||||
);
|
payer: this.program.provider.wallet.publicKey,
|
||||||
|
})
|
||||||
|
.rpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getMangoAccount(
|
public async getMangoAccount(
|
||||||
group: Group,
|
group: Group,
|
||||||
ownerPk: PublicKey,
|
ownerPk: PublicKey,
|
||||||
): Promise<MangoAccount[]> {
|
): Promise<MangoAccount[]> {
|
||||||
return await getMangoAccountsForGroupAndOwner(
|
return (
|
||||||
this,
|
await this.program.account.mangoAccount.all([
|
||||||
group.publicKey,
|
{
|
||||||
ownerPk,
|
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(
|
public async deposit(
|
||||||
|
@ -140,17 +201,23 @@ export class MangoClient {
|
||||||
const healthRemainingAccounts: PublicKey[] =
|
const healthRemainingAccounts: PublicKey[] =
|
||||||
await this.buildHealthRemainingAccounts(group, mangoAccount, bank);
|
await this.buildHealthRemainingAccounts(group, mangoAccount, bank);
|
||||||
|
|
||||||
return await deposit(
|
return await this.program.methods
|
||||||
this,
|
.deposit(new BN(amount))
|
||||||
group.publicKey,
|
.accounts({
|
||||||
mangoAccount.publicKey,
|
group: group.publicKey,
|
||||||
bank.publicKey,
|
account: mangoAccount.publicKey,
|
||||||
bank.vault,
|
bank: bank.publicKey,
|
||||||
tokenAccountPk,
|
vault: bank.vault,
|
||||||
mangoAccount.owner,
|
tokenAccount: tokenAccountPk,
|
||||||
healthRemainingAccounts,
|
tokenAuthority: this.program.provider.wallet.publicKey,
|
||||||
amount,
|
})
|
||||||
);
|
.remainingAccounts(
|
||||||
|
healthRemainingAccounts.map(
|
||||||
|
(pk) =>
|
||||||
|
({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.rpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
public async withdraw(
|
public async withdraw(
|
||||||
|
@ -168,18 +235,23 @@ export class MangoClient {
|
||||||
const healthRemainingAccounts: PublicKey[] =
|
const healthRemainingAccounts: PublicKey[] =
|
||||||
await this.buildHealthRemainingAccounts(group, mangoAccount, bank);
|
await this.buildHealthRemainingAccounts(group, mangoAccount, bank);
|
||||||
|
|
||||||
return await withdraw(
|
return await this.program.methods
|
||||||
this,
|
.withdraw(new BN(amount), allowBorrow)
|
||||||
group.publicKey,
|
.accounts({
|
||||||
mangoAccount.publicKey,
|
group: group.publicKey,
|
||||||
bank.publicKey,
|
account: mangoAccount.publicKey,
|
||||||
bank.vault,
|
bank: bank.publicKey,
|
||||||
tokenAccountPk,
|
vault: bank.vault,
|
||||||
mangoAccount.owner,
|
tokenAccount: tokenAccountPk,
|
||||||
healthRemainingAccounts,
|
tokenAuthority: this.program.provider.wallet.publicKey,
|
||||||
amount,
|
})
|
||||||
allowBorrow,
|
.remainingAccounts(
|
||||||
);
|
healthRemainingAccounts.map(
|
||||||
|
(pk) =>
|
||||||
|
({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta),
|
||||||
|
),
|
||||||
|
)
|
||||||
|
.rpc();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// static
|
/// static
|
||||||
|
|
|
@ -54,7 +54,7 @@ async function main() {
|
||||||
group,
|
group,
|
||||||
usdcDevnetMint,
|
usdcDevnetMint,
|
||||||
usdcDevnetOracle.publicKey,
|
usdcDevnetOracle.publicKey,
|
||||||
0,
|
1,
|
||||||
);
|
);
|
||||||
} catch (error) {}
|
} catch (error) {}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue