ton 通过queryId关联交易

js发起交易代码:

async function sendTransaction(item, queryId) {
    // return false;
    // console.log('sendTransaction', item, address);
    const {
      beginCell,
      toNano,
      Address,
      TonClient,
      StateInit,
      storeStateInit,
      JettonMaster,
      Cell,
    } = await import("@ton/ton");
    const clienter = new TonClient({
      endpoint: import.meta.env.MODE === "production" ? "https://toncenter.com/api/v2/jsonRPC" : "https://testnet.toncenter.com/api/v2/jsonRPC",
    });
    const jettonMasterAddresser = Address.parse(coinAddress); // for example EQBlqsm144Dq6SjbPI4jjZvA1hqTIP3CvHovbIfW_t-SCALE
    const userAddress = Address.parse(address);
    const jettonMaster = clienter.open(
      JettonMaster.create(jettonMasterAddresser)
    );
    const walletAddress = await jettonMaster.getWalletAddress(userAddress);
    const jettonWalletContract = walletAddress.toString(true, true, true);
    const Wallet_DST = Address.parse(ownerAddress);
    const Wallet_SRC = Address.parse(address);
    // const queryId = Math.floor(Date.now() / 1000);
    // console.log(queryId);
    try {
      const body = beginCell()
        .storeUint(0xf8a7ea5, 32)
        .storeUint(queryId, 64)
        .storeCoins((Number(item.price) * Math.pow(10, Number(decimals))) / 100)
        .storeAddress(Wallet_DST)
        .storeAddress(Wallet_SRC)
        .storeUint(0, 1)
        .storeCoins(toNano(0.00000005))
        .storeUint(0, 1)
        .endCell();
      // console.log(body.toBoc().toString("base64"))
      const transaction = {
        validUntil: Math.floor(Date.now() / 1000) + 360,
        messages: [
          {
            address: jettonWalletContract,
            amount: "100000000",
            payload: body.toBoc().toString("base64"),
          },
        ],
      };
      // setRecharge(false);
      const result = await tonConnectUI.sendTransaction(transaction);
      if (result) {
        setStartTrading(false);
        if (result.boc) {
          const cell = Cell.fromBoc(Buffer.from(result.boc, "base64"))[0];
          const hash = cell.hash();
          if (hash) {
            setIsChargeOrderInfo(true);
            rechargelist.forEach((getItem) => {
              if (getItem.id === item.id) {
                getItem.is_loading = true;
              }
              if(getItem.star_price){
                getItem.increment_price_value = getItem.star_price;
              }
              if(getItem.gold_count){
                getItem.increment_gold_value = getItem.gold_count;
              }
            });
            setRechargelist([...rechargelist]);
            getChargeOrderInfo(queryId, hash.toString("hex"));
          }
        }
        // console.log('Transfer sent successfully', result.boc);
      } else {
        setStartTrading(false);
        console.log("No detailed result returned.");
      }
    } catch (error) {
      setStartTrading(false);
      console.error("USTD transfer failed", error);
    }
  }

的:

const body = beginCell()
        .storeUint(0xf8a7ea5, 32)
        .storeUint(queryId, 64)
        .storeCoins((Number(item.price) * Math.pow(10, Number(decimals))) / 100)
        .storeAddress(Wallet_DST)
        .storeAddress(Wallet_SRC)
        .storeUint(0, 1)
        .storeCoins(toNano(0.00000005))
        .storeUint(0, 1)
        .endCell();

 

链上查看:

posted @ 2024-10-24 10:45  若-飞  阅读(16)  评论(0编辑  收藏  举报