| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import * as api from './api.js' |
|
|
| let appState = null |
|
|
| export function init(state) { |
| appState = state |
| } |
|
|
| export async function openPosition(marketId, outcome, amount) { |
| const amt = parseFloat(amount) |
| if (!amt || amt <= 0) { |
| alert('Introduce una cantidad válida') |
| return |
| } |
|
|
| const m = appState.markets.find((x) => x.id === marketId) |
| if (!m) return |
|
|
| const entryPrice = outcome === 'YES' ? m.yesPrice : m.noPrice |
| const data = { |
| marketId, |
| outcome, |
| amountEur: amt, |
| entryPrice, |
| } |
|
|
| try { |
| const created = await api.createPosition(data) |
| appState.positions.push(created) |
| document.dispatchEvent(new CustomEvent('positions:changed')) |
| } catch (e) { |
| console.error('Error abriendo posicion:', e) |
| alert('No se pudo abrir la posición. ¿Has iniciado sesión?') |
| } |
| } |
|
|
| export async function closePosition(positionId) { |
| try { |
| await api.closePosition(positionId) |
| appState.positions = appState.positions.filter((p) => p.id !== positionId) |
| document.dispatchEvent(new CustomEvent('positions:changed')) |
| } catch (e) { |
| console.error('Error cerrando posicion:', e) |
| alert('No se pudo cerrar la posición') |
| } |
| } |
|
|