開始使用 Plutus Playground

Alvin Cho
4 min readJun 26, 2021

--

在 Cardano 的測試網還沒有公開之前,Playground是目前可以試用 Plutus 最好的方式。要使用 Plutus Playground 有兩種方式:

  1. 使用 IOHK 的線上版本 => 按這裡
  2. 本地安裝 Playground => 參考安裝說明

由於整個 Cardano 還在持續開發中,包括 Plutus,Marlowe,和其他開發工具也可能隨時都有變化,不同版本之間的程式碼可能不相容。今天我們只用 Playground 裡面附的範例來介紹,應該不會有相容問題。

Playground 界面

Playground 同時只能有一個程式碼,點選範例名稱會自動載入

Hello.world

Hello.world 只有23行,包含最重要的 endpoints 和 hello 這個主函數。

— | A ‘Contract’ that logs a message.
hello :: Contract () EmptySchema T.Text ()
hello = logInfo @Haskell.String “Hello, world”
endpoints :: Contract () EmptySchema T.Text ()
endpoints = hello
type DummySchema = Endpoint "dummy" ()

Hello 只做一個動作,logInfo 將一段文字寫進區塊裡。

Endpoints 是與 Playground 之間的界面,定義 endpoints 會在 Playground 中顯示操作界面。這裡定義了一個 endpoints 叫 dummy函數,這個函數呼叫 hello 函數。

Compile 後,狀態顯示 Compilation successful 編譯成功。

Simulate 進入模擬器。

智能合約模擬器

1. 回到編輯器:回到上一步

2. 執行合約:在模擬器依序執行動作

3. 錢包:參與模擬動作的錢包

4. 增加錢包:如果超過兩個錢包,需要手動增加

5. 初始餘額:每個錢包可以設定初始的餘額

6. Endpoint接口:以按鈕的方式呼叫在程式裡設定的 endpoints

7. 等待動作:如果動作之間需要等待,用這個按鈕加入等待動作。Wait For 是等待幾個區塊,Wait Until 是等待直到第幾個區塊。

設定動作

1. 加入 wallet 1 的 dummy 動作,這個動作沒有任何參數

2. 加入一個等待。設定等待1個區塊。

Evaluate,進入到 Transactions。

Transactions

1. 區塊:模擬過程中經歷的區塊,在 Cardano 每一個區塊是一個 slot。如果有多個區塊則選擇要顯示的區塊

2. 輸入資訊:在進入模擬時所輸入的參數會在這裡顯示

3. 交易資訊:交易相關的資訊,在這個例子裡只有交易 Hash 值

4. 輸出資訊,eUTxO 的O,包含所參與的錢包資訊還有手續費

Balances Carried Forward 餘額

顯示錢包的餘額

Logs & Trace

交易 Log

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Alvin Cho
Alvin Cho

Written by Alvin Cho

Independent consultant. 30+ years experience in enterprise applications for trading and risk management. 

No responses yet

Write a response