diff --git a/client/chain/chain.go b/client/chain/chain.go index 425d6270..33f46a23 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -43,6 +43,7 @@ import ( evmtypes "github.com/InjectiveLabs/sdk-go/chain/evm/types" exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types" exchangev2types "github.com/InjectiveLabs/sdk-go/chain/exchange/types/v2" + insurancetypes "github.com/InjectiveLabs/sdk-go/chain/insurance/types" permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types" chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types" chainstreamv2types "github.com/InjectiveLabs/sdk-go/chain/stream/types/v2" @@ -468,6 +469,12 @@ type ChainClient interface { FetchEVMCode(ctx context.Context, address string) (*evmtypes.QueryCodeResponse, error) FetchEVMBaseFee(ctx context.Context) (*evmtypes.QueryBaseFeeResponse, error) + // Insurance module + FetchInsuranceFund(ctx context.Context, marketId string) (*insurancetypes.QueryInsuranceFundResponse, error) + FetchInsuranceFunds(ctx context.Context) (*insurancetypes.QueryInsuranceFundsResponse, error) + FetchPendingRedemptions(ctx context.Context, marketId string, address string) (*insurancetypes.QueryPendingRedemptionsResponse, error) + FetchEstimatedRedemptions(ctx context.Context, marketId string, address string) (*insurancetypes.QueryEstimatedRedemptionsResponse, error) + CurrentChainGasPrice() int64 SetGasPrice(gasPrice int64) @@ -522,6 +529,7 @@ type chainClient struct { txfeesQueryClient txfeestypes.QueryClient txClient txtypes.ServiceClient wasmQueryClient wasmtypes.QueryClient + insuranceQueryClient insurancetypes.QueryClient subaccountToNonce map[ethcommon.Hash]uint32 closed int64 @@ -628,6 +636,7 @@ func NewChainClient( txfeesQueryClient: txfeestypes.NewQueryClient(conn), txClient: txtypes.NewServiceClient(conn), wasmQueryClient: wasmtypes.NewQueryClient(conn), + insuranceQueryClient: insurancetypes.NewQueryClient(conn), subaccountToNonce: make(map[ethcommon.Hash]uint32), } @@ -3694,6 +3703,40 @@ func (c *chainClient) FetchEVMBaseFee(ctx context.Context) (*evmtypes.QueryBaseF return res, err } +// Insurance module + +func (c *chainClient) FetchInsuranceFund(ctx context.Context, marketId string) (*insurancetypes.QueryInsuranceFundResponse, error) { + req := &insurancetypes.QueryInsuranceFundRequest{ + MarketId: marketId, + } + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.insuranceQueryClient.InsuranceFund, req) + return res, err +} + +func (c *chainClient) FetchInsuranceFunds(ctx context.Context) (*insurancetypes.QueryInsuranceFundsResponse, error) { + req := &insurancetypes.QueryInsuranceFundsRequest{} + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.insuranceQueryClient.InsuranceFunds, req) + return res, err +} + +func (c *chainClient) FetchPendingRedemptions(ctx context.Context, marketId string, address string) (*insurancetypes.QueryPendingRedemptionsResponse, error) { + req := &insurancetypes.QueryPendingRedemptionsRequest{ + MarketId: marketId, + Address: address, + } + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.insuranceQueryClient.PendingRedemptions, req) + return res, err +} + +func (c *chainClient) FetchEstimatedRedemptions(ctx context.Context, marketId string, address string) (*insurancetypes.QueryEstimatedRedemptionsResponse, error) { + req := &insurancetypes.QueryEstimatedRedemptionsRequest{ + MarketId: marketId, + Address: address, + } + res, err := common.ExecuteCall(ctx, c.network.ChainCookieAssistant, c.insuranceQueryClient.EstimatedRedemptions, req) + return res, err +} + // SyncBroadcastMsg sends Tx to chain and waits until Tx is included in block. func (c *chainClient) SyncBroadcastMsg(msgs ...sdk.Msg) (*txtypes.BroadcastTxResponse, error) { req, res, err := c.BroadcastMsg(txtypes.BroadcastMode_BROADCAST_MODE_SYNC, msgs...) diff --git a/client/chain/chain_test_support.go b/client/chain/chain_test_support.go index 5c7405b3..62d26326 100644 --- a/client/chain/chain_test_support.go +++ b/client/chain/chain_test_support.go @@ -28,6 +28,7 @@ import ( evmtypes "github.com/InjectiveLabs/sdk-go/chain/evm/types" exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types" exchangev2types "github.com/InjectiveLabs/sdk-go/chain/exchange/types/v2" + insurancetypes "github.com/InjectiveLabs/sdk-go/chain/insurance/types" permissionstypes "github.com/InjectiveLabs/sdk-go/chain/permissions/types" chainstreamtypes "github.com/InjectiveLabs/sdk-go/chain/stream/types" chainstreamv2types "github.com/InjectiveLabs/sdk-go/chain/stream/types/v2" @@ -1209,6 +1210,24 @@ func (c *MockChainClient) FetchEVMBaseFee(ctx context.Context) (*evmtypes.QueryB return &evmtypes.QueryBaseFeeResponse{}, nil } +// Insurance module + +func (c *MockChainClient) FetchInsuranceFund(ctx context.Context, marketId string) (*insurancetypes.QueryInsuranceFundResponse, error) { + return &insurancetypes.QueryInsuranceFundResponse{}, nil +} + +func (c *MockChainClient) FetchInsuranceFunds(ctx context.Context) (*insurancetypes.QueryInsuranceFundsResponse, error) { + return &insurancetypes.QueryInsuranceFundsResponse{}, nil +} + +func (c *MockChainClient) FetchPendingRedemptions(ctx context.Context, marketId string, address string) (*insurancetypes.QueryPendingRedemptionsResponse, error) { + return &insurancetypes.QueryPendingRedemptionsResponse{}, nil +} + +func (c *MockChainClient) FetchEstimatedRedemptions(ctx context.Context, marketId string, address string) (*insurancetypes.QueryEstimatedRedemptionsResponse, error) { + return &insurancetypes.QueryEstimatedRedemptionsResponse{}, nil +} + func (c *MockChainClient) CurrentChainGasPrice() int64 { return int64(injectiveclient.DefaultGasPrice) } diff --git a/examples/chain/insurance/1_InsuranceFund/example.go b/examples/chain/insurance/1_InsuranceFund/example.go new file mode 100644 index 00000000..6fba8217 --- /dev/null +++ b/examples/chain/insurance/1_InsuranceFund/example.go @@ -0,0 +1,53 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint) + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + "", + nil, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + ctx := context.Background() + marketId := "0xfc615d2dfe6cc437c77c0d2b1721bba6947f821ce45c1aa4094dcdf333f46881" + + res, err := chainClient.FetchInsuranceFund(ctx, marketId) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/insurance/2_InsuranceFunds/example.go b/examples/chain/insurance/2_InsuranceFunds/example.go new file mode 100644 index 00000000..8a80c5ae --- /dev/null +++ b/examples/chain/insurance/2_InsuranceFunds/example.go @@ -0,0 +1,52 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint) + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + "", + nil, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + ctx := context.Background() + + res, err := chainClient.FetchInsuranceFunds(ctx) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/insurance/3_PendingRedemptions/example.go b/examples/chain/insurance/3_PendingRedemptions/example.go new file mode 100644 index 00000000..70019b6a --- /dev/null +++ b/examples/chain/insurance/3_PendingRedemptions/example.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "os" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint) + if err != nil { + panic(err) + } + + userAddress, _, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + "", + nil, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + ctx := context.Background() + marketId := "0xfc615d2dfe6cc437c77c0d2b1721bba6947f821ce45c1aa4094dcdf333f46881" + userAddressStr := userAddress.String() + + res, err := chainClient.FetchPendingRedemptions(ctx, marketId, userAddressStr) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +} diff --git a/examples/chain/insurance/4_EstimatedRedemptions/example.go b/examples/chain/insurance/4_EstimatedRedemptions/example.go new file mode 100644 index 00000000..f0ca0353 --- /dev/null +++ b/examples/chain/insurance/4_EstimatedRedemptions/example.go @@ -0,0 +1,69 @@ +package main + +import ( + "context" + "encoding/json" + "fmt" + "os" + + "github.com/InjectiveLabs/sdk-go/client" + chainclient "github.com/InjectiveLabs/sdk-go/client/chain" + "github.com/InjectiveLabs/sdk-go/client/common" + rpchttp "github.com/cometbft/cometbft/rpc/client/http" +) + +func main() { + network := common.LoadNetwork("testnet", "lb") + tmClient, err := rpchttp.New(network.TmEndpoint) + if err != nil { + panic(err) + } + + userAddress, _, err := chainclient.InitCosmosKeyring( + os.Getenv("HOME")+"/.injectived", + "injectived", + "file", + "inj-user", + "12345678", + "5d386fbdbf11f1141010f81a46b40f94887367562bd33b452bbaa6ce1cd1381e", // keyring will be used if pk not provided + false, + ) + + if err != nil { + panic(err) + } + + clientCtx, err := chainclient.NewClientContext( + network.ChainId, + "", + nil, + ) + + if err != nil { + panic(err) + } + + clientCtx = clientCtx.WithNodeURI(network.TmEndpoint).WithClient(tmClient) + + chainClient, err := chainclient.NewChainClient( + clientCtx, + network, + common.OptionGasPrices(client.DefaultGasPriceWithDenom), + ) + + if err != nil { + panic(err) + } + + ctx := context.Background() + marketId := "0xfc615d2dfe6cc437c77c0d2b1721bba6947f821ce45c1aa4094dcdf333f46881" + userAddressStr := userAddress.String() + + res, err := chainClient.FetchEstimatedRedemptions(ctx, marketId, userAddressStr) + if err != nil { + fmt.Println(err) + } + + str, _ := json.MarshalIndent(res, "", " ") + fmt.Print(string(str)) +}