-
Notifications
You must be signed in to change notification settings - Fork 10
/
create_order.ts
65 lines (59 loc) · 1.99 KB
/
create_order.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
import { BigNumber } from "bignumber.js";
import * as BIP39 from "bip39";
import { OrderModule } from "../lib";
import { MsgCreateOrder } from "../lib/codec/order/tx";
import { CarbonTx } from "../lib/util";
import { CarbonSDK } from "./_sdk";
import "./_setup";
(async () => {
const mnemonics = process.env.MNEMONICS ?? BIP39.generateMnemonic();
console.log("mnemonics", mnemonics);
const sdk = await CarbonSDK.instance({
network: CarbonSDK.Network.LocalHost,
config: {
tmRpcUrl: process.env.TRPC_ENDPOINT,
},
});
const connectedSDK = await sdk.connectWithMnemonic(mnemonics);
console.log("connected sdk");
// retrieve market to use for order creation
const marketsResult = await sdk.query.market.MarketAll({})
console.log("markets", marketsResult.markets);
if (!marketsResult.markets.length) {
throw new Error("no markets found, create a market first");
}
const [market] = marketsResult.markets
// create an order using generic
// CarbonWallet.sendTx, no type checking
// on inputs
const genericTxValue: MsgCreateOrder = {
creator: connectedSDK.wallet.bech32Address,
isPostOnly: false,
isReduceOnly: false,
market: market.name,
orderType: "limit",
price: "1",
quantity: "100",
side: "buy",
stopPrice: "",
timeInForce: "gtc",
triggerType: "",
referralAddress: "",
referralCommission: 0,
}
const genericTxCallResult = await connectedSDK.wallet.sendTx({
typeUrl: CarbonTx.Types.MsgCreateOrder,
value: genericTxValue,
})
console.log("call generic tx", genericTxCallResult)
// create an order using Order Module
// for better input type checking
const moduleCallResult = await connectedSDK.order.create({
market: market.name,
orderType: OrderModule.OrderType.Limit,
price: new BigNumber(100),
quantity: new BigNumber(market.minQuantity),
side: OrderModule.OrderSide.Buy,
});
console.log("call from module", moduleCallResult);
})().catch(console.error).finally(() => process.exit(0));