extend example1-admin
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
3ed3220838
commit
eaf435cbbd
|
@ -1,7 +1,7 @@
|
||||||
import { I80F48, I80F48Dto } from './I80F48';
|
import { PublicKey, TransactionSignature } from '@solana/web3.js';
|
||||||
import { Keypair, PublicKey } from '@solana/web3.js';
|
|
||||||
import BN from 'bn.js';
|
import BN from 'bn.js';
|
||||||
import { MangoClient } from '../../client';
|
import { MangoClient } from '../../client';
|
||||||
|
import { I80F48, I80F48Dto } from './I80F48';
|
||||||
|
|
||||||
export class StubOracle {
|
export class StubOracle {
|
||||||
public price: I80F48;
|
public price: I80F48;
|
||||||
|
@ -44,14 +44,14 @@ export async function createStubOracle(
|
||||||
adminPk: PublicKey,
|
adminPk: PublicKey,
|
||||||
tokenMintPk: PublicKey,
|
tokenMintPk: PublicKey,
|
||||||
staticPrice: number,
|
staticPrice: number,
|
||||||
): Promise<void> {
|
): Promise<TransactionSignature> {
|
||||||
return await client.program.methods
|
return await client.program.methods
|
||||||
.createStubOracle({ val: I80F48.fromNumber(staticPrice).getData() })
|
.createStubOracle({ val: I80F48.fromNumber(staticPrice).getData() })
|
||||||
.accounts({
|
.accounts({
|
||||||
group: groupPk,
|
group: groupPk,
|
||||||
admin: adminPk,
|
admin: adminPk,
|
||||||
tokenMint: tokenMintPk,
|
tokenMint: tokenMintPk,
|
||||||
payer: adminPk
|
payer: adminPk,
|
||||||
})
|
})
|
||||||
.rpc();
|
.rpc();
|
||||||
}
|
}
|
||||||
|
@ -62,14 +62,14 @@ export async function setStubOracle(
|
||||||
adminPk: PublicKey,
|
adminPk: PublicKey,
|
||||||
tokenMintPk: PublicKey,
|
tokenMintPk: PublicKey,
|
||||||
staticPrice: number,
|
staticPrice: number,
|
||||||
): Promise<void> {
|
): Promise<TransactionSignature> {
|
||||||
return await client.program.methods
|
return await client.program.methods
|
||||||
.setStubOracle({ val: new BN(staticPrice) })
|
.setStubOracle({ val: new BN(staticPrice) })
|
||||||
.accounts({
|
.accounts({
|
||||||
group: groupPk,
|
group: groupPk,
|
||||||
admin: adminPk,
|
admin: adminPk,
|
||||||
tokenMint: tokenMintPk,
|
tokenMint: tokenMintPk,
|
||||||
payer: adminPk
|
payer: adminPk,
|
||||||
})
|
})
|
||||||
.rpc();
|
.rpc();
|
||||||
}
|
}
|
||||||
|
@ -78,8 +78,8 @@ export async function getStubOracleForGroupAndMint(
|
||||||
client: MangoClient,
|
client: MangoClient,
|
||||||
groupPk: PublicKey,
|
groupPk: PublicKey,
|
||||||
mintPk: PublicKey,
|
mintPk: PublicKey,
|
||||||
): Promise<StubOracle[]> {
|
): Promise<StubOracle> {
|
||||||
return (
|
const stubOracles = (
|
||||||
await client.program.account.stubOracle.all([
|
await client.program.account.stubOracle.all([
|
||||||
{
|
{
|
||||||
memcmp: {
|
memcmp: {
|
||||||
|
@ -95,4 +95,5 @@ export async function getStubOracleForGroupAndMint(
|
||||||
},
|
},
|
||||||
])
|
])
|
||||||
).map((pa) => StubOracle.from(pa.publicKey, pa.account));
|
).map((pa) => StubOracle.from(pa.publicKey, pa.account));
|
||||||
|
return stubOracles[0];
|
||||||
}
|
}
|
||||||
|
|
78
ts/client.ts
78
ts/client.ts
|
@ -5,8 +5,9 @@ import {
|
||||||
Bank,
|
Bank,
|
||||||
getBanksForGroup,
|
getBanksForGroup,
|
||||||
getMintInfoForTokenIndex,
|
getMintInfoForTokenIndex,
|
||||||
|
registerToken,
|
||||||
} from './accounts/types/bank';
|
} from './accounts/types/bank';
|
||||||
import { getGroupForAdmin, Group } from './accounts/types/group';
|
import { createGroup, getGroupForAdmin, Group } from './accounts/types/group';
|
||||||
import {
|
import {
|
||||||
createMangoAccount,
|
createMangoAccount,
|
||||||
deposit,
|
deposit,
|
||||||
|
@ -14,6 +15,12 @@ import {
|
||||||
MangoAccount,
|
MangoAccount,
|
||||||
withdraw,
|
withdraw,
|
||||||
} from './accounts/types/mangoAccount';
|
} 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(
|
||||||
|
@ -25,20 +32,85 @@ export class MangoClient {
|
||||||
|
|
||||||
/// public
|
/// public
|
||||||
|
|
||||||
|
// Group
|
||||||
|
|
||||||
|
public async createGroup() {
|
||||||
|
return await createGroup(this, this.program.provider.wallet.publicKey);
|
||||||
|
}
|
||||||
|
|
||||||
public async getGroup(adminPk: PublicKey): Promise<Group> {
|
public async getGroup(adminPk: PublicKey): Promise<Group> {
|
||||||
return await getGroupForAdmin(this, adminPk);
|
return await getGroupForAdmin(this, adminPk);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Tokens/Banks
|
||||||
|
|
||||||
|
public async registerToken(
|
||||||
|
group: Group,
|
||||||
|
mintPk: PublicKey,
|
||||||
|
oraclePk: PublicKey,
|
||||||
|
tokenIndex: number,
|
||||||
|
): Promise<TransactionSignature> {
|
||||||
|
return await registerToken(
|
||||||
|
this,
|
||||||
|
group.publicKey,
|
||||||
|
this.program.provider.wallet.publicKey,
|
||||||
|
mintPk,
|
||||||
|
oraclePk,
|
||||||
|
tokenIndex,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
public async getBanksForGroup(group: Group): Promise<Bank[]> {
|
public async getBanksForGroup(group: Group): Promise<Bank[]> {
|
||||||
return await getBanksForGroup(this, group.publicKey);
|
return await getBanksForGroup(this, group.publicKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async createStubOracle(
|
||||||
|
group: Group,
|
||||||
|
mintPk: PublicKey,
|
||||||
|
price: number,
|
||||||
|
): Promise<TransactionSignature> {
|
||||||
|
return await createStubOracle(
|
||||||
|
this,
|
||||||
|
group.publicKey,
|
||||||
|
this.program.provider.wallet.publicKey,
|
||||||
|
mintPk,
|
||||||
|
price,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async setStubOracle(
|
||||||
|
group: Group,
|
||||||
|
mintPk: PublicKey,
|
||||||
|
price: number,
|
||||||
|
): Promise<TransactionSignature> {
|
||||||
|
return await setStubOracle(
|
||||||
|
this,
|
||||||
|
group.publicKey,
|
||||||
|
this.program.provider.wallet.publicKey,
|
||||||
|
mintPk,
|
||||||
|
price,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getStubOracle(
|
||||||
|
group: Group,
|
||||||
|
mintPk: PublicKey,
|
||||||
|
): Promise<StubOracle> {
|
||||||
|
return await getStubOracleForGroupAndMint(this, group.publicKey, mintPk);
|
||||||
|
}
|
||||||
|
|
||||||
|
// MangoAccount
|
||||||
|
|
||||||
public async createMangoAccount(
|
public async createMangoAccount(
|
||||||
group: Group,
|
group: Group,
|
||||||
ownerPk: PublicKey,
|
|
||||||
accountNumber: number,
|
accountNumber: number,
|
||||||
): Promise<TransactionSignature> {
|
): Promise<TransactionSignature> {
|
||||||
return createMangoAccount(this, group.publicKey, ownerPk, accountNumber);
|
return createMangoAccount(
|
||||||
|
this,
|
||||||
|
group.publicKey,
|
||||||
|
this.program.provider.wallet.publicKey,
|
||||||
|
accountNumber,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getMangoAccount(
|
public async getMangoAccount(
|
||||||
|
|
|
@ -0,0 +1,69 @@
|
||||||
|
import { Provider, Wallet } from '@project-serum/anchor';
|
||||||
|
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
||||||
|
import fs from 'fs';
|
||||||
|
import { MangoClient } from './client';
|
||||||
|
|
||||||
|
async function main() {
|
||||||
|
const options = Provider.defaultOptions();
|
||||||
|
const connection = new Connection(
|
||||||
|
'https://mango.devnet.rpcpool.com',
|
||||||
|
options,
|
||||||
|
);
|
||||||
|
|
||||||
|
const admin = Keypair.fromSecretKey(
|
||||||
|
Buffer.from(
|
||||||
|
JSON.parse(fs.readFileSync(process.env.ADMIN_KEYPAIR!, 'utf-8')),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
const adminWallet = new Wallet(admin);
|
||||||
|
console.log(`Admin ${adminWallet.publicKey.toBase58()}`);
|
||||||
|
const adminProvider = new Provider(connection, adminWallet, options);
|
||||||
|
const client = await MangoClient.connect(adminProvider, true);
|
||||||
|
|
||||||
|
// group
|
||||||
|
try {
|
||||||
|
await client.createGroup();
|
||||||
|
} catch (error) {}
|
||||||
|
const group = await client.getGroup(admin.publicKey);
|
||||||
|
console.log(`Group ${group.publicKey}`);
|
||||||
|
|
||||||
|
// register token 0
|
||||||
|
const btcDevnetMint = new PublicKey(
|
||||||
|
'3UNBZ6o52WTWwjac2kPUb4FyodhU1vFkRJheu1Sh2TvU',
|
||||||
|
);
|
||||||
|
const btcDevnetOracle = new PublicKey(
|
||||||
|
'HovQMDrbAgAYPCmHVSrezcSmkMtXSSUsLDFANExrZh2J',
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
await client.registerToken(group, btcDevnetMint, btcDevnetOracle, 0);
|
||||||
|
} catch (error) {}
|
||||||
|
|
||||||
|
// stub oracle + register token 1
|
||||||
|
const usdcDevnetMint = new PublicKey(
|
||||||
|
'8FRFC6MoGGkMFQwngccyu69VnYbzykGeez7ignHVAFSN',
|
||||||
|
);
|
||||||
|
try {
|
||||||
|
await client.createStubOracle(group, usdcDevnetMint, 1.0);
|
||||||
|
} catch (error) {}
|
||||||
|
const usdcDevnetOracle = await client.getStubOracle(group, usdcDevnetMint);
|
||||||
|
try {
|
||||||
|
await client.registerToken(
|
||||||
|
group,
|
||||||
|
usdcDevnetMint,
|
||||||
|
usdcDevnetOracle.publicKey,
|
||||||
|
0,
|
||||||
|
);
|
||||||
|
} catch (error) {}
|
||||||
|
|
||||||
|
// log tokens/banks
|
||||||
|
const banks = await client.getBanksForGroup(group);
|
||||||
|
for (const bank of banks) {
|
||||||
|
console.log(
|
||||||
|
`Bank ${bank.tokenIndex} ${bank.publicKey}, mint ${bank.mint}, oracle ${bank.oracle}`,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
process.exit();
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
|
@ -38,7 +38,7 @@ async function main() {
|
||||||
let mangoAccount: MangoAccount;
|
let mangoAccount: MangoAccount;
|
||||||
mangoAccounts = await client.getMangoAccount(group, user.publicKey);
|
mangoAccounts = await client.getMangoAccount(group, user.publicKey);
|
||||||
if (mangoAccounts.length === 0) {
|
if (mangoAccounts.length === 0) {
|
||||||
await client.createMangoAccount(group, user.publicKey, 0);
|
await client.createMangoAccount(group, 0);
|
||||||
mangoAccounts = await client.getMangoAccount(group, user.publicKey);
|
mangoAccounts = await client.getMangoAccount(group, user.publicKey);
|
||||||
}
|
}
|
||||||
mangoAccount = mangoAccounts[0];
|
mangoAccount = mangoAccounts[0];
|
||||||
|
|
Loading…
Reference in New Issue