Skip to content

Commit

Permalink
set up some init code for preBalanceTxM
Browse files Browse the repository at this point in the history
  • Loading branch information
vvtran committed Jan 17, 2022
1 parent bb8611e commit 83a343f
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 11 deletions.
58 changes: 47 additions & 11 deletions src/PreBalanceTx.purs
@@ -1,11 +1,14 @@
module PreBalanceTx
( preBalanceTx
, preBalanceTxM
) where

import Prelude
import Control.Monad.Reader.Trans (runReaderT)
import Control.Monad.Trans.Class (lift)
import Data.Array as Array
import Data.BigInt (BigInt, fromInt)
import Data.Either (Either(..), hush, note)
import Data.Either (Either(..), fromRight, hush, isRight, note)
import Data.Foldable as Foldable
import Data.List ((:), List(..), partition)
import Data.Map as Map
Expand All @@ -14,24 +17,57 @@ import Data.Newtype (over, unwrap, wrap)
import Data.Set (Set)
import Data.Set as Set
import Data.Tuple.Nested ((/\), type (/\))
import Effect.Aff (Aff)
import Undefined (undefined)

import Ogmios (QueryConfig, QueryM(..))
import ProtocolParametersAlonzo (protocolParamUTxOCostPerWord)
import Types.Ada (adaSymbol, fromValue, getLovelace, lovelaceValueOf)
import Types.Transaction (Address, Credential(..), RequiredSigner, TransactionInput, TransactionOutput(..), TxBody(..), Utxo)
import Types.Transaction (Address, Credential(..), RequiredSigner, Transaction(..), TransactionInput, TransactionOutput(..), TxBody(..), Utxo, UtxoM)
import Types.Value (emptyValue, flattenValue, geq, getValue, isAdaOnly, isPos, isZero, minus, Value(..))

-- This module replicates functionality from
-- https://github.com/mlabs-haskell/mlabs-pab/blob/master/src/MLabsPAB/PreBalance.hs

preBalanceTx ::
Array (TransactionOutput /\ BigInt) ->
BigInt ->
Utxo ->
Address ->
Map.Map Address RequiredSigner ->
Array Address ->
TxBody ->
Either String TxBody
-- TO DO: convert utxosAt from Ogmios to Transaction space.
utxosAt :: Address -> QueryM UtxoM
utxosAt = undefined

preBalanceTxM
:: QueryConfig
-> Address
-> Map.Map Address RequiredSigner -- FIX ME: take from unbalanced tx?
-> Array Address -- FIX ME: take from unbalanced tx?
-> Transaction -- unbalanced transaction, FIX ME: do we need a newtype wrapper?
-> Aff (Either String Transaction)
preBalanceTxM qConfig ownAddr addReqSigners requiredAddrs unbalancedTx =
runReaderT
do
utxos <- unwrap <$> utxosAt ownAddr -- Do we want :: Either String UtxoM here?
let utxoIndex = utxos -- FIX ME: include newtype wrapper? UNWRAP
unwrapUnbalancedTx = unwrap unbalancedTx

pure
$ (wrap <<< unwrapUnbalancedTx { body = _ })
<$> preBalanceTx
[]
zero
utxoIndex
ownAddr
addReqSigners
requiredAddrs
unwrapUnbalancedTx.body
qConfig

preBalanceTx
:: Array (TransactionOutput /\ BigInt)
-> BigInt
-> Utxo
-> Address
-> Map.Map Address RequiredSigner
-> Array Address
-> TxBody
-> Either String TxBody
preBalanceTx minUtxos fees utxos ownAddr addReqSigners requiredAddrs tx =
addTxCollaterals utxos tx -- Take a single Ada only utxo collateral
>>= balanceTxIns utxos fees -- Add input fees for the Ada only collateral
Expand Down
27 changes: 27 additions & 0 deletions src/Types/Transaction.purs
Expand Up @@ -2,9 +2,11 @@ module Types.Transaction where

import Prelude
import Data.BigInt (BigInt)
import Data.Generic.Rep (class Generic)
import Data.Map (Map)
import Data.Maybe (Maybe)
import Data.Newtype (class Newtype)
import Data.Show.Generic (genericShow)
import Data.Tuple.Nested (type (/\))

import Types.Value (Value)
Expand All @@ -15,6 +17,7 @@ newtype Transaction = Transaction {
is_valid :: Boolean,
auxiliary_data :: Maybe AuxiliaryData
}
derive instance newtypeTransaction :: Newtype Transaction _

newtype TxBody = TxBody
{ inputs :: Array TransactionInput,
Expand Down Expand Up @@ -79,16 +82,28 @@ newtype TransactionInput = TransactionInput
index :: BigInt -- u32 TransactionIndex
}
derive instance eqTransactionInput :: Eq TransactionInput
derive instance genericTransactionInput :: Generic TransactionInput _
derive instance ordTransactionInput :: Ord TransactionInput

instance showTransactionInput :: Show TransactionInput where
show = genericShow

newtype TransactionOutput = TransactionOutput
{ address :: Address,
amount :: Value,
data_hash :: Maybe String -- DataHash>,
}
derive instance eqTransactionOutput :: Eq TransactionOutput
derive instance genericTransactionOutput :: Generic TransactionOutput _
derive instance newtypeTransactionOutput :: Newtype TransactionOutput _

instance showTransactionOutput :: Show TransactionOutput where
show = genericShow

newtype UtxoM = UtxoM Utxo
derive instance newtypeUtxoM :: Newtype UtxoM _
derive newtype instance showUtxoM :: Show UtxoM

type Utxo = Map TransactionInput TransactionOutput

newtype Coin = Coin BigInt
Expand All @@ -99,21 +114,33 @@ newtype Address = Address
{ "AddrType" :: BaseAddress
}
derive instance eqAddress :: Eq Address
derive instance genericAddress :: Generic Address _
derive instance ordAddress :: Ord Address
derive instance newtypeAddress :: Newtype Address _

instance showAddress :: Show Address where
show = genericShow

newtype BaseAddress = BaseAddress
{ network :: Int, -- u8,
stake :: Credential,
payment :: Credential
}
derive instance eqBaseAddress :: Eq BaseAddress
derive instance genericBaseAddress :: Generic BaseAddress _
derive instance ordBaseAddress :: Ord BaseAddress
derive instance newtypeBaseAddress :: Newtype BaseAddress _

instance showBaseAddress :: Show BaseAddress where
show = genericShow

newtype Credential = Credential String
derive instance eqCredential :: Eq Credential
derive instance genericCredential :: Generic Credential _
derive instance ordCredential :: Ord Credential

instance showCredential :: Show Credential where
show = genericShow
-- Below comes from Plutus API:
-- data Credential = PubKeyCredential String | ScriptCredential String

Expand Down

0 comments on commit 83a343f

Please sign in to comment.