| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125 |
- import axios from 'axios';
- import { CONFIG } from '../config/index.js';
- import { logger } from '../utils/index.js';
- export class ByrealAPI {
- static async fetchTargetPositions(targetWallet) {
- try {
- const url = `${CONFIG.BYREAL_API_BASE}/position/list`;
- const params = {
- userAddress: targetWallet,
- page: 1,
- pageSize: 50,
- status: 0,
- };
- const response = await axios.get(url, { params });
-
- if (response.data.retCode !== 0) {
- throw new Error(response.data.retMsg || 'Failed to fetch positions');
- }
- return response.data.result?.data || { positions: [], poolMap: {} };
- } catch (error) {
- logger.error('Error fetching target positions:', error.message);
- return { positions: [], poolMap: {} };
- }
- }
- static async getPriceFromTick(tick, decimalsA, decimalsB) {
- try {
- const url = `${CONFIG.TICK_API}?tick=${tick}&decimalsA=${decimalsA}&decimalsB=${decimalsB}&baseIn=false`;
- const response = await axios.get(url, {
- headers: { Authorization: CONFIG.AUTH_HEADER },
- });
- return parseFloat(response.data.price);
- } catch (error) {
- throw new Error(`Failed to fetch price for tick ${tick}: ${error.message}`);
- }
- }
- static async checkIfCopied(poolAddress, parentPositionAddress, myWallet) {
- try {
- const payload = {
- poolAddress: poolAddress,
- parentPositionAddress: parentPositionAddress,
- page: 1,
- pageSize: 10,
- sortField: 'liquidity',
- };
- const response = await axios.post(CONFIG.CHECK_COPY_URL, payload);
- if (response.data?.result?.data) {
- const records = response.data.result.data.records || [];
- const found = records.find(item => item.walletAddress === myWallet);
- return !!found;
- }
- return false;
- } catch (error) {
- logger.error('Error checking copy status:', error.message);
- return false;
- }
- }
- static async copyPosition(nftMintAddress, positionAddress, maxUsdValue) {
- try {
- const body = {
- nftMintAddress: nftMintAddress,
- positionAddress: positionAddress,
- maxUsdValue: maxUsdValue,
- };
- const headers = {
- Authorization: CONFIG.AUTH_HEADER,
- 'Content-Type': 'application/json',
- };
- logger.info('Sending copy request to Byreal...');
-
- const response = await axios.post(CONFIG.COPY_ACTION_URL, body, { headers });
-
- if (response.data?.success) {
- logger.success(`Position copied successfully: ${positionAddress}`);
- return true;
- } else {
- logger.error('Copy request failed:', response.data);
- return false;
- }
- } catch (error) {
- logger.error('Error executing copy:', error.message);
- if (error.response) {
- logger.error('Server response:', error.response.data);
- }
- return false;
- }
- }
- static async closePosition(positionAddress) {
- try {
- logger.info(`Closing position: ${positionAddress}`);
- const body = { positionAddress };
- const headers = {
- Authorization: CONFIG.AUTH_HEADER,
- 'Content-Type': 'application/json',
- };
- const response = await axios.post(CONFIG.CLOSE_ACTION_URL, body, { headers });
- if (response.data?.success) {
- logger.success(`Position closed successfully: ${positionAddress}`);
- return true;
- } else {
- logger.error('Close request failed:', response.data);
- return false;
- }
- } catch (error) {
- logger.error('Error closing position:', error.message);
- return false;
- }
- }
- }
- export default ByrealAPI;
|