Skip to content

Commit

Permalink
adjust logic fetch round data specific for ktx on Mantle (#591)
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 authored Nov 14, 2024
1 parent 880513a commit 7285184
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 21 deletions.
2 changes: 2 additions & 0 deletions pkg/source/gmx/abis.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ var (
fastPriceFeedV2ABI abi.ABI
pancakePairABI abi.ABI
priceFeedABI abi.ABI
priceFeedMantleABI abi.ABI
vaultABI abi.ABI
vaultPriceFeedABI abi.ABI
erc20ABI abi.ABI
Expand All @@ -27,6 +28,7 @@ func init() {
{&fastPriceFeedV2ABI, fastPriceFeedV2Json},
{&pancakePairABI, pancakePairJson},
{&priceFeedABI, priceFeedJson},
{&priceFeedMantleABI, priceFeedMantleJson},
{&vaultABI, vaultJson},
{&vaultPriceFeedABI, vaultPriceFeedJson},
{&erc20ABI, erc20Json},
Expand Down
1 change: 1 addition & 0 deletions pkg/source/gmx/abis/PriceFeedMantle.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"inputs":[],"name":"aggregator","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"description","outputs":[{"internalType":"string","name":"","type":"string"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint80","name":"roundId","type":"uint80"}],"name":"getRoundData","outputs":[{"internalType":"uint80","name":"","type":"uint80"},{"internalType":"int256","name":"","type":"int256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint256","name":"","type":"uint256"},{"internalType":"uint80","name":"","type":"uint80"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"latestAnswer","outputs":[{"internalType":"int256","name":"","type":"int256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"latestRound","outputs":[{"internalType":"uint80","name":"","type":"uint80"}],"stateMutability":"view","type":"function"}]
3 changes: 3 additions & 0 deletions pkg/source/gmx/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ var pancakePairJson []byte
//go:embed abis/PriceFeed.json
var priceFeedJson []byte

//go:embed abis/PriceFeedMantle.json
var priceFeedMantleJson []byte

//go:embed abis/Vault.json
var vaultJson []byte

Expand Down
63 changes: 45 additions & 18 deletions pkg/source/gmx/price_feed_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,31 +5,31 @@ import (
"math/big"

"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
"github.com/KyberNetwork/logger"
"github.com/ethereum/go-ethereum/accounts/abi"
)

type Param struct {
PriceFeedMethodLatestRoundData string
UseLegacyMethod bool
ABI abi.ABI
}

type PriceFeedReader struct {
param Param
abi abi.ABI
ethrpcClient *ethrpc.Client
log logger.Logger
}

func NewPriceFeedReader(ethrpcClient *ethrpc.Client) *PriceFeedReader {
return NewPriceFeedReaderWithParam(ethrpcClient, Param{
PriceFeedMethodLatestRoundData: priceFeedMethodLatestRoundData,
ABI: priceFeedABI,
})
}

func NewPriceFeedReaderWithParam(ethrpcClient *ethrpc.Client, param Param) *PriceFeedReader {
return &PriceFeedReader{
param: param,
abi: priceFeedABI,
ethrpcClient: ethrpcClient,
log: logger.WithFields(logger.Fields{
"liquiditySource": DexTypeGmx,
Expand All @@ -55,24 +55,51 @@ func (r *PriceFeedReader) Read(ctx context.Context, address string, roundCount i
}

func (r *PriceFeedReader) getLatestRoundData(ctx context.Context, address string, priceFeed *PriceFeed) error {
var latestRoundData RoundData
var (
latestRoundData RoundData
latestRound = bignumber.ZeroBI
latestAnswer = bignumber.ZeroBI
)

rpcRequest := r.ethrpcClient.NewRequest().SetContext(ctx)

rpcRequest.AddCall(&ethrpc.Call{
ABI: r.abi,
Target: address,
Method: r.param.PriceFeedMethodLatestRoundData,
Params: nil,
}, []interface{}{&latestRoundData})
if r.param.UseLegacyMethod {
rpcRequest.AddCall(&ethrpc.Call{
ABI: r.param.ABI,
Target: address,
Method: priceFeedMethodLatestRoundData,
Params: nil,
}, []interface{}{&latestRoundData})

if _, err := rpcRequest.Call(); err != nil {
return err
}

priceFeed.RoundID = latestRoundData.RoundId
priceFeed.Answer = latestRoundData.Answer
priceFeed.Answers[latestRoundData.RoundId.String()] = latestRoundData.Answer
} else {
rpcRequest.AddCall(&ethrpc.Call{
ABI: r.param.ABI,
Target: address,
Method: "latestRound",
Params: nil,
}, []interface{}{&latestRound})
rpcRequest.AddCall(&ethrpc.Call{
ABI: r.param.ABI,
Target: address,
Method: "latestAnswer",
Params: nil,
}, []interface{}{&latestAnswer})

if _, err := rpcRequest.Call(); err != nil {
return err
}
if _, err := rpcRequest.Aggregate(); err != nil {
return err
}

priceFeed.RoundID = latestRoundData.RoundId
priceFeed.Answer = latestRoundData.Answer
priceFeed.Answers[latestRoundData.RoundId.String()] = latestRoundData.Answer
priceFeed.RoundID = latestRound
priceFeed.Answer = latestAnswer
priceFeed.Answers[latestRound.String()] = latestAnswer
}

return nil
}
Expand All @@ -88,7 +115,7 @@ func (r *PriceFeedReader) getHistoryRoundData(ctx context.Context, address strin
roundID := new(big.Int).Sub(priceFeed.RoundID, big.NewInt(int64(i)))

rpcRequest.AddCall(&ethrpc.Call{
ABI: r.abi,
ABI: r.param.ABI,
Target: address,
Method: priceFeedMethodGetRoundData,
Params: []interface{}{roundID},
Expand Down
10 changes: 7 additions & 3 deletions pkg/source/gmx/vault_scanner.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,12 @@ func NewVaultScanner(
config *Config,
ethrpcClient *ethrpc.Client,
) *VaultScanner {
method := priceFeedMethodLatestRoundData
abi := priceFeedABI
useLegacyMethod := true

if config.ChainID == valueobject.ChainIDMantle && config.DexID == string(valueobject.ExchangeKTX) {
method = "latestRound"
abi = priceFeedMantleABI
useLegacyMethod = false
}

return &VaultScanner{
Expand All @@ -41,7 +44,8 @@ func NewVaultScanner(
fastPriceFeedV1Reader: NewFastPriceFeedV1Reader(ethrpcClient),
fastPriceFeedV2Reader: NewFastPriceFeedV2Reader(ethrpcClient),
priceFeedReader: NewPriceFeedReaderWithParam(ethrpcClient, Param{
PriceFeedMethodLatestRoundData: method,
UseLegacyMethod: useLegacyMethod,
ABI: abi,
}),
usdgReader: NewUSDGReader(ethrpcClient),
chainlinkFlagsReader: NewChainlinkFlagsReader(ethrpcClient),
Expand Down

0 comments on commit 7285184

Please sign in to comment.