Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 16 additions & 7 deletions src/js/18-chain-sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,10 @@ async function syncRysk(address) {
throw e;
}

const synced = loadSynced();
const newTrades = [];
let corrected = 0;
const synced = loadSynced();
const newTrades = [];
const correctedTrades = [];
let corrected = 0;

for (const r of (positions || [])) {
if (r.txHash && synced.has(r.txHash)) {
Expand All @@ -126,6 +127,7 @@ async function syncRysk(address) {
const existing = trades.find(t => t.txHash === r.txHash);
if (existing && existing.outcome === 'OPEN') {
existing.outcome = 'EXPIRED';
correctedTrades.push(existing);
corrected++;
}
}
Expand All @@ -148,9 +150,12 @@ async function syncRysk(address) {
save();
render();
saveSynced(synced);
const expiredNew = openTrades.filter(t => t.outcome === 'EXPIRED');
if (expiredNew.length) {
autoDetectOutcomes(expiredNew).then(changed => { if (changed) { save(); render(); } });
const toDetect = [
...openTrades.filter(t => t.outcome === 'EXPIRED'),
...correctedTrades,
];
if (toDetect.length) {
autoDetectOutcomes(toDetect).then(changed => { if (changed) { save(); render(); } });
}
}

Expand Down Expand Up @@ -390,14 +395,18 @@ async function autoDetectOutcomes(newTrades) {
}

let changed = false;
let unresolved = 0;
for (const t of toCheck) {
const spot = priceCache[t.asset + '|' + t.expiry];
if (spot == null) continue;
if (spot == null) { unresolved++; continue; }
const trade = trades.find(tr => tr.id === t.id);
if (!trade || trade.outcome !== 'EXPIRED') continue;
if (t.type === 'CALL' && spot >= t.strike) { trade.outcome = 'CALLED'; changed = true; }
if (t.type === 'PUT' && spot <= t.strike) { trade.outcome = 'ASSIGNED'; changed = true; }
}
if (unresolved > 0) {
toast(unresolved + ' expired option' + (unresolved > 1 ? 's' : '') + ' — verify outcome manually', 'info');
}
return changed;
}

Expand Down
Loading