Skip to content

Commit

Permalink
fix test
Browse files Browse the repository at this point in the history
  • Loading branch information
cherry-yl-sh committed May 7, 2024
1 parent b837d61 commit 07b0cfa
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 11 deletions.
4 changes: 2 additions & 2 deletions common/global_const/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ func IsStableToken(token TokenType) bool {
const (
TokenTypeUSDT TokenType = "USDT"
TokenTypeUSDC TokenType = "USDC"
ETH TokenType = "ETH"
OP TokenType = "OP"
TokenTypeETH TokenType = "ETH"
TokenTypeOP TokenType = "OP"
)
12 changes: 7 additions & 5 deletions common/utils/price_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@ type Price struct {

func init() {
URLMap = make(map[global_const.TokenType]string)
URLMap[global_const.ETH] = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
URLMap[global_const.OP] = "https://api.coingecko.com/api/v3/simple/price?ids=optimism&vs_currencies=usd"
URLMap[global_const.TokenTypeETH] = "https://api.coingecko.com/api/v3/simple/price?ids=ethereum&vs_currencies=usd"
URLMap[global_const.TokenTypeOP] = "https://api.coingecko.com/api/v3/simple/price?ids=optimism&vs_currencies=usd"
}

func GetPriceUsd(tokenType global_const.TokenType) (float64, error) {

if global_const.IsStableToken(tokenType) {
return 1, nil
}
if tokenType == global_const.ETH {
return 3100, nil
}
//if tokenType == global_const.ETH {
// return 3100, nil
//}
tokenUrl, ok := URLMap[tokenType]
if !ok {
return 0, xerrors.Errorf("tokens type [%w] not found", tokenType)
Expand All @@ -53,6 +53,8 @@ func GetPriceUsd(tokenType global_const.TokenType) (float64, error) {
usdstr := strings.TrimRight(strarr[2], "}}")
return strconv.ParseFloat(usdstr, 64)
}

// GetToken Get The FromToken/ToToken Rate
func GetToken(fromToken global_const.TokenType, toToken global_const.TokenType) (float64, error) {
if toToken == global_const.TokenTypeUSDT {
return GetPriceUsd(fromToken)
Expand Down
34 changes: 30 additions & 4 deletions common/utils/price_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,39 @@ import (
"testing"
)

func TestGetPriceUsd(t *testing.T) {
price, _ := GetPriceUsd(global_const.OP)
fmt.Println(price)
func TestPriceUtilTest(t *testing.T) {
tests := []struct {
name string
test func(t *testing.T)
}{
{
"test_ETH_GetPriceUsd",
func(t *testing.T) {
testGetPriceUsd(t, global_const.TokenTypeETH)
},
},
{
"test_OP_GetPriceUsd",
func(t *testing.T) {
testGetPriceUsd(t, global_const.TokenTypeOP)
},
},
}
for _, tt := range tests {
t.Run(tt.name, tt.test)
}
}
func testGetPriceUsd(t *testing.T, tokenType global_const.TokenType) {
price, err := GetPriceUsd(tokenType)
if err != nil {
t.Fatal(err)

}
t.Logf("price:%v", price)
}

func TestGetToken(t *testing.T) {
price, _ := GetToken(global_const.ETH, global_const.OP)
price, _ := GetToken(global_const.TokenTypeETH, global_const.TokenTypeUSDT)
fmt.Println(price)
}
func TestDemo(t *testing.T) {
Expand Down

0 comments on commit 07b0cfa

Please sign in to comment.