Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include types.proto for modules for type reuse #221

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .weeder.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@
- Proto.Modules.Auth_Fields
- Proto.Modules.Bank
- Proto.Modules.Bank_Fields
- Proto.Modules.Types
- Proto.Modules.Types_Fields
- Proto.Types.Transaction
- Proto.Types.Transaction_Fields
- message:
Expand All @@ -89,6 +91,8 @@
- Proto.Modules.Auth_Fields
- Proto.Modules.Bank
- Proto.Modules.Bank_Fields
- Proto.Modules.Types
- Proto.Modules.Types_Fields
- Proto.Types.Transaction
- Proto.Types.Transaction_Fields
- message:
Expand Down
2 changes: 2 additions & 0 deletions hs-abci-sdk/package.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,8 @@ library:
- Proto.Modules.Auth_Fields
- Proto.Modules.Bank
- Proto.Modules.Bank_Fields
- Proto.Modules.Types
- Proto.Modules.Types_Fields
- Proto.Types.Transaction
- Proto.Types.Transaction_Fields

Expand Down
12 changes: 3 additions & 9 deletions hs-abci-sdk/protos/modules/auth.proto
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
syntax = "proto3";
package Auth;

message CoinId {
string id = 1;
}

message Amount {
uint64 amount = 1;
}
import "modules/types.proto";

message Coin {
CoinId id = 1;
Amount amount = 2;
modules.CoinId id = 1;
modules.Amount amount = 2;
}

message Account {
Expand Down
10 changes: 6 additions & 4 deletions hs-abci-sdk/protos/modules/bank.proto
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
syntax = "proto3";
package Bank;

import "modules/types.proto";

message Transfer {
bytes to = 1;
bytes from = 2;
string cid = 3;
uint64 amount = 4;
modules.CoinId cid = 3;
modules.Amount amount = 4;
}

message Burn {
bytes address = 1;
string cid = 2;
uint64 amount = 3;
modules.CoinId cid = 2;
modules.Amount amount = 3;
}
10 changes: 10 additions & 0 deletions hs-abci-sdk/protos/modules/types.proto
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
syntax = "proto3";
package modules;

message CoinId {
string id = 1;
}

message Amount {
uint64 amount = 1;
}
14 changes: 8 additions & 6 deletions hs-abci-sdk/src/Tendermint/SDK/Modules/Auth/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ import GHC.Generics (Generic)
import GHC.TypeLits (symbolVal)
import qualified Proto.Modules.Auth as A
import qualified Proto.Modules.Auth_Fields as A
import qualified Proto.Modules.Types as AT
import qualified Proto.Modules.Types_Fields as AT
import Tendermint.SDK.BaseApp (AppError (..), IsAppError (..),
IsKey (..), Queryable (..))
import Tendermint.SDK.Codec (HasCodec (..),
Expand Down Expand Up @@ -58,15 +60,15 @@ instance IsAppError AuthError where
newtype CoinId = CoinId { unCoinId :: Text } deriving (Eq, Show, Generic)

instance Wrapped CoinId where
type Unwrapped CoinId = A.CoinId
type Unwrapped CoinId = AT.CoinId

_Wrapped' = iso t f
where
t CoinId {..} =
P.defMessage
& A.id .~ unCoinId
& AT.id .~ unCoinId
f message = CoinId
{ unCoinId = message ^. A.id
{ unCoinId = message ^. AT.id
}

instance HasCodec CoinId where
Expand All @@ -90,15 +92,15 @@ newtype Amount = Amount { unAmount :: Word64 }
deriving (Eq, Show, Num, Generic, Ord, JSON.ToJSON, JSON.FromJSON)

instance Wrapped Amount where
type Unwrapped Amount = A.Amount
type Unwrapped Amount = AT.Amount

_Wrapped' = iso t f
where
t Amount {..} =
P.defMessage
& A.amount .~ unAmount
& AT.amount .~ unAmount
f message = Amount
{ unAmount = message ^. A.amount
{ unAmount = message ^. AT.amount
}

instance HasCodec Amount where
Expand Down
18 changes: 9 additions & 9 deletions hs-abci-sdk/src/Tendermint/SDK/Modules/Bank/Messages.hs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module Tendermint.SDK.Modules.Bank.Messages where

import Control.Lens (Wrapped (..), from, iso, view,
(&), (.~), (^.))
(&), (.~), (^.), _Unwrapped')
import Data.Bifunctor (bimap)
import qualified Data.ProtoLens as P
import Data.String.Conversions (cs)
Expand Down Expand Up @@ -32,13 +32,13 @@ instance Wrapped Transfer where
P.defMessage
& B.to .~ addressToBytes transferTo
& B.from .~ addressToBytes transferFrom
& B.cid .~ unCoinId transferCoinId
& B.amount .~ unAmount transferAmount
& B.cid .~ transferCoinId ^. _Wrapped'
& B.amount .~ transferAmount ^. _Wrapped'
f message = Transfer
{ transferTo = addressFromBytes $ message ^. B.to
, transferFrom = addressFromBytes $ message ^. B.from
, transferCoinId = CoinId $ message ^. B.cid
, transferAmount = Amount $ message ^. B.amount
, transferCoinId = message ^. B.cid . _Unwrapped'
, transferAmount = message ^. B.amount . _Unwrapped'
}

instance HasMessageType Transfer where
Expand Down Expand Up @@ -67,12 +67,12 @@ instance Wrapped Burn where
t Burn {..} =
P.defMessage
& B.address .~ addressToBytes burnAddress
& B.cid .~ unCoinId burnCoinId
& B.amount .~ unAmount burnAmount
& B.cid .~ burnCoinId ^. _Wrapped'
& B.amount .~ burnAmount ^. _Wrapped'
f message = Burn
{ burnAddress = addressFromBytes $ message ^. B.address
, burnCoinId = CoinId $ message ^. B.cid
, burnAmount = Amount $ message ^. B.amount
, burnCoinId = message ^. B.cid . _Unwrapped'
, burnAmount = message ^. B.amount . _Unwrapped'
}

instance HasMessageType Burn where
Expand Down