remove deprecated
Signed-off-by: microwavedcola1 <microwavedcola@gmail.com>
This commit is contained in:
parent
9df109b141
commit
3f39ceefed
|
@ -1,10 +1,4 @@
|
|||
import {
|
||||
PublicKey,
|
||||
SYSVAR_RENT_PUBKEY,
|
||||
Transaction,
|
||||
TransactionInstruction,
|
||||
TransactionSignature,
|
||||
} from '@solana/web3.js';
|
||||
import { PublicKey } from '@solana/web3.js';
|
||||
import bs58 from 'bs58';
|
||||
import { MangoClient } from '../../client';
|
||||
import { I80F48, I80F48Dto } from './I80F48';
|
||||
|
@ -82,83 +76,6 @@ export class Bank {
|
|||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function registerToken(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
adminPk: PublicKey,
|
||||
mintPk: PublicKey,
|
||||
oraclePk: PublicKey,
|
||||
tokenIndex: number,
|
||||
): Promise<TransactionSignature> {
|
||||
const tx = new Transaction();
|
||||
const ix = await registerTokenIx(
|
||||
client,
|
||||
groupPk,
|
||||
adminPk,
|
||||
mintPk,
|
||||
oraclePk,
|
||||
tokenIndex,
|
||||
);
|
||||
tx.add(ix);
|
||||
return await client.program.provider.send(tx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function registerTokenIx(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
adminPk: PublicKey,
|
||||
mintPk: PublicKey,
|
||||
oraclePk: PublicKey,
|
||||
tokenIndex: number,
|
||||
): Promise<TransactionInstruction> {
|
||||
return await client.program.methods
|
||||
.registerToken(tokenIndex, 0.8, 0.6, 1.2, 1.4, 0.02)
|
||||
.accounts({
|
||||
group: groupPk,
|
||||
admin: adminPk,
|
||||
mint: mintPk,
|
||||
oracle: oraclePk,
|
||||
payer: adminPk,
|
||||
rent: SYSVAR_RENT_PUBKEY,
|
||||
})
|
||||
.instruction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function getBank(
|
||||
client: MangoClient,
|
||||
address: PublicKey,
|
||||
): Promise<Bank> {
|
||||
return Bank.from(address, await client.program.account.bank.fetch(address));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function getBanksForGroup(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
): Promise<Bank[]> {
|
||||
return (
|
||||
await client.program.account.bank.all([
|
||||
{
|
||||
memcmp: {
|
||||
bytes: groupPk.toBase58(),
|
||||
offset: 8,
|
||||
},
|
||||
},
|
||||
])
|
||||
).map((tuple) => Bank.from(tuple.publicKey, tuple.account));
|
||||
}
|
||||
|
||||
export async function getBankForGroupAndMint(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
|
|
|
@ -54,52 +54,3 @@ export class Group {
|
|||
);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function createGroup(
|
||||
client: MangoClient,
|
||||
adminPk: PublicKey,
|
||||
): Promise<TransactionSignature> {
|
||||
const tx = new Transaction();
|
||||
const ix = await createGroupIx(client, adminPk);
|
||||
tx.add(ix);
|
||||
return await client.program.provider.send(tx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function createGroupIx(
|
||||
client: MangoClient,
|
||||
adminPk: PublicKey,
|
||||
): Promise<TransactionInstruction> {
|
||||
return await client.program.methods
|
||||
.createGroup()
|
||||
.accounts({
|
||||
admin: adminPk,
|
||||
payer: adminPk,
|
||||
})
|
||||
.instruction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function getGroupForAdmin(
|
||||
client: MangoClient,
|
||||
adminPk: PublicKey,
|
||||
): Promise<Group> {
|
||||
const groups = (
|
||||
await client.program.account.group.all([
|
||||
{
|
||||
memcmp: {
|
||||
bytes: adminPk.toBase58(),
|
||||
offset: 8,
|
||||
},
|
||||
},
|
||||
])
|
||||
).map((tuple) => Group.from(tuple.publicKey, tuple.account));
|
||||
return groups[0];
|
||||
}
|
||||
|
|
|
@ -1,6 +1,4 @@
|
|||
import { BN } from '@project-serum/anchor';
|
||||
import {
|
||||
AccountMeta,
|
||||
PublicKey,
|
||||
Transaction,
|
||||
TransactionInstruction,
|
||||
|
@ -137,44 +135,6 @@ export class Serum3AccountDto {
|
|||
) {}
|
||||
}
|
||||
|
||||
export async function createMangoAccount(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
ownerPk: PublicKey,
|
||||
accountNumber: number,
|
||||
): Promise<TransactionSignature> {
|
||||
const tx = new Transaction();
|
||||
const ix = await createMangoAccountIx(
|
||||
client,
|
||||
groupPk,
|
||||
ownerPk,
|
||||
accountNumber,
|
||||
);
|
||||
tx.add(ix);
|
||||
return await client.program.provider.send(tx);
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* @deprecated
|
||||
*/
|
||||
export async function createMangoAccountIx(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
ownerPk: PublicKey,
|
||||
accountNumber: number,
|
||||
): Promise<TransactionInstruction> {
|
||||
return await client.program.methods
|
||||
.createAccount(accountNumber)
|
||||
.accounts({
|
||||
group: groupPk,
|
||||
owner: ownerPk,
|
||||
payer: ownerPk,
|
||||
})
|
||||
.signers()
|
||||
.instruction();
|
||||
}
|
||||
|
||||
export async function closeMangoAccount(
|
||||
client: MangoClient,
|
||||
accountPk: PublicKey,
|
||||
|
@ -226,160 +186,3 @@ export async function getMangoAccountsForGroup(
|
|||
])
|
||||
).map((pa) => MangoAccount.from(pa.publicKey, pa.account));
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function getMangoAccountsForGroupAndOwner(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
ownerPk: PublicKey,
|
||||
): Promise<MangoAccount[]> {
|
||||
return (
|
||||
await client.program.account.mangoAccount.all([
|
||||
{
|
||||
memcmp: {
|
||||
bytes: groupPk.toBase58(),
|
||||
offset: 8,
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
bytes: ownerPk.toBase58(),
|
||||
offset: 40,
|
||||
},
|
||||
},
|
||||
])
|
||||
).map((pa) => {
|
||||
return MangoAccount.from(pa.publicKey, pa.account);
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function deposit(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
mangoAccountPk: PublicKey,
|
||||
bankPk: PublicKey,
|
||||
vaultPk: PublicKey,
|
||||
tokenAccountPk: PublicKey,
|
||||
ownerPk: PublicKey,
|
||||
healthRemainingAccounts: PublicKey[],
|
||||
amount: number,
|
||||
): Promise<TransactionSignature> {
|
||||
const tx = new Transaction();
|
||||
const ix = await depositIx(
|
||||
client,
|
||||
groupPk,
|
||||
mangoAccountPk,
|
||||
bankPk,
|
||||
vaultPk,
|
||||
tokenAccountPk,
|
||||
ownerPk,
|
||||
healthRemainingAccounts,
|
||||
amount,
|
||||
);
|
||||
tx.add(ix);
|
||||
return await client.program.provider.send(tx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function depositIx(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
mangoAccountPk: PublicKey,
|
||||
bankPk: PublicKey,
|
||||
vaultPk: PublicKey,
|
||||
tokenAccountPk: PublicKey,
|
||||
ownerPk: PublicKey,
|
||||
healthRemainingAccounts: PublicKey[],
|
||||
amount: number,
|
||||
): Promise<TransactionInstruction> {
|
||||
return await client.program.methods
|
||||
.deposit(new BN(amount))
|
||||
.accounts({
|
||||
group: groupPk,
|
||||
account: mangoAccountPk,
|
||||
bank: bankPk,
|
||||
vault: vaultPk,
|
||||
tokenAccount: tokenAccountPk,
|
||||
tokenAuthority: ownerPk,
|
||||
})
|
||||
.remainingAccounts(
|
||||
healthRemainingAccounts.map(
|
||||
(pk) =>
|
||||
({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta),
|
||||
),
|
||||
)
|
||||
.instruction();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function withdraw(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
mangoAccountPk: PublicKey,
|
||||
bankPk: PublicKey,
|
||||
vaultPk: PublicKey,
|
||||
tokenAccountPk: PublicKey,
|
||||
ownerPk: PublicKey,
|
||||
healthRemainingAccounts: PublicKey[],
|
||||
amount: number,
|
||||
allowBorrow: boolean,
|
||||
): Promise<TransactionSignature> {
|
||||
const tx = new Transaction();
|
||||
const ix = await withdrawIx(
|
||||
client,
|
||||
groupPk,
|
||||
mangoAccountPk,
|
||||
bankPk,
|
||||
vaultPk,
|
||||
tokenAccountPk,
|
||||
ownerPk,
|
||||
healthRemainingAccounts,
|
||||
amount,
|
||||
allowBorrow,
|
||||
);
|
||||
tx.add(ix);
|
||||
return await client.program.provider.send(tx);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function withdrawIx(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
mangoAccountPk: PublicKey,
|
||||
bankPk: PublicKey,
|
||||
vaultPk: PublicKey,
|
||||
tokenAccountPk: PublicKey,
|
||||
ownerPk: PublicKey,
|
||||
healthRemainingAccounts: PublicKey[],
|
||||
amount: number,
|
||||
allowBorrow: boolean,
|
||||
): Promise<TransactionInstruction> {
|
||||
return await client.program.methods
|
||||
.withdraw(new BN(amount), allowBorrow)
|
||||
.accounts({
|
||||
group: groupPk,
|
||||
account: mangoAccountPk,
|
||||
bank: bankPk,
|
||||
vault: vaultPk,
|
||||
tokenAccount: tokenAccountPk,
|
||||
tokenAuthority: ownerPk,
|
||||
})
|
||||
.remainingAccounts(
|
||||
healthRemainingAccounts.map(
|
||||
(pk) =>
|
||||
({ pubkey: pk, isWritable: false, isSigner: false } as AccountMeta),
|
||||
),
|
||||
)
|
||||
.instruction();
|
||||
}
|
||||
|
|
|
@ -37,72 +37,3 @@ export class StubOracle {
|
|||
this.lastUpdated = lastUpdated.toNumber();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function createStubOracle(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
adminPk: PublicKey,
|
||||
tokenMintPk: PublicKey,
|
||||
staticPrice: number,
|
||||
): Promise<TransactionSignature> {
|
||||
return await client.program.methods
|
||||
.createStubOracle({ val: I80F48.fromNumber(staticPrice).getData() })
|
||||
.accounts({
|
||||
group: groupPk,
|
||||
admin: adminPk,
|
||||
tokenMint: tokenMintPk,
|
||||
payer: adminPk,
|
||||
})
|
||||
.rpc();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function setStubOracle(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
adminPk: PublicKey,
|
||||
tokenMintPk: PublicKey,
|
||||
staticPrice: number,
|
||||
): Promise<TransactionSignature> {
|
||||
return await client.program.methods
|
||||
.setStubOracle({ val: new BN(staticPrice) })
|
||||
.accounts({
|
||||
group: groupPk,
|
||||
admin: adminPk,
|
||||
tokenMint: tokenMintPk,
|
||||
payer: adminPk,
|
||||
})
|
||||
.rpc();
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
*/
|
||||
export async function getStubOracleForGroupAndMint(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
mintPk: PublicKey,
|
||||
): Promise<StubOracle> {
|
||||
const stubOracles = (
|
||||
await client.program.account.stubOracle.all([
|
||||
{
|
||||
memcmp: {
|
||||
bytes: groupPk.toBase58(),
|
||||
offset: 8,
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
bytes: mintPk.toBase58(),
|
||||
offset: 40,
|
||||
},
|
||||
},
|
||||
])
|
||||
).map((pa) => StubOracle.from(pa.publicKey, pa.account));
|
||||
return stubOracles[0];
|
||||
}
|
||||
|
|
|
@ -1,12 +1,5 @@
|
|||
import {
|
||||
AccountMeta,
|
||||
PublicKey,
|
||||
Transaction,
|
||||
TransactionInstruction,
|
||||
TransactionSignature,
|
||||
} from '@solana/web3.js';
|
||||
import { AccountMeta, PublicKey } from '@solana/web3.js';
|
||||
import BN from 'bn.js';
|
||||
import * as bs58 from 'bs58';
|
||||
import { MangoClient } from '../../client';
|
||||
|
||||
export class Serum3Market {
|
||||
|
@ -45,97 +38,6 @@ export class Serum3Market {
|
|||
) {}
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export async function serum3RegisterMarket(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
adminPk: PublicKey,
|
||||
serumProgramPk: PublicKey,
|
||||
serumMarketExternalPk: PublicKey,
|
||||
quoteBankPk: PublicKey,
|
||||
baseBankPk: PublicKey,
|
||||
marketIndex: number,
|
||||
): Promise<TransactionSignature> {
|
||||
const tx = new Transaction();
|
||||
const ix = await serum3RegisterMarketIx(
|
||||
client,
|
||||
groupPk,
|
||||
adminPk,
|
||||
serumProgramPk,
|
||||
serumMarketExternalPk,
|
||||
quoteBankPk,
|
||||
baseBankPk,
|
||||
marketIndex,
|
||||
);
|
||||
tx.add(ix);
|
||||
return await client.program.provider.send(tx);
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export async function serum3RegisterMarketIx(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
adminPk: PublicKey,
|
||||
serumProgramPk: PublicKey,
|
||||
serumMarketExternalPk: PublicKey,
|
||||
quoteBankPk: PublicKey,
|
||||
baseBankPk: PublicKey,
|
||||
marketIndex: number,
|
||||
): Promise<TransactionInstruction> {
|
||||
return await client.program.methods
|
||||
.serum3RegisterMarket(marketIndex)
|
||||
.accounts({
|
||||
group: groupPk,
|
||||
admin: adminPk,
|
||||
serumProgram: serumProgramPk,
|
||||
serumMarketExternal: serumMarketExternalPk,
|
||||
quoteBank: quoteBankPk,
|
||||
baseBank: baseBankPk,
|
||||
payer: adminPk,
|
||||
})
|
||||
.instruction();
|
||||
}
|
||||
|
||||
/** @deprecated */
|
||||
export async function getSerum3MarketForBaseAndQuote(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
baseTokenIndex: number,
|
||||
quoteTokenIndex: number,
|
||||
): Promise<Serum3Market[]> {
|
||||
const bbuf = Buffer.alloc(2);
|
||||
bbuf.writeUInt16LE(baseTokenIndex);
|
||||
|
||||
const qbuf = Buffer.alloc(2);
|
||||
qbuf.writeUInt16LE(quoteTokenIndex);
|
||||
|
||||
const bumpfbuf = Buffer.alloc(1);
|
||||
bumpfbuf.writeUInt8(255);
|
||||
|
||||
return (
|
||||
await client.program.account.serum3Market.all([
|
||||
{
|
||||
memcmp: {
|
||||
bytes: groupPk.toBase58(),
|
||||
offset: 8,
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
bytes: bs58.encode(bbuf),
|
||||
offset: 106,
|
||||
},
|
||||
},
|
||||
{
|
||||
memcmp: {
|
||||
bytes: bs58.encode(qbuf),
|
||||
offset: 108,
|
||||
},
|
||||
},
|
||||
])
|
||||
).map((tuple) => Serum3Market.from(tuple.publicKey, tuple.account));
|
||||
}
|
||||
|
||||
export async function serum3CreateOpenOrders(
|
||||
client: MangoClient,
|
||||
groupPk: PublicKey,
|
||||
|
|
|
@ -347,7 +347,7 @@ export class MangoClient {
|
|||
filters.push({
|
||||
memcmp: {
|
||||
bytes: bs58.encode(qbuf),
|
||||
offset: 106,
|
||||
offset: 108,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
|
374
ts/example0.ts
374
ts/example0.ts
|
@ -1,374 +0,0 @@
|
|||
import { Provider, Wallet, web3 } from '@project-serum/anchor';
|
||||
import { Market } from '@project-serum/serum';
|
||||
import * as spl from '@solana/spl-token';
|
||||
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
||||
import fs from 'fs';
|
||||
import {
|
||||
Bank,
|
||||
getBank,
|
||||
getBankForGroupAndMint,
|
||||
getMintInfoForTokenIndex,
|
||||
registerToken,
|
||||
} from './accounts/types/bank';
|
||||
import { createGroup, getGroupForAdmin, Group } from './accounts/types/group';
|
||||
import {
|
||||
createMangoAccount,
|
||||
deposit,
|
||||
getMangoAccount,
|
||||
getMangoAccountsForGroupAndOwner,
|
||||
MangoAccount,
|
||||
withdraw,
|
||||
} from './accounts/types/mangoAccount';
|
||||
import {
|
||||
createStubOracle,
|
||||
getStubOracleForGroupAndMint,
|
||||
StubOracle,
|
||||
} from './accounts/types/oracle';
|
||||
import {
|
||||
getSerum3MarketForBaseAndQuote,
|
||||
serum3CreateOpenOrders,
|
||||
Serum3Market,
|
||||
Serum3OrderType,
|
||||
serum3PlaceOrder,
|
||||
serum3RegisterMarket,
|
||||
Serum3SelfTradeBehavior,
|
||||
Serum3Side,
|
||||
} from './accounts/types/serum3';
|
||||
import { MangoClient } from './client';
|
||||
import {
|
||||
DEVNET_MINTS,
|
||||
DEVNET_ORACLES,
|
||||
DEVNET_SERUM3_PROGRAM_ID,
|
||||
} from './constants';
|
||||
import { findOrCreate } from './utils';
|
||||
|
||||
//
|
||||
// Deprecated, see other examples
|
||||
//
|
||||
|
||||
//
|
||||
// An example which uses low level global methods
|
||||
//
|
||||
|
||||
async function main() {
|
||||
//
|
||||
// Setup
|
||||
//
|
||||
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 adminClient = await MangoClient.connect(adminProvider, true);
|
||||
|
||||
// const payer = Keypair.fromSecretKey(
|
||||
// Buffer.from(
|
||||
// JSON.parse(fs.readFileSync(process.env.PAYER_KEYPAIR!, 'utf-8')),
|
||||
// ),
|
||||
// );
|
||||
// console.log(`Payer ${payer.publicKey.toBase58()}`);
|
||||
//
|
||||
// Find existing or create a new group
|
||||
//
|
||||
const group: Group = await findOrCreate(
|
||||
'group',
|
||||
getGroupForAdmin,
|
||||
[adminClient, admin.publicKey],
|
||||
createGroup,
|
||||
[adminClient, admin.publicKey],
|
||||
);
|
||||
console.log(`Group ${group.publicKey}`);
|
||||
|
||||
//
|
||||
// Find existing or register new oracles
|
||||
//
|
||||
const usdcDevnetMint = new PublicKey(DEVNET_MINTS['USDC']);
|
||||
const usdcDevnetStubOracle = await findOrCreate<StubOracle>(
|
||||
'stubOracle',
|
||||
getStubOracleForGroupAndMint,
|
||||
[adminClient, group.publicKey, usdcDevnetMint],
|
||||
createStubOracle,
|
||||
[adminClient, group.publicKey, admin.publicKey, usdcDevnetMint, 1],
|
||||
);
|
||||
console.log(
|
||||
`usdcDevnetStubOracle ${usdcDevnetStubOracle.publicKey.toBase58()}`,
|
||||
);
|
||||
const btcDevnetMint = new PublicKey(DEVNET_MINTS['BTC']);
|
||||
const btcDevnetOracle = new PublicKey(DEVNET_ORACLES['BTC']);
|
||||
|
||||
//
|
||||
// Find existing or register new tokens
|
||||
//
|
||||
// TODO: replace with 4zMMC9srt5Ri5X14GAgXhaHii3GnPAEERYPJgZJDncDU,
|
||||
// see https://developers.circle.com/docs/usdc-on-testnet#usdc-on-solana-testnet
|
||||
const btcBank = await findOrCreate<Bank>(
|
||||
'bank',
|
||||
getBankForGroupAndMint,
|
||||
[adminClient, group.publicKey, btcDevnetMint],
|
||||
registerToken,
|
||||
[
|
||||
adminClient,
|
||||
group.publicKey,
|
||||
admin.publicKey,
|
||||
btcDevnetMint,
|
||||
btcDevnetOracle,
|
||||
|
||||
0,
|
||||
],
|
||||
);
|
||||
console.log(`BtcBank ${btcBank.publicKey}`);
|
||||
const usdcBank = await findOrCreate<Bank>(
|
||||
'bank',
|
||||
getBankForGroupAndMint,
|
||||
[adminClient, group.publicKey, usdcDevnetMint],
|
||||
registerToken,
|
||||
[
|
||||
adminClient,
|
||||
group.publicKey,
|
||||
admin.publicKey,
|
||||
usdcDevnetMint,
|
||||
usdcDevnetStubOracle.publicKey,
|
||||
|
||||
1,
|
||||
],
|
||||
);
|
||||
console.log(`UsdcBank ${usdcBank.publicKey}`);
|
||||
|
||||
//
|
||||
// User operations
|
||||
//
|
||||
|
||||
const user = Keypair.fromSecretKey(
|
||||
Buffer.from(
|
||||
JSON.parse(fs.readFileSync(process.env.USER_KEYPAIR!, 'utf-8')),
|
||||
),
|
||||
);
|
||||
const userWallet = new Wallet(user);
|
||||
const userProvider = new Provider(connection, userWallet, options);
|
||||
const userClient = await MangoClient.connect(userProvider, true);
|
||||
console.log(`User ${userWallet.publicKey.toBase58()}`);
|
||||
|
||||
//
|
||||
// Create mango account
|
||||
//
|
||||
const mangoAccount = await findOrCreate<MangoAccount>(
|
||||
'mangoAccount',
|
||||
getMangoAccountsForGroupAndOwner,
|
||||
[userClient, group.publicKey, user.publicKey],
|
||||
createMangoAccount,
|
||||
[userClient, group.publicKey, user.publicKey, 0],
|
||||
);
|
||||
console.log(`MangoAccount ${mangoAccount.publicKey}`);
|
||||
|
||||
// close mango account, note: close doesnt settle/withdraw for user atm,
|
||||
// only use when you want to free up a mango account address for testing on not-mainnet
|
||||
// await closeMangoAccount(userClient, account.publicKey, user.publicKey);
|
||||
// accounts = await getMangoAccountsForGroupAndOwner(
|
||||
// userClient,
|
||||
// group.publicKey,
|
||||
// user.publicKey,
|
||||
// );
|
||||
// if (accounts.length === 0) {
|
||||
// console.log(`Closed account ${account.publicKey}`);
|
||||
// }
|
||||
|
||||
//
|
||||
// Find existing or register a new serum3 market
|
||||
//
|
||||
|
||||
const serumMarketExternalPk = new web3.PublicKey(
|
||||
'DW83EpHFywBxCHmyARxwj3nzxJd7MUdSeznmrdzZKNZB',
|
||||
);
|
||||
const serum3Market = await findOrCreate<Serum3Market>(
|
||||
'serum3Market',
|
||||
getSerum3MarketForBaseAndQuote,
|
||||
[adminClient, group.publicKey, btcBank.tokenIndex, usdcBank.tokenIndex],
|
||||
serum3RegisterMarket,
|
||||
[
|
||||
adminClient,
|
||||
group.publicKey,
|
||||
admin.publicKey,
|
||||
DEVNET_SERUM3_PROGRAM_ID,
|
||||
serumMarketExternalPk,
|
||||
usdcBank.publicKey,
|
||||
btcBank.publicKey,
|
||||
0,
|
||||
],
|
||||
);
|
||||
console.log(`Serum3Market ${serum3Market.publicKey}`);
|
||||
|
||||
//
|
||||
// Serum3 OO
|
||||
//
|
||||
if (mangoAccount.serum3[0].marketIndex == 65535) {
|
||||
console.log('Creating serum3 open orders account...');
|
||||
await serum3CreateOpenOrders(
|
||||
userClient,
|
||||
group.publicKey,
|
||||
mangoAccount.publicKey,
|
||||
serum3Market.publicKey,
|
||||
DEVNET_SERUM3_PROGRAM_ID,
|
||||
serumMarketExternalPk,
|
||||
user.publicKey,
|
||||
);
|
||||
}
|
||||
|
||||
//
|
||||
// Deposit & withdraw
|
||||
//
|
||||
console.log(`Depositing...1000`);
|
||||
const btcTokenAccount = await spl.getAssociatedTokenAddress(
|
||||
btcDevnetMint,
|
||||
user.publicKey,
|
||||
);
|
||||
|
||||
// Aggregate all PKs of users active assets, banks, oracles and serum OOs
|
||||
const healthRemainingAccounts: PublicKey[] = [];
|
||||
{
|
||||
const mintInfos = await Promise.all(
|
||||
mangoAccount.tokens
|
||||
.filter((token) => token.tokenIndex !== 65535)
|
||||
.map(async (token) =>
|
||||
getMintInfoForTokenIndex(
|
||||
userClient,
|
||||
group.publicKey,
|
||||
token.tokenIndex,
|
||||
),
|
||||
),
|
||||
);
|
||||
// banks
|
||||
healthRemainingAccounts.push(
|
||||
...mintInfos.flatMap((mintinfos) => {
|
||||
return mintinfos.flatMap((mintinfo) => {
|
||||
return mintinfo.bank;
|
||||
});
|
||||
}),
|
||||
);
|
||||
// oracles
|
||||
healthRemainingAccounts.push(
|
||||
...mintInfos.flatMap((mintinfos) => {
|
||||
return mintinfos.flatMap((mintinfo) => {
|
||||
return mintinfo.oracle;
|
||||
});
|
||||
}),
|
||||
);
|
||||
// serum OOs
|
||||
healthRemainingAccounts.push(
|
||||
...mangoAccount.serum3
|
||||
.filter((serum3Account) => serum3Account.marketIndex !== 65535)
|
||||
.map((serum3Account) => serum3Account.openOrders),
|
||||
);
|
||||
}
|
||||
|
||||
await deposit(
|
||||
userClient,
|
||||
group.publicKey,
|
||||
mangoAccount.publicKey,
|
||||
btcBank.publicKey,
|
||||
btcBank.vault,
|
||||
btcTokenAccount,
|
||||
user.publicKey,
|
||||
healthRemainingAccounts,
|
||||
1000,
|
||||
);
|
||||
|
||||
console.log(`Withdrawing...500`);
|
||||
await withdraw(
|
||||
userClient,
|
||||
group.publicKey,
|
||||
mangoAccount.publicKey,
|
||||
btcBank.publicKey,
|
||||
btcBank.vault,
|
||||
btcTokenAccount,
|
||||
user.publicKey,
|
||||
healthRemainingAccounts,
|
||||
500,
|
||||
false,
|
||||
);
|
||||
|
||||
// log
|
||||
const freshBank = await getBank(userClient, btcBank.publicKey);
|
||||
console.log(freshBank.toString());
|
||||
|
||||
let freshAccount = await getMangoAccount(userClient, mangoAccount.publicKey);
|
||||
console.log(
|
||||
`- Mango account ${freshAccount.getNativeDeposit(
|
||||
freshBank,
|
||||
)} Deposits for bank ${freshBank.tokenIndex}`,
|
||||
);
|
||||
|
||||
//
|
||||
// Place serum3 order
|
||||
//
|
||||
console.log('Placing serum3 order...');
|
||||
const serum3MarketExternal = await Market.load(
|
||||
userClient.program.provider.connection,
|
||||
serumMarketExternalPk,
|
||||
{ commitment: userClient.program.provider.connection.commitment },
|
||||
DEVNET_SERUM3_PROGRAM_ID,
|
||||
);
|
||||
const serum3MarketExternalVaultSigner = await PublicKey.createProgramAddress(
|
||||
[
|
||||
serumMarketExternalPk.toBuffer(),
|
||||
serum3MarketExternal.decoded.vaultSignerNonce.toArrayLike(
|
||||
Buffer,
|
||||
'le',
|
||||
8,
|
||||
),
|
||||
],
|
||||
DEVNET_SERUM3_PROGRAM_ID,
|
||||
);
|
||||
const clientOrderId = Date.now();
|
||||
await serum3PlaceOrder(
|
||||
userClient,
|
||||
group.publicKey,
|
||||
mangoAccount.publicKey,
|
||||
user.publicKey,
|
||||
mangoAccount.serum3[0].openOrders,
|
||||
serum3Market.publicKey,
|
||||
DEVNET_SERUM3_PROGRAM_ID,
|
||||
serumMarketExternalPk,
|
||||
serum3MarketExternal.bidsAddress,
|
||||
serum3MarketExternal.asksAddress,
|
||||
serum3MarketExternal.decoded.eventQueue,
|
||||
serum3MarketExternal.decoded.requestQueue,
|
||||
serum3MarketExternal.decoded.baseVault,
|
||||
serum3MarketExternal.decoded.quoteVault,
|
||||
serum3MarketExternalVaultSigner,
|
||||
usdcBank.publicKey,
|
||||
usdcBank.vault,
|
||||
btcBank.publicKey,
|
||||
btcBank.vault,
|
||||
healthRemainingAccounts,
|
||||
Serum3Side.bid,
|
||||
40000,
|
||||
1,
|
||||
1000000,
|
||||
Serum3SelfTradeBehavior.decrementTake,
|
||||
Serum3OrderType.limit,
|
||||
clientOrderId,
|
||||
10,
|
||||
);
|
||||
|
||||
const ordersForOwner = await serum3MarketExternal.loadOrdersForOwner(
|
||||
userClient.program.provider.connection,
|
||||
group.publicKey,
|
||||
);
|
||||
const orderJustPlaced = ordersForOwner.filter(
|
||||
(order) => order.clientId?.toNumber() === clientOrderId,
|
||||
)[0];
|
||||
console.log(`- Serum3 order orderId ${orderJustPlaced.orderId}`);
|
||||
|
||||
process.exit(0);
|
||||
}
|
||||
|
||||
main();
|
|
@ -37,14 +37,14 @@ async function main() {
|
|||
console.log(`Group ${group.publicKey}`);
|
||||
|
||||
// register token 0
|
||||
const btcDevnetMint = new PublicKey(DEVNET_MINTS['BTC']);
|
||||
const btcDevnetOracle = new PublicKey(DEVNET_ORACLES['BTC']);
|
||||
const btcDevnetMint = new PublicKey(DEVNET_MINTS.get('BTC')!);
|
||||
const btcDevnetOracle = new PublicKey(DEVNET_ORACLES.get('BTC')!);
|
||||
try {
|
||||
await client.registerToken(group, btcDevnetMint, btcDevnetOracle, 0);
|
||||
} catch (error) {}
|
||||
|
||||
// stub oracle + register token 1
|
||||
const usdcDevnetMint = new PublicKey(DEVNET_MINTS['USDC']);
|
||||
const usdcDevnetMint = new PublicKey(DEVNET_MINTS.get('BTC')!);
|
||||
try {
|
||||
await client.createStubOracle(group, usdcDevnetMint, 1.0);
|
||||
} catch (error) {}
|
||||
|
@ -68,7 +68,7 @@ async function main() {
|
|||
|
||||
// register serum market
|
||||
const serumMarketExternalPk = new PublicKey(
|
||||
DEVNET_SERUM3_MARKETS['BTC/USDC'],
|
||||
DEVNET_SERUM3_MARKETS.get('BTC/USDC')!,
|
||||
);
|
||||
try {
|
||||
} catch (error) {
|
||||
|
|
38
ts/index.ts
38
ts/index.ts
|
@ -1,44 +1,14 @@
|
|||
export {
|
||||
Bank,
|
||||
getBank,
|
||||
getBankForGroupAndMint,
|
||||
getBanksForGroup,
|
||||
registerToken,
|
||||
registerTokenIx,
|
||||
} from './accounts/types/bank';
|
||||
export {
|
||||
createGroup,
|
||||
createGroupIx,
|
||||
getGroupForAdmin,
|
||||
Group,
|
||||
} from './accounts/types/group';
|
||||
export { Bank, getBankForGroupAndMint } from './accounts/types/bank';
|
||||
export { Group } from './accounts/types/group';
|
||||
export * from './accounts/types/I80F48';
|
||||
export {
|
||||
closeMangoAccount,
|
||||
closeMangoAccountIx,
|
||||
createMangoAccount,
|
||||
createMangoAccountIx,
|
||||
deposit,
|
||||
depositIx,
|
||||
getMangoAccount,
|
||||
getMangoAccountsForGroup,
|
||||
getMangoAccountsForGroupAndOwner,
|
||||
MangoAccount,
|
||||
TokenAccount,
|
||||
TokenAccountDto,
|
||||
withdraw,
|
||||
withdrawIx,
|
||||
} from './accounts/types/mangoAccount';
|
||||
export {
|
||||
createStubOracle,
|
||||
getStubOracleForGroupAndMint,
|
||||
setStubOracle,
|
||||
StubOracle,
|
||||
} from './accounts/types/oracle';
|
||||
export {
|
||||
getSerum3MarketForBaseAndQuote,
|
||||
Serum3Market,
|
||||
serum3RegisterMarket,
|
||||
serum3RegisterMarketIx,
|
||||
} from './accounts/types/serum3';
|
||||
export { StubOracle } from './accounts/types/oracle';
|
||||
export { Serum3Market } from './accounts/types/serum3';
|
||||
export * from './client';
|
||||
|
|
Loading…
Reference in New Issue