Skip to content

Commit

Permalink
Extend Gov.SubmitProposal example to Gov.SubmitVoteSimple
Browse files Browse the repository at this point in the history
  • Loading branch information
errfrom committed Jul 29, 2024
1 parent 25948fc commit b09c459
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,15 +1,22 @@
module Ctl.Examples.Gov.SubmitProposal
module Ctl.Examples.Gov.SubmitVoteSimple
( contract
, example
, main
) where

import Contract.Prelude

import Cardano.Transaction.Builder (TransactionBuilderStep(SubmitProposal))
import Cardano.Transaction.Builder
( TransactionBuilderStep(SubmitProposal, SubmitVotingProcedure)
)
import Cardano.Types
( Address(RewardAddress)
, Credential(PubKeyHashCredential)
, GovernanceActionId
, RewardAddress
, Vote(VoteYes)
, Voter(Drep)
, VotingProcedure(VotingProcedure)
, VotingProposal(VotingProposal)
)
import Cardano.Types (GovernanceAction(Info)) as GovAction
Expand All @@ -23,10 +30,10 @@ import Contract.Log (logInfo')
import Contract.Monad (Contract, launchAff_, liftedM, runContract)
import Contract.ProtocolParameters (getProtocolParameters)
import Contract.Transaction (awaitTxConfirmed, submitTxFromBuildPlan)
import Contract.Wallet (getRewardAddresses)
import Contract.Wallet (getRewardAddresses, ownDrepPubKeyHash)
import Ctl.Examples.Gov.Internal.Common (dummyAnchor)
import Data.Array (head) as Array
import Data.Map (empty) as Map
import Data.Map (empty, singleton) as Map

main :: Effect Unit
main = example $ testnetConfig
Expand All @@ -38,6 +45,23 @@ example = launchAff_ <<< flip runContract contract

contract :: Contract Unit
contract = do
logInfo' "Running Examples.Gov.SubmitVoteSimple"
govActionId <- submitProposal
logInfo' $ "Successfully submitted voting proposal. Action id: " <> show
govActionId
submitVote govActionId
logInfo' "Successfully voted on the proposal."

{-
{ transactionId: unsafePartial fromJust $ decodeCbor $ wrap $
hexToByteArrayUnsafe
"fec3c9c4c8bf9b02237bbdccca9460eee1e5b67a5052fdbd5eb1d7ec1719d9f0"
, index: zero
}
-}

submitProposal :: Contract GovernanceActionId
submitProposal = do
logInfo' "Running Examples.Gov.SubmitProposal"

govActionDeposit <- _.govActionDeposit <<< unwrap <$> getProtocolParameters
Expand All @@ -53,9 +77,24 @@ contract = do
, returnAddr: rewardAddr
}
]
let txHash = Transaction.hash tx
awaitTxConfirmed txHash
pure $ wrap { transactionId: txHash, index: zero }

-- NOTE: The wallet must be registered as DRep to submit a vote.
-- See Ctl.Examples.Gov.RegisterDrep
submitVote :: GovernanceActionId -> Contract Unit
submitVote govActionId = do
drepCred <- PubKeyHashCredential <$> ownDrepPubKeyHash

tx <- submitTxFromBuildPlan Map.empty mempty
[ SubmitVotingProcedure (Drep drepCred)
( Map.singleton govActionId $
VotingProcedure { vote: VoteYes, anchor: Nothing }
)
Nothing
]
awaitTxConfirmed $ Transaction.hash tx
logInfo' "Successfully submitted voting proposal."

asRewardAddress :: Address -> Maybe RewardAddress
asRewardAddress = case _ of
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
module Ctl.Examples.KeyWallet.SubmitProposal
module Ctl.Examples.KeyWallet.SubmitVoteSimple
( main
) where

import Contract.Prelude

import Ctl.Examples.Gov.SubmitProposal (contract) as Gov.SubmitProposal
import Ctl.Examples.Gov.SubmitVoteSimple (contract) as Gov.SubmitVoteSimple
import Ctl.Examples.KeyWallet.Internal.Contract (runKeyWalletContract)

main :: Effect Unit
main =
runKeyWalletContract \unlock ->
Gov.SubmitProposal.contract *> liftEffect unlock
Gov.SubmitVoteSimple.contract *> liftEffect unlock
2 changes: 1 addition & 1 deletion packages.dhall
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ let additions =
]
, repo =
"https://github.com/mlabs-haskell/purescript-cardano-transaction-builder"
, version = "48866bd7f5eeb8e0870c97384264d08bda9c2725"
, version = "40628bbb1049f3eb5364d304f949135e2ee2a2fb"
}
, mote-testplan =
{ dependencies =
Expand Down
8 changes: 4 additions & 4 deletions spago-packages.nix

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b09c459

Please sign in to comment.