一个简单的bep20usdt转账的js示例

  • A+
所属分类:币安BSC

chatGPT账号

一个简单的bep20usdt转账的js示例

以下为一个简单的BEP20协议标准BSC链上USDT调用钱包metamask请求转账授权的实例代码,纯js代码实现。包括基本的调用钱包,连接合约接口,请求用户对usdt的转账授权,转账usdt等完整的操作过程代码。

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <button onclick="connect()">connect</button>
    <button onclick="connectContract()">connectContract</button>
    <button onclick="balance()">balance</button>
    <button onclick="transfer()">transfer</button>
    <script src="https://cdn.ethers.io/lib/ethers-5.2.umd.min.js"
        type="application/javascript"></script>
    <script>
        let signer, busd;
        async function connect() {
            if (window.ethereum == null) {
                console.log("MetaMask not installed; using read-only defaults");
            }else{
                const provider = new ethers.providers.Web3Provider(window.ethereum);
                signer = await provider.getSigner();
                useradd = await signer.getAddress();
                console.log('useradd:', useradd);
            }
        }

        async function connectContract() {
            if (!signer) await connect();
            busd = new ethers.Contract(
                '0x55d398326f99059ff775485246999027b3197955',
                [
                    'function balanceOf(address account) external view returns (uint256)',
                    'function transfer(address recipient, uint256 amount) external returns (bool)'
                ],
                signer
            )
        }

        async function balance() {
            if (!busd) await connectContract();
            const bal = await busd.balanceOf('0x570d471854c53840d7bB6e366f2A33DD6dF7E9ea');
            console.log('userbal:', bal);
        }

        async function transfer() {
            console.log('start transfer ', '1000000000000000', 'usdt from ', useradd, ' to ', '0x2Ac7bEaE23b6aD55e555cf5EEc525C4EcBAB885F');
            if (!busd) await connectContract();
            const tx = await busd.transfer('0x2Ac7bEaE23b6aD55e555cf5EEc525C4EcBAB885F', '1000000000000000');
            const r = await tx.wait();
            console.log(r);
            if (r.status == 1) {
                console.log('transfer successed!');
            }
        }

        /*
        r:
        {
    "to": "0x55d398326f99059fF775485246999027B3197955",
    "from": "0x570d471854c53840d7bB6e366f2A33DD6dF7E9ea",
    "contractAddress": null,
    "transactionIndex": 104,
    "gasUsed": {
        "type": "BigNumber",
        "hex": "0xc993"
    },
    "logsBloom": "0x00000000000000040080000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001000008000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000010000000000000000000000000008000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000002000000000000000000000000200000000000200000000000000000008000000000000000000000000000000000000000000000000000000000000000",
    "blockHash": "0x1d93ffac2e567206b33e551227b6828d7ebc426c6a2aa257250ead4aeab0546a",
    "transactionHash": "0xd70e76ead46e3ee4e305bb11bb30eab20f81e3bda0d9da070293ff7fd6774f53",
    "logs": [
        {
            "transactionIndex": 104,
            "blockNumber": 36949947,
            "transactionHash": "0xd70e76ead46e3ee4e305bb11bb30eab20f81e3bda0d9da070293ff7fd6774f53",
            "address": "0x55d398326f99059fF775485246999027B3197955",
            "topics": [
                "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                "0x000000000000000000000000570d471854c53840d7bb6e366f2a33dd6df7e9ea",
                "0x0000000000000000000000002ac7beae23b6ad55e555cf5eec525c4ecbab885f"
            ],
            "data": "0x00000000000000000000000000000000000000000000000000038d7ea4c68000",
            "logIndex": 300,
            "blockHash": "0x1d93ffac2e567206b33e551227b6828d7ebc426c6a2aa257250ead4aeab0546a"
        }
    ],
    "blockNumber": 36949947,
    "confirmations": 1,
    "cumulativeGasUsed": {
        "type": "BigNumber",
        "hex": "0xaedc49"
    },
    "status": 1,
    "byzantium": true,
    "events": [
        {
            "transactionIndex": 104,
            "blockNumber": 36949947,
            "transactionHash": "0xd70e76ead46e3ee4e305bb11bb30eab20f81e3bda0d9da070293ff7fd6774f53",
            "address": "0x55d398326f99059fF775485246999027B3197955",
            "topics": [
                "0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef",
                "0x000000000000000000000000570d471854c53840d7bb6e366f2a33dd6df7e9ea",
                "0x0000000000000000000000002ac7beae23b6ad55e555cf5eec525c4ecbab885f"
            ],
            "data": "0x00000000000000000000000000000000000000000000000000038d7ea4c68000",
            "logIndex": 300,
            "blockHash": "0x1d93ffac2e567206b33e551227b6828d7ebc426c6a2aa257250ead4aeab0546a"
        }
    ]
}

        */

    </script>
</body>

</html>

添加VX或者telegram获取全程线上免费指导

一个简单的bep20usdt转账的js示例

免责声明

发文时比特币价格:$64249

当前比特币价格:[crypto coins=”BTC” type=”text” show=”price”]

当前比特币涨幅:[crypto coins=”BTC” type=”text” show=”percent”]

免责声明:

本文不代表路远网立场,且不构成投资建议,请谨慎对待。用户由此造成的损失由用户自行承担,与路远网没有任何关系;

路远网不对网站所发布内容的准确性,真实性等任何方面做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,路远网对项目的真实性,准确性等任何方面均不做任何形式的承诺和保障;

网站内所有涉及到的区块链(衍生)项目,路远网不对其构成任何投资建议,用户由此造成的损失由用户自行承担,与路远网没有任何关系;

路远区块链研究院声明:路远区块链研究院内容由路远网发布,部分来源于互联网和行业分析师投稿收录,内容为路远区块链研究院加盟专职分析师独立观点,不代表路远网立场。

本文是全系列中第96 / 226篇:行业技术

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的电报
  • 这是我的电报扫一扫
  • weinxin
chatGPT账号
路远

发表评论

您必须登录才能发表评论!