Communication
send
Contractはsendステートメントを使って他アカウント、他のContractと通信できます。
send msgs
メッセージのリストを送信します。
※現在のScillaにおいてメッセージの送信はリスト形式で可能となっていますが、テスト環境含めたZilliqa本体が未対応です。
そのため、現在はメッセージリストの1件目のみ送信されます。
次のメッセージは、_tag、_recipient、_amount、paramの4つを宣言しています。
_tag、_recipient、_amountの3つは必須となりますが、以降は変数名を含めて自由に追加できます。
(*Assume contractAddress is the address of the contract being called and the contract contains the transition setHello*)
msg = { _tag : "setHello"; _recipient : contractAddress; _amount : Uint128 0; param : Uint32 0 };
送信先が他のContractとなる場合
_tagには呼び出すContractのTransition名を、_recipientには呼び出すContractのアドレスを設定します。追加で指定する変数は、呼び出し先のTransitionの引数名と合わせることで、呼び出し先で引数として受け取ることが出来ます。
また、呼び出し先のContractのTransitionにて変更された状態を取得したい場合、Transitionには戻り値は存在しません。この場合は、今度は逆に呼び出し元のContractを呼び出してもらう必要があります。このときのアドレスは、_senderで取得できます。
Contract呼び出しのサンプル
呼び出し元Contract
contract HelloWorldCaller
(callee_address : ByStr20)
field get_welcome_msg : String = ""
transition callHello ()
msg = {_tag : "getHello"; _recipient : callee_address; _amount : Uint128 0};
msgs = one_msg msg;
send msgs
end
transition callBackHello (rtMsg : String)
get_welcome_msg := rtMsg;
msg = {_tag : "Main"; _recipient : _sender; _amount : Uint128 0; msg : rtMsg};
msgs = one_msg msg;
send msgs
end
呼び出し先Contract
contract HelloWorld
()
field welcome_msg : String = "HelloWorld"
transition getHello ()
r <- welcome_msg;
msg = {_tag : "callBackHello"; _recipient : _sender; _amount : Uint128 0; msg : r};
msgs = one_msg msg;
send msgs
end
event
Contractはeventをクライアント(off-chain)に向けて発行できます。
※これはEthereumにおけるSolidityのeventと似たものです。
event e
イベントeを発行します。
下記では、eventNameという名前でイベント発行しています。
e = { _eventname : "eventName"; <entry>_2 ; <entry>_3 };
(*where <entry> is of the form: b : x as in a message expression.*)
(*Here b is the identifier, and x the variable, whose value is bound to the
identifier.*)
event e;
_eventnameパラメータは必須です。