A powerful JavaScript SDK for integrating with the 1DEX decentralized exchange platform, providing seamless access to trading functionality and market data.
1DEX JavaScript SDK
A powerful JavaScript SDK for integrating with the 1DEX decentralized exchange platform, providing seamless access to trading functionality and market data.
Installation
npm install 1dex-js-sdk
Getting Started
Initializing the SDK
import { DexKit, ETradeSide, EOrderType } from "1dex-js-sdk";
// Create a new DexKit instance
const dexKit = new DexKit({
apiKey: "YOUR_API_KEY",
secretKey: "YOUR_SECRET_KEY",
enableProxy: false, // Optional, defaults to true
apiClientConfig: {
baseUrl: "https://api-v1.example.com", // Optional, defaults to 'https://api-v1.1dex.com'
},
blockchainRpcClientConfig: {
chainID: "YOUR_CHAIN_ID", // Optional, defaults to 'aca376f206b8fc25a6ed44dbdc66547c36c6c33e3a119ffbeaef943642f0e906'
rpcUrl: "https://rpc.example.com", // Optional, defaults to 'https://spring-rpc.1dex.com'
},
devMode: false, // Optional, if set to true, it will output debug information of certain methods in the console
});
// Initialize the DexKit instance
await dexKit.initialize();
DexKit Interface
The DexKit class provides methods for trading operations on 1DEX.
Core Methods
initialize()
Initializes the DexKit instance by fetching system metadata, account information, and pool data.
await dexKit.initialize();
initializeWithProvidedConfig(config)
Initializes the DexKit instance using the provided configuration instead of getting it from the server.
All parameters are optional.
dexKit.initializeWithProvidedConfig({
appContract: "app_contract", // app.1dex
accountContract: "account_contract", // Your main account name
pools: [
{
id: 1,
baseToken: "TOKEN1",
quoteToken: "TOKEN2",
baseTokenContract: "token1.contract",
quoteTokenContract: "token2.contract",
baseTokenDecimals: 4,
quoteTokenDecimals: 4,
pricePrecision: 4,
},
],
proxyAccount: {
name: "proxy.account",
permission: "active",
},
account: {
name: "user.account", // Your sub-account name
permission: "trader", // Your sub-account permission
},
});
placeLimitOrder(params)
Places a limit order on the specified pool.
If it is a buy order, the amount is the quantity of quote coin, if it is a sell order, the amount is the quantity of base coin.
For example, there is a BTC/USDT trading pair. If it is a buy order, 'amount = 80000' means '80000 USDT', if it is a sell order, 'amount = 1' means '1 BTC'.
Note: the amount will be rounded up to the precision unit, which means that if you enter 10.00001 EOS, it will be formatted as 10.0001 EOS, so it is recommended to enter the amount according to the token decimals.
const { txID, clientOrderID } = await dexKit.placeLimitOrder({
poolID: 1,
side: ETradeSide.BUY, // or ETradeSide.SELL
orderType: EOrderType.NO_RESTRICTION, // or EOrderType.IMMEDIATE_OR_CANCEL || EOrderType.FILL_OR_KILL || EOrderType.POST_ONLY
amount: "10.0000", // if 'buy', amount is quote coin quantity; if 'sell', amount is base coin quantity
price: "0.5000",
clientOrderID: "your defined client order id", // optional
});
placeMarketOrder(params)
Places a market order on the specified pool.
If it is a buy order, the amount is the quantity of quote coin, if it is a sell order, the amount is the quantity of base coin.
For example, there is a BTC/USDT trading pair. If it is a buy order, 'amount = 80000' means '80000 USDT', if it is a sell order, 'amount = 1' means '1 BTC'.
Note: the amount will be rounded up to the precision unit, which means that if you enter 10.00001 EOS, it will be formatted as 10.0001 EOS, so it is recommended to enter the amount according to the token decimals.
const { txID, clientOrderID } = await dexKit.placeMarketOrder({
poolID: 1,
side: ETradeSide.BUY, // or ETradeSide.SELL
amount: "10.0000", // if 'buy', amount is quote coin quantity; if 'sell', amount is base coin quantity
clientOrderID: "your defined client order id", // optional
});
The DexKit instance exposes an httpApiClient property that provides access to various API endpoints for retrieving market data and account information.
// Get all pools
const allPools = await dexKit.httpApiClient.getAllPools();
// Get pools filtered by base or quote coin
const filteredPools = await dexKit.httpApiClient.getAllPools({
baseCoin: "EOS",
quoteCoin: "USDT",
});
getPoolInfo(symbolOrPoolID)
Retrieves detailed information about a specific pool.
// Get pool by ID
const poolInfo = await dexKit.httpApiClient.getPoolInfo(1);
getDepth(params)
Retrieves order book depth data for a specific pool.
The SDK uses standardized error handling. Errors thrown by the SDK include detailed information about what went wrong. It's recommended to use try-catch blocks when interacting with the SDK: