From 24c51d4a18f7473bf6173c9584f0134ce6ff5bc7 Mon Sep 17 00:00:00 2001 From: nhannamsiu Date: Tue, 26 Apr 2022 19:58:41 +0700 Subject: [PATCH] Add exchange authz msg builder to chain client, update grant example --- client/chain/chain.go | 137 ++++++++++++++++++++++++-- examples/chain/19_MsgGrant/example.go | 18 +++- 2 files changed, 142 insertions(+), 13 deletions(-) diff --git a/client/chain/chain.go b/client/chain/chain.go index 25c54d09..fd7feb67 100644 --- a/client/chain/chain.go +++ b/client/chain/chain.go @@ -65,7 +65,15 @@ type ChainClient interface { GetAuthzGrants(ctx context.Context, req authztypes.QueryGrantsRequest) (*authztypes.QueryGrantsResponse, error) BuildGenericAuthz(granter string, grantee string, msgtype string, expireIn time.Time) *authztypes.MsgGrant - BuildTypedAuthz(granter string, grantee string, subaccountId string, authzmsg string, markets []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 @@ -751,21 +759,134 @@ func (c *chainClient) BuildGenericAuthz(granter string, grantee string, msgtype } } -func (c *chainClient) BuildTypedAuthz(granter string, grantee string, subaccountId string, authzmsg string, markets []string, expireIn time.Time) *authztypes.MsgGrant { - typedAuthz := exchangetypes.CreateSpotLimitOrderAuthz{ +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, - MarketIds: markets, + SpotMarkets: spotMarkets, + DerivativeMarkets: derivativeMarkets, } - typedAuthzBytes, _ := typedAuthz.Marshal() - typedAuthzAny := &codectypes.Any{ - TypeUrl: "/" + proto.MessageName(&typedAuthz), + typedAuthzBytes, _:= typedAuthz.Marshal() + typedAuthzAny := codectypes.Any{ + TypeUrl: string(BatchUpdateOrdersAuthz), Value: typedAuthzBytes, } return &authztypes.MsgGrant{ Granter: granter, Grantee: grantee, Grant: authztypes.Grant{ - Authorization: typedAuthzAny, + Authorization: &typedAuthzAny, Expiration: expireIn, }, } diff --git a/examples/chain/19_MsgGrant/example.go b/examples/chain/19_MsgGrant/example.go index 348bc91f..3541d492 100644 --- a/examples/chain/19_MsgGrant/example.go +++ b/examples/chain/19_MsgGrant/example.go @@ -54,19 +54,27 @@ func main() { granter := senderAddress.String() grantee := "inj1hkhdaj2a2clmq5jq6mspsggqs32vynpk228q3r" - expireIn := time.Now().AddDate(1, 0, 0) //years months days + expireIn := time.Now().AddDate(1, 0, 0) // years months days // GENERIC AUTHZ //msgtype := "/injective.exchange.v1beta1.MsgCreateSpotLimitOrder" //msg := chainClient.BuildGenericAuthz(granter, grantee, msgtype, expireIn) - //TYPED AUTHZ - authzmsg := "CreateSpotLimitOrderAuthz" - msg := chainClient.BuildTypedAuthz( + // TYPED AUTHZ + //msg := chainClient.BuildExchangeBatchUpdateOrdersAuthz( + // granter, + // grantee, + // chainClient.DefaultSubaccount(senderAddress).String(), + // []string{"0xe0dc13205fb8b23111d8555a6402681965223135d368eeeb964681f9ff12eb2a"}, + // []string{"0xe0dc13205fb8b23111d8555a6402681965223135d368eeeb964681f9ff12eb2a"}, + // expireIn, + //) + + msg := chainClient.BuildExchangeAuthz( granter, grantee, + chainclient.CreateSpotLimitOrderAuthz, chainClient.DefaultSubaccount(senderAddress).String(), - authzmsg, []string{"0xe0dc13205fb8b23111d8555a6402681965223135d368eeeb964681f9ff12eb2a"}, expireIn, )