Submit DEX Order
Below are the samples of submitting/placing order on DEX.
Base Javascript and Python example submits order directly to chain, receiving back execution transaction. Note that the order_id returned from these examples may change in rare instances, due to blockchain forks or transaction propagation delays.
(Submit API) represents preparing a serialized transaction and signatures and posting to DEX endpoint, this provides order_id, ordinal_order_id and status of the orders created in your transaction. The ordinal order ID is a deterministic ID of the order that is fork and transaction propagation delay resistant.
// Example: XPR bid token, XMD ask token
// Replace accordingly
const USERNAME = 'pbonblockc'
const authorization = [{ actor: USERNAME, permission: 'active' }]
const BID_TOKEN_CONTRACT = 'eosio.token'
const BID_TOKEN_SYMBOL = 'XPR'
const BID_TOKEN_PRECISION = 4
const BID_AMOUNT = 700 // Amount of XPR to use for sell order
const ASK_TOKEN_CONTRACT = 'xmd.token'
const ASK_TOKEN_SYMBOL = 'XMD'
const ASK_TOKEN_PRECISION = 6
const ASK_AMOUNT = 10 // Amount of XMD to use for buy order
const MARKET_ID = 1 // Unique ID of market
const PRICE = 0.0021 // Price of XPR/XMD to place order at
const ORDER_SIDE = 2 // Buy = 1, Sell = 2
const ORDER_TYPE = 1 // Limit Order
const FILL_TYPE = 0 // Good Till Cancel
const [tokenContract, tokenSymbol, tokenPrecision, amount] =
ORDER_SIDE === 1
? [ASK_TOKEN_CONTRACT, ASK_TOKEN_SYMBOL, ASK_TOKEN_PRECISION, ASK_AMOUNT]
: [BID_TOKEN_CONTRACT, BID_TOKEN_SYMBOL, BID_TOKEN_PRECISION, BID_AMOUNT]
// Transaction actions
const actions = [
{
account: tokenContract,
name: 'transfer',
data: {
from: USERNAME,
to: 'dex',
quantity: `${amount.toFixed(tokenPrecision)} ${tokenSymbol}`,
memo: ''
},
authorization
},
{
account: 'dex',
name: 'placeorder',
data: {
market_id: MARKET_ID,
account: USERNAME,
order_type: ORDER_TYPE,
order_side: ORDER_SIDE,
fill_type: FILL_TYPE,
bid_symbol: {
sym: `${BID_TOKEN_PRECISION},${BID_TOKEN_SYMBOL}`,
contract: BID_TOKEN_CONTRACT
},
ask_symbol: {
sym: `${ASK_TOKEN_PRECISION},${ASK_TOKEN_SYMBOL}`,
contract: ASK_TOKEN_CONTRACT
},
referrer: '',
// Raw amount without precision (700.0000 XPR -> 7000000)
quantity: (amount * Math.pow(10, tokenPrecision)).toFixed(0),
// Raw ask price without precision, 6 in case of XMD (0.0021 -> 2100)
price: (PRICE * Math.pow(10, ASK_TOKEN_PRECISION)).toFixed(0),
// Trigger order price (0 if not trigger order)
trigger_price: 0
},
authorization
},
{
account: 'dex',
name: "process",
data: {
q_size: 20,
show_error_msg: 0
},
authorization
}
]
const main = async () => {
await transact(actions)
}
main()
Last updated