Node SDK
How to Install the sdk in your project
npm i @openocean.finance/limitorder-sdk
How to use the sdk in your project
import { LimitOrderNodeSdk } from '@openocean.finance/limitorder-sdk';
You can then use all the functions explored by the SDK (API and swapSdk).
Demo
// Import required dependencies
import { LimitOrderNodeSdk } from '@openocean.finance/limitorder-sdk';
import Web3 from "web3";
import axios from "axios";
// Configuration for Arbitrum network
const chainId = 42161
const providerUrl = 'https://arbitrum-one-rpc.publicnode.com';
const web3 = new Web3(providerUrl);
// Your wallet private key - Replace with your actual private key
const privateKey = 'Your wallet private key'
// Token configuration for USDC
const inToken = {
"address": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
"decimals": 6,
"symbol": "USDC",
}
// Token configuration for USDT
const outToken = {
"address": "0xfd086bc7cd5c481dcc9c85ebe478a1c0b69fcbb9",
"decimals": 6,
"symbol": "USDT"
}
// Gas price configuration (3 Gwei)
const gasPrice = 3 * (10 ** 9)
// OpenOcean API base URL
const baseUrl = 'https://open-api.openocean.finance'
// Create account object using private key
const account = web3.eth.accounts.privateKeyToAccount(privateKey);
// Add account to web3 wallet for later use
web3.eth.accounts.wallet.add(account);
// Initialize OpenOcean Limit Order SDK
const openoceanLimitOrderSdk = new LimitOrderNodeSdk(chainId, web3, account.address)
/**
* Test function to create a limit order
* Creates a limit order to swap 0.1 USDC for 0.2 USDT
*/
async function testCreate() {
// Create limit order data
const orderData = await openoceanLimitOrderSdk.createLimitOrder(
{
makerTokenAddress: inToken.address,
makerTokenDecimals: inToken.decimals,
takerTokenAddress: outToken.address,
takerTokenDecimals: outToken.decimals,
makerAmount: 0.1 * (10 ** inToken.decimals) + '',
takerAmount: 0.2 * (10 ** outToken.decimals) + '',
gasPrice,
expire: '6Month'
}
)
// Submit the order to OpenOcean API
const result = await axios.post(
`${baseUrl}/v1/${chainId}/limit-order`,
orderData,
{
headers: {
'Content-Type': 'application/json',
},
}
);
console.log(result)
}
// testCreate()
/**
* Test function to cancel an existing limit order
* Cancels the first order from the user's order list
*/
async function testCancel() {
const orderList = await getOrderList()
if (orderList && orderList.length) {
// Cancel the first order
const order = orderList[0];
// First try to cancel through API
const result = await axios.post(
`${baseUrl}/v1/${chainId}/limit-order/cancelLimitOrder`,
{ orderHash: order.orderHash }
);
// If API cancellation fails, try on-chain cancellation
const { status } = (result && result.data) || {};
if (!(status === 3 || status === 4)) {
const res = await openoceanLimitOrderSdk.cancelLimitOrder(
{
orderData: order.data,
gasPrice,
}
);
console.log(res)
}
}
}
// testCancel()
/**
* Get list of user's limit orders
* Fetches orders with status 1 (active), 2 (filled), or 5 (expired)
* @returns Array of order objects
*/
async function getOrderList() {
const reqUrl = `${baseUrl}/v1/${chainId}/limit-order/address/${openoceanLimitOrderSdk.signerAddress}?page=1&limit=100&statuses=[1,2,5]&sortBy=createDateTime&exclude=0`;
const { data } = await axios.get(reqUrl);
return data.data
}
// getOrderList()
Last updated