Skip to content

Commit

Permalink
fix: test and add multiple mergedPFVersion support to chainlink/compo…
Browse files Browse the repository at this point in the history
…site PF
  • Loading branch information
harsh-98 committed Feb 16, 2024
1 parent 4992a11 commit 2edccb3
Show file tree
Hide file tree
Showing 25 changed files with 206 additions and 104 deletions.
107 changes: 107 additions & 0 deletions ds/mergedPFVersion.go
Original file line number Diff line number Diff line change
@@ -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,
})
}
1 change: 1 addition & 0 deletions engine/index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
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-20240216014533-b42549c9dbfa => ../sdk-go
// replace github.com/Gearbox-protocol/sdk-go v0.0.0-20240216014533-b42549c9dbfa => ../sdk-go
4 changes: 2 additions & 2 deletions jsonnet/account_lifecycle/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@
{
blockNum: 3,
feed: '#ChainlinkPriceFeed_1',
isPriceInUSD: false,
mergedPFVersion: 1,
price: 0.0004,
priceBI: '400000000000000',
roundId: 1,
Expand Down Expand Up @@ -537,7 +537,7 @@
{
blockNum: 6,
feed: '#ChainlinkPriceFeed_2',
isPriceInUSD: false,
mergedPFVersion: 1,
price: 8,
priceBI: '8000000000000000000',
roundId: 1,
Expand Down
4 changes: 2 additions & 2 deletions jsonnet/account_lifecycle_v2/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,7 @@
{
blockNum: 5,
feed: '#ChainlinkPriceFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 2500,
priceBI: '250000000000',
roundId: 1,
Expand Down Expand Up @@ -619,7 +619,7 @@
{
blockNum: 7,
feed: '#ChainlinkPriceFeed_2',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 20000,
priceBI: '2000000000000',
roundId: 1,
Expand Down
38 changes: 19 additions & 19 deletions jsonnet/aqf/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{
blockNum: 4,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001004,
priceBI: '1004',
roundId: 0,
Expand All @@ -12,7 +12,7 @@
{
blockNum: 4,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001004,
priceBI: '1004',
roundId: 0,
Expand All @@ -21,7 +21,7 @@
{
blockNum: 4,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001004,
priceBI: '1004',
roundId: 0,
Expand All @@ -30,7 +30,7 @@
{
blockNum: 11,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001011,
priceBI: '1011',
roundId: 0,
Expand All @@ -39,7 +39,7 @@
{
blockNum: 11,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001011,
priceBI: '1011',
roundId: 0,
Expand All @@ -48,7 +48,7 @@
{
blockNum: 11,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001011,
priceBI: '1011',
roundId: 0,
Expand All @@ -57,7 +57,7 @@
{
blockNum: 26,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001026,
priceBI: '1026',
roundId: 0,
Expand All @@ -66,7 +66,7 @@
{
blockNum: 26,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001026,
priceBI: '1026',
roundId: 0,
Expand All @@ -75,7 +75,7 @@
{
blockNum: 26,
feed: '#YearnFeed_1',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00001026,
priceBI: '1026',
roundId: 0,
Expand All @@ -84,7 +84,7 @@
{
blockNum: 51,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003051,
priceBI: '3051',
roundId: 0,
Expand All @@ -93,7 +93,7 @@
{
blockNum: 51,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003051,
priceBI: '3051',
roundId: 0,
Expand All @@ -102,7 +102,7 @@
{
blockNum: 51,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003051,
priceBI: '3051',
roundId: 0,
Expand All @@ -111,7 +111,7 @@
{
blockNum: 53,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003053,
priceBI: '3053',
roundId: 0,
Expand All @@ -120,7 +120,7 @@
{
blockNum: 53,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003053,
priceBI: '3053',
roundId: 0,
Expand All @@ -129,7 +129,7 @@
{
blockNum: 53,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003053,
priceBI: '3053',
roundId: 0,
Expand All @@ -138,7 +138,7 @@
{
blockNum: 56,
feed: '#YearnFeed_4',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.0001647,
priceBI: '16470',
roundId: 0,
Expand All @@ -147,7 +147,7 @@
{
blockNum: 58,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003058,
priceBI: '3058',
roundId: 0,
Expand All @@ -156,7 +156,7 @@
{
blockNum: 58,
feed: '#YearnFeed_3',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00003058,
priceBI: '3058',
roundId: 0,
Expand All @@ -165,7 +165,7 @@
{
blockNum: 58,
feed: '#YearnFeed_4',
isPriceInUSD: true,
mergedPFVersion: 2,
price: 0.00004058,
priceBI: '4058',
roundId: 0,
Expand Down
1 change: 1 addition & 0 deletions jsonnet/dao_operations/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
Args: {
priceFeed: '#Oracle_4',
token: '#Token_4',
reserve: false,
},
BlockNumber: 3,
Contract: '#PriceOracle_1',
Expand Down
2 changes: 1 addition & 1 deletion jsonnet/liquidate_credit_account/blocks.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
feed: '#ChainlinkPriceFeed_1',
price: 0.0008,
priceBI: '800000000000000',
isPriceInUSD: false,
mergedPFVersion: 1,
roundId: 2,
token: '#Token_1',
},
Expand Down
2 changes: 2 additions & 0 deletions jsonnet/mocks/syncAdapter1.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
oracle: '#Oracle_1',
token: '#Token_1',
dieselToken: '#DieselToken_1',
mergedPFVersion: 1,
},
lastSync: 2,
version: 1,
Expand All @@ -80,6 +81,7 @@
details: {
oracle: '#Oracle_2',
token: '#Token_2',
mergedPFVersion: 1,
},
lastSync: 2,
version: 1,
Expand Down
4 changes: 4 additions & 0 deletions jsonnet/mocks/syncAdapterV2.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
details: {
oracle: '#Oracle_0',
token: '#Token_1',
mergedPFVersion: 1,
},
lastSync: 2,
version: 1,
Expand Down Expand Up @@ -94,6 +95,7 @@
details: {
oracle: '#Oracle_1',
token: '#Token_1',
mergedPFVersion: 2,
},
lastSync: 2,
version: 2,
Expand All @@ -105,6 +107,7 @@
details: {
oracle: '#Oracle_2',
token: '#Token_2',
mergedPFVersion: 2,
},
lastSync: 2,
version: 2,
Expand All @@ -115,6 +118,7 @@
details: {
oracle: '#Oracle_3',
token: '#Token_3',
mergedPFVersion: 2,
},
lastSync: 2,
version: 2,
Expand Down
Loading

0 comments on commit 2edccb3

Please sign in to comment.