ton利用多cell发送数据

若-飞 / 2024-10-23 / 原文

tact合约是这样:

import "@stdlib/deploy";
import "@stdlib/ownable";


contract StudyContract with Deployable {
  msg: String = "123";

  // Constructor
  init(){
    self.msg = "123"; // 初始化
  }

  // 接收字符串并更新 
  receive(newMsg: String) {
      self.msg = newMsg; // 更新状态
      self.reply(beginComment().concat(newMsg).toCell());
  }

  // 获取当前字符串
  get fun GetTestMsg() : String {
      return self.msg; 
  }
}

ts调用利用多cell的调用方式是这样:

describe('Contract Tests', () => {
    test('Send TestMsg By Pure string', async () => {
        const client4 = new TonClient4({
            endpoint: "https://sandbox-v4.tonhubapi.com", // Test-net
        });

        // Send TestMsg
        let TestMsg =  beginCell()
        .storeUint(0, 32) // 发送普通字符串
        .storeBuffer(Buffer.from(generateBitHex(1023/8 - 8, 0x11), "hex")) // 发送最大长度的字符串
        .storeRef(beginCell().storeBuffer(Buffer.from(generateBitHex(1023/8, 0x22), "hex")).endCell())
        .storeRef(beginCell().storeBuffer(Buffer.from(generateBitHex(1023/8, 0x33), "hex")).endCell())
        .storeRef(beginCell().storeBuffer(Buffer.from(generateBitHex(1023/8, 0x44), "hex")).endCell())
        .storeRef(beginCell().storeBuffer(Buffer.from(generateBitHex(1023/8, 0x55), "hex")).endCell())
        .endCell()

        let seqno: number = await wallet_contract.getSeqno()
        console.log("wallet_contract seqno is ",seqno);
        let result =await wallet_contract.sendTransfer({
                seqno: seqno,
                secretKey: keyPair.secretKey,
                messages: [
                    internal({
                        to: deployContract,
                        value: toNano("0.2"), // 给个0.2Ton作为gas费
                        init: null,
                        bounce: false,
                        body: TestMsg ,
                    }),
                ],
        });
        console.log("TestMsg sent, result: ", result);
    });
});

通过getTestMsg的合约方法查看数据:

b5ee9c72010205010002810004ee11111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111110102030400fe2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222200fe3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333300fe4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444400fe55555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555

实践证明多个cell就是自然拼接在一起的