-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
get token price data from gnoswap api
- Loading branch information
Showing
2 changed files
with
176 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
package vwap | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
) | ||
|
||
const priceEndpoint = "http://dev.api.gnoswap.io/v1/tokens/prices" | ||
|
||
type TokenPrice struct { | ||
Path string `json:"path"` | ||
USD string `json:"usd"` | ||
PricesBefore PricesBefore `json:"pricesBefore"` | ||
MarketCap string `json:"marketCap"` | ||
LockedTokensUSD string `json:"lockedTokensUsd"` | ||
VolumeUSD24h string `json:"volumeUsd24h"` | ||
FeeUSD24h string `json:"feeUsd24h"` | ||
MostLiquidityPool string `json:"mostLiquidityPool"` | ||
Last7d []Last7d `json:"last7d"` | ||
} | ||
|
||
type PricesBefore struct { | ||
LatestPrice string `json:"latestPrice"` | ||
Price1h string `json:"price1h"` | ||
PriceToday string `json:"priceToday"` | ||
Price1d string `json:"price1d"` | ||
Price7d string `json:"price7d"` | ||
Price30d string `json:"price30d"` | ||
Price60d string `json:"price60d"` | ||
Price90d string `json:"price90d"` | ||
} | ||
|
||
type Last7d struct { | ||
Date string `json:"date"` | ||
Price string `json:"price"` | ||
} | ||
|
||
type APIResponse struct { | ||
Error json.RawMessage `json:"error"` | ||
Data []TokenPrice `json:"data"` | ||
} | ||
|
||
func fetchTokenPrices(endpoint string) ([]TokenPrice, error) { | ||
resp, err := http.Get(endpoint) | ||
if err != nil { | ||
return nil, err | ||
} | ||
defer resp.Body.Close() | ||
|
||
var apiResponse APIResponse | ||
err = json.NewDecoder(resp.Body).Decode(&apiResponse) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
return apiResponse.Data, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,119 @@ | ||
package vwap | ||
|
||
import ( | ||
"encoding/json" | ||
"net/http" | ||
"net/http/httptest" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestFetchTokenPrices(t *testing.T) { | ||
t.Parallel() | ||
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
tokenPricesResponse := APIResponse{ | ||
Data: []TokenPrice{ | ||
{ | ||
Path: "gno.land/r/demo/bar", | ||
USD: "30.087345", | ||
PricesBefore: PricesBefore{ | ||
LatestPrice: "30.087345", | ||
Price1h: "30.087345", | ||
PriceToday: "30.087345", | ||
Price1d: "30.087345", | ||
Price7d: "0.000000", | ||
Price30d: "0.000000", | ||
Price60d: "0.000000", | ||
Price90d: "0.000000", | ||
}, | ||
MarketCap: "15043681760.213797", | ||
LockedTokensUSD: "351004463.170473", | ||
VolumeUSD24h: "0.000000", | ||
FeeUSD24h: "0", | ||
MostLiquidityPool: "gno.land/r/demo/bar:gno.land/r/demo/baz:100", | ||
Last7d: []Last7d{ | ||
{ | ||
Date: "2024-05-09T08:00:00Z", | ||
Price: "30.087345468020313", | ||
}, | ||
{ | ||
Date: "2024-05-09T07:00:00Z", | ||
Price: "30.087345468020313", | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
json.NewEncoder(w).Encode(tokenPricesResponse) | ||
})) | ||
defer server.Close() | ||
|
||
apiEndpoint := server.URL | ||
|
||
prices, err := fetchTokenPrices(apiEndpoint) | ||
assert.NoError(t, err, "Failed to fetch token prices") | ||
|
||
expectedPrices := []TokenPrice{ | ||
{ | ||
Path: "gno.land/r/demo/bar", | ||
USD: "30.087345", | ||
PricesBefore: PricesBefore{ | ||
LatestPrice: "30.087345", | ||
Price1h: "30.087345", | ||
PriceToday: "30.087345", | ||
Price1d: "30.087345", | ||
Price7d: "0.000000", | ||
Price30d: "0.000000", | ||
Price60d: "0.000000", | ||
Price90d: "0.000000", | ||
}, | ||
MarketCap: "15043681760.213797", | ||
LockedTokensUSD: "351004463.170473", | ||
VolumeUSD24h: "0.000000", | ||
FeeUSD24h: "0", | ||
MostLiquidityPool: "gno.land/r/demo/bar:gno.land/r/demo/baz:100", | ||
Last7d: []Last7d{ | ||
{ | ||
Date: "2024-05-09T08:00:00Z", | ||
Price: "30.087345468020313", | ||
}, | ||
{ | ||
Date: "2024-05-09T07:00:00Z", | ||
Price: "30.087345468020313", | ||
}, | ||
}, | ||
}, | ||
} | ||
|
||
assert.Equal(t, expectedPrices, prices, "Fetched token prices do not match expected prices") | ||
} | ||
|
||
func TestFetchTokenPricesLive(t *testing.T) { | ||
t.Parallel() | ||
|
||
prices, err := fetchTokenPrices(priceEndpoint) | ||
if err != nil { | ||
t.Fatalf("Failed to fetch token prices: %v", err) | ||
} | ||
|
||
if len(prices) == 0 { | ||
t.Errorf("Fetched token prices are empty") | ||
} | ||
|
||
for _, price := range prices { | ||
if price.Path == "" { | ||
t.Errorf("Token path is empty") | ||
} | ||
if price.USD == "" { | ||
t.Errorf("Token USD price is empty") | ||
} | ||
if price.MarketCap == "" { | ||
t.Errorf("Token market cap is empty") | ||
} | ||
if price.VolumeUSD24h == "" { | ||
t.Errorf("Token 24h volume USD is empty") | ||
} | ||
} | ||
} |