Skip to content

Commit c8284ad

Browse files
author
gege251
committed
Add example with wait action between transactions
1 parent 1f2003d commit c8284ad

File tree

5 files changed

+61
-32
lines changed

5 files changed

+61
-32
lines changed

examples/plutus-transfer/README.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
### plutus-helloworld
1+
### plutus-transfer
22

3-
This directory contains a simple "Hello World" script. There are two versions: one using an integer literal (needed because the Plutus interpreter doesn't currently accept byte string literals) and a slighly more complicated one using a bytestring parameter.
4-
5-
``plutus-helloworld'' -- very simple numeric version
6-
7-
``plutus-helloworld-bytestring'' -- more compex version using bytestring constant
3+
Simple value transfer from an address to multiple addresses. With the tfpOutputPerTx option,
4+
payment tx outputs can be grouped together into separate txs, the Contract waits for at least one
5+
block in between.

examples/plutus-transfer/ada-transfer.sh

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22
CONTRACT_INST_ID=$(curl --location --request POST 'localhost:9080/api/contract/activate' \
33
--header 'Content-Type: application/json' \
44
--data-raw '{
5-
"caID": [
6-
[ {"getPubKeyHash": "981fc565bcf0c95c0cfa6ee6693875b60d529d87ed7082e9bf03c6a4"},
7-
{"getValue": [[{"unCurrencySymbol":""},[[{"unTokenName": ""}, 1500000]]]]}
8-
]
5+
"caID": {
6+
"tfpOutputPerTx": 50,
7+
"tfpPayments": [
8+
[ {"getPubKeyHash": "981fc565bcf0c95c0cfa6ee6693875b60d529d87ed7082e9bf03c6a4"},
9+
{"getValue": [[{"unCurrencySymbol":""},[[{"unTokenName": ""}, 1500000]]]]}
10+
]
911
]
12+
}
1013
}' | jq -r .unContractInstanceId )
1114

1215
echo $CONTRACT_INST_ID

examples/plutus-transfer/app/Main.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,13 @@ module Main (main) where
66
import Cardano.Api (NetworkId (Testnet), NetworkMagic (..))
77
import Cardano.PlutusExample.Transfer (
88
TransferSchema,
9+
TransferParams,
910
transfer,
1011
)
1112
import Data.Aeson qualified as JSON
1213
import Data.Aeson.TH (defaultOptions, deriveJSON)
1314
import Data.ByteString.Lazy qualified as LazyByteString
1415
import Data.Maybe (fromMaybe)
15-
import Ledger.Crypto (PubKeyHash)
16-
import Ledger.Value (Value)
1716
import BotPlutusInterface qualified
1817
import BotPlutusInterface.Types (
1918
CLILocation (Local),
@@ -40,7 +39,7 @@ instance HasDefinitions TransferContracts where
4039
Transfer payments ->
4140
SomeBuiltin $ transfer payments
4241

43-
newtype TransferContracts = Transfer [(PubKeyHash, Value)]
42+
newtype TransferContracts = Transfer TransferParams
4443
deriving stock (Show)
4544

4645
$(deriveJSON defaultOptions ''TransferContracts)
Lines changed: 35 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,46 @@
1-
module Cardano.PlutusExample.Transfer (TransferSchema, transfer) where
1+
{-# LANGUAGE DeriveAnyClass #-}
2+
{-# LANGUAGE TemplateHaskell #-}
23

3-
import Control.Monad (void)
4+
module Cardano.PlutusExample.Transfer (
5+
TransferParams (TransferParams),
6+
TransferSchema,
7+
transfer,
8+
) where
9+
10+
import Control.Monad (forM_)
11+
import Data.Aeson.TH (defaultOptions, deriveJSON)
412
import Data.Bifunctor (first)
513
import Data.Monoid (Last (Last))
614
import Data.Text (Text)
15+
import GHC.Generics (Generic)
716
import Ledger hiding (singleton)
817
import Ledger.Constraints as Constraints
9-
import Plutus.Contract (Contract, Endpoint, submitTx, tell)
10-
import PlutusTx.Prelude hiding (Semigroup (..), unless)
18+
import Plutus.Contract (Contract, Endpoint, submitTx, tell, waitNSlots)
19+
import Schema (ToSchema)
20+
import Prelude
1121

1222
type TransferSchema =
13-
Endpoint "transfer" [(PubKeyHash, Value)]
23+
Endpoint "transfer" TransferParams
24+
25+
data TransferParams = TransferParams
26+
{ tfpOutputPerTx :: Int
27+
, tfpPayments :: [(PubKeyHash, Value)]
28+
}
29+
deriving stock (Show, Eq, Generic)
30+
deriving anyclass (ToSchema)
1431

15-
transfer :: [(PubKeyHash, Value)] -> Contract (Last Text) TransferSchema Text ()
16-
transfer payments = do
32+
$(deriveJSON defaultOptions ''TransferParams)
33+
34+
transfer :: TransferParams -> Contract (Last Text) TransferSchema Text ()
35+
transfer (TransferParams outputPerTx allPayments) = do
1736
tell $ Last $ Just "Contract started"
18-
let tx = mconcat $ map (uncurry Constraints.mustPayToPubKey . first PaymentPubKeyHash) payments
19-
void $ submitTx tx
37+
let txs =
38+
map (mconcat . map (uncurry Constraints.mustPayToPubKey . first PaymentPubKeyHash)) $
39+
group outputPerTx $ allPayments
40+
forM_ txs $ \tx -> submitTx tx >> waitNSlots 1
2041
tell $ Last $ Just "Finished"
42+
43+
group :: Int -> [a] -> [[a]]
44+
group n list
45+
| length list <= n = [list]
46+
| otherwise = let (xs, xss) = splitAt n list in xs : group n xss

examples/plutus-transfer/token-transfer.sh

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22
CONTRACT_INST_ID=$(curl --location --request POST 'localhost:9080/api/contract/activate' \
33
--header 'Content-Type: application/json' \
44
--data-raw '{
5-
"caID": [
6-
[ {"getPubKeyHash": "981fc565bcf0c95c0cfa6ee6693875b60d529d87ed7082e9bf03c6a4"},
7-
{"getValue": [[{"unCurrencySymbol":"1d6445ddeda578117f393848e685128f1e78ad0c4e48129c5964dc2e"},[[{"unTokenName": "testToken"}, 15]]]]}
8-
],
9-
[ {"getPubKeyHash": "6696936bb8ae24859d0c2e4d05584106601f58a5e9466282c8561b88"},
10-
{"getValue": [[{"unCurrencySymbol":"1d6445ddeda578117f393848e685128f1e78ad0c4e48129c5964dc2e"},[[{"unTokenName": "testToken"}, 18]]]]}
11-
],
12-
[ {"getPubKeyHash": "a11767a73ea3f59fb11f17c1627706115de75e2d2c444b0e43789567"},
13-
{"getValue": [[{"unCurrencySymbol":"1d6445ddeda578117f393848e685128f1e78ad0c4e48129c5964dc2e"},[[{"unTokenName": "testToken"}, 20]]]]}
14-
]
5+
"caID": {
6+
"tfpOutputPerTx": 2,
7+
"tfpPayments": [
8+
[ {"getPubKeyHash": "981fc565bcf0c95c0cfa6ee6693875b60d529d87ed7082e9bf03c6a4"},
9+
{"getValue": [[{"unCurrencySymbol":"1d6445ddeda578117f393848e685128f1e78ad0c4e48129c5964dc2e"},[[{"unTokenName": "testToken"}, 15]]]]}
10+
],
11+
[ {"getPubKeyHash": "6696936bb8ae24859d0c2e4d05584106601f58a5e9466282c8561b88"},
12+
{"getValue": [[{"unCurrencySymbol":"1d6445ddeda578117f393848e685128f1e78ad0c4e48129c5964dc2e"},[[{"unTokenName": "testToken"}, 18]]]]}
13+
],
14+
[ {"getPubKeyHash": "a11767a73ea3f59fb11f17c1627706115de75e2d2c444b0e43789567"},
15+
{"getValue": [[{"unCurrencySymbol":"1d6445ddeda578117f393848e685128f1e78ad0c4e48129c5964dc2e"},[[{"unTokenName": "testToken"}, 20]]]]}
16+
]
1517
]
18+
}
1619
}' | jq -r .unContractInstanceId )
1720

1821
echo $CONTRACT_INST_ID

0 commit comments

Comments
 (0)