Skip to content

Commit

Permalink
Merge pull request #39 from InjectiveLabs/f/move_authzfunc_to_chain
Browse files Browse the repository at this point in the history
chore: move authz functions to the chain client
  • Loading branch information
achilleas-kal authored Apr 27, 2022
2 parents 0ffa7b1 + 6d63e6d commit 8942cfc
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 43 deletions.
161 changes: 160 additions & 1 deletion client/chain/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"github.com/InjectiveLabs/sdk-go/client/common"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/client/tx"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cosmtypes "github.com/cosmos/cosmos-sdk/types"
sdk "github.com/cosmos/cosmos-sdk/types"
txtypes "github.com/cosmos/cosmos-sdk/types/tx"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
eth "github.com/ethereum/go-ethereum/common"
"github.com/gogo/protobuf/proto"
"github.com/pkg/errors"
"github.com/shopspring/decimal"
log "github.com/xlab/suplog"
Expand Down Expand Up @@ -62,6 +64,17 @@ type ChainClient interface {
GetBankBalances(ctx context.Context, address string) (*banktypes.QueryAllBalancesResponse, error)
GetAuthzGrants(ctx context.Context, req authztypes.QueryGrantsRequest) (*authztypes.QueryGrantsResponse, error)

BuildGenericAuthz(granter string, grantee string, msgtype string, expireIn time.Time) *authztypes.MsgGrant
BuildExchangeAuthz(granter string, grantee string, authzType ExchangeAuthz, subaccountId string, markets []string, expireIn time.Time) *authztypes.MsgGrant
BuildExchangeBatchUpdateOrdersAuthz(
granter string,
grantee string,
subaccountId string,
spotMarkets []string,
derivativeMarkets []string,
expireIn time.Time,
) *authztypes.MsgGrant

DefaultSubaccount(acc cosmtypes.AccAddress) eth.Hash

GetSubAccountNonce(ctx context.Context, subaccountId eth.Hash) (*exchangetypes.QuerySubaccountTradeNonceResponse, error)
Expand Down Expand Up @@ -89,7 +102,7 @@ type chainClient struct {
accNum uint64
accSeq uint64

sessionCookie string
sessionCookie string
sessionEnabled bool

txClient txtypes.ServiceClient
Expand Down Expand Up @@ -733,6 +746,152 @@ func (c *chainClient) GetAuthzGrants(ctx context.Context, req authztypes.QueryGr
return c.authzQueryClient.Grants(ctx, &req)
}

func (c *chainClient) BuildGenericAuthz(granter string, grantee string, msgtype string, expireIn time.Time) *authztypes.MsgGrant {
authz := authztypes.NewGenericAuthorization(msgtype)
authzAny := codectypes.UnsafePackAny(authz)
return &authztypes.MsgGrant{
Granter: granter,
Grantee: grantee,
Grant: authztypes.Grant{
Authorization: authzAny,
Expiration: expireIn,
},
}
}

type ExchangeAuthz string
var (
CreateSpotLimitOrderAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.CreateSpotLimitOrderAuthz{}))
CreateSpotMarketOrderAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.CreateSpotMarketOrderAuthz{}))
BatchCreateSpotLimitOrdersAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.BatchCreateSpotLimitOrdersAuthz{}))
CancelSpotOrderAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.CancelSpotOrderAuthz{}))
BatchCancelSpotOrdersAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.BatchCancelSpotOrdersAuthz{}))

CreateDerivativeLimitOrderAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.CreateDerivativeLimitOrderAuthz{}))
CreateDerivativeMarketOrderAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.CreateDerivativeMarketOrderAuthz{}))
BatchCreateDerivativeLimitOrdersAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.BatchCreateDerivativeLimitOrdersAuthz{}))
CancelDerivativeOrderAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.CancelDerivativeOrderAuthz{}))
BatchCancelDerivativeOrdersAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.BatchCancelDerivativeOrdersAuthz{}))

BatchUpdateOrdersAuthz = ExchangeAuthz("/" + proto.MessageName(&exchangetypes.BatchUpdateOrdersAuthz{}))
)

func (c *chainClient) BuildExchangeAuthz(granter string, grantee string, authzType ExchangeAuthz, subaccountId string, markets []string, expireIn time.Time) *authztypes.MsgGrant {
var typedAuthzAny codectypes.Any
var typedAuthzBytes []byte
switch authzType {
// spot msgs
case CreateSpotLimitOrderAuthz:
typedAuthz := &exchangetypes.CreateSpotLimitOrderAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case CreateSpotMarketOrderAuthz:
typedAuthz := &exchangetypes.CreateSpotMarketOrderAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case BatchCreateSpotLimitOrdersAuthz:
typedAuthz := &exchangetypes.BatchCreateSpotLimitOrdersAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case CancelSpotOrderAuthz:
typedAuthz := &exchangetypes.CancelSpotOrderAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case BatchCancelSpotOrdersAuthz:
typedAuthz := &exchangetypes.BatchCancelSpotOrdersAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
// derivative msgs
case CreateDerivativeLimitOrderAuthz:
typedAuthz := &exchangetypes.CreateDerivativeLimitOrderAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case CreateDerivativeMarketOrderAuthz:
typedAuthz := &exchangetypes.CreateDerivativeMarketOrderAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case BatchCreateDerivativeLimitOrdersAuthz:
typedAuthz := &exchangetypes.BatchCreateDerivativeLimitOrdersAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case CancelDerivativeOrderAuthz:
typedAuthz := &exchangetypes.CancelDerivativeOrderAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
case BatchCancelDerivativeOrdersAuthz:
typedAuthz := &exchangetypes.BatchCancelDerivativeOrdersAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ = typedAuthz.Marshal()
// common msgs
case BatchUpdateOrdersAuthz:
panic("please use BuildExchangeBatchUpdateOrdersAuthz for BatchUpdateOrdersAuthz")
default:
panic("unsupported exchange authz type")
}

typedAuthzAny = codectypes.Any{
TypeUrl: string(authzType),
Value: typedAuthzBytes,
}

return &authztypes.MsgGrant{
Granter: granter,
Grantee: grantee,
Grant: authztypes.Grant{
Authorization: &typedAuthzAny,
Expiration: expireIn,
},
}
}

func (c *chainClient) BuildExchangeBatchUpdateOrdersAuthz(
granter string,
grantee string,
subaccountId string,
spotMarkets []string,
derivativeMarkets []string,
expireIn time.Time,
) *authztypes.MsgGrant {
typedAuthz := &exchangetypes.BatchUpdateOrdersAuthz{
SubaccountId: subaccountId,
SpotMarkets: spotMarkets,
DerivativeMarkets: derivativeMarkets,
}
typedAuthzBytes, _:= typedAuthz.Marshal()
typedAuthzAny := codectypes.Any{
TypeUrl: string(BatchUpdateOrdersAuthz),
Value: typedAuthzBytes,
}
return &authztypes.MsgGrant{
Granter: granter,
Grantee: grantee,
Grant: authztypes.Grant{
Authorization: &typedAuthzAny,
Expiration: expireIn,
},
}
}

type DerivativeOrderData struct {
OrderType exchangetypes.OrderType
Price cosmtypes.Dec
Expand Down
51 changes: 9 additions & 42 deletions examples/chain/19_MsgGrant/example.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,15 @@ package main

import (
"fmt"
exchangetypes "github.com/InjectiveLabs/sdk-go/chain/exchange/types"
"github.com/gogo/protobuf/proto"
"os"
"time"

codectypes "github.com/cosmos/cosmos-sdk/codec/types"
authztypes "github.com/cosmos/cosmos-sdk/x/authz"
rpchttp "github.com/tendermint/tendermint/rpc/client/http"

chainclient "github.com/InjectiveLabs/sdk-go/client/chain"
"github.com/InjectiveLabs/sdk-go/client/common"
)

func buildGenericAuthz(granter string, grantee string) *authztypes.MsgGrant {
authz := authztypes.NewGenericAuthorization("/injective.exchange.v1beta1.MsgCreateSpotLimitOrder")
authzAny := codectypes.UnsafePackAny(authz)
expireIn := time.Now().AddDate(1, 0, 0)
return &authztypes.MsgGrant{
Granter: granter,
Grantee: grantee,
Grant: authztypes.Grant{
Authorization: authzAny,
Expiration: expireIn,
},
}
}

func buildTypedAuthz(granter string, grantee string, subaccountId string, markets []string) *authztypes.MsgGrant {
typedAuthz := exchangetypes.CreateSpotLimitOrderAuthz{
SubaccountId: subaccountId,
MarketIds: markets,
}
typedAuthzBytes, _ := typedAuthz.Marshal()
typedAuthzAny := &codectypes.Any{
TypeUrl: "/" + proto.MessageName(&typedAuthz),
Value: typedAuthzBytes,
}

expireIn := time.Now().AddDate(1, 0, 0)
return &authztypes.MsgGrant{
Granter: granter,
Grantee: grantee,
Grant: authztypes.Grant{
Authorization: typedAuthzAny,
Expiration: expireIn,
},
}
}

func main() {
// network := common.LoadNetwork("mainnet", "k8s")
network := common.LoadNetwork("testnet", "k8s")
Expand Down Expand Up @@ -94,13 +54,20 @@ func main() {

granter := senderAddress.String()
grantee := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r"
expireIn := time.Now().AddDate(1, 0, 0) // years months days

//GENERIC AUTHZ
//msgtype := "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder"
//msg := chainClient.BuildGenericAuthz(granter, grantee, msgtype, expireIn)

//msg := buildGenericAuthz(granter, grantee)
msg := buildTypedAuthz(
// TYPED AUTHZ
msg := chainClient.BuildExchangeAuthz(
granter,
grantee,
chainclient.CreateSpotLimitOrderAuthz,
chainClient.DefaultSubaccount(senderAddress).String(),
[]string{"0xe0dc13205fb8b23111d8555a6402681965223135d368eeeb964681f9ff12eb2a"},
expireIn,
)

err = chainClient.QueueBroadcastMsg(msg)
Expand Down

0 comments on commit 8942cfc

Please sign in to comment.