feat: implement real Drift trading history using SDK methods

- Updated getTradingHistory to fetch actual Drift order records
- Added fallback to local database for trade history
- Enhanced executeTrade to store trades in database for history tracking
- Fixed hydration issues in AutoTradingPanel and TradingHistory components
- Improved error handling and logging for trading history retrieval
This commit is contained in:
mindesbunister
2025-07-13 02:24:12 +02:00
parent 0fa687d1d7
commit 836455d1a4
6 changed files with 286 additions and 427 deletions

View File

@@ -144,7 +144,7 @@ export class TradingViewAutomation {
console.error('ERROR: Failed to launch browser:', error)
// Cleanup any partial state
await this.forceCleanup()
throw new Error(`Failed to launch browser: ${error) + ")"
throw new Error('Failed to launch browser: ' + error)
}
if (!this.browser) {
@@ -402,7 +402,7 @@ export class TradingViewAutomation {
for (const selector of anonymousSelectors) {
try {
if (await this.page.locator(selector).isVisible({ timeout: 1500 })) {
console.log("ERROR: Found anonymous indicator: " + selector} - not logged in`)
console.log(`ERROR: Found anonymous indicator: ${selector} - not logged in`)
foundAnonymousElement = true
break
}
@@ -450,7 +450,7 @@ export class TradingViewAutomation {
}
}
console.log("DATA: Total cookies: ${cookies.length}, Auth cookies found: " + hasAuthCookies) + ")"
console.log('DATA: Total cookies: ' + cookies.length + ', Auth cookies found: ' + hasAuthCookies)
}
// Strategy 5: Try to detect personal elements by checking page content
@@ -628,7 +628,7 @@ export class TradingViewAutomation {
}
} catch (e) {
console.log("ERROR: Failed to load " + url}:`, e)
console.log(`ERROR: Failed to load ${url}:`, e)
continue
}
}
@@ -686,7 +686,7 @@ export class TradingViewAutomation {
// Get all buttons and check their text content
const buttons = await this.page.locator('button').all()
console.log("CHECKING: Found " + buttons.length} buttons to check`)
console.log(`CHECKING: Found ${buttons.length} buttons to check`)
for (let i = 0; i < buttons.length; i++) {
try {
@@ -695,7 +695,7 @@ export class TradingViewAutomation {
const text = await button.textContent() || ''
const trimmedText = text.trim().toLowerCase()
console.log(`INFO:
console.log(`INFO: Button ${i + 1}: "${trimmedText}"`)
if (trimmedText.includes('email') ||
trimmedText.includes('continue with email') ||
@@ -798,7 +798,7 @@ export class TradingViewAutomation {
console.log('🔄 Selector approach failed, trying manual input search...')
try {
const inputs = await this.page.locator('input').all()
console.log("CHECKING: Found " + inputs.length} inputs to check`)
console.log(`CHECKING: Found ${inputs.length} inputs to check`)
for (let i = 0; i < inputs.length; i++) {
try {
@@ -808,14 +808,14 @@ export class TradingViewAutomation {
const name = await input.getAttribute('name') || ''
const placeholder = await input.getAttribute('placeholder') || ''
console.log("INFO: Input ${i + 1}: type="${type}" name="${name}" placeholder="" + placeholder}"`)
console.log(`INFO: Input ${i + 1}: type="${type}" name="${name}" placeholder="${placeholder}"`)
if (type === 'email' ||
name.toLowerCase().includes('email') ||
name.toLowerCase().includes('username') ||
placeholder.toLowerCase().includes('email') ||
placeholder.toLowerCase().includes('username')) {
console.log("TARGET: Found email input manually: " + name || type || placeholder) + ")"
console.log(`TARGET: Found email input manually: ${name || type || placeholder}`)
emailInput = `input:nth-of-type(${i + 1})`
break
}
@@ -973,7 +973,7 @@ export class TradingViewAutomation {
console.log('🔄 Selector approach failed, trying manual button search for submit...')
try {
const buttons = await this.page.locator('button').all()
console.log("CHECKING: Found " + buttons.length} buttons to check for submit`)
console.log(`CHECKING: Found ${buttons.length} buttons to check for submit`)
for (let i = 0; i < buttons.length; i++) {
try {
@@ -982,13 +982,13 @@ export class TradingViewAutomation {
const text = (await button.textContent() || '').toLowerCase()
const type = await button.getAttribute('type') || ''
console.log("INFO: Submit Button ${i + 1}: "${text}" type="" + type}"`)
console.log(`INFO: Submit Button ${i + 1}: "${text}" type="${type}"`)
if (type === 'submit' ||
text.includes('sign in') ||
text.includes('login') ||
text.includes('submit')) {
console.log("TARGET: Found submit button manually: "" + text}"`)
console.log(`TARGET: Found submit button manually: "${text}"`)
submitButton = `button:nth-of-type(${i + 1})`
break
}
@@ -1026,7 +1026,7 @@ export class TradingViewAutomation {
await this.page.waitForTimeout(1000) // Wait 1 second
attempts++
console.log("🔄 Login check attempt ${attempts}/" + maxAttempts) + ")"
console.log('Login check attempt ' + attempts + '/' + maxAttempts)
// Check if we navigated away from login page
const currentUrl = await this.page.url()
@@ -1059,8 +1059,8 @@ export class TradingViewAutomation {
for (const selector of errorSelectors) {
if (await this.page.locator(selector).isVisible({ timeout: 500 })) {
const errorText = await this.page.locator(selector).textContent()
console.log("ERROR: Login error detected: " + errorText) + ")"
throw new Error(`Login failed: ${errorText) + ")"
console.log('ERROR: Login error detected: ' + errorText)
throw new Error('Login failed: ' + errorText)
}
}
} catch (e) {
@@ -1312,17 +1312,17 @@ export class TradingViewAutomation {
// Change symbol if not BTC
if (symbol !== 'BTCUSD') {
console.log("Changing symbol to " + symbol}...`)
console.log(`Changing symbol to ${symbol}...`)
await this.changeSymbol(symbol)
}
// Change timeframe if specified
if (timeframe) {
console.log("Setting timeframe to " + timeframe}...`)
console.log(`Setting timeframe to ${timeframe}...`)
await this.changeTimeframe(timeframe)
}
console.log("Successfully navigated to ${symbol} chart with " + timeframe} timeframe`)
console.log(`Successfully navigated to ${symbol} chart with ${timeframe} timeframe`)
return true
} catch (error) {
@@ -1352,7 +1352,7 @@ export class TradingViewAutomation {
symbolElement = selector
break
} catch (e) {
console.log("Symbol selector " + selector} not found, trying next...`)
console.log(`Symbol selector ${selector} not found, trying next...`)
}
}
@@ -1377,7 +1377,7 @@ export class TradingViewAutomation {
searchInput = selector
break
} catch (e) {
console.log("Search input selector " + selector} not found, trying next...`)
console.log(`Search input selector ${selector} not found, trying next...`)
}
}
@@ -1409,7 +1409,7 @@ export class TradingViewAutomation {
break
}
} catch (e) {
console.log("Result selector " + selector} not found, trying next...`)
console.log(`Result selector ${selector} not found, trying next...`)
}
}
@@ -1481,7 +1481,7 @@ export class TradingViewAutomation {
break
}
} catch (e) {
console.log("Interval legend selector " + selector} not found`)
console.log(`Interval legend selector ${selector} not found`)
}
}
@@ -1539,7 +1539,7 @@ export class TradingViewAutomation {
break
}
} catch (e) {
console.log("Timeframe option selector " + selector} not found or not clickable`)
console.log(`Timeframe option selector ${selector} not found or not clickable`)
}
}
if (found) break
@@ -1572,7 +1572,7 @@ export class TradingViewAutomation {
console.log("SUCCESS: Successfully changed timeframe to " + timeframe) + ")"
await this.takeDebugScreenshot('after_timeframe_change')
} else {
console.log("ERROR: Could not change timeframe to " + timeframe} - timeframe options not found`)
console.log(`ERROR: Could not change timeframe to ${timeframe} - timeframe options not found`)
// Take a debug screenshot to see current state
await this.takeDebugScreenshot('timeframe_change_failed')
@@ -1848,7 +1848,7 @@ export class TradingViewAutomation {
const cookiesData = await fs.readFile(COOKIES_FILE, 'utf8')
const cookies = JSON.parse(cookiesData)
await this.context!.addCookies(cookies)
console.log("SUCCESS: Loaded " + cookies.length} cookies from saved session`)
console.log(`SUCCESS: Loaded ${cookies.length} cookies from saved session`)
}
// Note: Session storage will be loaded after page navigation
@@ -1870,7 +1870,7 @@ export class TradingViewAutomation {
// Save cookies
const cookies = await this.context.cookies()
await fs.writeFile(COOKIES_FILE, JSON.stringify(cookies, null, 2))
console.log("SUCCESS: Saved " + cookies.length} cookies`)
console.log(`SUCCESS: Saved ${cookies.length} cookies`)
// Save session storage and localStorage
const sessionData = await this.page.evaluate(() => {
@@ -2072,7 +2072,7 @@ export class TradingViewAutomation {
if (!this.humanBehaviorEnabled) return
const delay = Math.random() * (maxMs - minMs) + minMs
console.log("⏱️ Human-like delay: " + Math.round(delay)}ms`)
console.log(`⏱️ Human-like delay: ${Math.round(delay)}ms`)
await new Promise(resolve => setTimeout(resolve, delay))
}
@@ -2129,7 +2129,7 @@ export class TradingViewAutomation {
if (timeSinceLastRequest < minInterval) {
const waitTime = minInterval - timeSinceLastRequest
console.log("🚦 Throttling request: waiting ${Math.round(waitTime / 1000)}s before next request (request #" + this.requestCount + 1})`)
console.log(`🚦 Throttling request: waiting ${Math.round(waitTime / 1000)}s before next request (request #${this.requestCount + 1})`)
await new Promise(resolve => setTimeout(resolve, waitTime))
}
@@ -2288,7 +2288,7 @@ export class TradingViewAutomation {
}
await fs.writeFile(captchaMarkerFile, JSON.stringify(markerData, null, 2))
console.log("INFO: Marked captcha detection #${markerData.count} at " + markerData.timestamp) + ")"
console.log('INFO: Marked captcha detection #' + markerData.count + ' at ' + markerData.timestamp)
} catch (error) {
console.log('WARNING: Error marking captcha detection:', error)
}