基本trait
Tact 中的每个合约和特性都会隐式继承 BaseTrait
特性,该特性包含许多适用于各种合约的内部函数,以及一个面向 Tact 高级用户的常量 self.storageReserve
。
常数
self.storageReserve
virtual const storageReserve: Int = 0;
用法示例:
contract AllYourStorageBelongsToUs { // This would change the behavior of self.forward() function, // causing it to try reserving this amount of nanoToncoins before // forwarding a message with SendRemainingBalance mode override const storageReserve: Int = ton("0.1");}
函数
self.reply
virtual fun reply(body: Cell?);
使用以下参数调用 self.forward()
函数的别名:
self.forward(sender(), body, true, null);// ↑ ↑ ↑ ↑// | | | init: StateInit?// | | bounce: Bool// | body: Cell?// to: Address
示例用法:
// 这条信息会反弹给我们!self.reply("Beware, this is my reply to you!".asComment());
self.notify
virtual fun notify(body: Cell?);
使用以下参数调用 self.forward()
函数的别名:
self.forward(sender(), body, false, null);// ↑ ↑ ↑ ↑// | | | init: StateInit?// | | bounce: Bool// | body: Cell?// to: Address
示例用法:
// 此消息不会跳转!self.notify("Beware, this is my reply to you!".asComment());
self.forward
virtual fun forward(to: Address, body: Cell?, bounce: Bool, init: StateInit?);
将消息排入队列(可回弹或不可回弹),以发送到指定的地址 to
。 您可以选择提供消息的 body
和 init
包。
当 self.storageReserve
常量被覆盖为大于 时,在发送消息之前,它会尝试从剩余余额中预留 self.storageReserve
数量的 nanoToncoins,然后再以 SendRemainingBalance
模式进行发送 ()。
如果预留尝试失败,或者在没有尝试的默认情况下,消息将改为使用 SendRemainingValue
() 模式发送。
用法示例:
import "@stdlib/ownable";
message PayoutOk { address: Address; value: Int as coins;}
contract Payout with Ownable { completed: Bool; owner: Address;
init(owner: Address) { self.owner = owner; self.completed = false; }
// ... some actions there ...
// Bounced receiver function, which is called when the specified outgoing message bounces back bounced(msg: bounced<PayoutOk>) { // Reset completed flag if our message bounced self.completed = false;
// Send a notification that the payout failed using the remaining funds for processing this send self.forward(self.owner, "Payout failed".asComment(), false, null); }}