2

此代码可以编译,但是当我单击 Evaluate 时出现此错误。我不确定这是否来自操场,因为我使用了他们新教程中的相同代码。

当我编译它时不会显示错误,只有当我通过单击 Evaluate 尝试查看事务时才会发生。我不确定这是否是代码的问题,因为它通过了编译。

错误消息似乎与代码本身无关。哎呀,测试区块链上的交易为零,甚至出错。我尝试了不同的交易变化。我知道这些更新是本周刚刚发布的。我仔细检查了文档。而且我没有看到它在说什么。

https://docs.cardano.org/projects/plutus/en/latest/tutorials/plutus-playground.html

https://playground.plutus.iohkdev.io/

Interpreter Errors
 error:
    • Variable not in scope: endpoints :: Contract s0 T.Text a0
    • Perhaps you meant ‘endpoint’ (imported from Language.Plutus.Contract)
    |
101 | main = printJson $ stage endpoints "\"[{\\\"blocks\\\":10,\\\"tag\\\":\\\"AddBlocks\\\"},{\\\"blocks\\\":10,\\\"tag\\\":\\\"AddBlocks\\\"}]\"" "[{\"simulatorWalletBalance\":{\"getValue\":[[{\"unCurrencySymbol\":\"\"},[[{\"unTokenName\":\"\"},10]]]]},\"simulatorWalletWallet\":{\"getWallet\":1}},{\"simulatorWalletBalance\":{\"getValue\":[[{\"unCurrencySymbol\":\"\"},[[{\"unTokenName\":\"\"},10]]]]},\"simulatorWalletWallet\":{\"getWallet\":2}}]"
    |                          ^^^^^^^^
import           Control.Monad             (void)
import           Data.Aeson                (FromJSON, ToJSON)
import qualified Data.Text                 as T
import           GHC.Generics              (Generic)
import           Language.Plutus.Contract
import qualified Language.PlutusTx         as PlutusTx
import           Language.PlutusTx.Prelude
import           Ledger
import qualified Ledger.Ada                as Ada
import qualified Ledger.Constraints        as Constraints
import qualified Ledger.Typed.Scripts      as Scripts
import           Schema
import           Wallet.Emulator.Wallet
import           Playground.Contract


data SplitData = 
    SplitData
        { recipient1 :: PubKeyHash -- First Recipent of the funds
        , recipient2 :: PubKeyHash -- Second Recipentt of the funds
        , amount     :: Ada        -- How much ADA gets locked
        }
    deriving stock (Show, Generic)

PlutusTx.makeIsData ''SplitData
PlutusTx.makeLift ''SplitData


validateSplit :: SplitData -> () -> ValidatorCtx -> Bool
validateSplit SplitData{recipient1, recipient2, amount} _ ValidatorCtx{valCtxTxInfo} =
    let half = Ada.divide amount 2 in 
    Ada.fromValue (valuePaidTo valCtxTxInfo recipient1) >= half &&
    Ada.fromValue (valuePaidTo valCtxTxInfo recipient2) >= (amount - half)

data Split 
instance Scripts.ScriptType Split where
    type instance RedeemerType Split = ()
    type instance DatumType Split = SplitData

splitInstance :: Scripts.ScriptInstance Split
splitInstance = Scripts.validator @Split 
    $$(PlutusTx.compile [|| validateSplit ||])
    $$(PlutusTx.compile [|| warp ||]) where
        warp = Scripts.wrapValidator @SplitData @()

data LockArgs = 
        LockArgs
        {  recipient1Wallet :: Wallet
        ,  recipient2Wallet :: Wallet
        ,  totalAda         :: Ada 
        }
    deriving stock (Show, Generic)
    deriving anyclass (ToJSON, FromJSON, ToSchema)

type SplitSchema = 
    BlockchainActions
        .\/ Endpoint "lock" LockArgs
        .\/ Endpoint "unlock" LockArgs

lock :: Contract SplitSchema T.Text LockArgs
lock = endpoint @"lock"

unlock :: Contract SplitSchema T.Text LockArgs
unlock = endpoint @"unlock"

mkSplitData :: LockArgs -> SplitData
mkSplitData LockArgs{recipient1Wallet, recipient2Wallet, totalAda} =
    let convert :: Wallet -> PubKeyHash
        convert = pubKeyHash . walletPubKey
    in
    SplitData
    { recipient1 = convert recipient1Wallet
    , recipient2 = convert recipient2Wallet
    , amount = totalAda
    }

lockFunds :: SplitData -> Contract SplitSchema T.Text ()
lockFunds s@SplitData{amount} = do
    logInfo $ "Locking" <> show amount
    let tx = Constraints.mustPayToTheScript s (Ada.toValue amount)
    void $ submitTxConstraints splitInstance tx

unlockFunds :: SplitData -> Contract SplitSchema T.Text ()
unlockFunds SplitData{recipient1, recipient2, amount} = do
    let contractAddress = (Ledger.scriptAddress (Scripts.validatorScript splitInstance))
    utxos <- utxoAt contractAddress
    let half = Ada.divide amount 2
        tx = 
            collectFromScript utxos ()
            <> Constraints.mustPayToPubKey recipient1 (Ada.toValue half)
            <> Constraints.mustPayToPubKey recipient2 (Ada.toValue  $ amount - half)
    void $ submitTxConstraintsSpending splitInstance utxos tx 


mkSchemaDefinitions ''SplitSchema

$(mkKnownCurrencies [])```
4

1 回答 1

2

您缺少endpoints声明。您必须为 UI 的任何 Plutus Playground 合约定义端点,其类型为:

endpoints :: Contract SplitSchema T.Text ()

对于有问题的拆分应用程序:

endpoints = (lock >>= lockFunds . mkSplitData) `select` (unlock >>= unlockFunds . mkSplitData)
...

endpoints = (lock >>= lockFunds . mkSplitData) `select` (unlock >>= unlockFunds . mkSplitData)

mkSchemaDefinitions ''SplitSchema

$(mkKnownCurrencies [])
于 2021-03-04T10:09:41.880 回答