Skip to content

Commit

Permalink
Merge pull request #111 from InjectiveLabs/chore/portfolio-examples
Browse files Browse the repository at this point in the history
chore: add AccountPortfolio examples
  • Loading branch information
achilleas-kal authored Feb 28, 2023
2 parents cb96f6f + fe98003 commit a54be06
Show file tree
Hide file tree
Showing 4 changed files with 155 additions and 0 deletions.
44 changes: 44 additions & 0 deletions client/exchange/exchange.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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)
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)
GetVersion(ctx context.Context, req metaPB.VersionRequest) (metaPB.VersionResponse, error)
Expand Down Expand Up @@ -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",
Expand All @@ -147,6 +153,7 @@ type exchangeClient struct {
insuranceClient insurancePB.InjectiveInsuranceRPCClient
spotExchangeClient spotExchangePB.InjectiveSpotExchangeRPCClient
derivativeExchangeClient derivativeExchangePB.InjectiveDerivativeExchangeRPCClient
portfolioExchangeClient portfolioExchangePB.InjectivePortfolioRPCClient

closed int64
}
Expand Down Expand Up @@ -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) 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{
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()
}
29 changes: 29 additions & 0 deletions examples/exchange/portfolio/1_AccountPortfolio/example.go
Original file line number Diff line number Diff line change
@@ -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() {
// 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 := "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt"
res, err := exchangeClient.GetAccountPortfolio(ctx, accountAddress)
if err != nil {
fmt.Println(err)
}

str, _ := json.MarshalIndent(res, "", " ")
fmt.Print(string(str))
}
Original file line number Diff line number Diff line change
@@ -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() {
// 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)
}

ctx := context.Background()

stream, err := exchangeClient.StreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "", "")
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))
}
}
}
Original file line number Diff line number Diff line change
@@ -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() {
// 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)
}

ctx := context.Background()

stream, err := exchangeClient.StreamAccountPortfolio(ctx, "inj1clw20s2uxeyxtam6f7m84vgae92s9eh7vygagt", "0xc7dca7c15c364865f77a4fb67ab11dc95502e6fe000000000000000000000001", "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))
}
}
}

0 comments on commit a54be06

Please sign in to comment.