diff --git a/ds/mergedPFVersion.go b/ds/mergedPFVersion.go new file mode 100644 index 00000000..34aa72d6 --- /dev/null +++ b/ds/mergedPFVersion.go @@ -0,0 +1,107 @@ +package ds + +import ( + "reflect" + + "github.com/Gearbox-protocol/sdk-go/core" + "github.com/Gearbox-protocol/sdk-go/core/schemas" + "github.com/Gearbox-protocol/sdk-go/log" +) + +// for use in chainlinkPriceFeed and compositePriceFeed + +type entry struct { + BlockNumber int64 `json:"blockNum"` + MergedPFVersion schemas.MergedPFVersion `json:"mergedPFVersion"` + Token string `json:"-"` +} +type MergedPFManager []entry + +func (mdl *MergedPFManager) add(v int64, details core.Json, discoveredAt int64) { + *mdl = append(*mdl, entry{ + MergedPFVersion: schemas.MergedPFVersion(v), + BlockNumber: discoveredAt, + Token: details["token"].(string), + }) + + delete(details, "mergedPFVersion") +} +func (mdl *MergedPFManager) Load(details core.Json, discoveredAt int64) { + if details == nil { + return + } + if details["mergedPFVersion"] != nil { + + switch v := details["mergedPFVersion"].(type) { + case float64: + mdl.add(int64(v), details, discoveredAt) + return + case int64: + mdl.add(int64(v), details, discoveredAt) + return + case schemas.MergedPFVersion: + mdl.add(int64(v), details, discoveredAt) + return + case []interface{}: + for _, val := range v { + x := val.(map[string]interface{}) + *mdl = append(*mdl, entry{ + MergedPFVersion: schemas.MergedPFVersion(x["mergedPFVersion"].(float64)), + BlockNumber: int64(x["blockNum"].(float64)), + Token: details["token"].(string), + }) + } + default: + log.Fatal("can't get mergedPFVersion", details, reflect.TypeOf(details["mergedPFVersion"])) + } + + } + // if it is nil +} + +func (mdl MergedPFManager) Save(details *core.Json) { + (*details)["mergedPFVersion"] = mdl + log.Info((*details)["mergedPFVersion"]) +} + +func (mdl MergedPFManager) GetMergedPFVersion(blockNum int64) schemas.MergedPFVersion { + for _, entry := range mdl { + if entry.BlockNumber <= blockNum { + return entry.MergedPFVersion + } + } + log.Fatal("Can't get mergedPFVersion", mdl) + return schemas.MergedPFVersion(0) +} +func (mdl *MergedPFManager) AddToken(token string, blockNum int64, pfVersion schemas.PFVersion) { + var last schemas.MergedPFVersion + if len(*mdl) != 0 { + obj := (*mdl)[len(*mdl)-1] + if obj.Token != token { + log.Fatal("stored token for chainlink is different from new added token", obj.Token, token) + } + last = obj.MergedPFVersion + } + *mdl = append(*mdl, entry{ + Token: token, + MergedPFVersion: schemas.MergedPFVersion(pfVersion) | last, + BlockNumber: blockNum, + }) +} + +func (mdl *MergedPFManager) DisableToken(token string, blockNum int64, pfVersion schemas.PFVersion) { + var last schemas.MergedPFVersion + if len(*mdl) != 0 { + obj := (*mdl)[len(*mdl)-1] + if obj.Token != token { + log.Fatal("stored token for chainlink is different from new added token", obj.Token, token) + } + last = obj.MergedPFVersion + } + final := last ^ schemas.MergedPFVersion(pfVersion) + *mdl = append(*mdl, entry{ + Token: token, + MergedPFVersion: final, + BlockNumber: blockNum, + }) +} diff --git a/engine/index.go b/engine/index.go index 4d9511f7..2a94848f 100644 --- a/engine/index.go +++ b/engine/index.go @@ -194,6 +194,7 @@ func (e *Engine) SyncModel(mdl ds.SyncAdapterI, syncTill int64, wg *sync.WaitGro if len(addrsToFetchLogs) == 0 { return } + txLogs, err := e.GetLogs(syncFrom, syncTill, addrsToFetchLogs, mdl.Topics()) log.CheckFatal(err) log.Infof("Sync %s(%s)[addrs: %d] from %d to %d: no: %d", mdl.GetName(), mdl.GetAddress(), len(addrsToFetchLogs), syncFrom, syncTill, len(txLogs)) diff --git a/go.mod b/go.mod index d9d1ed14..7b7f2ac8 100644 --- a/go.mod +++ b/go.mod @@ -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-20240216014533-b42549c9dbfa => ../sdk-go +// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20240216014533-b42549c9dbfa => ../sdk-go diff --git a/jsonnet/account_lifecycle/blocks.jsonnet b/jsonnet/account_lifecycle/blocks.jsonnet index 8ec416e2..0d6acb94 100644 --- a/jsonnet/account_lifecycle/blocks.jsonnet +++ b/jsonnet/account_lifecycle/blocks.jsonnet @@ -225,7 +225,7 @@ { blockNum: 3, feed: '#ChainlinkPriceFeed_1', - isPriceInUSD: false, + mergedPFVersion: 1, price: 0.0004, priceBI: '400000000000000', roundId: 1, @@ -537,7 +537,7 @@ { blockNum: 6, feed: '#ChainlinkPriceFeed_2', - isPriceInUSD: false, + mergedPFVersion: 1, price: 8, priceBI: '8000000000000000000', roundId: 1, diff --git a/jsonnet/account_lifecycle_v2/blocks.jsonnet b/jsonnet/account_lifecycle_v2/blocks.jsonnet index ae71d72a..216b7fe3 100644 --- a/jsonnet/account_lifecycle_v2/blocks.jsonnet +++ b/jsonnet/account_lifecycle_v2/blocks.jsonnet @@ -361,7 +361,7 @@ { blockNum: 5, feed: '#ChainlinkPriceFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 2500, priceBI: '250000000000', roundId: 1, @@ -619,7 +619,7 @@ { blockNum: 7, feed: '#ChainlinkPriceFeed_2', - isPriceInUSD: true, + mergedPFVersion: 2, price: 20000, priceBI: '2000000000000', roundId: 1, diff --git a/jsonnet/aqf/blocks.jsonnet b/jsonnet/aqf/blocks.jsonnet index 2df224f2..02812b4c 100644 --- a/jsonnet/aqf/blocks.jsonnet +++ b/jsonnet/aqf/blocks.jsonnet @@ -3,7 +3,7 @@ { blockNum: 4, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001004, priceBI: '1004', roundId: 0, @@ -12,7 +12,7 @@ { blockNum: 4, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001004, priceBI: '1004', roundId: 0, @@ -21,7 +21,7 @@ { blockNum: 4, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001004, priceBI: '1004', roundId: 0, @@ -30,7 +30,7 @@ { blockNum: 11, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001011, priceBI: '1011', roundId: 0, @@ -39,7 +39,7 @@ { blockNum: 11, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001011, priceBI: '1011', roundId: 0, @@ -48,7 +48,7 @@ { blockNum: 11, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001011, priceBI: '1011', roundId: 0, @@ -57,7 +57,7 @@ { blockNum: 26, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001026, priceBI: '1026', roundId: 0, @@ -66,7 +66,7 @@ { blockNum: 26, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001026, priceBI: '1026', roundId: 0, @@ -75,7 +75,7 @@ { blockNum: 26, feed: '#YearnFeed_1', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00001026, priceBI: '1026', roundId: 0, @@ -84,7 +84,7 @@ { blockNum: 51, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003051, priceBI: '3051', roundId: 0, @@ -93,7 +93,7 @@ { blockNum: 51, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003051, priceBI: '3051', roundId: 0, @@ -102,7 +102,7 @@ { blockNum: 51, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003051, priceBI: '3051', roundId: 0, @@ -111,7 +111,7 @@ { blockNum: 53, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003053, priceBI: '3053', roundId: 0, @@ -120,7 +120,7 @@ { blockNum: 53, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003053, priceBI: '3053', roundId: 0, @@ -129,7 +129,7 @@ { blockNum: 53, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003053, priceBI: '3053', roundId: 0, @@ -138,7 +138,7 @@ { blockNum: 56, feed: '#YearnFeed_4', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.0001647, priceBI: '16470', roundId: 0, @@ -147,7 +147,7 @@ { blockNum: 58, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003058, priceBI: '3058', roundId: 0, @@ -156,7 +156,7 @@ { blockNum: 58, feed: '#YearnFeed_3', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00003058, priceBI: '3058', roundId: 0, @@ -165,7 +165,7 @@ { blockNum: 58, feed: '#YearnFeed_4', - isPriceInUSD: true, + mergedPFVersion: 2, price: 0.00004058, priceBI: '4058', roundId: 0, diff --git a/jsonnet/dao_operations/blocks.jsonnet b/jsonnet/dao_operations/blocks.jsonnet index 0a1438e9..4e0ad031 100644 --- a/jsonnet/dao_operations/blocks.jsonnet +++ b/jsonnet/dao_operations/blocks.jsonnet @@ -3,6 +3,7 @@ Args: { priceFeed: '#Oracle_4', token: '#Token_4', + reserve: false, }, BlockNumber: 3, Contract: '#PriceOracle_1', diff --git a/jsonnet/liquidate_credit_account/blocks.jsonnet b/jsonnet/liquidate_credit_account/blocks.jsonnet index e6613609..2c853a58 100644 --- a/jsonnet/liquidate_credit_account/blocks.jsonnet +++ b/jsonnet/liquidate_credit_account/blocks.jsonnet @@ -47,7 +47,7 @@ feed: '#ChainlinkPriceFeed_1', price: 0.0008, priceBI: '800000000000000', - isPriceInUSD: false, + mergedPFVersion: 1, roundId: 2, token: '#Token_1', }, diff --git a/jsonnet/mocks/syncAdapter1.jsonnet b/jsonnet/mocks/syncAdapter1.jsonnet index d4bb72ec..17d4b918 100644 --- a/jsonnet/mocks/syncAdapter1.jsonnet +++ b/jsonnet/mocks/syncAdapter1.jsonnet @@ -61,6 +61,7 @@ oracle: '#Oracle_1', token: '#Token_1', dieselToken: '#DieselToken_1', + mergedPFVersion: 1, }, lastSync: 2, version: 1, @@ -80,6 +81,7 @@ details: { oracle: '#Oracle_2', token: '#Token_2', + mergedPFVersion: 1, }, lastSync: 2, version: 1, diff --git a/jsonnet/mocks/syncAdapterV2.jsonnet b/jsonnet/mocks/syncAdapterV2.jsonnet index e22c950b..9fd565d0 100644 --- a/jsonnet/mocks/syncAdapterV2.jsonnet +++ b/jsonnet/mocks/syncAdapterV2.jsonnet @@ -48,6 +48,7 @@ details: { oracle: '#Oracle_0', token: '#Token_1', + mergedPFVersion: 1, }, lastSync: 2, version: 1, @@ -94,6 +95,7 @@ details: { oracle: '#Oracle_1', token: '#Token_1', + mergedPFVersion: 2, }, lastSync: 2, version: 2, @@ -105,6 +107,7 @@ details: { oracle: '#Oracle_2', token: '#Token_2', + mergedPFVersion: 2, }, lastSync: 2, version: 2, @@ -115,6 +118,7 @@ details: { oracle: '#Oracle_3', token: '#Token_3', + mergedPFVersion: 2, }, lastSync: 2, version: 2, diff --git a/jsonnet/mocks/yearn_feed.jsonnet b/jsonnet/mocks/yearn_feed.jsonnet index 6c1fd6bd..ba7f3c5b 100644 --- a/jsonnet/mocks/yearn_feed.jsonnet +++ b/jsonnet/mocks/yearn_feed.jsonnet @@ -6,6 +6,7 @@ details: { token: { '#Token_2': [1, 50], '#Token_3': [1, 50], '#Token_4': [1, 50] }, // removed at 50 pfType: 'YearnPF', + mergedPFVersion: 2, }, lastSync: 1, version: 2, @@ -16,6 +17,7 @@ details: { token: { '#Token_2': [50], '#Token_3': [50], '#Token_4': [50] }, pfType: 'YearnPF', + mergedPFVersion: 2, }, lastSync: 1, version: 2, diff --git a/jsonnet/open_credit_account/blocks.jsonnet b/jsonnet/open_credit_account/blocks.jsonnet index 60e7a4d2..89f24cab 100644 --- a/jsonnet/open_credit_account/blocks.jsonnet +++ b/jsonnet/open_credit_account/blocks.jsonnet @@ -193,7 +193,7 @@ { blockNum: 3, feed: '#ChainlinkPriceFeed_1', - isPriceInUSD: false, + mergedPFVersion: 1, price: 1e-18, priceBI: '1', roundId: 300000000000000, diff --git a/jsonnet/oracle/blocks.jsonnet b/jsonnet/oracle/blocks.jsonnet index 0c7d82f6..93eadde6 100644 --- a/jsonnet/oracle/blocks.jsonnet +++ b/jsonnet/oracle/blocks.jsonnet @@ -7,7 +7,7 @@ feed: '#ChainlinkPriceFeed_1', price: 0.0004, priceBI: '400000000000000', - isPriceInUSD: false, + mergedPFVersion: 1, roundId: 1, token: '#Token_1', }, @@ -16,7 +16,7 @@ feed: '#ChainlinkPriceFeed_2', price: 8, priceBI: '8000000000000000000', - isPriceInUSD: false, + mergedPFVersion: 1, roundId: 1, token: '#Token_2', }, @@ -32,6 +32,7 @@ feed: '#ChainlinkPriceFeed_4', oracle: '#Oracle_2', token: '#Token_2', + reserve: false, version: 1, }, ], @@ -43,6 +44,7 @@ Args: { priceFeed: '#Oracle_3', token: '#Token_1', + reserve: false, }, BlockNumber: 5, Contract: '#PriceOracle_1', @@ -57,7 +59,7 @@ feed: '#ChainlinkPriceFeed_4', price: 10, priceBI: '10000000000000000000', - isPriceInUSD: false, + mergedPFVersion: 1, roundId: 1, token: '#Token_2', }, @@ -68,6 +70,7 @@ blockNum: 5, feed: '#ChainlinkPriceFeed_3', oracle: '#Oracle_3', + reserve: false, token: '#Token_1', version: 1, }, diff --git a/jsonnet/oracle/token_oracle.jsonnet b/jsonnet/oracle/token_oracle.jsonnet index eed262ac..4f0e50a1 100644 --- a/jsonnet/oracle/token_oracle.jsonnet +++ b/jsonnet/oracle/token_oracle.jsonnet @@ -1,10 +1,11 @@ { - 'false': { + '1': { '#Token_1': { blockNum: 5, feed: '#ChainlinkPriceFeed_3', oracle: '#Oracle_3', token: '#Token_1', + reserve: false, version: 1, }, '#Token_2': { @@ -12,6 +13,7 @@ feed: '#ChainlinkPriceFeed_4', oracle: '#Oracle_2', token: '#Token_2', + reserve: false, version: 1, }, }, diff --git a/jsonnet/sync_adapters/adapters.jsonnet b/jsonnet/sync_adapters/adapters.jsonnet index 2d2d750f..c97ea34e 100644 --- a/jsonnet/sync_adapters/adapters.jsonnet +++ b/jsonnet/sync_adapters/adapters.jsonnet @@ -48,6 +48,10 @@ details: { oracle: '#Oracle_1', token: '#Token_1', + mergedPFVersion: [{ + blockNum: 3, + mergedPFVersion: 1, + }], }, disabled: false, disabled_at: 0, @@ -63,6 +67,10 @@ details: { oracle: '#Oracle_5', token: '#Token_1', + mergedPFVersion: [{ + blockNum: 6, + mergedPFVersion: 2, + }], }, disabled: false, disabled_at: 0, diff --git a/jsonnet/sync_adapters/blocks.jsonnet b/jsonnet/sync_adapters/blocks.jsonnet index b33c27bd..4d6c0b1a 100644 --- a/jsonnet/sync_adapters/blocks.jsonnet +++ b/jsonnet/sync_adapters/blocks.jsonnet @@ -5,6 +5,7 @@ Args: { priceFeed: '#Oracle_5', token: '#Token_1', + reserve: false, }, BlockNumber: 6, Contract: '#PriceOracle_2', @@ -17,18 +18,18 @@ { blockNum: 6, feed: '#ChainlinkPriceFeed_1', - isPriceInUSD: false, price: 0.0004, priceBI: '400000000000000', roundId: 1, + mergedPFVersion: 1, token: '#Token_1', }, { blockNum: 6, feed: '#ChainlinkPriceFeed_5', - isPriceInUSD: true, price: 1, priceBI: '100000000', + mergedPFVersion: 2, roundId: 1, token: '#Token_1', }, @@ -40,6 +41,7 @@ feed: '#ChainlinkPriceFeed_5', oracle: '#Oracle_5', token: '#Token_1', + reserve: false, version: 2, }, ], diff --git a/models/aggregated_block_feed/query_price_feed/token_ds.go b/models/aggregated_block_feed/query_price_feed/token_ds.go index 419b5e03..359989df 100644 --- a/models/aggregated_block_feed/query_price_feed/token_ds.go +++ b/models/aggregated_block_feed/query_price_feed/token_ds.go @@ -35,6 +35,9 @@ func (obj *DetailsDS) Load(in core.Json, version core.VersionType) { if obj.Tokens == nil { obj.Tokens = map[string]map[schemas.PFVersion][]int64{} } + if len(obj.Reduntant) != 0 && (obj.MergedPFVersion == nil || *obj.MergedPFVersion == 0) { + log.Fatal("For reduntant, mergedPFVersion should be set", utils.ToJson(obj.Reduntant), obj.MergedPFVersion) + } for token, blockNums := range obj.Reduntant { if obj.Tokens[token] == nil { obj.Tokens[token] = map[schemas.PFVersion][]int64{} diff --git a/models/chainlink_price_feed/model.go b/models/chainlink_price_feed/model.go index 25d62c04..46fafcc2 100644 --- a/models/chainlink_price_feed/model.go +++ b/models/chainlink_price_feed/model.go @@ -13,8 +13,9 @@ import ( type ChainlinkPriceFeed struct { *ds.SyncAdapter - Token string - MainAgg *ChainlinkMainAgg + Token string + MainAgg *ChainlinkMainAgg + mergedPFManager *ds.MergedPFManager } // if oracle and address are same then the normal chainlink interface is not working for this price feed @@ -93,6 +94,8 @@ func NewChainlinkPriceFeedFromAdapter(adapter *ds.SyncAdapter, includeLastLogBef } } obj.DataProcessType = ds.ViaMultipleLogs + obj.mergedPFManager = &ds.MergedPFManager{} + obj.mergedPFManager.Load(obj.Details, obj.DiscoveredAt) return obj } @@ -116,6 +119,7 @@ func (mdl *ChainlinkPriceFeed) AfterSyncHook(syncedTill int64) { }, mdl.upperLimit().Cmp(new(big.Int)) != 0) // if upperLImit is not zero, then the price is bounded by upperLimit } mdl.SyncAdapter.AfterSyncHook(syncedTill) + mdl.mergedPFManager.Save(&mdl.Details) } func (mdl *ChainlinkPriceFeed) upperLimit() *big.Int { diff --git a/models/chainlink_price_feed/on_log.go b/models/chainlink_price_feed/on_log.go index 39e05939..e290c271 100644 --- a/models/chainlink_price_feed/on_log.go +++ b/models/chainlink_price_feed/on_log.go @@ -58,7 +58,7 @@ func (mdl *ChainlinkPriceFeed) OnLogs(txLogs []types.Log) { RoundId: roundId, PriceBI: (*core.BigInt)(answerBI), Price: utils.GetFloat64Decimal(answerBI, pfVersion.Decimals()), - MergedPFVersion: mdl.GetMergedPFVersion(), + MergedPFVersion: mdl.mergedPFManager.GetMergedPFVersion(blockNum), } mdl.Repo.AddPriceFeed(priceFeed) blockNums = append(blockNums, blockNum) @@ -71,36 +71,13 @@ func (mdl *ChainlinkPriceFeed) OnLogs(txLogs []types.Log) { } -func (mdl ChainlinkPriceFeed) GetMergedPFVersion() schemas.MergedPFVersion { - if mdl.Details["mergedPFVersion"] != nil { - if v, ok := mdl.Details["mergedPFVersion"].(int8); ok { - return schemas.MergedPFVersion(v) - } - if v, ok := mdl.Details["mergedPFVersion"].(float64); ok { - return schemas.MergedPFVersion(v) - } - return schemas.MergedPFVersion(mdl.Details["mergedPFVersion"].(schemas.MergedPFVersion)) - } - log.Fatal("Can't get mergedPFVersion", utils.ToJson(mdl.Details)) - return schemas.MergedPFVersion(0) -} -func (mdl ChainlinkPriceFeed) AddToken(token string, pfVersion schemas.PFVersion) { - if mdl.Details["token"] != nil { - if mdl.Details["token"].(string) != token { - log.Fatal("stored token for chainlink is different from new added token", mdl.Details["token"].(string), token) - } - } - mdl.Details["mergedPFVersion"] = mdl.GetMergedPFVersion() | schemas.MergedPFVersion(pfVersion) +func (mdl *ChainlinkPriceFeed) AddToken(token string, blockNum int64, pfVersion schemas.PFVersion) { + mdl.mergedPFManager.AddToken(token, blockNum, pfVersion) } func (mdl ChainlinkPriceFeed) DisableToken(token string, blockNum int64, pfVersion schemas.PFVersion) { - if mdl.Details["token"] != nil { - if mdl.Details["token"].(string) != token { - log.Fatal("stored token for chainlink is different from new added token", mdl.Details["token"].(string), token) - } - } - final := mdl.GetMergedPFVersion() ^ schemas.MergedPFVersion(pfVersion) - mdl.Details["mergedPFVersion"] = final + mdl.mergedPFManager.DisableToken(token, blockNum, pfVersion) + final := mdl.mergedPFManager.GetMergedPFVersion(blockNum) if final == 0 { mdl.SetBlockToDisableOn(blockNum) } diff --git a/models/chainlink_price_feed/on_logs_test.go b/models/chainlink_price_feed/on_logs_test.go index 1a4ab605..d5c9b638 100644 --- a/models/chainlink_price_feed/on_logs_test.go +++ b/models/chainlink_price_feed/on_logs_test.go @@ -22,23 +22,26 @@ func (x *OnLogsChecker) AddPriceFeed(pf *schemas.PriceFeed) { } func TestOnLogs(t *testing.T) { validPf := &schemas.PriceFeed{ - Feed: utils.RandomAddr(), - Token: utils.RandomAddr(), - BlockNumber: 1, - PriceBI: (*core.BigInt)(big.NewInt(222)), - RoundId: 3, + Feed: utils.RandomAddr(), + Token: utils.RandomAddr(), + BlockNumber: 1, + PriceBI: (*core.BigInt)(big.NewInt(222)), + RoundId: 3, + MergedPFVersion: schemas.MergedPFVersion(schemas.V2PF), } repo := &OnLogsChecker{} obj := &ChainlinkPriceFeed{SyncAdapter: &ds.SyncAdapter{ Repo: repo, SyncAdapterSchema: &schemas.SyncAdapterSchema{ Contract: &schemas.Contract{ - Address: validPf.Feed, + Address: validPf.Feed, + DiscoveredAt: 1, }, V: core.NewVersion(1), - Details: core.Json{"token": validPf.Token}, + Details: core.Json{"token": validPf.Token, "mergedPFVersion": validPf.MergedPFVersion}, }, - }} + }, mergedPFManager: &ds.MergedPFManager{}} + obj.mergedPFManager.Load(obj.Details, obj.DiscoveredAt) txLogs := []types.Log{ { BlockNumber: 1, @@ -63,6 +66,7 @@ func TestOnLogs(t *testing.T) { if len(repo.pfs) != 1 || repo.pfs[0].BlockNumber != validPf.BlockNumber || repo.pfs[0].Feed != validPf.Feed || + repo.pfs[0].MergedPFVersion != validPf.MergedPFVersion || repo.pfs[0].RoundId != validPf.RoundId || repo.pfs[0].PriceBI.Cmp(validPf.PriceBI) != 0 { t.Fatal(utils.ToJson(repo.pfs)) diff --git a/models/composite_chainlink/model.go b/models/composite_chainlink/model.go index 70b645b9..ad732399 100644 --- a/models/composite_chainlink/model.go +++ b/models/composite_chainlink/model.go @@ -22,6 +22,7 @@ type CompositeChainlinkPF struct { TokenETHPrice *big.Int ETHUSDPrice *big.Int decimalsOfBasePF int8 + mergedPFManager *ds.MergedPFManager } // compositeChainlink price feed has token base oracle and base usd oracle for calculating the price of token in usd. @@ -97,6 +98,9 @@ func NewCompositeChainlinkPFFromAdapter(adapter *ds.SyncAdapter) *CompositeChain compositeMdl.MainAgg = cpf.NewMainAgg(adapter.Client, compositeMdl.getAddrFromDetails("target")) compositeMdl.DataProcessType = ds.ViaMultipleLogs compositeMdl.setPrices(adapter.LastSync) + // + compositeMdl.mergedPFManager = &ds.MergedPFManager{} + compositeMdl.mergedPFManager.Load(compositeMdl.Details, compositeMdl.DiscoveredAt) return compositeMdl } @@ -154,6 +158,7 @@ func getDecimals(client core.ClientI, addr common.Address, blockNum int64) int8 func (mdl *CompositeChainlinkPF) AfterSyncHook(syncedTill int64) { mdl.SyncAdapter.AfterSyncHook(syncedTill) + mdl.mergedPFManager.Save(&mdl.Details) } // there are two type of composite oracle diff --git a/models/composite_chainlink/on_log.go b/models/composite_chainlink/on_log.go index de58a2e9..4752a316 100644 --- a/models/composite_chainlink/on_log.go +++ b/models/composite_chainlink/on_log.go @@ -113,7 +113,7 @@ func (mdl *CompositeChainlinkPF) addPriceToDB(blockNum int64) { RoundId: 0, PriceBI: (*core.BigInt)(answerBI), Price: utils.GetFloat64Decimal(answerBI, 8), - MergedPFVersion: mdl.GetMergedPFVersion(), + MergedPFVersion: mdl.mergedPFManager.GetMergedPFVersion(blockNum), } mdl.Repo.AddPriceFeed(priceFeed) } @@ -122,36 +122,13 @@ func (mdl *CompositeChainlinkPF) OnLog(types.Log) { } -func (mdl CompositeChainlinkPF) GetMergedPFVersion() schemas.MergedPFVersion { - if mdl.Details["mergedPFVersion"] != nil { - if v, ok := mdl.Details["mergedPFVersion"].(int8); ok { - return schemas.MergedPFVersion(v) - } - if v, ok := mdl.Details["mergedPFVersion"].(float64); ok { - return schemas.MergedPFVersion(v) - } - return schemas.MergedPFVersion(mdl.Details["mergedPFVersion"].(schemas.MergedPFVersion)) - } - log.Fatal(utils.ToJson(mdl.Details)) - return schemas.MergedPFVersion(0) -} -func (mdl CompositeChainlinkPF) AddToken(token string, pfVersion schemas.PFVersion) { - if mdl.Details["token"] != nil { - if mdl.Details["token"].(string) != token { - log.Fatal("stored token for chainlink is different from new added token", mdl.Details["token"].(string), token) - } - } - mdl.Details["mergedPFVersion"] = mdl.GetMergedPFVersion() | schemas.MergedPFVersion(pfVersion) +func (mdl *CompositeChainlinkPF) AddToken(token string, blockNum int64, pfVersion schemas.PFVersion) { + mdl.mergedPFManager.AddToken(token, blockNum, pfVersion) } func (mdl CompositeChainlinkPF) DisableToken(token string, blockNum int64, pfVersion schemas.PFVersion) { - if mdl.Details["token"] != nil { - if mdl.Details["token"].(string) != token { - log.Fatal("stored token for chainlink is different from new added token", mdl.Details["token"].(string), token) - } - } - final := mdl.GetMergedPFVersion() ^ schemas.MergedPFVersion(pfVersion) - mdl.Details["mergedPFVersion"] = final + mdl.mergedPFManager.DisableToken(token, blockNum, pfVersion) + final := mdl.mergedPFManager.GetMergedPFVersion(blockNum) if final == 0 { mdl.SetBlockToDisableOn(blockNum) } diff --git a/models/credit_manager/cm_common/facade_actions.go b/models/credit_manager/cm_common/facade_actions.go index 97c25694..be0f8348 100644 --- a/models/credit_manager/cm_common/facade_actions.go +++ b/models/credit_manager/cm_common/facade_actions.go @@ -104,7 +104,7 @@ func (mdl *CommonCMAdapter) validateAndSaveFacadeActions(version core.VersionTyp if utils.Contains([]string{ "LiquidateCreditAccount(address,address,address,uint256)", "CloseCreditAccount(address,address)", - }, mainEvent.Action) { // REV_COL_LIQ_V3: v3 liquidate reverse the collateral + }, mainEvent.Action) { // REV_COL_LIQ_V3: v3 liquidate reserve the collateral // also revert at closeCreditAccount as collateralunderlying should be // since liquidation the withdraw collateral is not to the account owner. mdl.AddCollateralToSession(event.BlockNumber, event.SessionId, diff --git a/models/price_oracle/on_log.go b/models/price_oracle/on_log.go index f2bbed28..338aab55 100644 --- a/models/price_oracle/on_log.go +++ b/models/price_oracle/on_log.go @@ -43,7 +43,7 @@ func (mdl *PriceOracle) OnLog(txLog types.Log) { Args: &core.Json{ "priceFeed": oracle, "token": token, - "reverse": isReverse, + "reserve": isReverse, }, }) diff --git a/repository/handlers/token_oracle.go b/repository/handlers/token_oracle.go index a8baed1e..2177b778 100644 --- a/repository/handlers/token_oracle.go +++ b/repository/handlers/token_oracle.go @@ -105,10 +105,10 @@ func (repo *TokenOracleRepo) disablePrevAdapterAndAddNewTokenOracle(newTokenOrac log.Error("Adapter not found for", oldFeed, utils.ToJson(oldTokenOracle)) } else if adapter.GetName() != ds.QueryPriceFeed { if mdl, ok := adapter.(*chainlink_price_feed.ChainlinkPriceFeed); ok { - mdl.DisableToken(oldTokenOracle.Token, oldTokenOracle.BlockNumber, pfVersion) + mdl.DisableToken(oldTokenOracle.Token, newTokenOracle.BlockNumber, pfVersion) } if mdl, ok := adapter.(*composite_chainlink.CompositeChainlinkPF); ok { - mdl.DisableToken(oldTokenOracle.Token, oldTokenOracle.BlockNumber, pfVersion) + mdl.DisableToken(oldTokenOracle.Token, newTokenOracle.BlockNumber, pfVersion) } } else { repo.adapters.GetAggregatedFeed().DisableYearnFeed( @@ -190,7 +190,7 @@ func (repo *TokenOracleRepo) AddNewPriceOracleEvent(newTokenOracle *schemas.Toke repo.disablePrevAdapterAndAddNewTokenOracle(newTokenOracle) // if adapter := repo.adapters.GetAdapter(obj.Address); adapter != nil { - adapter.(*chainlink_price_feed.ChainlinkPriceFeed).AddToken(newTokenOracle.Token, pfVersion) + adapter.(*chainlink_price_feed.ChainlinkPriceFeed).AddToken(newTokenOracle.Token, newTokenOracle.BlockNumber, pfVersion) return } // SPECIAL CASE @@ -219,7 +219,7 @@ func (repo *TokenOracleRepo) AddNewPriceOracleEvent(newTokenOracle *schemas.Toke repo.disablePrevAdapterAndAddNewTokenOracle(newTokenOracle) if adapter := repo.adapters.GetAdapter(obj.Address); adapter != nil { - adapter.(*composite_chainlink.CompositeChainlinkPF).AddToken(newTokenOracle.Token, pfVersion) + adapter.(*composite_chainlink.CompositeChainlinkPF).AddToken(newTokenOracle.Token, newTokenOracle.BlockNumber, pfVersion) return } repo.adapters.AddSyncAdapter(obj)