generated from KyberNetwork/go-project-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b441c43
commit 31131dc
Showing
13 changed files
with
514 additions
and
103 deletions.
There are no files selected for viewing
Binary file not shown.
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
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
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
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,116 @@ | ||
package mtm | ||
|
||
import ( | ||
"context" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
|
||
tokenStorage "github.com/KyberNetwork/tradelogs/v2/pkg/storage/dashboard/types" | ||
) | ||
|
||
type MtmClient struct { | ||
host string | ||
} | ||
|
||
func NewMtmClient(host string) *MtmClient { | ||
return &MtmClient{ | ||
host: host, | ||
} | ||
} | ||
|
||
func (m *MtmClient) GetListTokens(ctx context.Context) ([]tokenStorage.Token, error) { | ||
const path = "/tokens" | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.host+path, nil) | ||
if err != nil { | ||
return nil, fmt.Errorf("unable to create request: %w", err) | ||
} | ||
req.Header.Set("Accept", "application/json") | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return nil, fmt.Errorf("request failed: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
var tokens TokenResponse | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&tokens); err != nil { | ||
return nil, fmt.Errorf("failed to decode JSON: %w", err) | ||
} | ||
|
||
return tokens.Data, nil | ||
} | ||
|
||
func (m *MtmClient) GetHistoricalRate( | ||
ctx context.Context, | ||
base, quote, chainId string, | ||
timestamp int64, | ||
) (float64, error) { | ||
const path = "/v3/historical" | ||
queryParam := fmt.Sprintf("?base=%s"e=%s&time=%d&chainid=%s", | ||
base, | ||
quote, | ||
timestamp, | ||
chainId, | ||
) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.host+path+queryParam, nil) | ||
if err != nil { | ||
return 0, fmt.Errorf("unable to create request: %w", err) | ||
} | ||
req.Header.Set("Accept", "application/json") | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return 0, fmt.Errorf("request failed: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var rate RateResponse | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&rate); err != nil { | ||
return 0, fmt.Errorf("failed to decode JSON: %w", err) | ||
} | ||
|
||
return rate.Data.Price, nil | ||
} | ||
|
||
func (m *MtmClient) GetCurrentRate(ctx context.Context, base, quote, chainId string) (float64, error) { | ||
const path = "/v3/rate" | ||
queryParam := fmt.Sprintf("?base=%s"e=%s&chainid=%s", | ||
base, | ||
quote, | ||
chainId, | ||
) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, m.host+path+queryParam, nil) | ||
if err != nil { | ||
return 0, fmt.Errorf("unable to create request: %w", err) | ||
} | ||
req.Header.Set("Accept", "application/json") | ||
client := &http.Client{} | ||
resp, err := client.Do(req) | ||
if err != nil { | ||
return 0, fmt.Errorf("request failed: %w", err) | ||
} | ||
defer resp.Body.Close() | ||
|
||
var rate RateResponse | ||
|
||
if err := json.NewDecoder(resp.Body).Decode(&rate); err != nil { | ||
return 0, fmt.Errorf("failed to decode JSON: %w", err) | ||
} | ||
|
||
return rate.Data.Price, nil | ||
} | ||
|
||
type RateResponse struct { | ||
Success bool `json:"success"` | ||
Data struct { | ||
Price float64 `json:"price"` | ||
TimeUnix int64 `json:"timeUnix"` | ||
} `json:"data"` | ||
} | ||
|
||
type TokenResponse struct { | ||
Success bool `json:"success"` | ||
Data []tokenStorage.Token `json:"data"` | ||
} |
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,28 @@ | ||
package mtm | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/test-go/testify/require" | ||
) | ||
|
||
func TestNewMtmClient(t *testing.T) { | ||
// need mtm url | ||
t.Skip() | ||
MTM_URL := "" | ||
client := NewMtmClient(MTM_URL) | ||
|
||
rate, err := client.GetCurrentRate(context.Background(), "0x9be89d2a4cd102d8fecc6bf9da793be995c22541", "0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7", "1") | ||
require.NoError(t, err) | ||
fmt.Println("rate", rate) | ||
|
||
rate, err = client.GetHistoricalRate(context.Background(), "0x9be89d2a4cd102d8fecc6bf9da793be995c22541", "0x9702230a8ea53601f5cd2dc00fdbc13d4df4a8c7", "1", 1732608687) | ||
require.NoError(t, err) | ||
fmt.Println("historical rate", rate) | ||
|
||
tokens, err := client.GetListTokens(context.Background()) | ||
require.NoError(t, err) | ||
fmt.Println("tokens", tokens) | ||
} |
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
Oops, something went wrong.