-
Notifications
You must be signed in to change notification settings - Fork 50
/
Schema.hs
115 lines (97 loc) · 3.6 KB
/
Schema.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
{-# LANGUAGE DeriveAnyClass #-}
{-# LANGUAGE DeriveGeneric #-}
{-# LANGUAGE FlexibleInstances #-}
{-# LANGUAGE StandaloneDeriving #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeSynonymInstances #-}
-- |
module Common.Schema where
import Control.Monad.Identity
import Data.Aeson (FromJSON(..), ToJSON(..))
import Data.Int
import Data.Text
import Database.Beam
import Database.Beam.Backend.SQL.Types (SqlSerial)
data Db f = Db
{ _db_contracts :: f (TableEntity ContractT)
, _db_pooledTokens :: f (TableEntity PooledTokenT)
, _db_txFeeDataSet :: f (TableEntity TxFeeDataSetT)
, _db_pools :: f (TableEntity PoolT)
}
deriving (Generic, Database be)
data ContractT f = Contract
{ _contract_id :: Columnar f Text
, _contract_walletId :: Columnar f Int32
}
deriving (Generic)
data PooledTokenT f = PooledToken
{ _pooledToken_symbol :: Columnar f Text
, _pooledToken_name :: Columnar f Text
}
deriving (Generic)
-- Possible Improvements: There could be optional fields of information depending on
-- which smart contract action and parameters are used in order to improve regression
-- accuracy. For example staking amounts, swap amounts, etc.
data TxFeeDataSetT f = TxFeeDataSet
{ _txFeeDataSet_id :: Columnar f (SqlSerial Int32)
, _txFeeDataSet_txFee :: Columnar f Int32
, _txFeeDataSet_smartContractAction :: Columnar f Text
, _txFeeDataSet_estProcessingTime :: Columnar f Int32
, _txFeeDataSet_scriptSize :: Columnar f Int32
}
deriving (Generic)
-- Liquidity Pool
data PoolT f = Pool
{ _pool_tokenASymbol :: Columnar f Text
, _pool_tokenBSymbol :: Columnar f Text
, _pool_tokenAAmount :: Columnar f Int32
, _pool_tokenBAmount :: Columnar f Int32
, _pool_liquiditySymbol :: Columnar f Text
, _pool_liquidityAmount :: Columnar f Int32
}
deriving (Generic)
instance Beamable ContractT
instance Beamable PooledTokenT
instance Beamable TxFeeDataSetT
instance Beamable PoolT
instance Table ContractT where
newtype PrimaryKey ContractT f = ContractId { _contractId_id :: Columnar f Int32 }
deriving (Generic)
primaryKey = ContractId . _contract_walletId
instance Table PooledTokenT where
data PrimaryKey PooledTokenT f = PooledTokenId { _pooledTokenId_symbol :: Columnar f Text, _pooledTokenId_name :: Columnar f Text }
deriving (Generic)
primaryKey pl = PooledTokenId (_pooledToken_symbol pl) (_pooledToken_name pl)
instance Table TxFeeDataSetT where
data PrimaryKey TxFeeDataSetT f = TxFeeDataSetId { _txFeeDataId_id :: Columnar f (SqlSerial Int32)}
deriving (Generic)
primaryKey = TxFeeDataSetId . _txFeeDataSet_id
instance Table PoolT where
newtype PrimaryKey PoolT f = LiquiditySymbol { _liquiditySymbol_id :: Columnar f Text }
deriving (Generic)
primaryKey = LiquiditySymbol . _pool_liquiditySymbol
instance Beamable (PrimaryKey ContractT)
instance Beamable (PrimaryKey PooledTokenT)
instance Beamable (PrimaryKey TxFeeDataSetT)
instance Beamable (PrimaryKey PoolT)
type Contract = ContractT Identity
type PooledToken = PooledTokenT Identity
type TxFeeData = TxFeeDataSetT Identity
type LPool = PoolT Identity
deriving instance Show Contract
deriving instance Eq Contract
deriving instance FromJSON Contract
deriving instance ToJSON Contract
deriving instance Show PooledToken
deriving instance Eq PooledToken
deriving instance Ord PooledToken
deriving instance FromJSON PooledToken
deriving instance ToJSON PooledToken
deriving instance Show TxFeeData
deriving instance Eq TxFeeData
deriving instance FromJSON TxFeeData
deriving instance ToJSON TxFeeData
deriving instance Show LPool
deriving instance Eq LPool
deriving instance FromJSON LPool
deriving instance ToJSON LPool