Skip to content

Commit 167698e

Browse files
committed
fix: if chainlink price feed has 2 events in same block, use last
1 parent d7909d1 commit 167698e

File tree

6 files changed

+81
-11
lines changed

6 files changed

+81
-11
lines changed

.github/workflows/go.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,4 @@ jobs:
2626
- run: go get github.com/google/go-jsonnet/cmd/jsonnet && ./generate-test.sh
2727
if: steps.cache-packages.outputs.cache-hit != 'true'
2828
- name: Run Test
29-
run: go test ./tests && go test ./services && go test ./ds && go test ./models/credit_manager
29+
run: go test ./tests && go test ./services && go test ./ds && go test ./models/...

ds/dc_wrapper/mainnet.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ func NewMainnetDC(client core.ClientI) *MainnetDC {
2929
}
3030
}
3131

32-
func (m *MainnetDC) setAddr(addr common.Address) {
32+
func (m *MainnetDC) SetAddr(addr common.Address) {
3333
dc, err := mainnet.NewDataCompressor(addr, m.client)
3434
log.CheckFatal(err)
3535
m.dc = dc
3636
}
3737

38-
func (repo *MainnetDC) addCreditManagerToFilter(cmAddr, cfAddr string) {
38+
func (repo *MainnetDC) AddCreditManagerToFilter(cmAddr, cfAddr string) {
3939
repo.creditManagerToFilter[common.HexToAddress(cmAddr)] = common.HexToAddress(cfAddr)
4040
}
4141

ds/dc_wrapper/wrapper.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,13 +200,13 @@ func (dcw *DataCompressorWrapper) setv1DC(discoveredAt int64) {
200200
dcw.mu.Lock()
201201
defer dcw.mu.Unlock()
202202
addr := dcw.discoveredAtToAddr[discoveredAt]
203-
dcw.v1DC.setAddr(addr)
203+
dcw.v1DC.SetAddr(addr)
204204
}
205205

206206
func (dcw *DataCompressorWrapper) AddCreditManagerToFilter(cmAddr, cfAddr string) {
207207
dcw.mu.Lock()
208208
defer dcw.mu.Unlock()
209-
dcw.v1DC.addCreditManagerToFilter(cmAddr, cfAddr)
209+
dcw.v1DC.AddCreditManagerToFilter(cmAddr, cfAddr)
210210
}
211211

212212
func (dcw *DataCompressorWrapper) setv2DC(discoveredAt int64) {

ds/repo_dummy.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ func (DummyRepo) GetExecuteParser() ExecuteParserI {
5858
// price feed/oracle funcs
5959
func (DummyRepo) DirectlyAddTokenOracle(tokenOracle *schemas.TokenOracle) {
6060
}
61-
func (DummyRepo) AddPriceFeed(pf *schemas.PriceFeed) {
61+
func (*DummyRepo) AddPriceFeed(pf *schemas.PriceFeed) {
6262
}
6363

6464
// token funcs

models/chainlink_price_feed/on_log.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,21 @@ func (mdl *ChainlinkPriceFeed) OnLog(txLog types.Log) {
1717
}
1818
func (mdl *ChainlinkPriceFeed) OnLogs(txLogs []types.Log) {
1919
var priceFeeds []*schemas.PriceFeed
20-
var prevBlock int64
21-
for _, txLog := range txLogs {
20+
for txLogInd, txLog := range txLogs {
2221
var priceFeed *schemas.PriceFeed
2322
blockNum := int64(txLog.BlockNumber)
2423
switch txLog.Topics[0] {
2524
case core.Topic("AnswerUpdated(int256,uint256,uint256)"):
26-
// there might be 2 AnswerUpdated events for same block
25+
// there might be 2 AnswerUpdated events for same block, use the last one
2726
// example
2827
// https://goerli.etherscan.io/tx/0x03308a0b6f024e6c35a92e7708ab5a72322f733d22427d51624862d82ca1983a
2928
// https://goerli.etherscan.io/tx/0x38e5551ae639d22554072ba1a53e026a0858c2cfedcedb83e5cc63bb1c8b8ea8
30-
if prevBlock == blockNum {
29+
// on mainnet
30+
// https://etherscan.io/tx/0xb3aaa84cac23a30ab20cbd254b2297840f23057faf1f05e7655304be6cffc19e#eventlog
31+
// https://etherscan.io/tx/0x3112f0a42f288ca56a2c8f8003355ad20e87e1f23c3ffa991633f6bb25eb8c58#eventlog
32+
if txLogInd+1 < len(txLogs) && int64(txLogs[txLogInd+1].BlockNumber) == blockNum {
3133
continue
3234
}
33-
prevBlock = blockNum
3435
//
3536
roundId, err := strconv.ParseInt(txLog.Topics[2].Hex()[50:], 16, 64)
3637
if err != nil {
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package chainlink_price_feed
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
7+
"github.com/Gearbox-protocol/sdk-go/core"
8+
"github.com/Gearbox-protocol/sdk-go/core/schemas"
9+
"github.com/Gearbox-protocol/sdk-go/utils"
10+
"github.com/Gearbox-protocol/third-eye/ds"
11+
"github.com/ethereum/go-ethereum/common"
12+
"github.com/ethereum/go-ethereum/core/types"
13+
)
14+
15+
type OnLogsChecker struct {
16+
ds.DummyRepo
17+
pfs []*schemas.PriceFeed
18+
}
19+
20+
func (x *OnLogsChecker) AddPriceFeed(pf *schemas.PriceFeed) {
21+
x.pfs = append(x.pfs, pf)
22+
}
23+
func TestOnLogs(t *testing.T) {
24+
validPf := &schemas.PriceFeed{
25+
Feed: utils.RandomAddr(),
26+
Token: utils.RandomAddr(),
27+
BlockNumber: 1,
28+
PriceBI: (*core.BigInt)(big.NewInt(222)),
29+
RoundId: 3,
30+
}
31+
repo := &OnLogsChecker{}
32+
obj := &ChainlinkPriceFeed{SyncAdapter: &ds.SyncAdapter{
33+
Repo: repo,
34+
SyncAdapterSchema: &schemas.SyncAdapterSchema{
35+
Contract: &schemas.Contract{
36+
Address: validPf.Feed,
37+
},
38+
Details: core.Json{"token": validPf.Token},
39+
},
40+
}}
41+
txLogs := []types.Log{
42+
{
43+
BlockNumber: 1,
44+
Index: 1,
45+
Topics: []common.Hash{
46+
core.Topic("AnswerUpdated(int256,uint256,uint256)"),
47+
common.BytesToHash([]byte{1}),
48+
common.BytesToHash([]byte{1}),
49+
},
50+
},
51+
{
52+
BlockNumber: uint64(validPf.BlockNumber),
53+
Index: 2,
54+
Topics: []common.Hash{
55+
core.Topic("AnswerUpdated(int256,uint256,uint256)"),
56+
common.BytesToHash(validPf.PriceBI.Convert().Bytes()),
57+
common.BytesToHash([]byte{byte(validPf.RoundId)}),
58+
},
59+
},
60+
}
61+
obj.OnLogs(txLogs)
62+
if len(repo.pfs) != 1 ||
63+
repo.pfs[0].BlockNumber != validPf.BlockNumber ||
64+
repo.pfs[0].Feed != validPf.Feed ||
65+
repo.pfs[0].RoundId != validPf.RoundId ||
66+
repo.pfs[0].PriceBI.Cmp(validPf.PriceBI) != 0 {
67+
t.Fatal(utils.ToJson(repo.pfs))
68+
}
69+
}

0 commit comments

Comments
 (0)