-
Notifications
You must be signed in to change notification settings - Fork 17
/
takeOrder.ts
73 lines (65 loc) · 1.91 KB
/
takeOrder.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
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,
OrderType,
SelfTradeBehavior,
} 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(
"2Hj72s8LRTs532YBDSU7R95DgHw2bSSN5nmwzeYwgJr3"
);
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 args: PlaceOrderArgs = {
side: Side.Bid,
priceLots: new BN(1000 + 1000),
maxBaseLots: new BN(1000),
maxQuoteLotsIncludingFees: new BN(100000000),
clientOrderId: new BN(105),
orderType: OrderType.Market,
expiryTimestamp: new BN(0),
selfTradeBehavior: SelfTradeBehavior.DecrementTake,
limit: 255,
};
let remainings = new Array<PublicKey>();
const [ix, signers] = await client.placeTakeOrderIx(
marketPublicKey,
market,
userBaseAcc.address,
userQuoteAcc.address,
null,
args,
remainings
);
const tx = await client.sendAndConfirmTransaction([ix], {
additionalSigners: [signers],
});
console.log("Take order ", tx);
}
main();