commit
c93670412d
|
@ -0,0 +1,40 @@
|
|||
# Mango Client
|
||||
A client to engage with the mango program
|
||||
|
||||
|
||||
### Example
|
||||
```
|
||||
async function main() {
|
||||
const client = new MangoClient()
|
||||
const cluster = 'mainnet-beta'
|
||||
const clusterUrl = process.env.CLUSTER_URL || IDS.cluster_urls[cluster]
|
||||
const connection = new Connection(clusterUrl, 'singleGossip')
|
||||
const programId = new PublicKey(IDS[cluster].mango_program_id)
|
||||
const dexProgramId = new PublicKey(IDS[cluster].dex_program_id)
|
||||
const mangoGroupPk = new PublicKey(IDS[cluster].mango_groups['BTC_ETH_USDT'].mango_group_pk)
|
||||
|
||||
const keyPairPath = process.env.KEYPAIR || homedir() + '/.config/solana/id.json'
|
||||
|
||||
const payer = new Account(JSON.parse(fs.readFileSync(keyPairPath, 'utf-8')))
|
||||
const mangoGroup = await client.getMangoGroup(connection, mangoGroupPk)
|
||||
const prices = await mangoGroup.getPrices(connection)
|
||||
const marginAccounts = (await client.getMarginAccountsForOwner(connection, programId, mangoGroup, payer))
|
||||
marginAccounts.sort(
|
||||
(a, b) => (a.computeValue(mangoGroup, prices) > b.computeValue(mangoGroup, prices) ? -1 : 1)
|
||||
)
|
||||
let marginAccount = marginAccounts[0]
|
||||
|
||||
const market = await Market.load(connection, mangoGroup.spotMarkets[0], { skipPreflight: true, commitment: 'singleGossip'}, mangoGroup.dexProgramId)
|
||||
console.log('placing order')
|
||||
const txid = await client.placeOrder(connection, programId, mangoGroup, marginAccount, market, payer, 'buy', 48000, 0.0001)
|
||||
console.log('order placed')
|
||||
|
||||
await sleep(5000)
|
||||
marginAccount = await client.getMarginAccount(connection, marginAccount.publicKey, mangoGroup.dexProgramId)
|
||||
const bids = await market.loadBids(connection)
|
||||
const asks = await market.loadAsks(connection)
|
||||
console.log('canceling orders')
|
||||
await marginAccount.cancelAllOrdersByMarket(connection, client, programId, mangoGroup, market, bids, asks, payer)
|
||||
console.log('orders canceled')
|
||||
}
|
||||
```
|
|
@ -2,7 +2,8 @@ import {
|
|||
Account,
|
||||
Connection,
|
||||
PublicKey,
|
||||
sendAndConfirmRawTransaction, SimulatedTransactionResponse,
|
||||
sendAndConfirmRawTransaction,
|
||||
SimulatedTransactionResponse,
|
||||
SYSVAR_CLOCK_PUBKEY,
|
||||
SYSVAR_RENT_PUBKEY,
|
||||
Transaction,
|
||||
|
@ -12,18 +13,25 @@ import {
|
|||
import {
|
||||
encodeMangoInstruction,
|
||||
MangoGroupLayout,
|
||||
MarginAccountLayout, MAX_RATE,
|
||||
MarginAccountLayout,
|
||||
MAX_RATE,
|
||||
NUM_MARKETS,
|
||||
NUM_TOKENS, OPTIMAL_RATE, OPTIMAL_UTIL,
|
||||
NUM_TOKENS,
|
||||
OPTIMAL_RATE,
|
||||
OPTIMAL_UTIL,
|
||||
WideBits,
|
||||
} from './layout';
|
||||
import BN from 'bn.js';
|
||||
import {
|
||||
awaitTransactionSignatureConfirmation,
|
||||
createAccountInstruction,
|
||||
getFilteredProgramAccounts, getUnixTs,
|
||||
nativeToUi, parseTokenAccountData,
|
||||
promiseUndef, simulateTransaction, sleep,
|
||||
getFilteredProgramAccounts,
|
||||
getUnixTs,
|
||||
nativeToUi,
|
||||
parseTokenAccountData,
|
||||
promiseUndef,
|
||||
simulateTransaction,
|
||||
sleep,
|
||||
uiToNative,
|
||||
zeroKey,
|
||||
} from './utils';
|
||||
|
@ -32,7 +40,7 @@ import { SRM_DECIMALS } from '@project-serum/serum/lib/token-instructions';
|
|||
import { Order } from '@project-serum/serum/lib/market';
|
||||
import Wallet from '@project-serum/sol-wallet-adapter';
|
||||
import { makeCancelOrderInstruction, makeSettleFundsInstruction } from './instruction';
|
||||
import { Aggregator } from './schema'
|
||||
import { Aggregator } from './schema';
|
||||
import { TOKEN_PROGRAM_ID } from '@solana/spl-token';
|
||||
|
||||
|
||||
|
|
152
src/index.ts
152
src/index.ts
|
@ -7,155 +7,3 @@ export * from './layout';
|
|||
export * from './utils'
|
||||
|
||||
export { IDS }
|
||||
|
||||
|
||||
// async function tests() {
|
||||
// const cluster = "mainnet-beta";
|
||||
// const client = new MangoClient();
|
||||
// const clusterIds = IDS[cluster]
|
||||
//
|
||||
// const connection = new Connection(IDS.cluster_urls[cluster], 'singleGossip')
|
||||
// const mangoGroupPk = new PublicKey(clusterIds.mango_groups.BTC_ETH_USDT.mango_group_pk);
|
||||
// const mangoProgramId = new PublicKey(clusterIds.mango_program_id);
|
||||
// const keyPairPath = homedir() + '/.config/solana/blw.json'
|
||||
// const payer = new Account(JSON.parse(fs.readFileSync(keyPairPath, 'utf-8')))
|
||||
//
|
||||
// async function testInitMarginAccount() {
|
||||
// const mangoGroup = await client.getMangoGroup(connection, mangoGroupPk)
|
||||
// for (const token of mangoGroup.tokens) {
|
||||
// console.log(token.toBase58())
|
||||
// }
|
||||
// const marginAccountPk = await client.initMarginAccount(connection, mangoProgramId, mangoGroup, payer)
|
||||
// console.log(marginAccountPk.toBase58())
|
||||
// await sleep(2000)
|
||||
// const marginAccount = await client.getMarginAccount(connection, marginAccountPk, mangoGroup.dexProgramId)
|
||||
// console.log(marginAccount.toPrettyString(mangoGroup, await mangoGroup.getPrices(connection)))
|
||||
// console.log(marginAccount.owner.toBase58())
|
||||
// }
|
||||
//
|
||||
// await testInitMarginAccount()
|
||||
// }
|
||||
// tests()
|
||||
|
||||
|
||||
// async function testMangoGroup() {
|
||||
|
||||
// const oraclePk = new PublicKey(IDS['mainnet-beta'].oracles['BTC/USDT'])
|
||||
//
|
||||
// const mangoGroup = await client.getMangoGroup(connection, mangoGroupPk)
|
||||
//
|
||||
// console.log(mangoGroup)
|
||||
// }
|
||||
//
|
||||
// testMangoGroup()
|
||||
//
|
||||
// async function testSolink() {
|
||||
// const cluster = "mainnet-beta";
|
||||
// const client = new MangoClient();
|
||||
// const clusterIds = IDS[cluster]
|
||||
//
|
||||
// const connection = new Connection(IDS.cluster_urls[cluster], 'singleGossip')
|
||||
// const mangoGroupPk = new PublicKey(clusterIds.mango_groups['BTC_ETH_USDT'].mango_group_pk);
|
||||
// const mangoProgramId = new PublicKey(clusterIds.mango_program_id);
|
||||
// const oraclePk = new PublicKey(IDS[cluster].oracles['ETH/USDT'])
|
||||
// const agg = await Aggregator.loadWithConnection(oraclePk, connection)
|
||||
//
|
||||
// // const agg = await Aggregator.loadWithConnection(oraclePk, connection)
|
||||
// console.log(agg.answer.median.toNumber(), agg.answer.updatedAt.toNumber(), agg.round.id.toNumber())
|
||||
//
|
||||
// }
|
||||
//
|
||||
// testSolink()
|
||||
|
||||
|
||||
// async function main() {
|
||||
// const cluster = "devnet";
|
||||
// const client = new MangoClient();
|
||||
// const clusterIds = IDS[cluster]
|
||||
//
|
||||
// const connection = new Connection(IDS.cluster_urls[cluster], 'singleGossip')
|
||||
// const mangoGroupPk = new PublicKey(clusterIds.mango_groups.BTC_ETH_USDC.mango_group_pk);
|
||||
// const mangoProgramId = new PublicKey(clusterIds.mango_program_id);
|
||||
//
|
||||
// const mangoGroup = await client.getMangoGroup(connection, mangoGroupPk);
|
||||
//
|
||||
// const keyPairPath = '/home/dd/.config/solana/id.json'
|
||||
// const payer = new Account(JSON.parse(fs.readFileSync(keyPairPath, 'utf-8')))
|
||||
//
|
||||
// // TODO auto fetch
|
||||
// const marginAccountPk = new PublicKey("DrKz21L9EqmykcEtiX3sY7ibHbVezgSY225upn3UA8Ju")
|
||||
// let marginAccount = await client.getMarginAccount(connection, marginAccountPk)
|
||||
//
|
||||
// console.log(marginAccount.toPrettyString(mangoGroup))
|
||||
//
|
||||
// const marketIndex = 0 // index for BTC/USDC
|
||||
// const spotMarket = await Market.load(
|
||||
// connection,
|
||||
// mangoGroup.spotMarkets[marketIndex],
|
||||
// {skipPreflight: true, commitment: 'singleGossip'},
|
||||
// mangoGroup.dexProgramId
|
||||
// )
|
||||
// const prices = await mangoGroup.getPrices(connection)
|
||||
// console.log(prices)
|
||||
//
|
||||
// // // margin short 0.1 BTC
|
||||
// // await client.placeOrder(
|
||||
// // connection,
|
||||
// // mangoProgramId,
|
||||
// // mangoGroup,
|
||||
// // marginAccount,
|
||||
// // spotMarket,
|
||||
// // payer,
|
||||
// // 'sell',
|
||||
// // 30000,
|
||||
// // 0.1
|
||||
// // )
|
||||
// //
|
||||
// // await spotMarket.matchOrders(connection, payer, 10)
|
||||
// //
|
||||
// await client.settleFunds(
|
||||
// connection,
|
||||
// mangoProgramId,
|
||||
// mangoGroup,
|
||||
// marginAccount,
|
||||
// payer,
|
||||
// spotMarket
|
||||
// )
|
||||
// //
|
||||
// // await client.settleBorrow(connection, mangoProgramId, mangoGroup, marginAccount, payer, mangoGroup.tokens[2], 5000)
|
||||
// // await client.settleBorrow(connection, mangoProgramId, mangoGroup, marginAccount, payer, mangoGroup.tokens[0], 1.0)
|
||||
//
|
||||
// marginAccount = await client.getMarginAccount(connection, marginAccount.publicKey)
|
||||
// console.log(marginAccount.toPrettyString(mangoGroup))
|
||||
// }
|
||||
// main()
|
||||
|
||||
//
|
||||
// async function testAll() {
|
||||
// const cluster = "devnet"
|
||||
// const client = new MangoClient()
|
||||
// const clusterIds = IDS[cluster]
|
||||
//
|
||||
// const connection = new Connection(IDS.cluster_urls[cluster], 'singleGossip')
|
||||
// const mangoGroupPk = new PublicKey(clusterIds.mango_groups.BTC_ETH_USDC.mango_group_pk);
|
||||
// const mangoProgramId = new PublicKey(clusterIds.mango_program_id);
|
||||
//
|
||||
// const mangoGroup = await client.getMangoGroup(connection, mangoGroupPk);
|
||||
//
|
||||
// const keyPairPath = '/home/dd/.config/solana/id.json'
|
||||
// const payer = new Account(JSON.parse(fs.readFileSync(keyPairPath, 'utf-8')))
|
||||
//
|
||||
// // TODO auto fetch
|
||||
// const marginAccounts = await client.getMarginAccountsForOwner(connection, mangoProgramId, mangoGroup, payer)
|
||||
// for (const x of marginAccounts) {
|
||||
// // get value of each margin account and select highest
|
||||
//
|
||||
// console.log(x.publicKey.toBase58())
|
||||
// }
|
||||
//
|
||||
// }
|
||||
//
|
||||
//
|
||||
//
|
||||
//
|
||||
// testAll()
|
||||
|
|
Loading…
Reference in New Issue