以太坊ERC20代币合约案例之web3调用代码如下所示:
//1.1 导入 编译好的 合约的 字节代码 和 abi const path = require('path'); const fs = require('fs'); //1.2 合约编译后的文件路径 const bytecodeFilePath = path.resolve(__dirname,'./TokenERC20.bytecode'); const bytecodeJsonStr = fs.readFileSync(bytecodeFilePath,'utf-8'); const byteCodeJsonObj = JSON.parse(bytecodeJsonStr); //const {bytecode,interface} = require('./compilCaiPiao'); const bytecode = byteCodeJsonObj.bytecode; const interface = byteCodeJsonObj.interface; //2.导入 hd钱包provider const HDWalletProvider = require("truffle-hdwallet-provider"); //3.助记词(相当于是我们的私钥) const mnemonic = "jar ... cat beef"; // 12 word mnemonic //4.创建 provider,可以用来访问 以太坊真实网络节点 const provider = new HDWalletProvider(mnemonic, "https://rinkeby.infura.io/v3/3a60f2b160....",1);//最后的0 是获取 助记词 的第1个地址 //5.创建web对象 const Web3 = require('web3'); const web3= new Web3(provider); async function main(){ console.log('开始与以太网交互......'); const usrAdr = await web3.eth.getAccounts();//0x6c57CD10B4384C605aC748937b6cC1dF6C8eddA9 web3.eth.defaultAccount = usrAdr[0]; console.log('当前调用者的地址:' + web3.eth.defaultAccount); //6.部署合约到 以太网节点 //let contractObj =await deployContract(); //7.调用合约 //7.0 创建 远程智能合约 const contractObj = await new web3.eth.Contract(JSON.parse(interface),'0x46495b091cd3Fcb789cC336c3B5e9041E28555b0'); console.log('获取【合约】对象成功!'); // // 7.1 获取指定地址余额 await getBalanceAt(contractObj,'0x85BCc0F34718e80c332d41C513B72f8640B05249'); //await transferTo(contractObj,'0x83E9e99B7f5018680930baE0C7801555C850D9C5',10000000); //await approveTo(contractObj,'0x83E9e99B7f5018680930baE0C7801555C850D9C5',1000000000); await allowanceAt(contractObj,'0x85BCc0F34718e80c332d41C513B72f8640B05249','0x83E9e99B7f5018680930baE0C7801555C850D9C5'); await transferFrom(contractObj,'0x85BCc0F34718e80c332d41C513B72f8640B05249','0x737Df786f6e86625258960970c6752Fd7926F752',1); await allowanceAt(contractObj,'0x85BCc0F34718e80c332d41C513B72f8640B05249','0x83E9e99B7f5018680930baE0C7801555C850D9C5'); //await getBalanceAt(contractObj,'0x737Df786f6e86625258960970c6752Fd7926F752'); // //7.2 显示合约账户余额 // await showContracMoney(contractObj); // //查看调用者购买的号码 // await showInvokerLuckNum(contractObj); // 7.3 开奖+ // await withdrawLottery(contractObj); // 7.4 显示买家账户列表 // await showUsrList(contractObj); // await showContracMoney(contractObj); // 7.5 重置数据 //await resetContract(contractObj); //await showManageAddress(contractObj); //await killContract(contractObj); console.log('结束!'); } //启动 main(); // 1.部署合约 async function deployContract() { console.log('开始部署合约......'); let contractObj = await new web3.eth.Contract(JSON.parse(interface)) .deploy({ data: bytecode,//TokenERC20(uint256 initialSupply, string tokenName, string tokenSymbol) arguments: ['1000000000','梦想零钱','DreamCoin'] }).send({ from:web3.eth.defaultAccount, gas:'1000000' }); console.log('部署成功,合约地址:【'+contractObj.options.address+'】'); return contractObj; } // 2.查询余额 async function getBalanceAt(contractObj,usrAdr) { let usrMoney = await contractObj.methods.balanceOf(usrAdr).call(); console.log('地址【'+usrAdr+'】余额:'+usrMoney); } //3.将当前调用者的钱 转 money 金额给 usrAdrTo async function transferTo(contractObj,usrAdrTo, money) { let result = await contractObj.methods.transfer(usrAdrTo,money).send({ from:web3.eth.defaultAccount, gas:'1000000' }); console.log('【'+web3.eth.defaultAccount+'】向【'+usrAdrTo+'】转账【'+money+'】完毕'); console.log('转账完毕~记录如下:') console.log(result); } //4.授权(当前调用者授权给 spenderAdr 操作数额为 money 的代币) async function approveTo(contractObj,spenderAdr, money){ let result = await contractObj.methods.approve(spenderAdr,money).send({ from:web3.eth.defaultAccount, gas:'1000000' }); console.log('【'+web3.eth.defaultAccount+'】向【'+spenderAdr+'】授权【'+money+'】完毕!'); //console.log(result); } //5.查询授权 async function allowanceAt(contractObj,ownerAdr,spenderAdr) { let result = await contractObj.methods.allowance(ownerAdr,spenderAdr).call(); console.log('查询:【'+ownerAdr+'】给【'+spenderAdr+'】剩余授权余额为:'+result); } //6.转账(汇款人地址,收款人地址,金额) async function transferFrom(contractObj,usrAdrFrom, usrAdrTo, money) { let result = await contractObj.methods.transferFrom(usrAdrFrom,usrAdrTo,money).send({ from:web3.eth.defaultAccount, gas:'1000000' }); console.log('【'+usrAdrFrom+'】向【'+usrAdrTo+'】转账【'+money+'】完毕'); //console.log(result); }
猜你喜欢 沐佛节是哪个少数民族的节日 沐佛节是哪个族的特色节日 皮鞋怎么去褶皱 皮鞋如何去除褶皱 电热毯棉的好还是绒的好 电热毯用什么绒的好 咸阳昭陵博物馆自驾路线 咸阳昭陵博物馆自驾路线图 熟透的菠萝蜜什么颜色 吃菠萝蜜有什么禁忌 怎么做家常炒板栗 炒板栗家常做法 买石榴怎么挑选 买石榴怎么挑选才好吃