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

越简单越好,能在tp或者其他钱包里调起bep20的usdt转账 可以有偿可以私聊

请先 登录 后评论

最佳答案 2024-03-14 16:40

<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>
请先 登录 后评论

其它 2 个回答

Tiny熊
  擅长:智能合约,以太坊
请先 登录 后评论
用户_18975
请先 登录 后评论
  • 2 关注
  • 0 收藏,1273 浏览
  • 用户_18975 提出于 2024-03-13 21:29