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()
const { JsonRpc, Api, JsSignatureProvider } = require('@proton/js')
const fetch = require('node-fetch');
// For testnet use https://rpc.api.testnet.metalx.com
const ENDPOINTS = [
'https://rpc.api.mainnet.metalx.com',
]
// For testnet use "https://dex.api.testnet.metalx.com/dex/v1/orders/submit"
const url = "https://dex.api.mainnet.metalx.com/dex/v1/orders/submit"
// To export private key from your wallet, follow:
// https://help.xprnetwork.org/hc/en-us/articles/4410313687703-How-do-I-backup-my-private-key-in-the-WebAuth-Wallet-
const PRIVATE_KEY = 'PVT_K1_2fdW4UGdbgG59mUaTiMXLy1rFv3afbmSWEZGWFz6zF8dR1VZPb'
// Initialize
const rpc = new JsonRpc(ENDPOINTS)
const api = new Api({
rpc,
signatureProvider: new JsSignatureProvider([PRIVATE_KEY])
})
const transact = (actions) => api.transact({actions}, {
blocksBehind: 300,
expireSeconds: 3000,
broadcast: false,
})
// Example: XPR bid token, XMD ask token
// Replace accordingly
const USERNAME = 'metaltest1'
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: 25,
show_error_msg: 0
},
authorization
}
]
async function main() {
const { serializedTransaction, serializedContextFreeData, signatures } = await transact(actions)
const hexTrx = Buffer.from(serializedTransaction).toString('hex')
const response = await fetch(url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
'serialized_tx_hex': hexTrx,
'signatures': signatures
}),
});
const data = await response.json();
console.dir(data, { depth: null })
}
main()
import os
import json
import requests
from pyeoskit import eosapi, wallet
from math import pow
# Import your account private key here
wallet.import_key('mywallet', 'PVT_K1_2fdW4UGdbgG59mUaTiMXLy1rFv3afbmSWEZGWFz6zF8dR1VZPb')
# For testnet, use https://rpc.api.testnet.metalx.com
eosapi.set_node('https://rpc.api.mainnet.metalx.com')
# Replace with your account name
USERNAME = 'username'
permission = {USERNAME: 'active'}
# Replace Trade Parameters
BID_TOKEN_CONTRACT = 'eosio.token'
BID_TOKEN_SYMBOL = 'XPR'
BID_TOKEN_PRECISION = 4
BID_AMOUNT = 700 # Amount of XPR to use for sell order
ASK_TOKEN_CONTRACT = 'xmd.token'
ASK_TOKEN_SYMBOL = 'XMD'
ASK_TOKEN_PRECISION = 6
ASK_AMOUNT = 10 # Amount of XMD to use for buy order
MARKET_ID = 1 # Unique ID of market
PRICE = 0.0021 # Price of XPR/XMD to place order at
ORDER_SIDE = 2 # Buy = 1, Sell = 2
ORDER_TYPE = 1 # Limit Order
FILL_TYPE = 0 # Good Till Cancel
if ORDER_SIDE == 1:
token_contract = ASK_TOKEN_CONTRACT
token_symbol = ASK_TOKEN_SYMBOL
token_precision = ASK_TOKEN_PRECISION
amount = ASK_AMOUNT
else:
token_contract = BID_TOKEN_CONTRACT
token_symbol = BID_TOKEN_SYMBOL
token_precision = BID_TOKEN_PRECISION
amount = BID_AMOUNT
args1 = {
'from': USERNAME,
'to': 'dex',
'quantity': f'{amount:.{token_precision}f} {token_symbol}',
'memo': ''
}
args2 = {
'market_id': MARKET_ID,
'account': USERNAME,
'order_type': ORDER_TYPE,
'order_side': ORDER_SIDE,
'fill_type': FILL_TYPE,
'bid_symbol': {
'sym': f'{BID_TOKEN_PRECISION},{BID_TOKEN_SYMBOL}',
'contract': BID_TOKEN_CONTRACT
},
'ask_symbol': {
'sym': f'{ASK_TOKEN_PRECISION},{ASK_TOKEN_SYMBOL}',
'contract': ASK_TOKEN_CONTRACT
},
'referrer': '',
'quantity': int(amount * pow(10, token_precision)),
'price': int(PRICE * pow(10, ASK_TOKEN_PRECISION)),
'trigger_price': 0
}
args3 = {
'q_size': 20,
'show_error_msg': 0
}
a1 = [token_contract, 'transfer', args1, permission]
a2 = ['dex', 'placeorder', args2, permission]
a3 = ['dex', 'process', args3, permission]
eosapi.push_actions([a1, a2, a3])
import os
import json
import requests
from pyeoskit import eosapi, wallet
from math import pow
# Import your account private key here
wallet.import_key('mywallet', 'PVT_K1_2fdW4UGdbgG59mUaTiMXLy1rFv3afbmSWEZGWFz6zF8dR1VZPb')
# For testnet, use https://rpc.api.testnet.metalx.com
eosapi.set_node('https://rpc.api.mainnet.metalx.com')
# Replace with your account name
USERNAME = 'username'
permission = {USERNAME: 'active'}
# For testnet use "https://dex.api.testnet.metalx.com/dex/v1/orders/submit"
url = "https://dex.api.mainnet.metalx.com/dex/v1/orders/submit"
headers = {"content-type": "application/json", "Accept-Charset": "UTF-8"}
# Replace Trade Parameters
BID_TOKEN_CONTRACT = 'eosio.token'
BID_TOKEN_SYMBOL = 'XPR'
BID_TOKEN_PRECISION = 4
BID_AMOUNT = 700 # Amount of XPR to use for sell order
ASK_TOKEN_CONTRACT = 'xmd.token'
ASK_TOKEN_SYMBOL = 'XMD'
ASK_TOKEN_PRECISION = 6
ASK_AMOUNT = 10 # Amount of XMD to use for buy order
MARKET_ID = 1 # Unique ID of market
PRICE = 0.0021 # Price of XPR/XMD to place order at
ORDER_SIDE = 2 # Buy = 1, Sell = 2
ORDER_TYPE = 1 # Limit Order
FILL_TYPE = 0 # Good Till Cancel
if ORDER_SIDE == 1:
token_contract = ASK_TOKEN_CONTRACT
token_symbol = ASK_TOKEN_SYMBOL
token_precision = ASK_TOKEN_PRECISION
amount = ASK_AMOUNT
else:
token_contract = BID_TOKEN_CONTRACT
token_symbol = BID_TOKEN_SYMBOL
token_precision = BID_TOKEN_PRECISION
amount = BID_AMOUNT
args1 = {
'from': USERNAME,
'to': 'dex',
'quantity': f'{amount:.{token_precision}f} {token_symbol}',
'memo': ''
}
args2 = {
'market_id': MARKET_ID,
'account': USERNAME,
'order_type': ORDER_TYPE,
'order_side': ORDER_SIDE,
'fill_type': FILL_TYPE,
'bid_symbol': {
'sym': f'{BID_TOKEN_PRECISION},{BID_TOKEN_SYMBOL}',
'contract': BID_TOKEN_CONTRACT
},
'ask_symbol': {
'sym': f'{ASK_TOKEN_PRECISION},{ASK_TOKEN_SYMBOL}',
'contract': ASK_TOKEN_CONTRACT
},
'referrer': '',
'quantity': int(amount * pow(10, token_precision)),
'price': int(PRICE * pow(10, ASK_TOKEN_PRECISION)),
'trigger_price': 0
}
args3 = {
'q_size': 20,
'show_error_msg': 0
}
a1 = [token_contract, 'transfer', args1, permission]
a2 = ['dex', 'placeorder', args2, permission]
a3 = ['dex', 'process', args3, permission]
# Generate packed transaction
info = eosapi.get_info()
final_tx = eosapi.generate_packed_transaction(
[a1, a2, a3],
60,
info['last_irreversible_block_id'],
info['chain_id']
)
mtx = json.loads(final_tx)
# Print transaction details
print('\nSignature:', mtx["signatures"], '\nPacked Tx:', mtx["packed_trx"])
# Submit transaction
payload = {
"serialized_tx_hex": mtx["packed_trx"],
"signatures": mtx["signatures"]
}
response = requests.post(url, json=payload, headers=headers)
data = response.json()
# Print response data
print(data)
Last updated