ton 透過queryId關聯交易

若-飞發表於2024-10-24

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();

鏈上檢視:

ton 透過queryId關聯交易

相關文章