Skip to content

Commit

Permalink
fix: use moralis and etherscan api
Browse files Browse the repository at this point in the history
  • Loading branch information
harsh-98 committed Sep 8, 2024
1 parent 9130911 commit 9f07e3c
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 61 deletions.
53 changes: 48 additions & 5 deletions pkg/eth_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
"time"

"github.com/Gearbox-protocol/sdk-go/core"
"github.com/Gearbox-protocol/sdk-go/log"
Expand Down Expand Up @@ -174,7 +175,7 @@ func (lf Node) GetLogsForTransfer(queryFrom, queryTill int64, hexAddrs []common.
return logs, nil
}

func GetEtherscanUrl(etherscanAPI string, chainId int64, ts int64) string {
func getEtherscanUrl(etherscanAPI string, chainId int64, ts int64) string {
url := "https://%s/api?module=block&action=getblocknobytime&timestamp=%d&closest=before&apikey=%s"
var suffix string
switch log.GetBaseNet(chainId) {
Expand All @@ -188,8 +189,14 @@ func GetEtherscanUrl(etherscanAPI string, chainId int64, ts int64) string {
url = fmt.Sprintf(url, suffix, ts, etherscanAPI)
return url
}
func GetBlockNumForTs(etherscanAPI string, chainId int64, ts int64) (int64, error) {
url := GetEtherscanUrl(etherscanAPI, chainId, ts)
// dont use outside sdk-go, chainid should be of main network, not testnet
func getEtherscanBlockNum(chainId int64, ts int64) (int64, error) {
etherscanAPI := utils.GetEnvOrDefault(fmt.Sprintf("%s_API_KEY", log.GetNetworkName(chainId)), "")
if etherscanAPI == "" {
log.Fatalf("%s_API_KEY can't be empty", log.GetNetworkName(chainId))
}

url := getEtherscanUrl(etherscanAPI, chainId, ts)
resp, err := http.Get(url)
if err != nil {
return 0, err
Expand All @@ -209,7 +216,13 @@ func GetBlockNumForTs(etherscanAPI string, chainId int64, ts int64) (int64, erro
}
return blockNum, nil
}
func MoralisGetBlockNumForTs(chainId int64, ts int64) (int64, error) {
// dont use outside sdk-go, chainid should be of main network, not testnet
func getMoralisBlockNum(chainId int64, ts int64) (int64, error) {
moralis := utils.GetEnvOrDefault("MORALIS_API_KEY", "")
if moralis == "" {
return 0, fmt.Errorf("MORALIS_API_KEY not set")
}
//
var chain string
switch log.GetBaseNet(chainId) {
case log.MAINNET:
Expand All @@ -226,7 +239,6 @@ func MoralisGetBlockNumForTs(chainId int64, ts int64) (int64, error) {
return 0, err
}
req.Header.Set("accept", "application/json")
moralis := utils.GetEnvOrDefault("MORALIS_API_KEY", "")
req.Header.Set("X-API-Key", moralis)

resp, err := http.DefaultClient.Do(req)
Expand All @@ -241,3 +253,34 @@ func MoralisGetBlockNumForTs(chainId int64, ts int64) (int64, error) {
utils.ReadJsonReaderAndSetInterface(resp.Body, msg)
return msg.Block, nil
}


func GetBlockNum(ts uint64, chainId int64) int64 {
network := log.GetBaseNet(chainId)
chainId = log.GetNetworkToChainId(network)
if ts == 0 {
log.Fatalf("ts can't be 0 for %d", chainId)
}
//
var errEtherScan error
{
for i := 0; i < 2; i++ {
blockNum, _err := getEtherscanBlockNum(chainId, int64(ts))
if _err == nil {
return blockNum
}
time.Sleep(5 * time.Second)
errEtherScan = _err
}
}
var moralisErr error
{
blockNum, err := getMoralisBlockNum(chainId, int64(ts))
if err == nil {
return blockNum
}
moralisErr = err
}
log.Warn( "for ts", ts, chainId, "blockNum is 0", errEtherScan, moralisErr)
return 0
}
5 changes: 5 additions & 0 deletions pkg/priceFetcher/1inch_oracle.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ type URLsAndResolve struct {
result map[string]*core.BigInt
}

// if resolve is set, set all 3 clients.
// else client for given network
func (d *URLsAndResolve) resolve(client core.ClientI) {
if d.Resolve {
d.mainclient = GetNetworkClient(d.Urls, log.MAINNET)
Expand Down Expand Up @@ -236,6 +238,9 @@ func (calc *OneInchOracle) GetCalls() []multicall.Multicall2Call {
// yearn dependent on curve and base
// curve dependent on base
// const and base doesn't dependent on any token.
// for mainnet fetch all 3network tokens.
// for optimism, mainnet+optimism
// for arbitrum, mainnet+optimism
func (calc OneInchOracle) GetPrices(results []multicall.Multicall2Result, blockNumber int64, ts uint64) map[string]*core.BigInt {
defer utils.Elapsed(fmt.Sprintf("GetPrices for block: %d", blockNumber))()
if len(results) != len(calc.generatedCalls) {
Expand Down
62 changes: 6 additions & 56 deletions pkg/priceFetcher/inch_client_for_net.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package priceFetcher

import (
"strings"
"time"

"github.com/Gearbox-protocol/sdk-go/artifacts/multicall"
"github.com/Gearbox-protocol/sdk-go/core"
Expand Down Expand Up @@ -83,32 +82,13 @@ func (calc OneInchOracle) arbForMainnet(mainnetTs uint64, prices map[string]*cor
if calc.extraurls.arbclient != nil {
defer utils.Elapsed("arbitrum price fetch")()
calls := calc.GetArbBaseCalls()
results := core.MakeMultiCall(calc.extraurls.arbclient, getArbBlockNum(mainnetTs), false, calls)
calc.processSeparateBaseResults(results, prices, calc.ArbBaseTokens)
}
}

func getArbBlockNum(ts uint64) int64 {
if ts != 0 {
etherscanAPI := utils.GetEnvOrDefault("ARBISCAN_API_KEY", "")
if etherscanAPI == "" {
log.Fatal("arbiscan_api_key can't be empty")
}
var err error
for i := 0; i < 2; i++ {
blockNum, _err := pkg.GetBlockNumForTs(etherscanAPI, 42161, int64(ts))
if _err == nil {
return blockNum
}
time.Sleep(5 * time.Second)
err = _err
arbblock := pkg.GetBlockNum(mainnetTs, 42161)
if arbblock == 0 {
return
}
log.Warn(err, "for ts", ts, pkg.GetEtherscanUrl(etherscanAPI, 42161, int64(ts)))
return 0
} else {
log.Fatal("ts can't be 0")
results := core.MakeMultiCall(calc.extraurls.arbclient, arbblock, false, calls)
calc.processSeparateBaseResults(results, prices, calc.ArbBaseTokens)
}
return 0
}

func (calc OneInchOracle) GetArbBaseCalls() (calls []multicall.Multicall2Call) {
Expand All @@ -135,7 +115,7 @@ func (calc OneInchOracle) optForMainnet(mainnetTs uint64, prices map[string]*cor
if calc.extraurls.optclient != nil {
defer utils.Elapsed("optimism price fetch")()
calls := calc.GetOptBaseCalls()
optblock := getOptBlockNum(mainnetTs)
optblock := pkg.GetBlockNum(mainnetTs, 10)
if optblock == 0 {
return
}
Expand All @@ -144,36 +124,6 @@ func (calc OneInchOracle) optForMainnet(mainnetTs uint64, prices map[string]*cor
}
}

func getOptBlockNum(ts uint64) int64 {
if ts != 0 {
blockNum, err := pkg.MoralisGetBlockNumForTs(10, int64(ts))
if err == nil {
return blockNum
}
}
//
if ts != 0 {
etherscanAPI := utils.GetEnvOrDefault("OPTIMISM_API_KEY", "")
if etherscanAPI == "" {
log.Fatal("optimism_api_key can't be empty")
}
var err error
for i := 0; i < 2; i++ {
blockNum, _err := pkg.GetBlockNumForTs(etherscanAPI, 10, int64(ts))
if _err == nil {
return blockNum
}
time.Sleep(5 * time.Second)
err = _err
}
log.Warn(err, "for ts", ts, pkg.GetEtherscanUrl(etherscanAPI, 10, int64(ts)))
return 0
} else {
log.Fatal("ts can't be 0")
}
return 0
}

func (calc OneInchOracle) GetOptBaseCalls() (calls []multicall.Multicall2Call) {
symToAddr := core.GetSymToAddrByChainId(10)
pfABI := core.GetAbi("1InchOracle")
Expand Down
2 changes: 2 additions & 0 deletions pkg/priceFetcher/token_store.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ func (mdl TokensStore) Exists(token common.Address) bool {
return ok
}

// filter already fetched tokens
func (mdl TokensStore) getNotPresentAddrs(addrs []common.Address) (ans []common.Address) {
for _, addr := range addrs {
if mdl.tokens[addr] == nil {
Expand All @@ -84,6 +85,7 @@ func (mdl TokensStore) getNotPresentAddrs(addrs []common.Address) (ans []common.
}
return
}
// if already has data on that token, doesn't fetch again
func (mdl TokensStore) GetDecimalsForList(addrs []common.Address) {
mdl.mu.Lock()
defer mdl.mu.Unlock()
Expand Down

0 comments on commit 9f07e3c

Please sign in to comment.