Skip to content

Commit

Permalink
Merge pull request #29 from Plex-Engineer/api-fixes
Browse files Browse the repository at this point in the history
Api fixes
  • Loading branch information
tkkwon1998 authored Dec 5, 2023
2 parents 41e1be4 + ba8e234 commit 494434e
Show file tree
Hide file tree
Showing 22 changed files with 498 additions and 110 deletions.
18 changes: 10 additions & 8 deletions .env_example
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
CANTO_MAINNET_RPC_URL =
CANTO_BACKUP_RPC_URL =
PORT = :3000
DB_HOST = localhost
DB_PORT = 6379
CANTO_MAINNET_GRPC_URL =
MULTICALL_ADDRESS =
QUERY_INTERVAL = 3
CANTO_MAINNET_RPC_URL=""
CANTO_MAINNET_GRPC_URL =""
DB_HOST="redis"
DB_PORT=6379
PORT=":3000"
QUERY_INTERVAL = 3
MULTICALL_ADDRESS=""
TESTNET=false
REDIS_HOST_PASSWORD=""
CANTO_BACKUP_RPC_URLS="...,...,..."
59 changes: 46 additions & 13 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"fmt"
"os"
"strconv"
"strings"

"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/ethclient"
Expand All @@ -22,6 +23,7 @@ const (
CSRMap = "CSR_MAP"
AllProposals = "ALL_PROPOSALS"
ProposalMap = "PROPOSAL_MAP"
AllProposalsHttp = "ALL_PROPOSALS_HTTP"
Pairs = "PAIRS"
ProcessedPairs = "PROCESSED_PAIRS"
ProcessedPairsMap = "PROCESSED_PAIRS_MAP"
Expand All @@ -38,6 +40,7 @@ var (
MulticallAddress common.Address
QueryInterval uint
FPIConfig TokensInfo
BackupRpcIndex int // index of backup rpc url
)

/*
Expand All @@ -46,18 +49,18 @@ var (
* @return: none
* @desc: initialize config variables (acts as a constructor)
*/
func NewConfig(fpiJsonFile string, contractsJsonFile string) {

func NewConfig() {
err := godotenv.Load(".env")
if err != nil {
fmt.Println("Error loading .env file")
}
// Initialize redis client
dbHost := os.Getenv("DB_HOST")
dbPort := os.Getenv("DB_PORT")
dbPassword := os.Getenv("REDIS_HOST_PASSWORD")
RDB = redis.NewClient(&redis.Options{
Addr: fmt.Sprintf("%s:%s", dbHost, dbPort),
Password: "",
Password: dbPassword,
DB: 0,
})

Expand All @@ -75,12 +78,27 @@ func NewConfig(fpiJsonFile string, contractsJsonFile string) {
if err != nil {
log.Fatal().Msgf("Error initializing grpc client: %v", err)
}

// is testnet
isTestnet := os.Getenv("TESTNET")
var fpiFile string
var contractsFile string
if isTestnet == "true" {
fpiFile = "./config/jsons/fpi_testnet.json"
contractsFile = "./config/jsons/contracts_testnet.json"
} else {
fpiFile = "./config/jsons/fpi_mainnet.json"
contractsFile = "./config/jsons/contracts.json"
}
// get tokens data from tokens.json
FPIConfig, err = getFPIFromJson(fpiJsonFile)
FPIConfig, err = getFPIFromJson(fpiFile)
if err != nil {
log.Fatal().Msgf("Error getting tokens data from json: %v", err)
}
// get general contracts from contracts.json
generalCalls, err := getContractsFromJson(contractsFile)
if err != nil {
log.Fatal().Msgf("Error getting general contracts from json: %v", err)
}

// set multicall address
mcAddress := os.Getenv("MULTICALL_ADDRESS")
Expand All @@ -93,21 +111,36 @@ func NewConfig(fpiJsonFile string, contractsJsonFile string) {
}
QueryInterval = uint(queryInterval)

// get general contracts from contracts.json
generalCalls, err := getContractsFromJson(contractsJsonFile)
if err != nil {
log.Fatal().Msgf("Error getting general contracts from json: %v", err)
}

// get FPI contracts from tokens.json
fpiCalls := getAllFPI()
calls := append(fpiCalls, generalCalls...)
ContractCalls = calls

// Backup RPC Index starts at -1 since we increment it before using it
BackupRpcIndex = -1

}

func SetBackupRPC() {
// Initialize eth client using backup rpc
rpcUrl := os.Getenv("CANTO_BACKUP_RPC_URL")
// get backup rpc urls from env
rpcUrls := os.Getenv("CANTO_BACKUP_RPC_URLS")
// split rpc urls into array
rpcUrlsArr := strings.Split(rpcUrls, ",")
// get length of rpc urls array
rpcUrlsArrLen := len(rpcUrlsArr)
// increment backup rpc index
BackupRpcIndex++
// check if backup rpc index is greater than or equal to length of rpc urls array
if BackupRpcIndex >= rpcUrlsArrLen {
log.Log().Msg("Used all backup rpc urls, resetting index to 0")
// reset backup rpc index
BackupRpcIndex = 0
}
// get backup rpc url
rpcUrl := rpcUrlsArr[BackupRpcIndex]
// log
log.Log().Msgf("Using backup rpc url: %s", rpcUrl)
// initialize eth client using backup rpc
ethclient, err := ethclient.Dial(rpcUrl)
if err != nil {
log.Fatal().Msgf("Error initializing eth client: %v", err)
Expand Down
40 changes: 33 additions & 7 deletions config/fpi.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ func getCTokenContractCalls() []Contract {
},
})

// getUnderlyingPrice data of ctoken from router contract
// getUnderlyingPrice data of ctoken from price oracle contract
calls = append(calls, Contract{
Name: token.Symbol + "pricefeed",
Address: FPIConfig.Router,
Address: FPIConfig.PriceOracle,
Keys: []string{
"cTokens:" + token.Address + ":underlyingPrice",
},
Expand Down Expand Up @@ -88,6 +88,21 @@ func getCTokenContractCalls() []Contract {
{token.Address},
},
})

// get total supply of underlying too, will help with calculating liquidity
calls = append(calls, Contract{
Name: token.Symbol + "underlyingSupply",
Address: token.Underlying,
Keys: []string{
"cTokens:" + token.Address + ":underlyingSupply",
},
Methods: []string{
"totalSupply()(uint256)",
},
Args: [][]interface{}{
{},
},
})
}

return calls
Expand Down Expand Up @@ -123,25 +138,36 @@ func getPairsContractsCalls() []Contract {
{},
},
})

// get reserves, underlying prices of tokenA, tokenB and Lp from router contract
// get reserves
calls = append(calls, Contract{
Name: pair.Symbol + "pricefeed",
Name: pair.Symbol + "reserves",
Address: FPIConfig.Router,
Keys: []string{
"lpPairs:" + pair.Address + ":reserves",
},
Methods: []string{
"getReserves(address,address,bool)(uint256, uint256)",
},
Args: [][]interface{}{
{pair.TokenA, pair.TokenB, pair.Stable},
},
})

// get underlying prices of tokenA, tokenB and Lp from price oracle contract
calls = append(calls, Contract{
Name: pair.Symbol + "pricefeed",
Address: FPIConfig.PriceOracle,
Keys: []string{
"lpPairs:" + pair.Address + ":underlyingPriceTokenA",
"lpPairs:" + pair.Address + ":underlyingPriceTokenB",
"lpPairs:" + pair.Address + ":underlyingPriceLp",
},
Methods: []string{
"getReserves(address,address,bool)(uint256, uint256)",
"getUnderlyingPrice(address)(uint256)",
"getUnderlyingPrice(address)(uint256)",
"getUnderlyingPrice(address)(uint256)",
},
Args: [][]interface{}{
{pair.TokenA, pair.TokenB, pair.Stable},
{GetCTokenAddress(pair.TokenA)},
{GetCTokenAddress(pair.TokenB)},
{GetCTokenAddress(pair.Address)},
Expand Down
54 changes: 54 additions & 0 deletions config/jsons/contracts_testnet.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[
{
"Name": "factory",
"Address": "0x760a17e00173339907505B38F95755d28810570C",
"Methods": [
"allPairsLength()(uint256)"
],
"Args": [
[]
]
},
{
"Name": "pricefeed",
"Address": "0x4F04048721A9D893839f9E7d88FA211a629cFB22",
"Methods": [
"getUnderlyingPrice(address)(uint256)"
],
"Args": [
[
"0x477eaF5DECf6299EE937954084f0d53EFc57346F"
]
]
},
{
"Name": "comptroller",
"Address": "0x9514c07bC6e80B652e4264E64f589C59065C231f",
"Methods": [
"markets(address)(bool, uint256, bool)",
"compSupplySpeeds(address)(uint256)",
"borrowCaps(address)(uint256)"
],
"Args": [
[
"0x477eaF5DECf6299EE937954084f0d53EFc57346F"
],
[
"0x477eaF5DECf6299EE937954084f0d53EFc57346F"
],
[
"0x477eaF5DECf6299EE937954084f0d53EFc57346F"
]
]
},
{
"Name": "factorydetails",
"Address": "0x760a17e00173339907505B38F95755d28810570C",
"Methods": [
"admin()(address)"
],
"Args": [
[]
]
}
]
Loading

0 comments on commit 494434e

Please sign in to comment.