@kanalabs/trade
v3.0.19
Published
SDK to interact with econia core functions
Downloads
202
Readme
INSTALLATION
npm i @kanalabs/trade
MODULE IMPORTS
import { BUY,
EconiaTrade,
RegisteredMarket,
TakerEvents
} from '@kanalabs/trade';
INITIALISING THE SDK
const client = new AptosClient('https://fullnode.testnet.aptoslabs.com/v1');
const econia = new EconiaTrade(client);
TESTNET FAUCET
import { getPayloadForTestAptFaucet, getPayloadForTestUsdcFaucet } from '@kanalabs/trade';
const payload = getPayloadForTestAptFaucet()
const transaction = await client.generateTransaction(account.address(), payload);
const sign = await client.signTransaction(account,transaction)
const submit = await client.submitTransaction(sign)
await client.waitForTransaction(submit.hash)
const payload = getPayloadForTestUsdcFaucet()
const transaction = await client.generateTransaction(account.address(), payload);
const sign = await client.signTransaction(account,transaction)
const submit = await client.submitTransaction(sign)
await client.waitForTransaction(submit.hash)
EVENT SUBSCRIPTION
import { AptosAccount, AptosClient } from 'aptos';
import {
ENVIRONMENT,
EconiaMarkets,
EconiaTrade,
RegisteredMarket,
TradeTable,
eventId,
subscribeEvents,
} from '@kanalabs/trade';
import { config } from 'dotenv';
config({ path: '../../.env' });
import { TradeEventEmitter, tradeEventEmitter } from '@kanalabs/trade';
const client = new AptosClient('https://fullnode.testnet.aptoslabs.com/v1');
tradeEventEmitter.on(eventId.orderBook, (data) => {
console.log(data);
});
tradeEventEmitter.on(eventId.tradeHistory, (data) => {
console.log(data);
});
const orderBook = async () => {
const econia = new EconiaTrade(client);
let registeredMarkets: RegisteredMarket[] = [];
let marketData: EconiaMarkets;
registeredMarkets = await econia.fetchRegisteredMarkets(ENVIRONMENT.TESTNET); //tested
marketData = await econia.markets(registeredMarkets.filter((market) => market.marketId == 3)[0], ENVIRONMENT.TESTNET);
// // Example usage
const myObject = new TradeEventEmitter(marketData);
await myObject.triggerToOrderBook();
//To unsubscribe events
setTimeout(() => {
myObject.terminateOrderBookEvent();
}, 20000); // Stop after 5 seconds
};
orderBook();
const tradeHistory = async () => {
const econia = new EconiaTrade(client);
let registeredMarkets: RegisteredMarket[] = [];
let marketData: EconiaMarkets;
registeredMarkets = await econia.fetchRegisteredMarkets(ENVIRONMENT.TESTNET); //tested
marketData = await econia.markets(registeredMarkets.filter((market) => market.marketId == 3)[0], ENVIRONMENT.TESTNET);
// // Example usage
const myObject = new TradeEventEmitter(marketData);
await myObject.triggerTradeHistory();
//To unsubscribe events
setTimeout(() => {
myObject.terminateOrderHistoryEvent();
}, 100000); // Stop after 5 seconds
};
tradeHistory();
GETTING ALL REGISTERED MARKETS
const registeredMarkets = await econia.fetchRegisteredMarkets();
Example Response
Returns an array of type RegisteredMarket[]
{
baseNameGeneric: '',
baseType: StructTag$1 {
address: [HexString],
module: 'aptos_coin',
name: 'AptosCoin',
typeParams: [],
kind: 'StructTag'
},
lotSize: BigNumber { s: 1, e: 0, c: [Array] },
marketId: 5,
minSize: BigNumber { s: 1, e: 0, c: [Array] },
quoteType: StructTag$1 {
address: [HexString],
module: 'coins',
name: 'USDT',
typeParams: [],
kind: 'StructTag'
},
tickSize: BigNumber { s: 1, e: 0, c: [Array] },
underwriterId: 0,
isRecognized: false
},
Getting Available Markets
const availableMarkets = await econia.getAvailableMarkets(registeredMarkets);
Example Response
[
{
market: 'APT/USDT',
marketId: 5,
lotSize: 1e-8,
tickSize: 0.000001,
minSize: 1e-8,
recognized: false
},
{
market: 'EVGEN/DANI',
marketId: 4,
lotSize: 0.000001,
tickSize: 0.000001,
minSize: 0.000001,
recognized: false
},
{
market: 'eAPT/eUSDC',
marketId: 3,
lotSize: 0.001,
tickSize: 0.000001,
minSize: 0.5,
recognized: true
},
{
market: 'eAPT/eUSDC',
marketId: 2,
lotSize: 0.001,
tickSize: 0.000001,
minSize: 0.001,
recognized: false
},
{
market: 'eAPT/eUSDC',
marketId: 1,
lotSize: 0.001,
tickSize: 0.001,
minSize: 0.001,
recognized: false
}
]
GETTING ECONIA MARKET INSTANCE
By default Environment will be mainnet , Currently mainnet is not available please use Testnet.
const marketData = await econia.markets(registeredMarkets.filter((market) => market.marketId == 3)[0],ENVIRONMENT.TESTNET);
TRADE PAIR INFO
const tradePairInfo = await marketData.getTradePairInfo();
Example Response
{
baseCoinInfo: {
decimals: 8,
name: 'Example Aptos Coin',
supply: { vec: [] },
symbol: 'eAPT'
},
quoteCoinInfo: {
decimals: 6,
name: 'Example USD Coin',
supply: { vec: [] },
symbol: 'eUSDC'
}
}
GETTING TRADE HISTORY
Please set the limited parameter to true if you require only the limited data to display only on the UI
const getAllTrades = await marketData.getAllTrades(true,{
offset : 10, // starting point for data retrieval
limit : 10, // maximum number of records to return from the query
order : 'desc'}
) as TradeTable[];
Example response (limited : true)
[
{
maker_custodian_id: 0,
maker_address: '0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515',
market_id: 3,
maker_order_id: 7.268018150302766e+21,
maker_side: true,
size_: 1.903,
price_: 5.252,
txn_version: 703432124
},
{
maker_custodian_id: 0,
maker_address: '0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515',
market_id: 3,
maker_order_id: 7.268018150302766e+21,
maker_side: true,
size_: 1.903,
price_: 5.252,
txn_version: 703432124
},
]
Example response (limited : false)
[
{
txn_version: 661929648,
event_idx: 1,
emit_address: '0x4f34a70a0ee084147e5f808560d24bfff635c151faf6e16320dc28b4a429a6a5',
time: '2023-09-04T17:10:08.303873+00:00',
maker_address: '0xfbc3e92658956143d6e05c74e923292c77f0a4354cc0d06d7a9bff0185b7f24',
maker_custodian_id: 0,
maker_order_id: 36893628897792360000,
maker_side: true,
market_id: 3,
price: 2000,
sequence_number_for_trade: 0,
size: 500,
taker_address: '0x4f34a70a0ee084147e5f808560d24bfff635c151faf6e16320dc28b4a429a6a5',
taker_custodian_id: 0,
taker_order_id: 55340232221128655000,
taker_quote_fees_paid: 500
},
{
txn_version: 661929648,
event_idx: 2,
emit_address: '0x0fbc3e92658956143d6e05c74e923292c77f0a4354cc0d06d7a9bff0185b7f24',
time: '2023-09-04T17:10:08.303873+00:00',
maker_address: '0xfbc3e92658956143d6e05c74e923292c77f0a4354cc0d06d7a9bff0185b7f24',
maker_custodian_id: 0,
maker_order_id: 36893628897792360000,
maker_side: true,
market_id: 3,
price: 2000,
sequence_number_for_trade: 0,
size: 500,
taker_address: '0x4f34a70a0ee084147e5f808560d24bfff635c151faf6e16320dc28b4a429a6a5',
taker_custodian_id: 0,
taker_order_id: 55340232221128655000,
taker_quote_fees_paid: 500
}
]
GETTING ORDER BOOK DETAILS
const getOrderBook = await marketData.getOrderBook();
Example Response
{
"asks": [
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7268018150302766077060",
"price": "5252",
"remaining_size": "1888916",
"side": true,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7304911497704106890377",
"price": "5257",
"remaining_size": "2010119",
"side": true,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "6806849829952183866508",
"price": "5260",
"remaining_size": "5000",
"side": true,
"user": "0x9ff26b9a0502fb488699a1919b7f43436e2a4f50ebe632dbd22989d225179635"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7341804422892982637710",
"price": "5262",
"remaining_size": "2091647",
"side": true,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7378698192489608647830",
"price": "5270",
"remaining_size": "1869147",
"side": true,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7415592243595571106971",
"price": "5275",
"remaining_size": "1889656",
"side": true,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "5423343179990447429142",
"price": "5654",
"remaining_size": "1000",
"side": true,
"user": "0xf6b26fc47156cda04c679dd71c866d79be8f00cd1d26e353a6608f3382382446"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "5386449973283645298229",
"price": "5685",
"remaining_size": "1000",
"side": true,
"user": "0x2517feee72e6d751024c2b59c3aac3a587053893abacddbcc02ef09247d524ed"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "848550368141012651552",
"price": "20000",
"remaining_size": "5000",
"side": true,
"user": "0x9538c839fe490ccfaf32ad9f7491b5e84e610ff6edc110ff883f06ebde82463d"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "866997112223312137760",
"price": "20000",
"remaining_size": "10000",
"side": true,
"user": "0x9538c839fe490ccfaf32ad9f7491b5e84e610ff6edc110ff883f06ebde82463d"
}
],
"bids": [
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7249570702494370108524",
"price": "5228",
"remaining_size": "1573323",
"side": false,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7286464612811304604775",
"price": "5223",
"remaining_size": "1955227",
"side": false,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7323357960229825287266",
"price": "5218",
"remaining_size": "1787314",
"side": false,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7360251026173369259098",
"price": "5210",
"remaining_size": "2067347",
"side": false,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "7397145499551926326357",
"price": "5205",
"remaining_size": "1935220",
"side": false,
"user": "0x7676c6ad191680ee72a03f724aa750a66a9570ca90c7b6861c94a7c2c4ec5515"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "5441789923999732470757",
"price": "5093",
"remaining_size": "1000",
"side": false,
"user": "0xf6b26fc47156cda04c679dd71c866d79be8f00cd1d26e353a6608f3382382446"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "4703920583280995341279",
"price": "5087",
"remaining_size": "1000",
"side": false,
"user": "0xf6b26fc47156cda04c679dd71c866d79be8f00cd1d26e353a6608f3382382446"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "4722367327363294827487",
"price": "5087",
"remaining_size": "1000",
"side": false,
"user": "0xf6b26fc47156cda04c679dd71c866d79be8f00cd1d26e353a6608f3382382446"
},
{
"custodian_id": "0",
"market_id": "3",
"order_id": "5109749093665863634848",
"price": "4000",
"remaining_size": "10000",
"side": false,
"user": "0x8115e523937721388acbd77027da45b1c88a6313f99615c4da4c6a32ab161b1a"
}
]
}
GETTING TRADE VOLUME
const tradeVolume = await marketData.getTradeVolume();
Example Response
tradeVolume: {
priceChange: '0',
price: '5.252',
totalTrades: 242,
baseVolume: '43434.346',
quoteVolume: '224932.85527'
}
GETTING MARKET PRICE
If limited is set to true then only the required parameter to display on the ui is returned
const marketPrice_ = await marketData.marketPrice(true);
Example Response (limited : true)
marketPrice_: {
bestAskPrice: '5.252',
bestBidPrice: '5.228',
maxBuyQuote: '51650008019',
maxSellSize: '9331431'
}
Example Response (limited : false)
marketPrice_: {
bestAskPrice: BigNumber { s: 1, e: 0, c: [ 5, 25200000000000 ] },
bestBidPrice: BigNumber { s: 1, e: 0, c: [ 5, 22800000000000 ] },
maxBuyQuote: BigNumber { s: 1, e: 10, c: [ 51650008019 ] },
maxSellSize: BigNumber { s: 1, e: 6, c: [ 9331431 ] },
getExecutionPrice: [Function: getExecutionPrice],
getExecutionPriceQuote: [Function: getExecutionPriceQuote]
}
DEPOSIT BASE COIN
const payload = marketData.depositBaseCoinPayload('100000')
const transaction = await client.generateTransaction(account.address(), payload);
const sign = await client.signTransaction(account,transaction)
const submit = await client.submitTransaction(sign)
await client.waitForTransaction(submit.hash)
DEPOSIT QUOTE COIN
const payload = marketData.depositQuoteCoinPayload('100000')
const transaction = await client.generateTransaction(account.address(), payload);
const sign = await client.signTransaction(account,transaction)
const submit = await client.submitTransaction(sign)
await client.waitForTransaction(submit.hash)
GET USER MARKET ACCOUNT INFO
const marketInfo = await marketData.getUserMarketAccount(account.address().toString());
Example Response
{
"asks": [],
"base_available": "100000",
"base_ceiling": "100000",
"base_total": "100000",
"bids": [],
"custodian_id": "0",
"market_id": "3",
"quote_available": "100000",
"quote_ceiling": "100000",
"quote_total": "100000"
}
PLACE MARKET BUY ORDER
const estimatedPrice = await marketData.getEstimatedPrice(1, BUY);
const payload = await marketData.placeMarketOrder(BUY);
const transaction = await client.generateTransaction(account.address(), payload);
const simulate = await client.simulateTransaction(account, transaction);
const status = simulate[0].success;
const sign = await client.signTransaction(account, transaction);
const submit = await client.submitTransaction(sign);
await client.waitForTransaction(submit.hash);
PLACE MARKET SELL ORDER
const estimatedPrice = await marketData.getEstimatedPrice(1, SELL);
const payload = await marketData.placeMarketOrder(SELL);
const transaction = await client.generateTransaction(account.address(), payload);
const simulate = await client.simulateTransaction(account, transaction);
const status = simulate[0].success;
const sign = await client.signTransaction(account, transaction);
const submit = await client.submitTransaction(sign);
await client.waitForTransaction(submit.hash);
PLACE LIMIT BUY ORDER
const buyOrderPayload = await marketData.placeLimitOrder(BUY, 1, 5);
const transaction = await client.generateTransaction(account.address(), buyOrderPayload);
const simulate = await client.simulateTransaction(account, transaction);
const status = simulate[0].success;
const sign = await client.signTransaction(account, transaction);
const submit = await client.submitTransaction(sign);
await client.waitForTransaction(submit.hash);
const openOrders = await marketData.getOpenOrders(account.address().toString());
PLACE LIMIT SELL ORDER
const buyOrderPayload = await marketData.placeLimitOrder(SELL, 1, 5.6);
const transaction = await client.generateTransaction(account.address(), buyOrderPayload);
const simulate = await client.simulateTransaction(account, transaction);
const status = simulate[0].success;
const sign = await client.signTransaction(account, transaction);
const submit = await client.submitTransaction(sign);
await client.waitForTransaction(submit.hash);
const openOrders = await marketData.getOpenOrders(account.address().toString());
GET USER ORDER HISTORY
Order type open returns current open orders, canceled returns canceled order , all returns all orders
const openOrders = await marketData.getOpenOrders(account.address().toString(),'open');
Example Response
{
asks: [],
asksStackTop: 1,
baseAvailable: BigNumber { s: 1, e: 8, c: [ 600100000 ] },
baseCeiling: BigNumber { s: 1, e: 8, c: [ 800100000 ] },
baseNameGeneric: '',
baseTotal: BigNumber { s: 1, e: 8, c: [ 600100000 ] },
baseType: {
account_address: '0xc0de11113b427d35ece1d8991865a941c0578b0f349acabbe9753863c24109ff',
module_name: '0x6578616d706c655f617074',
struct_name: '0x4578616d706c65415054'
},
bids: [
{
marketOrderId: [U128],
price: [BigNumber],
counter: [BigNumber],
size: [BigNumber]
},
{
marketOrderId: [U128],
price: [BigNumber],
counter: [BigNumber],
size: [BigNumber]
}
],
bidsStackTop: 2,
lotSize: BigNumber { s: 1, e: 5, c: [ 100000 ] },
minSize: BigNumber { s: 1, e: 2, c: [ 500 ] },
quoteAvailable: BigNumber { s: 1, e: 7, c: [ 30801707 ] },
quoteCeiling: BigNumber { s: 1, e: 7, c: [ 40901707 ] },
quoteTotal: BigNumber { s: 1, e: 7, c: [ 40901707 ] },
quoteType: {
account_address: '0xc0de11113b427d35ece1d8991865a941c0578b0f349acabbe9753863c24109ff',
module_name: '0x6578616d706c655f75736463',
struct_name: '0x4578616d706c6555534443'
},
tickSize: BigNumber { s: 1, e: 0, c: [ 1 ] },
underwriterId: 0
}
CANCELING AN ORDER
const openOrders = await marketData.getOpenOrders(account.address().toString(),'open');
const cancel = openOrders.bids[0];
const closeOrder = marketData.cancelOrder(BUY, cancel.marketOrderId.toBigInt().toString());
const transaction = await client.generateTransaction(account.address(), closeOrder);
const sign = await client.signTransaction(account, transaction);
const submit = await client.submitTransaction(sign);
GETTING ORDER HISTORY
//LIMIT ORDER HISTORY
const orderHistoryLimit = await marketData.getOrderHistory(account.address().toString(),'limit')
//MARKET ORDER HISTORY
const orderHistoryMarket = await marketData.getOrderHistory(account.address().toString(),'market')
Example Response
// LIMIT ORDER
{
market_id: 3,
order_id: 1.2728254818535122e+21,
user: '0x9538c839fe490ccfaf32ad9f7491b5e84e610ff6edc110ff883f06ebde82463d',
custodian_id: 0,
self_matching_behavior: 3,
restriction: 0,
created_at: '2023-09-16T07:45:30.393582+00:00',
last_updated_at: null,
integrator: '0xd718181a753f5b759518d9b896018dd7eb3d77d80bf90ba77fffaf678f781929',
total_filled: 0,
remaining_size: 10000,
order_status: 'open',
order_type: 'limit',
price: 20000,
last_increase_stamp: 1.251484360051991e+28,
side: 'ask'
},
//MARKET ORDER
{
market_id: 3,
order_id: 1.0883579003488635e+21,
user: '0x9538c839fe490ccfaf32ad9f7491b5e84e610ff6edc110ff883f06ebde82463d',
custodian_id: 0,
direction: 'buy',
self_matching_behavior: 3,
created_at: '2023-09-15T06:46:42.558258+00:00',
last_updated_at: '2023-09-15T06:46:42.558258+00:00',
integrator: '0xd718181a753f5b759518d9b896018dd7eb3d77d80bf90ba77fffaf678f781929',
total_filled: 0,
remaining_size: 15007,
order_status: 'cancelled',
order_type: 'market'
},
EDIT ORDER SIZE
const openOrders = await marketData.getOpenOrders(account.address().toString(), 'open');
console.log('🚀 ~ file: limitOrder.test.ts:54 ~ it.only ~ openOrders:', openOrders);
const order = openOrders.bids[0];
const editOrderSize = marketData.changeOrderSize(BUY,order.marketOrderId.toBigInt().toString(),3)
const transaction = await client.generateTransaction(account.address(), editOrderSize);
const sign = await client.signTransaction(account, transaction);
const submit = await client.submitTransaction(sign);
await client.waitForTransaction(submit.hash)
console.log('🚀 ~ file: limitOrder.test.ts:50 ~ it.only ~ submit:', submit);
CANCEL ALL ORDERS
const editOrderSize = marketData.cancelAllOrders(BUY,)
const transaction = await client.generateTransaction(account.address(), editOrderSize);
const sign = await client.signTransaction(account, transaction);
const submit = await client.submitTransaction(sign);
await client.waitForTransaction(submit.hash)
console.log('🚀 ~ file: limitOrder.test.ts:50 ~ it.only ~ submit:', submit);
const openOrders = await marketData.getOpenOrders(account.address().toString(), 'open');
console.log('🚀 ~ file: limitOrder.test.ts:54 ~ it.only ~ openOrders:', openOrders);
const order = openOrders.bids[0];
CANDLE STICK RESOLUTIONS
const resolutions = await marketData.getCandleStickResolutions()
Example Response
[
{ resolution: 60 }, // 60 seconds
{ resolution: 300 },
{ resolution: 900 },
{ resolution: 1800 },
{ resolution: 3600 },
{ resolution: 14400 },
{ resolution: 43200 },
{ resolution: 86400 } // 24 hours
]
CANDLE STICK DATA
const resolutions = await marketData.getCandleStickData(900,3)
Example Response
[
{
market_id: 3,
resolution: 900,
start_time: '2023-11-06T10:45:00+00:00',
open: 6589,
high: 6589,
low: 6589,
close: 6589,
volume: 42828500
},
{
market_id: 3,
resolution: 900,
start_time: '2023-11-06T10:30:00+00:00',
open: 6589,
high: 6589,
low: 6589,
close: 6589,
volume: 167505558
},
{
market_id: 3,
resolution: 900,
start_time: '2023-11-06T10:15:00+00:00',
open: 6589,
high: 6589,
low: 6589,
close: 6589,
volume: 42828500
},
]