Skip to content

Commit 73f8070

Browse files
committed
balancing:
- calulatoing ExBudget using Cardano.Api tools - no need to set forced buget anymore - more work still required to assign exact budget to each input - sort of "max budget" that fits any input in Tx is used now
1 parent 0e3a56c commit 73f8070

File tree

21 files changed

+1052
-153
lines changed

21 files changed

+1052
-153
lines changed

bot-plutus-interface.cabal

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,22 +76,28 @@ library
7676
import: common-lang
7777
exposed-modules:
7878
BotPlutusInterface
79+
BotPlutusInterface.Balance
80+
BotPlutusInterface.BodyBuilder
7981
BotPlutusInterface.CardanoCLI
8082
BotPlutusInterface.ChainIndex
8183
BotPlutusInterface.Contract
8284
BotPlutusInterface.Effects
85+
BotPlutusInterface.Estimate
8386
BotPlutusInterface.Files
84-
BotPlutusInterface.Balance
87+
BotPlutusInterface.QueryNode
88+
BotPlutusInterface.Server
8589
BotPlutusInterface.Types
8690
BotPlutusInterface.UtxoParser
87-
BotPlutusInterface.Server
91+
8892
build-depends:
8993
, aeson ^>=1.5.0.0
94+
, QuickCheck
9095
, attoparsec >=0.13.2.2
9196
, bytestring ^>=0.10.12.0
9297
, cardano-api
9398
, cardano-crypto
9499
, cardano-ledger-alonzo
100+
, cardano-slotting
95101
, containers
96102
, data-default
97103
, data-default-class
@@ -118,7 +124,6 @@ library
118124
, plutus-tx
119125
, plutus-tx-plugin
120126
, process
121-
, QuickCheck
122127
, row-types
123128
, serialise
124129
, servant

cabal.project

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ packages: ./.
44
./examples/plutus-game/plutus-game.cabal
55
./examples/plutus-transfer/plutus-transfer.cabal
66
./examples/plutus-nft/plutus-nft.cabal
7+
./examples/ex-units/ex-units.cabal
78

89
tests: true
910
benchmarks: true

examples/ex-units/.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
scripts
2+
signing-keys
3+
txs

examples/ex-units/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Application for fast debugging of bpi balancing

examples/ex-units/app/Main.hs

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
module Main (main) where
2+
3+
import BotPlutusInterface.Contract qualified as BPI
4+
import BotPlutusInterface.QueryNode qualified as BPI
5+
import BotPlutusInterface.Types
6+
import Cardano.Api (NetworkId (Mainnet))
7+
import Cardano.Api.Shelley (ProtocolParameters)
8+
import Control.Concurrent.STM (newTVarIO)
9+
import Data.Aeson ((.=))
10+
import Data.Aeson qualified as JSON
11+
import Data.Default (def)
12+
import Data.Text (Text)
13+
import Data.Text qualified as Text
14+
import Data.UUID.V4 qualified as UUID
15+
import Ledger (PubKeyHash)
16+
import LockSpend (lockThenSpend)
17+
-- import LockSpendSingle (lockThenSpendSingle)
18+
import Plutus.PAB.Core.ContractInstance.STM (Activity (Active))
19+
import Servant.Client (BaseUrl (BaseUrl), Scheme (Http))
20+
import System.Directory (listDirectory)
21+
import System.Environment (getArgs, setEnv, getEnv)
22+
import System.FilePath ((</>))
23+
import Wallet.Types (ContractInstanceId (ContractInstanceId))
24+
import Prelude
25+
26+
main :: IO ()
27+
main = do
28+
-- TODO: export PATH=$PATH:/home/mike/dev/mlabs/local-cluster/node-bins
29+
let clusterDir = "/home/mike/dev/mlabs/local-cluster/data"
30+
[sockPath] <- getArgs
31+
setEnv "CARDANO_NODE_SOCKET_PATH" sockPath
32+
getEnv "PATH" >>= \p -> setEnv "PATH" (p ++ ":/home/mike/dev/mlabs/local-cluster/node-bins")
33+
let nodeInfo = BPI.NodeInfo Mainnet sockPath
34+
35+
cEnv <- mkContractEnv nodeInfo clusterDir
36+
res <- BPI.runContract cEnv lockThenSpend
37+
-- res <- BPI.runContract cEnv lockThenSpendSingle
38+
putStrLn $ case res of
39+
Right _ -> "=== OK ==="
40+
Left e -> "=== FAILED ===\n" ++ show e
41+
42+
mkContractEnv :: Monoid w => BPI.NodeInfo -> FilePath -> IO (ContractEnvironment w)
43+
mkContractEnv nodeInfo clusterDir = do
44+
(pparams, paramsFile) <- getPparams nodeInfo clusterDir
45+
contractInstanceID <- ContractInstanceId <$> UUID.nextRandom
46+
contractState <- newTVarIO (ContractState Active mempty)
47+
pkhs <- getPkhs clusterDir
48+
return $
49+
ContractEnvironment
50+
{ cePABConfig = mkPabConf pparams (Text.pack paramsFile) clusterDir (head pkhs),
51+
ceContractState = contractState,
52+
ceContractInstanceId = contractInstanceID
53+
}
54+
55+
getPparams :: BPI.NodeInfo -> FilePath -> IO (ProtocolParameters, FilePath)
56+
getPparams nodeInfo clusterDir = do
57+
pparams :: ProtocolParameters <- getOrFailM $ BPI.protocolParams nodeInfo
58+
let ppath = clusterDir </> "pparams.json"
59+
JSON.encodeFile ppath pparams
60+
return (pparams, ppath)
61+
62+
mkPabConf :: ProtocolParameters -> Text -> FilePath -> PubKeyHash -> PABConfig
63+
mkPabConf pparams pparamsFile clusterDir ownPkh =
64+
PABConfig
65+
{ pcCliLocation = Local,
66+
pcNetwork = Mainnet,
67+
pcChainIndexUrl = BaseUrl Http "localhost" 9083 "",
68+
pcPort = 9080,
69+
pcProtocolParams = pparams,
70+
pcTipPollingInterval = 1_000_000,
71+
pcSlotConfig = def,
72+
pcOwnPubKeyHash = ownPkh,
73+
pcScriptFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/scripts",
74+
pcSigningKeyFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/signing-keys",
75+
pcTxFileDir = Text.pack $ clusterDir </> "bot-plutus-interface/txs",
76+
pcDryRun = False,
77+
pcLogLevel = Error,
78+
pcProtocolParamsFile = pparamsFile,
79+
pcEnableTxEndpoint = True
80+
}
81+
82+
getPkhs :: FilePath -> IO [PubKeyHash]
83+
getPkhs bpiDir = do
84+
let dir = bpiDir </> "bot-plutus-interface/signing-keys"
85+
replace =
86+
Text.unpack
87+
. Text.replace "signing-key-" ""
88+
. Text.replace ".skey" ""
89+
. Text.pack
90+
keyNames <- listDirectory dir
91+
return $ map (parseKey . replace) keyNames
92+
where
93+
parseKey :: String -> PubKeyHash
94+
parseKey key =
95+
let res = JSON.fromJSON $ JSON.object ["getPubKeyHash" .= key]
96+
in case res of
97+
JSON.Success pkh -> pkh
98+
_ -> error "failed to parse pkh"
99+
100+
getOrFail :: Show e => Either e a -> a
101+
getOrFail = either (error . show) id
102+
103+
getOrFailM :: (Show e, Functor f) => f (Either e b) -> f b
104+
getOrFailM = (getOrFail <$>)

examples/ex-units/cabal.project

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Bump this if you need newer packages
2+
index-state: 2021-10-20T00:00:00Z
3+
4+
packages:
5+
./.
6+
../../.
7+
8+
-- You never, ever, want this.
9+
write-ghc-environment-files: never
10+
11+
-- Always build tests and benchmarks.
12+
tests: true
13+
benchmarks: true
14+

examples/ex-units/ex-units.cabal

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
cabal-version: 3.0
2+
name: ex-units
3+
version: 0.1.0.0
4+
synopsis: NFT example
5+
description: NFT example running on the fake pab
6+
homepage: https://github.com/mlabs-haskell/bot-plutus-interface
7+
bug-reports: https://github.com/mlabs-haskell/bot-plutus-interface
8+
license:
9+
license-file:
10+
author: MLabs
11+
maintainer: gergely@mlabs.city
12+
copyright: TODO
13+
build-type: Simple
14+
tested-with: GHC ==8.10.4
15+
extra-source-files: README.md
16+
17+
source-repository head
18+
type: git
19+
location: https://github.com/mlabs-haskell/bot-plutus-interface
20+
21+
-- Common sections
22+
23+
common common-lang
24+
ghc-options:
25+
-Wall -Wcompat -Wincomplete-record-updates
26+
-Wincomplete-uni-patterns -Wredundant-constraints
27+
-- -Werror
28+
-fobject-code -fno-ignore-interface-pragmas
29+
-fno-omit-interface-pragmas -Wunused-packages
30+
-fplugin-opt PlutusTx.Plugin:defer-errors
31+
32+
build-depends:
33+
, base ^>=4.14
34+
35+
default-extensions:
36+
NoImplicitPrelude
37+
BangPatterns
38+
BinaryLiterals
39+
ConstraintKinds
40+
DataKinds
41+
DeriveFunctor
42+
DeriveGeneric
43+
DeriveTraversable
44+
DerivingStrategies
45+
DerivingVia
46+
DuplicateRecordFields
47+
EmptyCase
48+
FlexibleContexts
49+
FlexibleInstances
50+
GADTs
51+
GeneralizedNewtypeDeriving
52+
HexFloatLiterals
53+
ImportQualifiedPost
54+
InstanceSigs
55+
KindSignatures
56+
LambdaCase
57+
MultiParamTypeClasses
58+
NumericUnderscores
59+
OverloadedStrings
60+
ScopedTypeVariables
61+
StandaloneDeriving
62+
TupleSections
63+
TypeApplications
64+
TypeOperators
65+
TypeSynonymInstances
66+
UndecidableInstances
67+
TemplateHaskell
68+
TypeFamilies
69+
70+
default-language: Haskell2010
71+
72+
-- Libraries
73+
74+
75+
library
76+
import: common-lang
77+
exposed-modules:
78+
LockSpend
79+
LockSpendSingle
80+
81+
build-depends:
82+
plutus-ledger
83+
, containers
84+
, plutus-contract
85+
, plutus-ledger-api
86+
, plutus-ledger-constraints
87+
, plutus-pab
88+
, plutus-tx
89+
, plutus-tx-plugin
90+
, text
91+
, aeson
92+
, lens
93+
94+
95+
96+
hs-source-dirs: src
97+
98+
executable ex-units-run
99+
import: common-lang
100+
build-depends:
101+
aeson ^>=1.5.0.0
102+
, bot-plutus-interface
103+
, cardano-api
104+
, data-default
105+
, ex-units
106+
, servant-client
107+
, filepath
108+
, text
109+
, directory
110+
, plutus-ledger
111+
, plutus-tx
112+
, uuid
113+
, stm
114+
, plutus-pab
115+
, plutus-contract
116+
117+
118+
main-is: Main.hs
119+
hs-source-dirs: app

examples/ex-units/hie.yaml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
cradle:
2+
cabal:
3+
- path: "./src"
4+
component: "lib:ex-units"
5+
6+
- path: "./app"
7+
component: "exe:ex-units-run"

0 commit comments

Comments
 (0)