|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "github.com/Gearbox-protocol/sdk-go/calc" |
| 5 | + "github.com/Gearbox-protocol/sdk-go/core/schemas/schemas_v3" |
| 6 | + "github.com/Gearbox-protocol/sdk-go/log" |
| 7 | + "github.com/Gearbox-protocol/sdk-go/pkg/priceFetcher" |
| 8 | + "github.com/Gearbox-protocol/third-eye/config" |
| 9 | + "github.com/Gearbox-protocol/third-eye/ethclient" |
| 10 | + "github.com/Gearbox-protocol/third-eye/repository" |
| 11 | +) |
| 12 | + |
| 13 | +func main() { |
| 14 | + cfg := config.NewConfig() |
| 15 | + db := repository.NewDBClient(cfg) |
| 16 | + client := ethclient.NewEthClient(cfg) |
| 17 | + |
| 18 | + tStore := priceFetcher.NewTokensStore(client) |
| 19 | + |
| 20 | + sessions := []*schemas_v3.TradingPriceObj{} |
| 21 | + err := db.Raw(`WITH cs as (select id, cm.underlying_token, since, remaining_funds from credit_sessions _cs |
| 22 | + JOIN credit_managers cm on cm.address = _cs.credit_manager where version=300), |
| 23 | + css AS (select distinct on (session_id) cs.since, _css.block_num, session_id, balances, |
| 24 | + borrowed_amount_bi, collateral_underlying FROM credit_session_snapshots _css |
| 25 | + JOIN cs ON cs.id =_css.session_id |
| 26 | + WHERE (SELECT count(*) FROM (select * from jsonb_object_keys(_css.balances) union (select cs.underlying_token)) t)>=2 |
| 27 | + ORDER BY session_id, block_num) |
| 28 | + SELECT * FROM cs JOIN css ON css.session_id = cs.id`).Find(&sessions).Error |
| 29 | + log.CheckFatal(err) |
| 30 | + // |
| 31 | + for _, session := range sessions { |
| 32 | + price := calc.CalcEntryPriceBySession(1, tStore, session) |
| 33 | + log.Info("session", session.SessionId, "entryPrice", price) |
| 34 | + } |
| 35 | + // |
| 36 | + |
| 37 | + err = db.Raw(`WITH cs as (select id, credit_manager, closed_at, remaining_funds from credit_sessions where version=300), |
| 38 | + debts as (select distinct on (session_id) session_id, cal_borrowed_amt_with_interest_bi from debts where session_id in (select id from cs) order by session_id, block_num desc) |
| 39 | + SELECT cs.*, debts.cal_borrowed_amt_with_interest_bi, cm.underlying_token, css.borrowed_amount_bi, css.collateral_underlying, css.balances FROM cs |
| 40 | + JOIN credit_session_snapshots css ON css.session_id = cs.id AND css.block_num = cs.closed_at-1 |
| 41 | + JOIN credit_managers cm ON cm.address = cs.credit_manager |
| 42 | + LEFT JOIN debts ON debts.session_id = cs.id`).Find(&sessions).Error |
| 43 | + log.CheckFatal(err) |
| 44 | + |
| 45 | + for _, session := range sessions { |
| 46 | + price := calc.CalcClosePriceBySession(1, tStore, session) |
| 47 | + log.Info("session", session.SessionId, "closePrice", price) |
| 48 | + } |
| 49 | +} |
0 commit comments