2022-04-07 08:16:46 -07:00
|
|
|
import { Provider, Wallet } from '@project-serum/anchor';
|
|
|
|
import { Connection, Keypair, PublicKey } from '@solana/web3.js';
|
|
|
|
import fs from 'fs';
|
2022-04-08 03:30:21 -07:00
|
|
|
import {
|
|
|
|
Serum3OrderType,
|
|
|
|
Serum3SelfTradeBehavior,
|
|
|
|
Serum3Side,
|
2022-04-12 08:28:47 -07:00
|
|
|
} from './accounts/serum3';
|
2022-04-07 08:16:46 -07:00
|
|
|
import { MangoClient } from './client';
|
2022-04-08 03:30:21 -07:00
|
|
|
import { DEVNET_GROUP, DEVNET_SERUM3_PROGRAM_ID } from './constants';
|
2022-04-07 08:16:46 -07:00
|
|
|
|
|
|
|
//
|
2022-04-07 10:52:13 -07:00
|
|
|
// An example for users based on high level api i.e. the client
|
2022-04-07 08:16:46 -07:00
|
|
|
//
|
|
|
|
async function main() {
|
|
|
|
const options = Provider.defaultOptions();
|
|
|
|
const connection = new Connection(
|
|
|
|
'https://mango.devnet.rpcpool.com',
|
|
|
|
options,
|
|
|
|
);
|
|
|
|
|
|
|
|
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 client = await MangoClient.connect(userProvider, true);
|
|
|
|
console.log(`User ${userWallet.publicKey.toBase58()}`);
|
|
|
|
|
2022-04-07 10:53:20 -07:00
|
|
|
// fetch group
|
2022-04-08 03:30:21 -07:00
|
|
|
const group = await client.getGroup(new PublicKey(DEVNET_GROUP));
|
|
|
|
console.log(`Group ${group.publicKey.toBase58()}`);
|
2022-04-07 08:58:20 -07:00
|
|
|
|
2022-04-09 12:18:07 -07:00
|
|
|
for (const bank of group.banksMap.values()) {
|
|
|
|
console.log(bank.publicKey.toBase58());
|
|
|
|
}
|
|
|
|
|
2022-04-07 10:53:20 -07:00
|
|
|
// create + fetch account
|
2022-04-08 03:30:21 -07:00
|
|
|
const mangoAccount = await client.getOrCreateMangoAccount(
|
|
|
|
group,
|
|
|
|
user.publicKey,
|
|
|
|
0,
|
2022-04-12 07:19:58 -07:00
|
|
|
'my_mango_account',
|
2022-04-08 03:30:21 -07:00
|
|
|
);
|
2022-04-07 08:58:20 -07:00
|
|
|
console.log(`MangoAccount ${mangoAccount.publicKey}`);
|
|
|
|
|
2022-04-07 10:53:20 -07:00
|
|
|
// deposit and withdraw
|
2022-04-08 11:47:12 -07:00
|
|
|
console.log(`Depositing...5000000`);
|
2022-04-08 11:51:22 -07:00
|
|
|
await client.deposit(group, mangoAccount, 'USDC', 50_000000);
|
2022-04-08 11:47:12 -07:00
|
|
|
await client.deposit(group, mangoAccount, 'BTC', 5000000);
|
|
|
|
console.log(`Withdrawing...1000000`);
|
2022-04-08 11:51:22 -07:00
|
|
|
await client.withdraw(group, mangoAccount, 'USDC', 1_000000, false);
|
2022-04-07 23:29:35 -07:00
|
|
|
|
|
|
|
// serum3
|
2022-04-08 07:57:37 -07:00
|
|
|
console.log(
|
|
|
|
`Placing serum3 bid which would not be settled since its relatively low then midprice`,
|
|
|
|
);
|
|
|
|
await client.serum3PlaceOrder(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
DEVNET_SERUM3_PROGRAM_ID,
|
|
|
|
'BTC/USDC',
|
|
|
|
Serum3Side.bid,
|
2022-04-08 11:47:12 -07:00
|
|
|
20,
|
2022-04-08 07:57:37 -07:00
|
|
|
0.0001,
|
|
|
|
Serum3SelfTradeBehavior.decrementTake,
|
|
|
|
Serum3OrderType.limit,
|
|
|
|
Date.now(),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
console.log(`Placing serum3 bid way above midprice`);
|
2022-04-08 03:30:21 -07:00
|
|
|
await client.serum3PlaceOrder(
|
2022-04-07 23:29:35 -07:00
|
|
|
group,
|
2022-04-08 03:30:21 -07:00
|
|
|
mangoAccount,
|
|
|
|
DEVNET_SERUM3_PROGRAM_ID,
|
|
|
|
'BTC/USDC',
|
|
|
|
Serum3Side.bid,
|
2022-04-08 07:57:37 -07:00
|
|
|
90000,
|
|
|
|
0.0001,
|
|
|
|
Serum3SelfTradeBehavior.decrementTake,
|
|
|
|
Serum3OrderType.limit,
|
|
|
|
Date.now(),
|
|
|
|
10,
|
|
|
|
);
|
|
|
|
console.log(`Placing serum3 ask way below midprice`);
|
|
|
|
await client.serum3PlaceOrder(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
DEVNET_SERUM3_PROGRAM_ID,
|
|
|
|
'BTC/USDC',
|
|
|
|
Serum3Side.ask,
|
|
|
|
30000,
|
|
|
|
0.0001,
|
2022-04-08 03:30:21 -07:00
|
|
|
Serum3SelfTradeBehavior.decrementTake,
|
|
|
|
Serum3OrderType.limit,
|
|
|
|
Date.now(),
|
|
|
|
10,
|
2022-04-07 23:29:35 -07:00
|
|
|
);
|
|
|
|
|
2022-04-08 07:57:37 -07:00
|
|
|
console.log(`Current own orders on OB...`);
|
|
|
|
let orders = await client.getSerum3Orders(
|
|
|
|
group,
|
|
|
|
DEVNET_SERUM3_PROGRAM_ID,
|
|
|
|
'BTC/USDC',
|
|
|
|
);
|
|
|
|
for (const order of orders) {
|
|
|
|
console.log(
|
|
|
|
`Order orderId ${order.orderId}, ${order.side}, ${order.price}, ${order.size}`,
|
|
|
|
);
|
|
|
|
console.log(`Cancelling order with ${order.orderId}`);
|
2022-04-08 11:47:12 -07:00
|
|
|
await client.serum3CancelOrder(
|
2022-04-08 07:57:37 -07:00
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
DEVNET_SERUM3_PROGRAM_ID,
|
|
|
|
'BTC/USDC',
|
|
|
|
order.side === 'buy' ? Serum3Side.bid : Serum3Side.ask,
|
|
|
|
order.orderId,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
console.log(`Current own orders on OB...`);
|
|
|
|
orders = await client.getSerum3Orders(
|
|
|
|
group,
|
|
|
|
DEVNET_SERUM3_PROGRAM_ID,
|
|
|
|
'BTC/USDC',
|
|
|
|
);
|
|
|
|
for (const order of orders) {
|
|
|
|
console.log(order);
|
|
|
|
}
|
|
|
|
|
2022-04-08 08:21:49 -07:00
|
|
|
// console.log(`Close mango account...`);
|
|
|
|
// await client.closeMangoAccount(mangoAccount);
|
|
|
|
|
2022-04-08 11:47:12 -07:00
|
|
|
console.log(`Settling funds...`);
|
|
|
|
await client.serum3SettleFunds(
|
|
|
|
group,
|
|
|
|
mangoAccount,
|
|
|
|
DEVNET_SERUM3_PROGRAM_ID,
|
|
|
|
'BTC/USDC',
|
|
|
|
);
|
|
|
|
|
2022-04-07 23:29:35 -07:00
|
|
|
process.exit();
|
2022-04-07 08:16:46 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
main();
|