Skip to content
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

Convenience for bootstraping a mockchain with custom assets #48

Closed
florentc opened this issue Dec 14, 2021 · 0 comments
Closed

Convenience for bootstraping a mockchain with custom assets #48

florentc opened this issue Dec 14, 2021 · 0 comments
Labels
enhancement New feature or request

Comments

@florentc
Copy link
Member

florentc commented Dec 14, 2021

During audits, some minting policies are not provided and it takes repetitive boilerplate code to specify them and then mint and distribute some assets among wallets before reaching the actual desired initial state where that currency is spread in the world alongside Ada.

For example this is a typical module to provide convenience functions regarding a currency that is supposed to already exist in the wild. We use a OneShotCurrency here because it is simple and we have an admin wallet mint everything and then distribute it among other wallets (this could be simplified and done in a single transaction without the "admin", it is just code taken from a recent audit where an admin actually exists).

adminCoin :: Cooked.Wallet
adminCoin = Cooked.wallet 1

adminCoinPKH :: Plutus.PubKeyHash
adminCoinPKH = Cooked.walletPKHash adminCoin

coinTotalAmount :: Integer
coinTotalAmount = 100_000_000

coinPolicy :: Plutus.MintingPolicy
coinPolicy =
  Plutus.curPolicy Plutus.$
    Plutus.OneShotCurrency
      (h, i)
      (Plutus.fromList [("coin", coinTotalAmount)])
  where
    Right ((h, i), _) = Cooked.runMockChain $ do -- TODO: make type safe
      [(Plutus.TxOutRef h' i', _)] <- Cooked.pkUtxos' adminCoinPKH
      return (h', i')

coinCurrency :: Plutus.CurrencySymbol
coinCurrency = Plutus.scriptCurrencySymbol coinPolicy

coinAsset :: Plutus.AssetClass
coinAsset = Plutus.assetClass coinCurrency "coin"

coinGeneration :: (Cooked.MonadMockChain m) => m ()
coinGeneration =
  Cooked.validateTxFromSkeleton $
    Cooked.txSkel
      adminCoin
      [ Cooked.mints [coinPolicy] oneHundredMillionCoin,
        Cooked.PaysPK adminCoinPKH oneHundredMillionCoin
      ]
  where
    oneHundredMillionCoin = Plutus.assetClassValue coinAsset 100_000_000

coin :: Integer -> Plutus.Value
coin = Plutus.assetClassValue coinAsset

-- | Distribute coins from admin to different pkh.
coinDistribution ::
  Cooked.MonadMockChain m =>
  -- | Quantity of coin to give to each recipient
  [(Plutus.PubKeyHash, Integer)] ->
  m ()
coinDistribution = Cooked.validateTxFromSkeleton . Cooked.txSkel adminCoin . map pays
  where
    pays (recipientPKH, quantity) = PaysPK recipientPKH (coin quantity)

It would be nice to be able to quickly specify:

  • a name for the tokens
  • an initial distribution of these among the known wallets :: [(Wallet, Integer)]
    And then be able to bootstrap a mockchain with the chosen initial distribution of freshly minted tokens and a set of helper functions mapping the chosen name to the token name, currency symbol, asset class, and value constructor.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant