From 5deab4dc58844af63217b8f36f3f2b8a2c84a9e3 Mon Sep 17 00:00:00 2001 From: Michele Meloni Date: Wed, 22 Feb 2023 16:39:43 +0100 Subject: [PATCH 1/3] chore: add AccountPortfolio examples --- client/exchange/exchange.go | 44 +++++++++++++++++++ .../portfolio/1_AccountPortfolio/example.go | 29 ++++++++++++ .../example.go | 41 +++++++++++++++++ .../example.go | 41 +++++++++++++++++ 4 files changed, 155 insertions(+) create mode 100644 examples/exchange/portfolio/1_AccountPortfolio/example.go create mode 100644 examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go create mode 100644 examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go diff --git a/client/exchange/exchange.go b/client/exchange/exchange.go index 6441fac1..9011f3a7 100644 --- a/client/exchange/exchange.go +++ b/client/exchange/exchange.go @@ -12,6 +12,7 @@ import ( insurancePB "github.com/InjectiveLabs/sdk-go/exchange/insurance_rpc/pb" metaPB "github.com/InjectiveLabs/sdk-go/exchange/meta_rpc/pb" oraclePB "github.com/InjectiveLabs/sdk-go/exchange/oracle_rpc/pb" + portfolioExchangePB "github.com/InjectiveLabs/sdk-go/exchange/portfolio_rpc/pb" spotExchangePB "github.com/InjectiveLabs/sdk-go/exchange/spot_exchange_rpc/pb" "google.golang.org/grpc/metadata" @@ -77,6 +78,10 @@ type ExchangeClient interface { GetSubaccountSpotTradesList(ctx context.Context, req spotExchangePB.SubaccountTradesListRequest) (spotExchangePB.SubaccountTradesListResponse, error) GetInsuranceFunds(ctx context.Context, req insurancePB.FundsRequest) (insurancePB.FundsResponse, error) GetRedemptions(ctx context.Context, req insurancePB.RedemptionsRequest) (insurancePB.RedemptionsResponse, error) + + GetAccountPortfolio(ctx context.Context, accountAddress string) (portfolioExchangePB.AccountPortfolioResponse, error) + GetStreamAccountPortfolio(ctx context.Context, accountAddress string, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) + StreamKeepalive(ctx context.Context) (metaPB.InjectiveMetaRPC_StreamKeepaliveClient, error) GetInfo(ctx context.Context, req metaPB.InfoRequest) (metaPB.InfoResponse, error) GetVersion(ctx context.Context, req metaPB.VersionRequest) (metaPB.VersionResponse, error) @@ -121,6 +126,7 @@ func NewExchangeClient(protoAddr string, options ...common.ClientOption) (Exchan insuranceClient: insurancePB.NewInjectiveInsuranceRPCClient(conn), spotExchangeClient: spotExchangePB.NewInjectiveSpotExchangeRPCClient(conn), derivativeExchangeClient: derivativeExchangePB.NewInjectiveDerivativeExchangeRPCClient(conn), + portfolioExchangeClient: portfolioExchangePB.NewInjectivePortfolioRPCClient(conn), logger: log.WithFields(log.Fields{ "module": "sdk-go", @@ -147,6 +153,7 @@ type exchangeClient struct { insuranceClient insurancePB.InjectiveInsuranceRPCClient spotExchangeClient spotExchangePB.InjectiveSpotExchangeRPCClient derivativeExchangeClient derivativeExchangePB.InjectiveDerivativeExchangeRPCClient + portfolioExchangeClient portfolioExchangePB.InjectivePortfolioRPCClient closed int64 } @@ -1103,6 +1110,43 @@ func (c *exchangeClient) StreamKeepalive(ctx context.Context) (metaPB.InjectiveM return stream, nil } +func (c *exchangeClient) GetAccountPortfolio(ctx context.Context, accountAddress string) (portfolioExchangePB.AccountPortfolioResponse, error) { + var header metadata.MD + ctx = c.getCookie(ctx) + res, err := c.portfolioExchangeClient.AccountPortfolio(ctx, &portfolioExchangePB.AccountPortfolioRequest{ + AccountAddress: accountAddress, + }, grpc.Header(&header)) + if err != nil { + fmt.Println(err) + return portfolioExchangePB.AccountPortfolioResponse{}, err + } + c.setCookie(header) + + return *res, nil +} + +func (c *exchangeClient) GetStreamAccountPortfolio(ctx context.Context, accountAddress string, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) { + var header metadata.MD + ctx = c.getCookie(ctx) + stream, err := c.portfolioExchangeClient.StreamAccountPortfolio(ctx, &portfolioExchangePB.StreamAccountPortfolioRequest{ + AccountAddress: accountAddress, + SubaccountId: subaccountId, + Type: balanceType, + }, grpc.Header(&header)) + if err != nil { + fmt.Println(err) + return nil, err + } + header, err = stream.Header() + if err != nil { + fmt.Println(err) + return nil, err + } + c.setCookie(header) + + return stream, nil +} + func (c *exchangeClient) Close() { c.conn.Close() } diff --git a/examples/exchange/portfolio/1_AccountPortfolio/example.go b/examples/exchange/portfolio/1_AccountPortfolio/example.go new file mode 100644 index 00000000..1263fa7c --- /dev/null +++ b/examples/exchange/portfolio/1_AccountPortfolio/example.go @@ -0,0 +1,29 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client/common" + exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" +) + +func main() { + //network := common.LoadNetwork("mainnet", "k8s") + network := common.LoadNetwork("devnet", "") + exchangeClient, err := exchangeclient.NewExchangeClient(network.ExchangeGrpcEndpoint, common.OptionTLSCert(network.ExchangeTlsCert)) + if err != nil { + panic(err) + } + + ctx := context.Background() + accountAddress := "inj1pjcw9hhx8kf462qtgu37p7l7shyqgpfr82r6em" + res, err := exchangeClient.GetAccountPortfolio(ctx, accountAddress) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go b/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go new file mode 100644 index 00000000..0a848603 --- /dev/null +++ b/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client/common" + exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" +) + +func main() { + //network := common.LoadNetwork("mainnet", "k8s") + network := common.LoadNetwork("devnet", "") + exchangeClient, err := exchangeclient.NewExchangeClient(network.ExchangeGrpcEndpoint, common.OptionTLSCert(network.ExchangeTlsCert)) + if err != nil { + fmt.Println(err) + } + + ctx := context.Background() + + stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1pjcw9hhx8kf462qtgu37p7l7shyqgpfr82r6em", "", "") + if err != nil { + fmt.Println(err) + } + + for { + select { + case <-ctx.Done(): + return + default: + res, err := stream.Recv() + if err != nil { + fmt.Println(err) + return + } + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + } + } +} diff --git a/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go b/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go new file mode 100644 index 00000000..a094c5da --- /dev/null +++ b/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go @@ -0,0 +1,41 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client/common" + exchangeclient "github.com/InjectiveLabs/sdk-go/client/exchange" +) + +func main() { + //network := common.LoadNetwork("mainnet", "k8s") + network := common.LoadNetwork("devnet", "") + exchangeClient, err := exchangeclient.NewExchangeClient(network.ExchangeGrpcEndpoint, common.OptionTLSCert(network.ExchangeTlsCert)) + if err != nil { + fmt.Println(err) + } + + ctx := context.Background() + + stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1pjcw9hhx8kf462qtgu37p7l7shyqgpfr82r6em", "0x0cb0e2dee63d935d280b4723e0fbfe85c8040523000000000000000000000000", "total_balances") + if err != nil { + fmt.Println(err) + } + + for { + select { + case <-ctx.Done(): + return + default: + res, err := stream.Recv() + if err != nil { + fmt.Println(err) + return + } + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) + } + } +} From 56d8faeae550a7c1790f329eb30e12df16c4f055 Mon Sep 17 00:00:00 2001 From: hanssun123 Date: Mon, 27 Feb 2023 18:16:20 -0400 Subject: [PATCH 2/3] Update portfolio examples to testnet --- examples/exchange/portfolio/1_AccountPortfolio/example.go | 6 +++--- .../2_StreamAccountPortfolioBankBalances/example.go | 6 +++--- .../3_StreamAccountPortfolioSubaccountBalances/example.go | 6 +++--- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/examples/exchange/portfolio/1_AccountPortfolio/example.go b/examples/exchange/portfolio/1_AccountPortfolio/example.go index 1263fa7c..c536f8b5 100644 --- a/examples/exchange/portfolio/1_AccountPortfolio/example.go +++ b/examples/exchange/portfolio/1_AccountPortfolio/example.go @@ -10,15 +10,15 @@ import ( ) func main() { - //network := common.LoadNetwork("mainnet", "k8s") - network := common.LoadNetwork("devnet", "") + // select network: local, testnet, mainnet + network := common.LoadNetwork("testnet", "k8s") exchangeClient, err := exchangeclient.NewExchangeClient(network.ExchangeGrpcEndpoint, common.OptionTLSCert(network.ExchangeTlsCert)) if err != nil { panic(err) } ctx := context.Background() - accountAddress := "inj1pjcw9hhx8kf462qtgu37p7l7shyqgpfr82r6em" + accountAddress := "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt" res, err := exchangeClient.GetAccountPortfolio(ctx, accountAddress) if err != nil { fmt.Println(err) diff --git a/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go b/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go index 0a848603..73a629b7 100644 --- a/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go +++ b/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go @@ -10,8 +10,8 @@ import ( ) func main() { - //network := common.LoadNetwork("mainnet", "k8s") - network := common.LoadNetwork("devnet", "") + // select network: local, testnet, mainnet + network := common.LoadNetwork("testnet", "k8s") exchangeClient, err := exchangeclient.NewExchangeClient(network.ExchangeGrpcEndpoint, common.OptionTLSCert(network.ExchangeTlsCert)) if err != nil { fmt.Println(err) @@ -19,7 +19,7 @@ func main() { ctx := context.Background() - stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1pjcw9hhx8kf462qtgu37p7l7shyqgpfr82r6em", "", "") + stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "", "") if err != nil { fmt.Println(err) } diff --git a/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go b/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go index a094c5da..f69d129a 100644 --- a/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go +++ b/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go @@ -10,8 +10,8 @@ import ( ) func main() { - //network := common.LoadNetwork("mainnet", "k8s") - network := common.LoadNetwork("devnet", "") + // select network: local, testnet, mainnet + network := common.LoadNetwork("testnet", "k8s") exchangeClient, err := exchangeclient.NewExchangeClient(network.ExchangeGrpcEndpoint, common.OptionTLSCert(network.ExchangeTlsCert)) if err != nil { fmt.Println(err) @@ -19,7 +19,7 @@ func main() { ctx := context.Background() - stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1pjcw9hhx8kf462qtgu37p7l7shyqgpfr82r6em", "0x0cb0e2dee63d935d280b4723e0fbfe85c8040523000000000000000000000000", "total_balances") + stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "0xc7dca7c15c364865f77a4fb67ab11dc95502e6fe000000000000000000000001", "total_balances") if err != nil { fmt.Println(err) } From fe9800315484e1b24d4ef17b1159e3ffaa8dcf2d Mon Sep 17 00:00:00 2001 From: hanssun123 Date: Mon, 27 Feb 2023 18:29:25 -0400 Subject: [PATCH 3/3] Fix: portfolio stream method naming convention --- client/exchange/exchange.go | 4 ++-- .../portfolio/2_StreamAccountPortfolioBankBalances/example.go | 2 +- .../3_StreamAccountPortfolioSubaccountBalances/example.go | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/client/exchange/exchange.go b/client/exchange/exchange.go index 9011f3a7..704e5246 100644 --- a/client/exchange/exchange.go +++ b/client/exchange/exchange.go @@ -80,7 +80,7 @@ type ExchangeClient interface { GetRedemptions(ctx context.Context, req insurancePB.RedemptionsRequest) (insurancePB.RedemptionsResponse, error) GetAccountPortfolio(ctx context.Context, accountAddress string) (portfolioExchangePB.AccountPortfolioResponse, error) - GetStreamAccountPortfolio(ctx context.Context, accountAddress string, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) + StreamAccountPortfolio(ctx context.Context, accountAddress string, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) StreamKeepalive(ctx context.Context) (metaPB.InjectiveMetaRPC_StreamKeepaliveClient, error) GetInfo(ctx context.Context, req metaPB.InfoRequest) (metaPB.InfoResponse, error) @@ -1125,7 +1125,7 @@ func (c *exchangeClient) GetAccountPortfolio(ctx context.Context, accountAddress return *res, nil } -func (c *exchangeClient) GetStreamAccountPortfolio(ctx context.Context, accountAddress string, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) { +func (c *exchangeClient) StreamAccountPortfolio(ctx context.Context, accountAddress string, subaccountId, balanceType string) (portfolioExchangePB.InjectivePortfolioRPC_StreamAccountPortfolioClient, error) { var header metadata.MD ctx = c.getCookie(ctx) stream, err := c.portfolioExchangeClient.StreamAccountPortfolio(ctx, &portfolioExchangePB.StreamAccountPortfolioRequest{ diff --git a/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go b/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go index 73a629b7..78edce1c 100644 --- a/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go +++ b/examples/exchange/portfolio/2_StreamAccountPortfolioBankBalances/example.go @@ -19,7 +19,7 @@ func main() { ctx := context.Background() - stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "", "") + stream, err := exchangeClient.StreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "", "") if err != nil { fmt.Println(err) } diff --git a/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go b/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go index f69d129a..29b9a47c 100644 --- a/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go +++ b/examples/exchange/portfolio/3_StreamAccountPortfolioSubaccountBalances/example.go @@ -19,7 +19,7 @@ func main() { ctx := context.Background() - stream, err := exchangeClient.GetStreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "0xc7dca7c15c364865f77a4fb67ab11dc95502e6fe000000000000000000000001", "total_balances") + stream, err := exchangeClient.StreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "0xc7dca7c15c364865f77a4fb67ab11dc95502e6fe000000000000000000000001", "total_balances") if err != nil { fmt.Println(err) }