-
Notifications
You must be signed in to change notification settings - Fork 16
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
fix: hard-keeper-bot client updates #73
Open
lbayas
wants to merge
11
commits into
master
Choose a base branch
from
lbayas/hard-keeper-bot-updates
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
dd3317c
create grpc client to fetch data
luke-kava ae4305e
minor updates, need to add tests
luke-kava 243180d
add test cases
luke-kava 79344a2
cleanup
luke-kava e6f9d56
more tests
luke-kava c4fc752
update go version
luke-kava f973b7e
updates
luke-kava cf67cbe
update go module dependencies
luke-kava 88e4f1c
update deps
luke-kava e38550e
update deps
luke-kava 58ec22b
update deps
luke-kava File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module github.com/kava-labs/go-tools | ||
|
||
go 1.21.9 | ||
go 1.23 | ||
|
||
require ( | ||
github.com/cosmos/cosmos-sdk v0.44.5 | ||
|
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,134 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
"crypto/tls" | ||
"fmt" | ||
"net/url" | ||
"strconv" | ||
|
||
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice" | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
grpctypes "github.com/cosmos/cosmos-sdk/types/grpc" | ||
hardtypes "github.com/kava-labs/kava/x/hard/types" | ||
pricefeedtypes "github.com/kava-labs/kava/x/pricefeed/types" | ||
|
||
"google.golang.org/grpc" | ||
"google.golang.org/grpc/credentials" | ||
"google.golang.org/grpc/metadata" | ||
) | ||
|
||
type GrpcClient struct { | ||
Conn *grpc.ClientConn | ||
TmClient tmservice.ServiceClient | ||
HardClient hardtypes.QueryClient | ||
PricefeedClient pricefeedtypes.QueryClient | ||
} | ||
|
||
var _ LiquidationClient = (*GrpcClient)(nil) | ||
|
||
func ctxAtHeight(height int64) context.Context { | ||
heightStr := strconv.FormatInt(height, 10) | ||
return metadata.AppendToOutgoingContext(context.Background(), grpctypes.GRPCBlockHeightHeader, heightStr) | ||
} | ||
|
||
func NewGrpcClient(target string) (*GrpcClient, error) { | ||
grpcURL, err := url.Parse(target) | ||
if err != nil { | ||
return nil, fmt.Errorf("invalid URL: %w", err) | ||
} | ||
|
||
var dialOptions grpc.DialOption | ||
switch grpcURL.Scheme { | ||
case "http": | ||
dialOptions = grpc.WithInsecure() | ||
case "https": | ||
dialOptions = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{})) | ||
default: | ||
return nil, fmt.Errorf("unsupported scheme: %s", grpcURL.Scheme) | ||
} | ||
|
||
conn, err := grpc.Dial(grpcURL.Host, dialOptions) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to connect: %w", err) | ||
} | ||
|
||
return &GrpcClient{ | ||
Conn: conn, | ||
TmClient: tmservice.NewServiceClient(conn), | ||
HardClient: hardtypes.NewQueryClient(conn), | ||
PricefeedClient: pricefeedtypes.NewQueryClient(conn), | ||
}, nil | ||
} | ||
|
||
func (c *GrpcClient) GetInfo() (*InfoResponse, error) { | ||
latestBlock, err := c.TmClient.GetLatestBlock(context.Background(), &tmservice.GetLatestBlockRequest{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch latest block: %w", err) | ||
} | ||
|
||
return &InfoResponse{ | ||
ChainId: latestBlock.Block.Header.ChainID, | ||
LatestHeight: latestBlock.Block.Header.Height, | ||
}, nil | ||
} | ||
|
||
func (c *GrpcClient) GetPrices(height int64) (pricefeedtypes.CurrentPrices, error) { | ||
pricesRes, err := c.PricefeedClient.Prices(ctxAtHeight(height), &pricefeedtypes.QueryPricesRequest{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch prices: %w", err) | ||
} | ||
|
||
prices := make([]pricefeedtypes.CurrentPrice, len(pricesRes.Prices)) | ||
for i, response := range pricesRes.Prices { | ||
prices[i] = pricefeedtypes.CurrentPrice{ | ||
MarketID: response.MarketID, | ||
Price: response.Price, | ||
} | ||
} | ||
|
||
return prices, nil | ||
} | ||
|
||
func (c *GrpcClient) GetMarkets(height int64) (hardtypes.MoneyMarkets, error) { | ||
paramsRes, err := c.HardClient.Params(ctxAtHeight(height), &hardtypes.QueryParamsRequest{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch money markets: %w", err) | ||
} | ||
|
||
return paramsRes.Params.MoneyMarkets, nil | ||
} | ||
|
||
func (c *GrpcClient) GetBorrows(height int64) (hardtypes.Borrows, error) { | ||
borrowRes, err := c.HardClient.Borrows(ctxAtHeight(height), &hardtypes.QueryBorrowsRequest{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch borrows: %w", err) | ||
} | ||
|
||
borrows := make([]hardtypes.Borrow, len(borrowRes.Borrows)) | ||
for i, response := range borrowRes.Borrows { | ||
borrows[i] = hardtypes.Borrow{ | ||
Borrower: sdk.AccAddress(response.Borrower), | ||
Amount: response.Amount, | ||
} | ||
} | ||
|
||
return borrows, nil | ||
} | ||
|
||
func (c *GrpcClient) GetDeposits(height int64) (hardtypes.Deposits, error) { | ||
depositRes, err := c.HardClient.Deposits(ctxAtHeight(height), &hardtypes.QueryDepositsRequest{}) | ||
if err != nil { | ||
return nil, fmt.Errorf("failed to fetch deposits: %w", err) | ||
} | ||
|
||
deposits := make([]hardtypes.Deposit, len(depositRes.Deposits)) | ||
for i, response := range depositRes.Deposits { | ||
deposits[i] = hardtypes.Deposit{ | ||
Depositor: sdk.AccAddress(response.Depositor), | ||
Amount: response.Amount, | ||
} | ||
} | ||
|
||
return deposits, 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,115 @@ | ||
package main | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
var invalidHeight int64 = 100 | ||
var latestHeight int64 | ||
var grpcClient *GrpcClient | ||
|
||
func TestMain(m *testing.M) { | ||
grpcClient, _ = NewGrpcClient("https://grpc.kava.io:443") | ||
|
||
os.Exit(m.Run()) | ||
} | ||
|
||
func TestGrpcClientInvalidUrl(t *testing.T) { | ||
_, err := NewGrpcClient("invalid-url") | ||
require.Error(t, err) | ||
} | ||
|
||
func TestHardKeeperGetInfo(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
res, err := grpcClient.GetInfo() | ||
require.NoError(t, err) | ||
require.Greater(t, res.LatestHeight, int64(11000000)) | ||
require.Equal(t, "kava_2222-10", res.ChainId) | ||
latestHeight = res.LatestHeight | ||
} | ||
|
||
func TestHardKeeperGetPrices(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
res, err := grpcClient.GetPrices(latestHeight) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, res) | ||
require.Equal(t, len(res), 29) | ||
} | ||
|
||
func TestHardKeeperGetPricesInvalidHeight(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
_, err := grpcClient.GetPrices(invalidHeight) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestHardKeeperGetMarkets(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
res, err := grpcClient.GetMarkets(latestHeight) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, res) | ||
require.Equal(t, len(res), 16) | ||
} | ||
|
||
func TestHardKeeperGetMarketsInvalidHeight(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
_, err := grpcClient.GetMarkets(invalidHeight) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestHardKeeperGetBorrows(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
res, err := grpcClient.GetBorrows(latestHeight) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, res) | ||
require.Equal(t, len(res), 100) | ||
} | ||
|
||
func TestHardKeeperGetBorrowsInvalidHeight(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
_, err := grpcClient.GetBorrows(invalidHeight) | ||
require.Error(t, err) | ||
} | ||
|
||
func TestHardKeeperGetDeposits(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
res, err := grpcClient.GetDeposits(latestHeight) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, res) | ||
require.Equal(t, len(res), 100) | ||
} | ||
|
||
func TestHardKeeperGetDepositsInvalidHeight(t *testing.T) { | ||
if testing.Short() { | ||
t.Skip("skipping test in short mode") | ||
} | ||
|
||
_, err := grpcClient.GetDeposits(invalidHeight) | ||
require.Error(t, err) | ||
} |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as a team, we want to create fewer of these grpc client. we currently have many of them and they increase our maintenance overhead. overtime, they become slightly different copy-pastas of each other. can you update your code to use the canonical client from the kava repo?
https://github.com/Kava-Labs/kava/tree/master/client/grpc
i believe all of the methods included here already exist on that client. updating to use that also makes upgrading kava versions on these types of services much simpler 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried updating to use
kavaGrpc "github.com/kava-labs/kava/client/grpc"
but am getting a plethora of dependency issues as a result. Will continue to investigate whats going on here