一个简单的bep20,usdt转账的html+js示例,用的是ethers!示例来源:B站的一个allthatblock阿婆主发布的ethers.js视频教程+自己整理和测试【ethers.js教程】https://www.bilibili.com/video/BV12Z4y1U7yr/?sh
一个简单的bep20,usdt转账的html+js示例,用的是ethers! 示例来源:B站的一个allthatblock阿婆主发布的ethers.js视频教程+自己整理和测试 【ethers.js教程】 https://www.bilibili.com/video/BV12Z4y1U7yr/?share_source=copy_web&vd_source=0bc2efa6137d3b6cf59b870dfec6d23e
<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, useradd;
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!');
}
}
/*
//console.log(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>
如果觉得我的文章对您有用,请随意打赏。你的支持将鼓励我继续创作!