• 全国 [切换]
  • 二维码
    微商筹货网

    手机WAP版

    手机也能找商机,信息同步6大终端平台!

    微信小程序

    微信公众号

    当前位置: 首页 » 行业新闻 » 热点新闻 » 正文

    uni-app eth/bsc dapp转账

    放大字体  缩小字体 发布日期:2024-11-08 02:56:31   浏览次数:12  发布人:90fa****  IP:124.223.189***  评论:0
    导读

    使用uni-app 开发eth/bsc dapp. 1.安装web3.js npm install web32.在页面中使用 <template><view><h1>钱包地址:{{ walletAddress }}</h1><!-- --><button @click="tokenTransfer()"> 代币转账</button><button @click="transfer()&

    使用uni-app 开发eth/bsc dapp.

    1.安装web3.js

    npm install web3

    2.在页面中使用

    <template> <view> <h1>钱包地址:{{ walletAddress }}</h1> <!-- --> <button @click="tokenTransfer()"> 代币转账</button> <button @click="transfer()"> ETH/BNB转账 </button> </view> </template> <script> import Web3 from 'web3'; // 合约ABI基本上可以通用 const contractAbi = [{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"guy","type":"address"},{"name":"wad","type":"uint256"}],"name":"approve","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"src","type":"address"},{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transferFrom","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[{"name":"wad","type":"uint256"}],"name":"withdraw","outputs":[],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint8"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"}],"name":"balanceOf","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"constant":false,"inputs":[{"name":"dst","type":"address"},{"name":"wad","type":"uint256"}],"name":"transfer","outputs":[{"name":"","type":"bool"}],"payable":false,"stateMutability":"nonpayable","type":"function"},{"constant":false,"inputs":[],"name":"deposit","outputs":[],"payable":true,"stateMutability":"payable","type":"function"},{"constant":true,"inputs":[{"name":"","type":"address"},{"name":"","type":"address"}],"name":"allowance","outputs":[{"name":"","type":"uint256"}],"payable":false,"stateMutability":"view","type":"function"},{"payable":true,"stateMutability":"payable","type":"fallback"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"guy","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Transfer","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"dst","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Deposit","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"src","type":"address"},{"indexed":false,"name":"wad","type":"uint256"}],"name":"Withdrawal","type":"event"}]; export default { data() { return { walletAddress:'', // 当前用户钱包地址 }; }, onLoad() { //自动检测钱包 // console.log(window.web3) if (window.ethereum) { window.ethereum.enable().then((res) => { //alert("当前钱包地址:"+res[0]) this.walletAddress = res[0]; console.log(this.walletAddress) }) }else{ alert("请安装metaMask钱包") } }, methods:{ // 自己发行的代币转账

    // 代币转账 async tokenTransfer(count){ //ETH转账 //合约地址,发布之后在以太坊上生成的合约地址 let contractAddr = "0x510B7E4966758615F5AA4fc497Ebb85684519173"; // bsc-usdt合约地址 let fromAddress = this.walletAddress; // fromAddress let toAddress = '0x417F36b1A773d7D602A56b9fBB5353E6A07cCafD' // toAddress //7、根据abi获取合约 let web3 = new Web3(window.web3.currentProvider); // 当前网络节点 var contract = new web3.eth.Contract(contractAbi, contractAddr); console.log(contract,'合约方法') contract.methods.balanceOf(fromAddress).call().then((err,result)=> { console.log(err,'当前代币余额') }) // 获取精度 let decimals = await contract.methods.decimals().call().then((res,err)=>{ console.log(res) return res; }); // 转账数量 const amountInWei = web3.utils.toWei(count.toString(), 'ether'); //console.log(amountInWei) web3.eth.getAccounts((err, accounts)=> { if (accounts.length == 0) { alert('Please check if the wallet is unlocked'); return; } }); let gasPrice = await web3.eth.getGasPrice() var gs = ((gasPrice * 80000) / (10 ** 18)).toFixed(6) // 代币转账 transfer: 合约方法名 contract.methods.transfer(toAddress, amountInWei).send({ from:fromAddress, gas:81000, gasPrice, },(err,res)=>{ console.log(err,res) if(!err){ console.log('转账hash',res) }else{ console.log('转账失败') } }).then((result)=>{ console.log('转账结束',result.transactionHash) }).catch(err=>{ console.log('转账取消') }) },

    //BNB转账 transfer(){ let web3 = new Web3(window.web3.currentProvider); console.log(web3,'web3 对象') // let fromAddress = web3.eth.getAccounts[0]; let amount = 0.01; const amountInWei = web3.utils.toWei(amount.toString(), 'ether'); let toAddress = "0xeA66e8573e9D03320718AB858B5713686860bE99"; web3.eth.sendTransaction({ gas: 21000, gasPrice: 5000000000, from: this.walletAddress, to: toAddress, value: amountInWei }, (err, result) => { console.log("转账Hash=",result) }) },

    } } </script> <style lang="less"> </style>

     
    (文/匿名(若涉版权问题请联系我们核实发布者) / 非法信息举报 / 删稿)
    打赏
    免责声明
    • 
    本文为昵称为 90fa**** 发布的作品,本文仅代表发布者个人观点,本站未对其内容进行核实,请读者仅做参考,如若文中涉及有违公德、触犯法律的内容,一经发现,立即删除,发布者需自行承担相应责任。涉及到版权或其他问题,请及时联系我们154208694@qq.com删除,我们积极做(权利人与发布者之间的调停者)中立处理。郑重说明:不 违规举报 视为放弃权利,本站不承担任何责任!
    有个别老鼠屎以营利为目的遇到侵权情况但不联系本站或自己发布违规信息然后直接向本站索取高额赔偿等情况,本站一概以诈骗报警处理,曾经有1例诈骗分子已经绳之以法,本站本着公平公正的原则,若遇 违规举报 我们100%在3个工作日内处理!
    0相关评论
     

    (c)2008-现在 chouhuo.com All Rights Reserved.