常见问题
最常用的内置 [全局静态函数] 列表(/book/functions#global-static-functions)。
上下文
now
fun now(): Int
返回当前 Unix 时间。
用法示例:
let timeOffset: Int = now() + 1000; // thousand seconds from now()
myBalance
fun myBalance(): Int;
返回当前交易的 计算阶段 开始时智能合约的 nano Toncoins 余额。
用法示例:
let iNeedADolla: Int = myBalance();
myAddress
fun myAddress(): Address;
以Address
的形式返回当前智能合约的地址。
用法示例:
let meMyselfAndI: Address = myAddress();
sender
fun sender(): Address;
返回当前信息发件人的 Address
。
用法示例:
contract MeSee { receive() { let whoSentMeMessage: Address = sender(); }}
context
fun context(): Context;
返回 Context
Struct,包含:
字段 | 类型 | 描述 |
---|---|---|
bounced | Bool | 传入消息的Bounced 标志. |
sender | Address | 发送方在 TON 区块链上的内部地址。 |
value | Int | 信息中 nanoToncoins 的数量。 |
raw | Slice | 信息的其余部分作为 Slice 。 它遵循 TON 的 内部消息布局,从目标 Address (dest:MsgAddressInt 在 TL-B 记法) 开始。 |
示例用法:
let ctx: Context = context();require(ctx.value != 68 + 1, "Invalid amount of nanoToncoins, bye!");
Addressing
newAddress
fun newAddress(chain: Int, hash: Int): Address;
根据chain
id和SHA-256编码的hash
值创建一个新的Address
。
此函数试图尽可能解析 compile-time 中的常数值。
示例用法:
let oldTonFoundationAddr: Address = newAddress(0, 0x83dfd552e63729b472fcbcc8c45ebcc6691702558b68ec7527e1ba403a0f31a8); // ↑ ------------------------------------------------------------------ // | ↑ // | sha-256 hash of contract's init package (StateInit) // chain id: 0 is a workchain, -1 is a masterchain
contractAddress
fun contractAddress(s: StateInit): Address;
根据智能合约的 StateInit
,计算智能合约在工作链 中的 Address
。
示例用法:
let foundMeSome: Address = contractAddress(initOf SomeContract());
contractAddressExt
fun contractAddressExt(chain: Int, code: Cell, data: Cell): Address;
根据 chain
id、合约 code
和合约初始状态 data
计算智能合约的 Address
。 使用 initOf
表达式获取给定合约的初始 code
和初始 data
。
用法示例:
let initPkg: StateInit = initOf SomeContract();let hereBeDragons: Address = contractAddressExt(0, initPkg.code, initPkg.data);
通信
send
fun send(params: SendParameters);
排序消息,以便使用 SendParameters
结构体 发送。
示例用法:
send(SendParameters{ to: sender(), // back to the sender, value: ton("1"), // with 1 Toncoin (1_000_000_000 nanoToncoin), // and no message body});
emit
fun emit(body: Cell);
将消息排序的 body
发送到外部世界,目的是进行日志记录并在链外进行后续分析。 该信息没有收件人,与使用 Tact 的其他信息发送功能相比更省 gas。
使用示例
emit("Catch me if you can, Mr. Holmes".asComment()); // asComment() converts a String to a Cell