准备消息
在使用TON Connect时,您应该为在各种交易中使用的Payload构造消息体。在此页面上,您可以找到与TON Connect SDK一起使用的payload的最相关示例。
期望您学习了创建TON Connect连接的基础知识。了解更多请参阅集成手册。
TON Connect JS SDK 示例
在深入研究构建消息之前,我们先来介绍一下 cell 的概念,消息体就是由 cell 组成的。
什么是 cell ?
cell 是 TON 区块链中的基本数据结构。它可以存储多达 1023
位的数据,并持有多达 4
个指向其他 cell 的引用,从而允许您存储更复杂的数据结构。
像 @ton/core
和 @ton-community/assets-sdk
这样的库提供了处理 cell 的有效方法。
您可以在 此处 了解有关 cell 的更多信息。
创建 cell
要创建 cell ,需要使用 beginCell()
函数。当 cell "打开" 时,您可以使用 store...()
函数存储各种数据类型。
完成后,使用 endCell()
函数关闭 cell 。
import { Address, beginCell } from "@ton/ton";
const cell = beginCell()
.storeUint(99, 64) // Stores uint 99 in 64 bits
.storeAddress(Address.parse('[SOME_ADDR]')) // Stores an address
.storeCoins(123) // Stores 123 as coins
.endCell() // Closes the cell
解析 cell
要从 cell 中读取或解析数据,需要调用 beginParse()
函数。使用类似的load...()
函数,读取数据的顺序与存储数据的顺序相同:
const slice = cell.beginParse();
const uint = slice.loadUint(64);
const address = slice.loadAddress();
const coins = slice.loadCoins();
数据量更大
每个 cell 都有 1023 位的限制。如果超过上限,就会发生错误:
// This will fail due to overflow
const cell = beginCell()
.storeUint(1, 256)
.storeUint(2, 256)
.storeUint(3, 256)
.storeUint(4, 256) // Exceeds 1023-bit limit (256 + 256 + 256 + 256 = 1024)
.endCell()
使用TON Connect JS SDK执行常规TON转账如下所示:
const cell = beginCell()
.storeUint(1, 256)
.storeUint(2, 256)
.storeRef(beginCell()
.storeUint(3, 256)
.storeUint(4, 256)
.endCell())
.endCell()
要加载引用(嵌套) cell , 请使用 loadRef()
:
const slice = cell.beginParse();
const uint1 = slice.loadUint(256);
const uint2 = slice.loadUint(256);
const innerSlice = slice.loadRef().beginParse(); // Load and parse nested cell
const uint3 = innerSlice.loadUint(256);
const uint4 = innerSlice.loadUint(256);
可选引用和值
cell 可以存储可选值(可能为空)。这些值使用 storeMaybe...()
函数存储:
const cell = beginCell()
.storeMaybeInt(null, 64) // Optionally stores an int
.storeMaybeInt(1, 64)
.storeMaybeRef(null) // Optionally stores a reference
.storeMaybeRef(beginCell()
.storeCoins(123)
.endCell());
您可以使用相应的 loadMaybe...() 函数解析可选值。返回值可以为空,因此不要忘记检查它们是否为空!
const slice = cell.beginParse();
const maybeInt = slice.loadMaybeUint(64);
const maybeInt1 = slice.loadMaybeUint(64);
const maybeRef = slice.loadMaybeRef();
const maybeRef1 = slice.loadMaybeRef();
if (maybeRef1) {
const coins = maybeRef1.beginParse().loadCoins();
}
使用 assets sdk 实现更简单的序列化和反序列化
手动处理 cell 可能很繁琐,因此 @ton-community/assets-sdk
提供了序列化和反序列化信息的便捷方法。
对于特定的自定义交易,必须定义特定的载荷。
- @ton-community/assets-sdk
- @ton/ton
import {Address, beginCell} from "@ton/core";
import {storeJettonTransferMessage, loadJettonTransferMessage} from "@ton-community/assets-sdk";
// serialization
const cell = beginCell()
.store(storeJettonTransferMessage({
queryId: 42n,
amount: 100n,
destination: Address.parse('[DESTINATION]'),
responseDestination: Address.parse('[RESPONSE_DESTINATION]'),
customPayload: null,
forwardAmount: 1n,
forwardPayload: null,
}))
.endCell()
// deserialization
const transferMessage = loadJettonTransferMessage(cell.beginParse());
import {Address, beginCell} from "@ton/core";
// serialization
const cell = beginCell()
.storeUint(260734629, 32)
.storeUint(42, 64)
.storeCoins(100)
.storeAddress(Address.parse('[DESTINATION]'))
.storeAddress(Address.parse('[RESPONSE_DESTINATION]'))
.storeMaybeRef(null)
.storeCoins(1)
.storeMaybeRef(null)
.endCell();
// deserialization
const slice = cell.beginParse();
const op = slice.loadUint(32);
const queryId = slice.loadUint(64);
const amount = slice.loadCoins();
const destination = slice.loadAddress();
const responseDestination = slice.loadAddress();
const customPayload = slice.loadMaybeRef();
const fwdAmount = slice.loadCoins();
const fwdPayload = slice.loadMaybeRef();
const transferMessage = { op, queryId, amount, destination, responseDestination, customPayload, fwdAmount, fwdPayload };
TON Connect JS SDK 示例
交易模板
无论开发者解决的是哪个层级的任务,通常都需要使用来自 @tonconnect/sdk
或 @tonconnect/ui
的连接器实体。
以下是基于 @tonconnect/sdk
和 @tonconnect/ui
创建的示例:
- @tonconnect/ui-react
- @tonconnect/ui
- @tonconnect/sdk
import { useTonConnectUI } from '@tonconnect/ui-react';
const transaction = {
//transaction body
}
export const Settings = () => {
const [tonConnectUI, setOptions] = useTonConnectUI();
return (
<div>
<button onClick={() => tonConnectUI.sendTransaction(transaction)}>
Send transaction
</button>
</div>
);
};
import TonConnectUI from '@tonconnect/ui';
const tonConnectUI = new TonConnectUI({ //connect application
manifestUrl: 'https://<YOUR_APP_URL>/tonconnect-manifest.json',
buttonRootId: '<YOUR_CONNECT_BUTTON_ANCHOR_ID>'
});
const transaction = {
//transaction body
}
const result = await tonConnectUI.sendTransaction(transaction)
import TonConnect from '@tonconnect/sdk';
const connector = new TonConnect();
await connector.sendTransaction({
//transaction body
})
常规 TON 转账
TON Connect SDK提供了发送消息的封装器,使准备两个钱包之间的Toncoin的常规转账作为默认交易无需载荷变得容易。
使用TON Connect JS SDK执行常规TON转账如下所示:
- @tonconnect/react-ui
- @tonconnect/ui
- @tonconnect/sdk
import { useTonConnectUI } from '@tonconnect/ui-react';
const [tonConnectUI] = useTonConnectUI();
const transaction = {
messages: [
{
address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F", // destination address
amount: "20000000" //Toncoin in nanotons
}
]
}
export const Settings = () => {
const [tonConnectUI, setOptions] = useTonConnectUI();
return (
<div>
<button onClick={() => tonConnectUI.sendTransaction(transaction)}>
Send transaction
</button>
</div>
);
};
import TonConnectUI from '@tonconnect/ui';
const tonConnectUI = new TonConnectUI({ //connect application
manifestUrl: 'https://<YOUR_APP_URL>/tonconnect-manifest.json',
buttonRootId: '<YOUR_CONNECT_BUTTON_ANCHOR_ID>'
});
const transaction = {
messages: [
{
address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F", // destination address
amount: "20000000" //Toncoin in nanotons
}
]
}
const result = await tonConnectUI.sendTransaction(transaction)
import TonConnect from '@tonconnect/sdk';
const connector = new TonConnect();
await connector.sendTransaction({
messages: [
{
address: "0:412410771DA82CBA306A55FA9E0D43C9D245E38133CB58F1457DFB8D5CD8892F", // destination address
amount: "20000000" //Toncoin in nanotons
}
]
})
了解有关 TON 智能合约地址 的更多信息。
对于特定的自定义交易,必须定义特定的载荷。