-
Notifications
You must be signed in to change notification settings - Fork 11
/
cancelAndPost.ts
90 lines (79 loc) · 2.2 KB
/
cancelAndPost.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
import { AnchorProvider, BN, Wallet } from "@coral-xyz/anchor";
import { PublicKey, Connection, Transaction } from "@solana/web3.js";
import { authority } from "./utils";
import { RPC, programId } from "./utils";
import {
OpenBookV2Client,
PlaceOrderArgs,
Side,
OrderType,
SelfTradeBehavior,
PlaceMultipleOrdersArgs,
} 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 marketPublicKey = new PublicKey(
"C3YPL3kYCSYKsmHcHrPWx1632GUXGqi2yMXJbfeCc57q"
);
const ooa = await client.findOpenOrdersForMarket(
wallet.publicKey,
marketPublicKey
);
if (ooa === null || ooa.length < 1) {
throw "No ooa";
}
const openOrdersPublicKey = ooa[0];
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
);
const nbOrders: number = 14;
let bids: PlaceMultipleOrdersArgs[] = [];
for (let i = 0; i < nbOrders; ++i) {
bids.push({
priceLots: new BN(100 - 1 - i),
maxQuoteLotsIncludingFees: new BN(1000),
expiryTimestamp: new BN(0),
});
}
let asks: PlaceMultipleOrdersArgs[] = [];
for (let i = 0; i < nbOrders; ++i) {
asks.push({
priceLots: new BN(100 + 1 + i),
maxQuoteLotsIncludingFees: new BN(1000),
expiryTimestamp: new BN(0),
});
}
const [ix, signers] = await client.cancelAllAndPlaceOrdersIx(
openOrdersPublicKey,
marketPublicKey,
market,
userBaseAcc.address,
userQuoteAcc.address,
null,
OrderType.ImmediateOrCancel,
bids,
asks
);
const tx = await client.sendAndConfirmTransaction([ix], {
additionalSigners: [signers],
});
console.log("Cancel and place order ", tx);
}
main();