byreal.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. import axios from 'axios';
  2. import { CONFIG } from '../config/index.js';
  3. import { logger } from '../utils/index.js';
  4. export class ByrealAPI {
  5. static async fetchTargetPositions(targetWallet) {
  6. try {
  7. const url = `${CONFIG.BYREAL_API_BASE}/position/list`;
  8. const params = {
  9. userAddress: targetWallet,
  10. page: 1,
  11. pageSize: 50,
  12. status: 0,
  13. };
  14. const response = await axios.get(url, { params });
  15. if (response.data.retCode !== 0) {
  16. throw new Error(response.data.retMsg || 'Failed to fetch positions');
  17. }
  18. return response.data.result?.data || { positions: [], poolMap: {} };
  19. } catch (error) {
  20. logger.error('Error fetching target positions:', error.message);
  21. return { positions: [], poolMap: {} };
  22. }
  23. }
  24. static async getPriceFromTick(tick, decimalsA, decimalsB) {
  25. try {
  26. const url = `${CONFIG.TICK_API}?tick=${tick}&decimalsA=${decimalsA}&decimalsB=${decimalsB}&baseIn=false`;
  27. const response = await axios.get(url, {
  28. headers: { Authorization: CONFIG.AUTH_HEADER },
  29. });
  30. return parseFloat(response.data.price);
  31. } catch (error) {
  32. throw new Error(`Failed to fetch price for tick ${tick}: ${error.message}`);
  33. }
  34. }
  35. static async checkIfCopied(poolAddress, parentPositionAddress, myWallet) {
  36. try {
  37. const payload = {
  38. poolAddress: poolAddress,
  39. parentPositionAddress: parentPositionAddress,
  40. page: 1,
  41. pageSize: 10,
  42. sortField: 'liquidity',
  43. };
  44. const response = await axios.post(CONFIG.CHECK_COPY_URL, payload);
  45. if (response.data?.result?.data) {
  46. const records = response.data.result.data.records || [];
  47. const found = records.find(item => item.walletAddress === myWallet);
  48. return !!found;
  49. }
  50. return false;
  51. } catch (error) {
  52. logger.error('Error checking copy status:', error.message);
  53. return false;
  54. }
  55. }
  56. static async copyPosition(nftMintAddress, positionAddress, maxUsdValue) {
  57. try {
  58. const body = {
  59. nftMintAddress: nftMintAddress,
  60. positionAddress: positionAddress,
  61. maxUsdValue: maxUsdValue,
  62. };
  63. const headers = {
  64. Authorization: CONFIG.AUTH_HEADER,
  65. 'Content-Type': 'application/json',
  66. };
  67. logger.info('Sending copy request to Byreal...');
  68. const response = await axios.post(CONFIG.COPY_ACTION_URL, body, { headers });
  69. if (response.data?.success) {
  70. logger.success(`Position copied successfully: ${positionAddress}`);
  71. return true;
  72. } else {
  73. logger.error('Copy request failed:', response.data);
  74. return false;
  75. }
  76. } catch (error) {
  77. logger.error('Error executing copy:', error.message);
  78. if (error.response) {
  79. logger.error('Server response:', error.response.data);
  80. }
  81. return false;
  82. }
  83. }
  84. static async closePosition(positionAddress) {
  85. try {
  86. logger.info(`Closing position: ${positionAddress}`);
  87. const body = { positionAddress };
  88. const headers = {
  89. Authorization: CONFIG.AUTH_HEADER,
  90. 'Content-Type': 'application/json',
  91. };
  92. const response = await axios.post(CONFIG.CLOSE_ACTION_URL, body, { headers });
  93. if (response.data?.success) {
  94. logger.success(`Position closed successfully: ${positionAddress}`);
  95. return true;
  96. } else {
  97. logger.error('Close request failed:', response.data);
  98. return false;
  99. }
  100. } catch (error) {
  101. logger.error('Error closing position:', error.message);
  102. return false;
  103. }
  104. }
  105. }
  106. export default ByrealAPI;