-
Notifications
You must be signed in to change notification settings - Fork 11
/
postOrderUI.ts
84 lines (74 loc) · 2.25 KB
/
postOrderUI.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import { AnchorProvider, BN, Wallet } from "@coral-xyz/anchor";
import { PublicKey, Connection } from "@solana/web3.js";
import { authority } from "./utils";
import { RPC, programId } from "./utils";
import {
OpenBookV2Client,
PlaceOrderArgs,
Side,
uiBaseToLots,
uiPriceToLots,
uiQuoteToLots,
} from "@openbook-dex/openbook-v2";
import { MintUtils } from "./mint_utils";
async function main() {
const wallet = new Wallet(authority);
const provider = new AnchorProvider(new Connection(RPC), wallet, {
commitment: "confirmed",
});
const client = new OpenBookV2Client(provider);
const openOrdersPublicKey = new PublicKey(
"EuaUfzypbyh5xtKD2nfHEfpQiTr8QSqu4VeRtLrfTF1c"
);
const marketPublicKey = new PublicKey(
"CwHc9CZ9UCZFayz4eBekuhhKsHapLDPYfX4tGFJrnTRt"
);
const market = await client.deserializeMarketAccount(marketPublicKey);
if (!market) {
throw "No market";
}
let mintUtils = new MintUtils(provider.connection, authority);
const userQuoteAcc = await mintUtils.getOrCreateTokenAccount(
market?.quoteMint,
authority,
client.walletPk
);
const userBaseAcc = await mintUtils.getOrCreateTokenAccount(
market?.baseMint,
authority,
client.walletPk
);
mintUtils.mintTo(market?.quoteMint, userQuoteAcc.address);
mintUtils.mintTo(market?.baseMint, userBaseAcc.address);
let side = Side.Bid;
let placeOrder = { limit: {} };
let selfTradeBehavior = { decrementTake: {} };
// Buy Sol at $20 with $100. Remember SBF was buying all at $3
// We set the maxBaseLots to maximum or big number to not restrict
const priceLots = uiPriceToLots(market, 20);
const maxQuoteLotsIncludingFees = uiQuoteToLots(market, 100);
const maxBaseLots = uiBaseToLots(market, 1000000);
let args: PlaceOrderArgs = {
side,
priceLots,
maxBaseLots,
maxQuoteLotsIncludingFees,
clientOrderId: new BN(123),
orderType: placeOrder,
expiryTimestamp: new BN(0),
selfTradeBehavior: selfTradeBehavior,
limit: 255,
};
const [ix, signers] = await client.placeOrderIx(
openOrdersPublicKey,
marketPublicKey,
market,
userQuoteAcc.address,
null,
args,
[]
);
const tx = await client.sendAndConfirmTransaction([ix], signers);
console.log("Placed order ", tx);
}
main();