aster_adapter_smoke.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. import 'dotenv/config'
  2. import { AsterAdapter } from '../src/exchanges/aster/asterAdapter'
  3. import { AsterExchangeAdapter } from '../src/exchanges/aster/AsterExchangeAdapter'
  4. async function main() {
  5. const base = process.env.ASTER_HTTP_BASE || 'https://fapi.asterdex.com'
  6. const symbol = process.env.ASTER_TEST_SYMBOL || 'BTCUSDT'
  7. const apiKey = process.env.ASTER_API_KEY || ''
  8. const apiSecret = process.env.ASTER_API_SECRET || ''
  9. const user = process.env.ASTER_ORDER_USER || ''
  10. const signer = process.env.ASTER_ORDER_SIGNER || ''
  11. const pk = process.env.PRIVATE_KEY || ''
  12. const adapter = new AsterAdapter({
  13. rpcUrl: '',
  14. chainId: 0,
  15. routerAddress: '',
  16. httpBase: base,
  17. defaultUser: user,
  18. defaultSigner: signer,
  19. apiKey,
  20. apiSecret,
  21. })
  22. const ex = new AsterExchangeAdapter(adapter)
  23. // Symbols
  24. const syms = await ex.symbols()
  25. console.log('symbols count =', syms.length)
  26. // Depth top
  27. try {
  28. const d = await ex.depth(symbol, 10)
  29. console.log('depth top', symbol, 'bid=', d.bids?.[0], 'ask=', d.asks?.[0])
  30. } catch (e: any) {
  31. console.warn('depth failed', e?.message || e)
  32. }
  33. // Balances / Positions (require HMAC keys)
  34. if (apiKey && apiSecret) {
  35. try {
  36. const bals = await ex.balances()
  37. console.log('balances[0]=', bals[0])
  38. } catch (e: any) {
  39. console.warn('balances failed', e?.message || e)
  40. }
  41. try {
  42. const poss = await ex.positions()
  43. console.log('positions[0]=', poss[0])
  44. } catch (e: any) {
  45. console.warn('positions failed', e?.message || e)
  46. }
  47. try {
  48. const opens = await ex.openOrders(symbol)
  49. console.log('openOrders len =', opens.length)
  50. } catch (e: any) {
  51. console.warn('openOrders failed', e?.message || e)
  52. }
  53. } else {
  54. console.log('HMAC not configured, skip balances/positions/openOrders')
  55. }
  56. // Optional order flow (requires user/signer/private key)
  57. if (process.env.ASTER_ENABLE_ORDER === '1') {
  58. if (!user || !signer || !pk) throw new Error('Missing ASTER_ORDER_USER/ASTER_ORDER_SIGNER/PRIVATE_KEY')
  59. const notionalUsd = process.env.ASTER_TEST_NOTIONAL_USD ? Number(process.env.ASTER_TEST_NOTIONAL_USD) : undefined
  60. let qty = Number(process.env.ASTER_TEST_QTY || '0')
  61. if (notionalUsd) {
  62. const px = await adapter.quote({ symbol, side: 'long', quantity: 0, slippage: 0 }).then(r => r.expectedPrice)
  63. if (!px) throw new Error('price not available')
  64. qty = +(notionalUsd / px).toFixed(3)
  65. }
  66. if (!qty || qty <= 0) qty = 0.001
  67. console.log('place MARKET BUY', { symbol, qty })
  68. const o1 = await ex.placeOrder({ symbol, side: 'BUY', type: 'MARKET', quantity: String(qty) })
  69. console.log('BUY result', o1)
  70. console.log('place MARKET SELL', { symbol, qty })
  71. const o2 = await ex.placeOrder({ symbol, side: 'SELL', type: 'MARKET', quantity: String(qty) })
  72. console.log('SELL result', o2)
  73. }
  74. }
  75. main().catch(e => {
  76. console.error('aster adapter smoke error', e?.message || e)
  77. process.exit(1)
  78. })