Node SDK
To integrate the DCA API, you'll need to use the Limit Order SDK, which provides functionality for creating and canceling limit orders.
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';
For DCA SDK, you would need to add a param of 'Dca'
(Capitalized the D) when create the DCA orders.
const openoceanLimitOrderSdk = new LimitOrderNodeSdk(chainId, web3, account.address, 'Dca')
Demo
// Import required dependencies
import { LimitOrderNodeSdk } from '@openocean.finance/limitorder-sdk';
import Web3 from "web3";
import axios from "axios";
// Configuration for Base network
const chainId = 8453
const providerUrl = 'https://base.llamarpc.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 AXLUSDC
const inToken = {
"address": "0xEB466342C4d449BC9f53A865D5Cb90586f405215",
"decimals": 6,
"symbol": "AXLUSDC",
}
// Token configuration for WETH
const outToken = {
"address": "0x4200000000000000000000000000000000000006",
"decimals": 18,
"symbol": "WETH"
}
// 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 Dca Order SDK
const openoceanLimitOrderSdk = new LimitOrderNodeSdk(chainId, web3, account.address, 'Dca')
/**
* Test function to create a dca order
* Creates a dca order 10 AXLUSDC
*/
async function testCreate() {
const gasPrice = await web3.eth.getGasPrice();
// Create dca order data
const orderData = await openoceanLimitOrderSdk.createLimitOrder(
{
makerTokenAddress: inToken.address,
makerTokenDecimals: inToken.decimals,
takerTokenAddress: outToken.address,
takerTokenDecimals: outToken.decimals,
makerAmount: 10 * (10 ** inToken.decimals) + '',
takerAmount:'1', // "1" as default
gasPrice: Number(gasPrice),
expire: '6Month'
}
)
let json = {
...orderData,
expireTime: 180,
time: 60, // 1 Minute
times: 2,
version: 'v2',
// minPrice:1,
// maxPrice:2,
}
// Submit the order to OpenOcean API
const result = await axios.post(
`${baseUrl}/v1/${chainId}/dca/swap`,
json,
{
headers: {
'Content-Type': 'application/json',
},
}
);
console.log(result)
}
// testCreate()
/**
* Test function to cancel an existing dca 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}/dca/cancel`,
{ orderHash: order.orderHash },
);
// If API cancellation fails, try on-chain cancellation
const { status } = (result && result.data) || {};
if (!(status === 3 || status === 4)) {
const gasPrice = await web3.eth.getGasPrice();
const res = await openoceanLimitOrderSdk.cancelLimitOrder(
{
orderData: order.data,
gasPrice,
}
);
console.log(res)
}
}
}
// testCancel()
/**
* Get list of user's dca 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}/dca/address/${account.address}?page=1&limit=100&statuses=[1,2,5]&sortBy=createDateTime&exclude=0`;
const { data } = await axios.get(reqUrl);
return data ? data.data : []
}
// getOrderList()
Last updated