You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

通过API将TON Crystals从Tonkeeper App转至Huobi的JavaScript实现

TON从Tonkeeper到Huobi转账分步实现(JavaScript)

一、获取Huobi的TON充值地址

要完成转账,首先得拿到Huobi平台的TON专属充值地址。你需要先在Huobi后台创建带充值权限的API密钥,然后调用官方接口获取地址。

代码片段:

const crypto = require('crypto'); // Node.js环境,浏览器环境需替换为Web Crypto API

// 替换为你的Huobi API密钥
const API_KEY = '你的Huobi API Key';
const API_SECRET = '你的Huobi API Secret';

// 生成Huobi API所需的签名
function generateHuobiSignature(params, secret) {
    const sortedKeys = Object.keys(params).sort();
    const queryString = sortedKeys.map(key => `${key}=${encodeURIComponent(params[key])}`).join('&');
    return crypto.createHmac('sha256', secret).update(queryString).digest('hex');
}

// 获取TON充值地址(含memo,部分场景必填)
async function getHuobiTONDepositAddress() {
    const params = {
        accessKey: API_KEY,
        currency: 'ton',
        timestamp: Date.now(),
        signatureMethod: 'HmacSHA256',
        signatureVersion: '2'
    };
    params.signature = generateHuobiSignature(params, API_SECRET);

    const response = await fetch(`https://api.huobi.pro/v1/account/deposit/address?${new URLSearchParams(params)}`, {
        method: 'GET',
        headers: { 'Content-Type': 'application/json' }
    });

    const data = await response.json();
    if (data.code === 200) {
        return data.data;
    } else {
        throw new Error(`Huobi API请求失败: ${data.msg}`);
    }
}

// 调用示例
getHuobiTONDepositAddress().then(info => {
    console.log('TON充值地址:', info.address);
    console.log('Memo(必填时使用):', info.memo);
}).catch(err => console.error(err));

二、通过Tonkeeper发起转账

Tonkeeper支持Ton Connect协议调用,推荐使用官方SDK @tonconnect/sdk 实现钱包交互,无需手动处理deeplink。

代码片段:

import { TonConnect } from '@tonconnect/sdk'; // 浏览器环境可通过npm安装或CDN引入

// 替换为你的Ton Connect清单文件地址(需托管在HTTPS域名下)
const manifestUrl = 'https://你的域名.com/tonconnect-manifest.json';

// 初始化Ton Connect实例
const tonConnect = new TonConnect({ manifestUrl });

// 向Huobi地址发起TON转账
async function sendTONToHuobi(recipient, amountTON, memo = '') {
    // 连接Tonkeeper钱包
    await tonConnect.connect();

    // 构建交易:TON转nanoTON(1TON = 1e9 nanoTON)
    const transaction = {
        validUntil: Math.floor(Date.now() / 1000) + 3600, // 交易1小时内有效
        messages: [
            {
                address: recipient,
                amount: (BigInt(amountTON) * BigInt(1e9)).toString(),
                ...(memo ? { comment: memo } : {})
            }
        ]
    };

    // 请求Tonkeeper签名并发送交易
    const result = await tonConnect.sendTransaction(transaction);

    if (result.transactionHash) {
        console.log('交易哈希:', result.transactionHash);
        return result.transactionHash;
    } else {
        throw new Error('Tonkeeper转账请求失败');
    }
}

// 调用示例(结合第一步的地址信息)
getHuobiTONDepositAddress().then(info => {
    sendTONToHuobi(info.address, '0.5', info.memo) // 转账0.5 TON
        .then(txHash => console.log('交易已提交,等待区块确认'))
        .catch(err => console.error(err));
}).catch(err => console.error(err));

Ton Connect清单文件示例(tonconnect-manifest.json):

{
  "url": "https://你的域名.com",
  "name": "你的应用名称",
  "iconUrl": "https://你的域名.com/app-icon.png"
}

三、验证Huobi到账状态

转账提交后,可通过Huobi API轮询充值记录,确认资金是否到账。

代码片段:

// 查询TON充值历史,验证到账状态
async function checkHuobiDepositStatus(txHash) {
    const params = {
        accessKey: API_KEY,
        currency: 'ton',
        timestamp: Date.now(),
        signatureMethod: 'HmacSHA256',
        signatureVersion: '2',
        txId: txHash
    };
    params.signature = generateHuobiSignature(params, API_SECRET);

    const response = await fetch(`https://api.huobi.pro/v1/account/deposit/history?${new URLSearchParams(params)}`, {
        method: 'GET',
        headers: { 'Content-Type': 'application/json' }
    });

    const data = await response.json();
    if (data.code === 200) {
        const record = data.data.find(item => item.txId === txHash);
        if (record) {
            console.log('当前充值状态:', record.status); // success表示到账
            return record.status === 'success';
        } else {
            console.log('暂未查询到记录,请等待区块确认');
            return false;
        }
    } else {
        throw new Error(`Huobi API查询失败: ${data.msg}`);
    }
}

// 轮询验证示例
sendTONToHuobi('...', '0.5', '...').then(txHash => {
    const checkInterval = setInterval(async () => {
        const isSuccess = await checkHuobiDepositStatus(txHash);
        if (isSuccess) {
            clearInterval(checkInterval);
            console.log('TON已成功到账Huobi');
        }
    }, 30000); // 每30秒查询一次
}).catch(err => console.error(err));

核心注意事项:

  • Ton Connect清单文件必须托管在HTTPS域名下,Tonkeeper会校验合法性。
  • 转账时务必将TON转换为nanoTON单位,否则会出现金额错误。
  • Huobi API密钥需妥善保管,避免泄露导致资产风险。
  • TON区块确认通常需要1-2分钟,Huobi平台到账可能额外需要审核时间。

内容的提问来源于stack exchange,提问作者iednaveef

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.12 22:37:08