请教下 web3.js 转换代币出现问题
发布于 1 个月前 作者 xzduanerlei 220 次浏览 来自 以太坊

我写的demo ,智能合约 已发布的私链 "use strict" const fs = require(‘fs’) const path = require(‘path’) const util = require(‘util’) const Web3 = require(‘web3’) ~(async () => { const web3 = new Web3(new Web3.providers.HttpProvider(“http://localhost:8545”)) util.log(“执行中开始…”) const abi = fs.readFileSync(‘sol/token.abi’, ‘utf-8’); const accountsList = await web3.eth.getAccounts(); const deployAddr = accountsList[1];

const contractAddress = '0x1365B714A2859d167dD0B9174c617887778FE8B9';

const coinbase = await web3.eth.getCoinbase();

await web3.eth.personal.unlockAccount(coinbase, "xxx");
await web3.eth.personal.unlockAccount(accountsList[2], "xxx");

const block1 = await web3.eth.getBalance(accountsList[1]).then();
util.log(accountsList[1]+"账户余额:"+block1);
const block2 = await web3.eth.getBalance(accountsList[2]).then();
util.log(accountsList[2]+"账户余额:"+block2);

const contract = new web3.eth.Contract(JSON.parse(abi), contractAddress,{from:accountsList[2],gas:160000});

contract.methods.transfer(accountsList[0],1).send().then(console.log).catch(console.error);
       // contract.methods.transferFrom(accountsList[2],accountsList[3],1).send().then(console.log);
// contract.methods.balanceOf(accountsList[0]).call().then(console.log);
// contract.methods.balanceOf(accountsList[1]).call().then(console.log);
// contract.methods.balanceOf(accountsList[2]).call().then(console.log);
// contract.methods.balanceOf(accountsList[3]).call().then(console.log);

})();

这个方法
contract.methods.transfer(accountsList[0],1).send().then(console.log).catch(console.error); 代币数大于1 会报错

Jun 15:01:41 - 执行中开始… 20 Jun 15:01:43 - 0x954232aab79042a052ac783aA772e0EA042Cf24e账户余额:40702990 20 Jun 15:01:43 - 0xaF420dd03D5F57BCaec77B0eae1c7ac826D04BAb账户余额:999999999999962152079 Error: Transaction ran out of gas. Please provide more gas: { “blockHash”: “0x8cf63b5a64646e9927a1090aab4643a35e3cb9c753b1e38c8bbf2bff69291181”, “blockNumber”: 108, “contractAddress”: null, “cumulativeGasUsed”: 60000, “from”: “0xaf420dd03d5f57bcaec77b0eae1c7ac826d04bab”, “gasUsed”: 60000, “logsBloom”: “0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000”, “status”: false, “to”: “0x1365b714a2859d167dd0b9174c617887778fe8b9”, “transactionHash”: “0x92a88bae91f3210954aeeff1b4cdd5427ca55f554e312b65ff5abede2e4f7d6d”, “transactionIndex”: 0, “events”: {} } at /Users/duanerlei/apicloudstudio/web3js1.0/node_modules/web3-core-method/src/index.js:370:33 at <anonymous> at process._tickCallback (internal/process/next_tick.js:118:7) 但是如果是1的话就可以执行成功

20 Jun 15:01:00 - 执行中开始… 20 Jun 15:01:02 - 0x954232aab79042a052ac783aA772e0EA042Cf24e账户余额:40649242 20 Jun 15:01:02 - 0xaF420dd03D5F57BCaec77B0eae1c7ac826D04BAb账户余额:999999999999962205827 0 0 0 10000000 { blockHash: ‘0xa047efb15a2de379550668d3916d49f796d82e27a9409df02e1e2cc06cb15690’, blockNumber: 107, contractAddress: null, cumulativeGasUsed: 53748, from: ‘0xaf420dd03d5f57bcaec77b0eae1c7ac826d04bab’, gasUsed: 53748, logsBloom: ‘0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000008000000000000000000100000000000000000000000000100000000000000000000000000000000000000200000000010000000000000000000000000000000000000000000000000000000000001000000000000000000000000800000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000002000200000000000000000000000000000000000000000000000000000000000000001000000000000000’, status: true, to: ‘0x1365b714a2859d167dd0b9174c617887778fe8b9’, transactionHash: ‘0xb9ab3cfa58b3cc14dd575c53c9b987ecab9051de1f1f9d225627ff6e0f133061’, transactionIndex: 0, events: { Transfer: { address: ‘0x1365B714A2859d167dD0B9174c617887778FE8B9’, blockNumber: 107, transactionHash: ‘0xb9ab3cfa58b3cc14dd575c53c9b987ecab9051de1f1f9d225627ff6e0f133061’, transactionIndex: 0, blockHash: ‘0xa047efb15a2de379550668d3916d49f796d82e27a9409df02e1e2cc06cb15690’, logIndex: 0, removed: false, id: ‘log_dba4f538’, returnValues: [Result], event: ‘Transfer’, signature: ‘0xddf252ad1be2c89b69c2b068fc378daa952ba7f163c4a11628f55a4df523b3ef’, raw: [Object] } } }

回到顶部