From 728518445ab79cc10bd47ec4e49d8144e9ec55bf Mon Sep 17 00:00:00 2001 From: SunSpirit <48086732+sunspirit99@users.noreply.github.com> Date: Thu, 14 Nov 2024 15:40:50 +0700 Subject: [PATCH] adjust logic fetch round data specific for ktx on Mantle (#591) --- pkg/source/gmx/abis.go | 2 + pkg/source/gmx/abis/PriceFeedMantle.json | 1 + pkg/source/gmx/embed.go | 3 ++ pkg/source/gmx/price_feed_reader.go | 63 +++++++++++++++++------- pkg/source/gmx/vault_scanner.go | 10 ++-- 5 files changed, 58 insertions(+), 21 deletions(-) create mode 100644 pkg/source/gmx/abis/PriceFeedMantle.json diff --git a/pkg/source/gmx/abis.go b/pkg/source/gmx/abis.go index bc4967276..1a6cccdaa 100644 --- a/pkg/source/gmx/abis.go +++ b/pkg/source/gmx/abis.go @@ -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 @@ -27,6 +28,7 @@ func init() { {&fastPriceFeedV2ABI, fastPriceFeedV2Json}, {&pancakePairABI, pancakePairJson}, {&priceFeedABI, priceFeedJson}, + {&priceFeedMantleABI, priceFeedMantleJson}, {&vaultABI, vaultJson}, {&vaultPriceFeedABI, vaultPriceFeedJson}, {&erc20ABI, erc20Json}, diff --git a/pkg/source/gmx/abis/PriceFeedMantle.json b/pkg/source/gmx/abis/PriceFeedMantle.json new file mode 100644 index 000000000..60936791d --- /dev/null +++ b/pkg/source/gmx/abis/PriceFeedMantle.json @@ -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"}] \ No newline at end of file diff --git a/pkg/source/gmx/embed.go b/pkg/source/gmx/embed.go index 302ecd42f..c249f9340 100644 --- a/pkg/source/gmx/embed.go +++ b/pkg/source/gmx/embed.go @@ -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 diff --git a/pkg/source/gmx/price_feed_reader.go b/pkg/source/gmx/price_feed_reader.go index a2b684f4c..b34826228 100644 --- a/pkg/source/gmx/price_feed_reader.go +++ b/pkg/source/gmx/price_feed_reader.go @@ -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, @@ -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(ðrpc.Call{ - ABI: r.abi, - Target: address, - Method: r.param.PriceFeedMethodLatestRoundData, - Params: nil, - }, []interface{}{&latestRoundData}) + if r.param.UseLegacyMethod { + rpcRequest.AddCall(ðrpc.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(ðrpc.Call{ + ABI: r.param.ABI, + Target: address, + Method: "latestRound", + Params: nil, + }, []interface{}{&latestRound}) + rpcRequest.AddCall(ðrpc.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 } @@ -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(ðrpc.Call{ - ABI: r.abi, + ABI: r.param.ABI, Target: address, Method: priceFeedMethodGetRoundData, Params: []interface{}{roundID}, diff --git a/pkg/source/gmx/vault_scanner.go b/pkg/source/gmx/vault_scanner.go index 5d5353b6a..98f13371d 100644 --- a/pkg/source/gmx/vault_scanner.go +++ b/pkg/source/gmx/vault_scanner.go @@ -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{ @@ -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),