"""#2779: v3 LP-remove events (liq kind=remove) from v3live.db + v3hist.db -> /opt/sw/lpburn.db (rebuild, idempotent)"""
import sqlite3,os
T='/opt/sw/lpburn.db.tmp'
if os.path.exists(T): os.remove(T)
o=sqlite3.connect(T)
o.execute("create table lpburn(pool text,block int,ts int,eth_out real,owner text,tx text,li int,tok_out real,pool_eth_before real,share_eth real,src text,primary key(tx,li))")
for s in ('v3live','v3hist'):
    c=sqlite3.connect(f'file:/opt/sw/{s}.db?mode=ro',uri=True,timeout=60)
    o.executemany("insert or ignore into lpburn values(?,?,?,?,?,?,?,?,?,?,?)",
      ((p,b,t,e,sg,tx,li,tk,pb,sh,s) for p,b,t,e,sg,tx,li,tk,pb,sh in
       c.execute("select pool,b,t,eth,signer,tx,li,tok,pool_eth_before,share_eth from liq where kind='remove'")))
o.execute("create index lb_p on lpburn(pool,block)");o.commit()
print(o.execute("select count(*),count(distinct pool),min(ts),max(ts) from lpburn").fetchone())
o.close();os.replace(T,'/opt/sw/lpburn.db')
