-
Notifications
You must be signed in to change notification settings - Fork 27
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
[Wallet] consolidating #46
base: main
Are you sure you want to change the base?
Changes from all commits
14c29d3
ef536dd
3f97620
f506062
ec8f9bb
64b7f72
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,5 @@ | ||
module Main (main) where | ||
|
||
|
||
|
||
import qualified Tokenomia.CLI as Tokenomia | ||
|
||
main :: IO () | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
{-# LANGUAGE TemplateHaskell #-} | ||
{-# LANGUAGE ExtendedDefaultRules #-} | ||
{-# LANGUAGE FlexibleContexts #-} | ||
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} | ||
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
module Tokenomia.Ada.Consolidate | ||
( consolidate ) where | ||
|
||
|
||
import Data.List.NonEmpty | ||
import qualified Data.ByteString.Lazy.Char8 as C | ||
import Control.Monad.Reader hiding (ask) | ||
import Control.Monad.Except | ||
import Ledger.Ada ( lovelaceValueOf ) | ||
import Data.Foldable | ||
import Ledger (Slot(Slot)) | ||
|
||
import Tokenomia.Adapter.Cardano.CLI.Node | ||
|
||
import Tokenomia.Adapter.Cardano.CLI.Environment | ||
import Tokenomia.Adapter.Cardano.CLI.UTxO | ||
import Tokenomia.Adapter.Cardano.CLI.Transaction hiding (value) | ||
import Shh.Internal | ||
( load, | ||
(|>), | ||
ExecReference(SearchPath), | ||
captureWords ) | ||
|
||
import Tokenomia.Adapter.Cardano.CLI.Wallet | ||
import Tokenomia.Adapter.Cardano.CLI.UTxO.Query (queryUTxOsFilterBy) | ||
import Tokenomia.Adapter.Cardano.CLI.Folder ( Folder(Transactions), getFolderPath ) | ||
import Tokenomia.Common.Error | ||
import Tokenomia.Wallet.Collateral.Read | ||
import Tokenomia.Wallet.CLI | ||
( selectBiggestStrictlyADAsNotCollateral, | ||
askToChooseAmongGivenWallets ) | ||
import Tokenomia.Common.Shell.Console (printLn) | ||
import PlutusTx.Prelude (AdditiveGroup((-))) | ||
|
||
|
||
load SearchPath ["cardano-cli", "md5sum", "mv", "echo"] | ||
|
||
consolidate | ||
:: ( MonadIO m | ||
, MonadReader Environment m | ||
, MonadError BuildingTxError m) | ||
=> m () | ||
consolidate = do | ||
wallet <- fetchWalletsWithCollateral >>= whenNullThrow NoWalletWithCollateral | ||
>>= \wallets -> do | ||
printLn "Select the minter wallet : " | ||
askToChooseAmongGivenWallets wallets | ||
utxo <- selectBiggestStrictlyADAsNotCollateral wallet >>= whenNothingThrow NoADAInWallet | ||
|
||
printLn $ "- Amount Available : " <> showValue (value utxo) | ||
consolidate' wallet | ||
|
||
consolidate' | ||
:: ( MonadIO m | ||
, MonadReader Environment m | ||
, MonadError BuildingTxError m) | ||
=> Wallet | ||
-> m () | ||
consolidate' wallet@Wallet {..} = do | ||
adas <- queryUTxOsFilterBy wallet containingStrictlyADAs | ||
let amount = fold (value <$> adas ) | ||
(txFolder, rawTx ) <- (\a-> (a,a <> "tx.raw")) <$> getFolderPath Transactions | ||
aGivenTxOutRef <- txOutRef <$> (selectBiggestStrictlyADAsNotCollateral wallet >>= whenNothingThrow NoADAInWallet) | ||
|
||
buildRaw "0" (toCardanoCLIOptions TxBuild | ||
{ wallet = wallet | ||
, txIns = fromList (fmap (FromWallet . txOutRef) adas ) | ||
, txOuts = ToWallet paymentAddress (lovelaceValueOf 1) :| [] | ||
, metadataMaybe = Nothing | ||
, validitySlotRangeMaybe = Nothing | ||
, tokenSupplyChangesMaybe = Nothing | ||
, ..}) | ||
fees <- computeFees (Prelude.length adas ) 1 | ||
Slot synSlotAsInt <- getCurrentSlotSynced | ||
buildRaw (show fees) (toCardanoCLIOptions TxBuild | ||
{ wallet = wallet | ||
, txIns = fromList (fmap (FromWallet . txOutRef) adas ) | ||
, txOuts = ToWallet paymentAddress (amount PlutusTx.Prelude.- lovelaceValueOf fees) :| [] | ||
, metadataMaybe = Nothing | ||
, validitySlotRangeMaybe = Nothing | ||
, tokenSupplyChangesMaybe = Nothing | ||
, ..} | ||
<> ["--invalid-hereafter", show (synSlotAsInt + 600)]) | ||
-- Hashing the tx.raw and getting its hash x for renaming the tx.raw into x.raw | ||
(rawHashTx,signedHashTx) <- (\txHash -> ( txFolder <> txHash <> ".raw" | ||
, txFolder <> txHash <> ".signed" ) ) | ||
. C.unpack . Prelude.head <$> liftIO (md5sum rawTx |> captureWords ) | ||
liftIO $ mv rawTx rawHashTx | ||
|
||
printLn "Signing Tx" >> sign_tx rawHashTx signedHashTx paymentSigningKeyPath | ||
printLn "Submitting Tx" >> submit_tx signedHashTx | ||
printLn "Waiting for confirmation..." | ||
awaitTxCommitted aGivenTxOutRef 0 | ||
printLn "\nTx committed into ledger" |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,9 +8,9 @@ | |
{-# LANGUAGE NamedFieldPuns #-} | ||
{-# LANGUAGE TupleSections #-} | ||
{-# LANGUAGE DuplicateRecordFields #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
{-# OPTIONS_GHC -fno-warn-missing-signatures #-} | ||
{-# OPTIONS_GHC -fno-warn-unused-top-binds #-} | ||
{-# LANGUAGE RecordWildCards #-} | ||
|
||
|
||
module Tokenomia.Adapter.Cardano.CLI.Transaction | ||
|
@@ -33,6 +33,8 @@ module Tokenomia.Adapter.Cardano.CLI.Transaction | |
, sign_tx | ||
, doubleSign_tx | ||
, toCardanoCLIOptions | ||
, computeFees | ||
, buildRaw | ||
) where | ||
|
||
|
||
|
@@ -50,7 +52,15 @@ import Control.Monad.Except | |
import Control.Concurrent | ||
import System.Random | ||
|
||
import Shh.Internal | ||
import Shh.Internal | ||
( (&>), | ||
capture, | ||
load, | ||
(|>), | ||
ExecArg(asArg), | ||
ExecReference(SearchPath), | ||
Stream(Truncate), | ||
captureWords ) | ||
|
||
import Ledger ( TxOutRef (..),Value,Slot (..) ) | ||
|
||
|
@@ -129,7 +139,7 @@ instance ToCardanoCLIOptions TxOut where | |
[ "--tx-out" , coerce address <> " " <> (T.unpack . toCLI) value | ||
, "--tx-out-datum-hash" , coerce datumHash] | ||
toCardanoCLIOptions ToWallet {..} = | ||
[ "--tx-out" , coerce address <> " " <> (T.unpack . toCLI) value] | ||
[ "--tx-out" , coerce address <> "+" <> (T.unpack . toCLI) value] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. you should not add a "+" here, the format is "address x lovelace + y policyhash.tokenName" , something like that |
||
|
||
instance ToCardanoCLIOptions ValiditySlotRange where | ||
toCardanoCLIOptions (ValiditySlotRange (Slot x) (Slot y)) = | ||
|
@@ -161,7 +171,7 @@ instance ToCardanoCLIOptions TxBuild where | |
<> toCardanoCLIOptions tokenSupplyChangesMaybe | ||
<> toCardanoCLIOptions validitySlotRangeMaybe | ||
<> toCardanoCLIOptions metadataMaybe | ||
|
||
submit' | ||
:: ( MonadIO m | ||
, MonadReader Environment m | ||
|
@@ -318,3 +328,36 @@ createMetadataFile message = do | |
liftIO (echo ("{\"" ++ show randomInt ++ "\":{\"message\":\"" ++ message ++ "\"}}") | ||
&> (Truncate . fromString) metadataJsonFilepath) | ||
return metadataJsonFilepath | ||
|
||
computeFees :: (MonadIO m, MonadReader Environment m) => Int -> Int -> m Integer | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
computeFees nbTxIn nbTxOut = do | ||
protocolParametersPath <- register_protocol_parameters | ||
magicN <- asks magicNumber | ||
(_, rawTx ) <- (\a-> (a,a <> "tx.raw")) <$> getFolderPath Transactions | ||
unparsedLovelaces <- liftIO (cardano_cli | ||
"transaction" | ||
"calculate-min-fee" | ||
"--tx-body-file" rawTx | ||
"--tx-in-count" (show nbTxIn) | ||
"--tx-out-count" (show $ nbTxOut) | ||
"--witness-count" "1" | ||
"--byron-witness-count" "0" | ||
"--testnet-magic" magicN | ||
"--protocol-params-file" protocolParametersPath |> capture) | ||
return $ parse unparsedLovelaces | ||
where parse lovelaces = read @Integer (head (words (C.unpack lovelaces))) | ||
|
||
buildRaw | ||
:: ( ExecArg a, MonadIO m, MonadReader Environment m) | ||
=> String | ||
-> a | ||
-> m () | ||
buildRaw fee buildTxBody = do | ||
(_, rawTx ) <- (\a-> (a,a <> "tx.raw")) <$> getFolderPath Transactions | ||
|
||
liftIO $ cardano_cli | ||
"transaction" | ||
"build-raw" | ||
(asArg buildTxBody) | ||
"--fee" fee | ||
"--out-file" rawTx |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
{-# LANGUAGE DerivingStrategies #-} | ||
{-# LANGUAGE DerivingVia #-} | ||
{-# LANGUAGE GeneralizedNewtypeDeriving #-} | ||
module Tokenomia.Adapter.Cardano.Types (Address (..), Hash(..)) where | ||
|
||
newtype Address = Address String deriving stock (Eq) | ||
import Data.String (IsString) | ||
newtype Address = Address String deriving stock (Eq,Ord) | ||
deriving newtype Show | ||
deriving newtype IsString | ||
|
||
newtype Hash = Hash String deriving (Show,Eq) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Tokenomia.Adapter.Data.List | ||
(removeDuplicates) where | ||
import Data.List | ||
|
||
removeDuplicates :: (Ord a) => [a] -> [a] | ||
removeDuplicates = Prelude.map Prelude.head . group . sort | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module Tokenomia.Adapter.Ledger.Value.CurrencySymbol | ||
( shortHashView ) where | ||
|
||
import Ledger.Value | ||
|
||
shortHashView :: CurrencySymbol -> String | ||
shortHashView x = take (length (show x) `div` 2) (show x) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why are you adding
["--invalid-hereafter", show (synSlotAsInt + 600)])
?