-
Notifications
You must be signed in to change notification settings - Fork 17
/
postOrder.ts
111 lines (101 loc) · 2.98 KB
/
postOrder.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
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,
} 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);
const nbOrders: number = 10;
for (let i = 0; i < nbOrders; ++i) {
let side = Side.Bid;
let placeOrder = { limit: {} };
let selfTradeBehavior = { decrementTake: {} };
let args: PlaceOrderArgs = {
side,
priceLots: new BN(1000 - 1 - i),
maxBaseLots: new BN(10),
maxQuoteLotsIncludingFees: new BN(1000000),
clientOrderId: new BN(i),
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], {
additionalSigners: [signers],
});
console.log("Placed order ", tx);
}
for (let i = 0; i < nbOrders; ++i) {
let side = Side.Ask;
let placeOrder = { limit: {} };
let selfTradeBehavior = { decrementTake: {} };
let args: PlaceOrderArgs = {
side,
priceLots: new BN(1000 + 1 + i),
maxBaseLots: new BN(10000),
maxQuoteLotsIncludingFees: new BN(1000000),
clientOrderId: new BN(i + nbOrders + 1),
orderType: placeOrder,
expiryTimestamp: new BN(0),
selfTradeBehavior: selfTradeBehavior,
limit: 255,
};
let remainings = new Array<PublicKey>();
const [ix, signers] = await client.placeOrderIx(
openOrdersPublicKey,
marketPublicKey,
market,
userBaseAcc.address,
null,
args,
remainings
);
const tx = await client.sendAndConfirmTransaction([ix], {
additionalSigners: [signers],
});
console.log("Placed order ", tx);
}
}
main();