Skip to content

Commit

Permalink
Merge pull request #76 from Gearbox-protocol/lmrewardsv3
Browse files Browse the repository at this point in the history
Lmrewardsv3
  • Loading branch information
harsh-98 authored Jan 16, 2024
2 parents 40ef9b7 + d250472 commit 199b939
Show file tree
Hide file tree
Showing 33 changed files with 992 additions and 85 deletions.
14 changes: 14 additions & 0 deletions db_scripts/delete_rewards_v3.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
delete from farm_V3;
delete from user_lmdetails_v3 ;
delete from sync_adapters where type='LMRewardsv3';

WITH data as (select * from sync_adapters where type = 'AddressProvider')
insert into sync_adapters(address, type, last_sync, firstlog_at, version, discovered_at, disabled) values (
'0x00000000000000000000000000000000000beef3',
'LMRewardsv3',
(SELECT firstlog_at-1 from data),
(SELECT firstlog_at from data),
'300',
(SELECT firstlog_at from data),
'f'
);
2 changes: 1 addition & 1 deletion db_scripts/reset_diesel_tables.sql
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
delete from diesel_balances; delete from diesel_transfers; delete from lm_rewards;
update sync_adapters set details='{}'::jsonb, last_sync=(SELECT firstlog_at-1 FROM sync_adapters WHERE type='AddressProvider') WHERE type='PoolLMRewards';
update sync_adapters set details='{}'::jsonb, last_sync=(SELECT firstlog_at-1 FROM sync_adapters WHERE type='AddressProvider') WHERE type='LMRewardsv2';
2 changes: 1 addition & 1 deletion debts/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func (eng *DebtEngine) notifiedIfLiquidable(sessionId string, notified bool) {
func (eng *DebtEngine) AreActiveAdapterSynchronized() bool {
data := schemas.DebtSync{}
query := `SELECT count(distinct last_sync) as last_calculated_at FROM sync_adapters
WHERE disabled=false AND type NOT IN ('RebaseToken','Treasury','PoolLMRewards','GearToken')`
WHERE disabled=false AND type NOT IN ('RebaseToken','Treasury','LMRewardv2','LMRewardv3','GearToken')`
err := eng.db.Raw(query).Find(&data).Error
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions ds/adapter_kit.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func (kit *AdapterKit) init() {
// another level created bcz of poolKeeper.
kit.AddLevel([]string{PoolQuotaWrapper, ChainlinkPriceFeed})
// REVERT_CM_WRAPPER
kit.AddLevel([]string{CMWrapper, AggregatedQueryFeedWrapper, PoolLMRewards})
kit.AddLevel([]string{CMWrapper, AggregatedQueryFeedWrapper, LMRewardsv2, LMRewardsv3})
// REVERT_CF_WRAPPER
kit.AddLevel([]string{CFWrapper, CreditConfigurator, Treasury})
// - we are dropping the uni check, so the dependency is reversed.
Expand All @@ -32,7 +32,7 @@ func (kit *AdapterKit) init() {
// - Treasury dependent on pools, so it is last
// - acl, PriceOracle and geartoken are independent
// - creditconfigurator and core.CreditFilter are same dependent on creditmanager
// - pool -> dieseltokens -> PoolLMRewards
// - pool -> dieseltokens -> LMRewardsv2 and LMRewardsv3
}

func (kit *AdapterKit) AddLevel(lvl []string) {
Expand Down
6 changes: 5 additions & 1 deletion ds/adapter_name.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const (
Treasury = "Treasury"
AccountManager = "AccountManager"
CreditConfigurator = "CreditConfigurator"
PoolLMRewards = "PoolLMRewards"
LMRewardsv2 = "LMRewardsv2"
//
RebaseToken = "RebaseToken"
// Wrapper
Expand All @@ -32,8 +32,12 @@ const (
PoolQuotaWrapper = "PoolQuotaWrapper"
// v3
PoolQuotaKeeper = "PoolKeeper"
LMRewardsv3 = "LMRewardsv3"
)

// beef2 -- v2LMRewards
// beef3 -- v3LMRewards

func IsWrapperAdapter(name string) bool {
return strings.HasSuffix(name, "Wrapper")
}
Expand Down
19 changes: 19 additions & 0 deletions ds/dc_wrapper/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,14 @@ func (dcw *DataCompressorWrapper) GetKeyAndAddress(version core.VersionType, blo
key, discoveredAt := dcw.getDataCompressorIndex(blockNum)
return key, dcw.getDCAddr(discoveredAt)
}
func (dcw *DataCompressorWrapper) GetLatestv3DC() (common.Address, bool) {
version := core.NewVersion(300)
dc, ok := dcw.versionToAddress[version]
if !ok {
return core.NULL_ADDR, false
}
return dc.address, true
}

func (dcw *DataCompressorWrapper) GetCreditAccountData(version core.VersionType, blockNum int64, creditManager common.Address, borrower common.Address, account common.Address) (
call multicall.Multicall2Call,
Expand Down Expand Up @@ -316,6 +324,17 @@ func (dcw *DataCompressorWrapper) GetCreditManagerData(version core.VersionType,
return
}

func (dcw *DataCompressorWrapper) GetPoolListv3() ([]dcv3.PoolData, bool) {
dcAddr, found := dcw.GetLatestv3DC()
if !found {
return nil, false
}
con, err := dcv3.NewDataCompressorv3(dcAddr, dcw.client)
log.CheckFatal(err)
poolList, err := con.GetPoolsV3List(nil)
log.CheckFatal(err)
return poolList, true
}
func (dcw *DataCompressorWrapper) GetPoolData(version core.VersionType, blockNum int64, _pool common.Address) (
call multicall.Multicall2Call,
resultFn func([]byte) (dc.PoolCallData, error),
Expand Down
16 changes: 10 additions & 6 deletions engine/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ import (
"github.com/Gearbox-protocol/third-eye/config"
"github.com/Gearbox-protocol/third-eye/ds"
"github.com/Gearbox-protocol/third-eye/models/address_provider"
"github.com/Gearbox-protocol/third-eye/models/pool_lmrewards"
lmrewardsv2 "github.com/Gearbox-protocol/third-eye/models/pool_lmrewards/v2"
lmrewardsv3 "github.com/Gearbox-protocol/third-eye/models/pool_lmrewards/v3"
"github.com/Gearbox-protocol/third-eye/models/rebase_token"
"github.com/Gearbox-protocol/third-eye/repository"
"github.com/ethereum/go-ethereum/common"
Expand Down Expand Up @@ -78,9 +79,12 @@ func (e *Engine) getLastSyncedTill() int64 {
obj := address_provider.NewAddressProvider(e.Client, e.repo, e.config.AddressProviderAddrs)
e.repo.AddSyncAdapter(obj)
// pool LM rewards
poolLMRewardObj := pool_lmrewards.NewPoolLMRewards("0x000000000000000000000000000000000000beef", obj.FirstLogAt-1, e.Client, e.repo)
lmrewardsv2Obj := lmrewardsv2.NewLMRewardsv2("0x00000000000000000000000000000000000beef2", obj.FirstLogAt-1, e.Client, e.repo)
e.repo.AddSyncAdapter(lmrewardsv2Obj)
lmrewardsv3Obj := lmrewardsv3.NewLMRewardsv3("0x00000000000000000000000000000000000beef3", obj.FirstLogAt-1, e.Client, e.repo)
e.repo.AddSyncAdapter(lmrewardsv3Obj)
//
e.addstETHToken()
e.repo.AddSyncAdapter(poolLMRewardObj)

return obj.GetLastSync()
} else {
Expand All @@ -100,13 +104,13 @@ func (e *Engine) SyncHandler() {
// only do batch sync if latestblock is far from currently synced block
if lastSyncedTill+e.batchSizeForHistory <= latestBlockNum {
syncedTill := e.syncLoop(lastSyncedTill, latestBlockNum)
log.Infof("Synced till %d sleeping for 5 mins", syncedTill)
log.Infof("Synced till %d", syncedTill)
}
for {
latestBlockNum = e.GetLatestFinalizedBlock(4)
e.SyncAndFlush(latestBlockNum)
log.Infof("Synced till %d sleeping for 5 mins", latestBlockNum)
time.Sleep(30 * time.Second) // on kovan 5 blocks in 1 min , sleep for 5 mins
log.Infof("Synced till %d sleeping for 2 mins", latestBlockNum)
time.Sleep(2 * time.Minute) // on kovan 5 blocks in 1 min , sleep for 5 mins
}
}

Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,4 @@ require (

replace github.com/btcsuite/btcd => github.com/btcsuite/btcd v0.22.1

// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20231219113729-4d2522d6c5ee => ../sdk-go
// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20240112091010-e6a3b573c3cb => ../sdk-go
41 changes: 41 additions & 0 deletions migrations/000037_inch_farming_rewards.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
create table farm_v3 (
pool varchar(42),
farm varchar(42),
--
checkpoint integer,
farmed_per_token varchar(80),
--
reward varchar(80),
period integer,
end_ts integer,
--
total_supply varchar(80),
diesel_token varchar(42),
PRIMARY KEY (farm)
);

create table user_lmdetails_v3 (
balances_bi varchar(80),
balances double precision,
correction varchar(80),
account varchar(42),
farm varchar(42),
diesel_sym varchar(42),
PRIMARY KEY(farm, account)
);

-- 1inch lm rewards are stored with gear.

update sync_adapters set type='LMRewardsv2', address='0x00000000000000000000000000000000000beef2' where type = 'PoolLMRewards';
--
WITH data as (select * from sync_adapters where type = 'AddressProvider')
insert into sync_adapters(address, type, last_sync, firstlog_at, version, discovered_at, disabled) values (
'0x00000000000000000000000000000000000beef3',
'LMRewardsv3',
(SELECT firstlog_at-1 from data),
(SELECT firstlog_at from data),
'300',
(SELECT firstlog_at from data),
'f'
);

127 changes: 127 additions & 0 deletions models/pool/pool_v3/farmed_fix_user.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
package pool_v3

import (
"math/big"

"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/core/schemas"
"github.com/Gearbox-protocol/sdk-go/log"
"github.com/Gearbox-protocol/sdk-go/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"gorm.io/gorm"
)

type UpdatePoolLedger struct {
Zapper string
Pool string
User string
BlockNum int64
TxHash string
Shares *big.Int
Type string
}

func (mdl *Poolv3) updateFarmedv3(txLog types.Log) {
from := common.BytesToAddress(txLog.Topics[1][:]).Hex()
to := common.BytesToAddress(txLog.Topics[2][:]).Hex()
shares := new(big.Int).SetBytes(txLog.Data[:])
txHash := txLog.TxHash.Hex()
blockNum := int64(txLog.BlockNumber)

if from == core.NULL_ADDR.Hex() || to == core.NULL_ADDR.Hex() {
return
}

if from == mdl.getZapPoolv2() {
mdl.updatesForPoolv2 = append(mdl.updatesForPoolv2, UpdatePoolLedger{
Zapper: from,
User: to,
Pool: mdl.getPoolv2(),
BlockNum: blockNum,
TxHash: txHash,
Type: "RemoveLiquidity",
})
}
if from == mdl.getZapUnderlying() || from == mdl.getZapPoolv2() { // usdc-farmedUSDCv3, dUSDC-farmedUSDCv3
if txHash != mdl.addLiquidityEvent.TxHash ||
blockNum != mdl.addLiquidityEvent.BlockNumber ||
shares.Cmp(mdl.addLiquidityEvent.SharesBI.Convert()) != 0 ||
from != mdl.addLiquidityEvent.User {
log.Fatal(utils.ToJson(mdl.addLiquidityEvent), "addLiquidityEvent", "txHash", txHash, "blockNum", blockNum, "shares", shares)
}
mdl.addLiquidityEvent.User = to
mdl.Repo.AddPoolLedger(mdl.addLiquidityEvent)
mdl.addLiquidityEvent = nil
}

if to == mdl.getZapUnderlying() || to == mdl.getZapPoolv2() {
mdl.removeLiqUpdate = &UpdatePoolLedger{
Zapper: to,
User: from,
Pool: mdl.Address,
BlockNum: blockNum,
TxHash: txHash,
Shares: shares,
}
}
if to == mdl.getZapPoolv2() {
mdl.updatesForPoolv2 = append(mdl.updatesForPoolv2, UpdatePoolLedger{
Zapper: to,
User: from,
Pool: mdl.getPoolv2(),
BlockNum: blockNum,
TxHash: txHash,
Type: "AddLiquidity",
})
}
}

func (mdl *Poolv3) changeAddressOnAddLiq(event *schemas.PoolLedger) {
if mdl.removeLiqUpdate != nil {
log.Fatal(utils.ToJson(mdl.removeLiqUpdate), "removeLiqUpdate is not nil on add Liq", utils.ToJson(event))
}
if mdl.addLiquidityEvent != nil {
log.Fatal(utils.ToJson(mdl.addLiquidityEvent), "addLiquidityEvent is not nil", "pool", mdl.Address, "event", event)
}
mdl.addLiquidityEvent = event
}
func (mdl *Poolv3) changeAddressOnRemoveLiq(event *schemas.PoolLedger) {
if mdl.removeLiqUpdate == nil {
log.Fatal(utils.ToJson(event))
}
txHash := mdl.removeLiqUpdate.TxHash
blockNum := mdl.removeLiqUpdate.BlockNum
shares := mdl.removeLiqUpdate.Shares
if txHash != event.TxHash ||
blockNum != event.BlockNumber ||
shares.Cmp(event.SharesBI.Convert()) != 0 ||
mdl.removeLiqUpdate.Zapper != event.User {
log.Fatal(utils.ToJson(mdl.removeLiqUpdate), "removeLiqUpdate", "txHash", event.TxHash, "blockNum", event.BlockNumber, "shares", event.SharesBI, "user", event.User)
}
if mdl.addLiquidityEvent != nil {
log.Fatal(utils.ToJson(mdl.addLiquidityEvent), "addLiquidityEvent is not nil", "pool", mdl.Address, "event", event)
}
//
//
event.User = mdl.removeLiqUpdate.User
mdl.Repo.AddPoolLedger(event)
mdl.removeLiqUpdate = nil
}

func (mdl *Poolv3) UpdatePoolv2Ledger(tx *gorm.DB) {
for _, update := range mdl.updatesForPoolv2 {
if update.Type == "" {
continue
}
x := tx.Exec(`UPDATE pool_ledger set user_address=? WHERE block_num=? AND event = ? AND tx_hash=? AND user_address in (?, ?)`,
update.User, update.BlockNum, update.Type, update.TxHash, update.User, update.Zapper)
log.CheckFatal(x.Error)
//

if x.RowsAffected != 1 {
log.Infof("SEELCT * from pool_ledger WHERE block_num=%d AND event = '%s' AND tx_hash='%s' AND user_address in ('%s', '%s')", update.BlockNum, update.Type, update.TxHash, update.User, update.Zapper)
log.Fatal("Can't update for", utils.ToJson(update))
}
}
}
Loading

0 comments on commit 199b939

Please sign in to comment.