Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

1693 update mayan endpoint #1703

Merged
merged 4 commits into from
Sep 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions jobs/jobs/protocols/repository/mayan.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func (d *MayanRestClient) GetActivity(ctx context.Context, from, to time.Time) (

func (d *MayanRestClient) GetStats(ctx context.Context) (Stats, error) {
decoratedLogger := d.logger
url := d.baseURL + "/v3/stats/wh/stats"
url := d.baseURL + "/v3/stats/overview"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
decoratedLogger.Error("failed creating http request for retrieving protocol stats", zap.Error(err))
Expand Down Expand Up @@ -157,14 +157,24 @@ func (d *MayanRestClient) GetStats(ctx context.Context) (Stats, error) {
decoratedLogger.Error("failed reading response body", zap.Error(err))
return Stats{}, errors.Wrapf(errors.WithStack(err), "failed reading response body from protocol stats. url:%s - status_code:%d", url, resp.StatusCode)
}
var stats Stats
err = json.Unmarshal(body, &stats)
var mayanResp mayanOverviewResponse
err = json.Unmarshal(body, &mayanResp)
if err != nil {
decoratedLogger.Error("failed reading response body", zap.Error(err), zap.String("response_body", string(body)))
return Stats{}, errors.Wrapf(errors.WithStack(err), "failed unmarshalling response body from protocol stats. url:%s - status_code:%d - response_body:%s", url, resp.StatusCode, string(body))
}

return stats, nil
return Stats{
TotalMessages: mayanResp.AllTime.TotalMessages,
Volume: float64(mayanResp.AllTime.Volume),
}, nil
}

type mayanOverviewResponse struct {
AllTime struct {
TotalMessages uint64 `json:"swaps"`
Volume uint64 `json:"volume"`
} `json:"allTime"`
}

func (d *MayanRestClient) ProtocolName() string {
Expand Down
98 changes: 89 additions & 9 deletions jobs/jobs/protocols/repository/mayan_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,11 @@ import (
"time"
)

const baseURL = "https://explorer-api.mayan.finance"

func Test_HttpRestClientActivity_FailRequestCreation(t *testing.T) {

a := repository.NewMayanRestClient("localhost", zap.NewNop(),
a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return nil, nil
}))
Expand All @@ -26,7 +28,7 @@ func Test_HttpRestClientActivity_FailRequestCreation(t *testing.T) {

func Test_HttpRestClientActivity_FailedRequestExecution(t *testing.T) {

a := repository.NewMayanRestClient("localhost", zap.NewNop(),
a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("mocked_http_client_do")
}))
Expand All @@ -37,7 +39,7 @@ func Test_HttpRestClientActivity_FailedRequestExecution(t *testing.T) {

func Test_HttpRestClientActivity_Status500(t *testing.T) {

a := repository.NewMayanRestClient("localhost", zap.NewNop(),
a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Expand All @@ -46,12 +48,12 @@ func Test_HttpRestClientActivity_Status500(t *testing.T) {
}))
_, err := a.GetActivity(context.Background(), time.Now(), time.Now())
assert.NotNil(t, err)
assert.Equal(t, "failed retrieving protocol activities from url:localhost/v3/stats/wh/activity - status_code:500 - response_body:response_body_test", err.Error())
assert.Equal(t, "failed retrieving protocol activities from url:https://explorer-api.mayan.finance/v3/stats/wh/activity - status_code:500 - response_body:response_body_test", err.Error())
}

func Test_HttpRestClientActivity_Status200_FailedReadBody(t *testing.T) {

a := repository.NewMayanRestClient("localhost", zap.NewNop(),
a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Expand All @@ -60,12 +62,12 @@ func Test_HttpRestClientActivity_Status200_FailedReadBody(t *testing.T) {
}))
_, err := a.GetActivity(context.Background(), time.Now(), time.Now())
assert.NotNil(t, err)
assert.Equal(t, "failed reading response body from protocol activities. url:localhost/v3/stats/wh/activity - status_code:200: mocked_fail_read", err.Error())
assert.Equal(t, "failed reading response body from protocol activities. url:https://explorer-api.mayan.finance/v3/stats/wh/activity - status_code:200: mocked_fail_read", err.Error())
}

func Test_HttpRestClientActivity_Status200_FailedParsing(t *testing.T) {

a := repository.NewMayanRestClient("localhost", zap.NewNop(),
a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Expand All @@ -74,12 +76,12 @@ func Test_HttpRestClientActivity_Status200_FailedParsing(t *testing.T) {
}))
_, err := a.GetActivity(context.Background(), time.Now(), time.Now())
assert.NotNil(t, err)
assert.Equal(t, "failed unmarshalling response body from protocol activities. url:localhost/v3/stats/wh/activity - status_code:200 - response_body:this should be a json: invalid character 'h' in literal true (expecting 'r')", err.Error())
assert.Equal(t, "failed unmarshalling response body from protocol activities. url:https://explorer-api.mayan.finance/v3/stats/wh/activity - status_code:200 - response_body:this should be a json: invalid character 'h' in literal true (expecting 'r')", err.Error())
}

func Test_HttpRestClientActivity_Status200_Succeed(t *testing.T) {

a := repository.NewMayanRestClient("localhost", zap.NewNop(),
a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Expand All @@ -98,3 +100,81 @@ func Test_HttpRestClientActivity_Status200_Succeed(t *testing.T) {
assert.Equal(t, uint64(88), resp.Activities[0].Txs)
assert.Equal(t, 648500.9762709612, resp.Activities[0].TotalUSD)
}

func Test_HttpRestClientStats_FailRequestCreation(t *testing.T) {

a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return nil, nil
}))
_, err := a.GetStats(nil) // passing ctx nil to force request creation error
assert.NotNil(t, err)
}

func Test_HttpRestClientStats_FailedRequestExecution(t *testing.T) {

a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return nil, errors.New("mocked_http_client_do")
}))
_, err := a.GetStats(context.Background())
assert.NotNil(t, err)
assert.Equal(t, "mocked_http_client_do", err.Error())
}

func Test_HttpRestClientStats_Status500(t *testing.T) {

a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusInternalServerError,
Body: io.NopCloser(bytes.NewBufferString("response_body_test")),
}, nil
}))
_, err := a.GetStats(context.Background())
assert.NotNil(t, err)
assert.Equal(t, "failed retrieving protocol stats from url:https://explorer-api.mayan.finance/v3/stats/overview - status_code:500 - response_body:response_body_test", err.Error())
}

func Test_HttpRestClientStats_Status200_FailedReadBody(t *testing.T) {

a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: &mocks.MockFailReadCloser{},
}, nil
}))
_, err := a.GetStats(context.Background())
assert.NotNil(t, err)
assert.Equal(t, "failed reading response body from protocol stats. url:https://explorer-api.mayan.finance/v3/stats/overview - status_code:200: mocked_fail_read", err.Error())
}

func Test_HttpRestClientStats_Status200_FailedParsing(t *testing.T) {

a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString("this should be a json")),
}, nil
}))
_, err := a.GetStats(context.Background())
assert.NotNil(t, err)
assert.Equal(t, "failed unmarshalling response body from protocol stats. url:https://explorer-api.mayan.finance/v3/stats/overview - status_code:200 - response_body:this should be a json: invalid character 'h' in literal true (expecting 'r')", err.Error())
}

func Test_HttpRestClientStats_Status200_Succeed(t *testing.T) {

a := repository.NewMayanRestClient(baseURL, zap.NewNop(),
mocks.MockHttpClient(func(req *http.Request) (*http.Response, error) {
return &http.Response{
StatusCode: http.StatusOK,
Body: io.NopCloser(bytes.NewBufferString("{\"allTime\":{\"swaps\":496486,\"volume\":1196504592},\"last24h\":{\"swaps\":1538,\"volume\":3456477}}")),
}, nil
}))
resp, err := a.GetStats(context.Background())
assert.Nil(t, err)
assert.Equal(t, uint64(496486), resp.TotalMessages)
assert.Equal(t, float64(1196504592), resp.Volume)
}
Loading