简体   繁体   English

如何使用 truffle 框架将大量值转换为solidity?

[英]How to convert the value in big number in solidity using truffle framework?

I am having this error while testing my contract.我在测试我的合同时遇到了这个错误。 All of the tests are passing except the last one.除最后一项外,所有测试均通过。 I don't understand this error, please help.我不明白这个错误,请帮助。 the smart contract is created with truffle framework.智能合约是使用 truffle 框架创建的。

my lottery-contract sol file:我的彩票合同 sol 文件:

 // SPDX-License-Identifier: GPL-3.0 pragma solidity >= 0.5.2; contract Lottery{ address public manager; address[] public players; constructor()public{ manager=msg.sender; } function enter() public payable{ require(msg.value>0.01 ether); players.push(msg.sender); } function random() private view returns(uint){ return uint(keccak256(abi.encodePacked(block.difficulty,block.timestamp,players))); } function getBalance() public view returns(uint){ return address(this).balance; } function pickWinner() public restricted { address payable winner; uint index=random()% players.length; winner=payable(players[index]); winner.transfer(getBalance()); players=new address[](0); } modifier restricted(){ require(msg.sender==manager); _; } function getPlayers() public view returns(address[] memory){ return players; }}

lottery test file彩票测试文件

 const assert = require('assert'); const ganache = require('ganache-cli'); const Web3 = require('web3'); const web3 = new Web3(ganache.provider()); const Lottery = artifacts.require('Lottery'); contract('Lottery', (accounts) => { let lottery; beforeEach(async () => { lottery = await Lottery.new({ from: accounts[0] }); }); describe('Lottery Contract', () => { it('deploys a contract', () => { assert.ok(lottery.address); }); it('allows one acc to enter', async () => { await lottery.enter({ from: accounts[0], value: web3.utils.toWei('0.02', 'ether'), }); const players = await lottery.getPlayers({ from: accounts[0] }); assert.equal(accounts[0], players[0]); assert.equal(1, players.length); }); it('allows multiple acc to enter', async () => { await lottery.enter({ from: accounts[0], value: web3.utils.toWei('0.02', 'ether'), }); await lottery.enter({ from: accounts[1], value: web3.utils.toWei('0.02', 'ether'), }); await lottery.enter({ from: accounts[2], value: web3.utils.toWei('0.02', 'ether'), }); const players = await lottery.getPlayers({ from: accounts[0] }); assert.equal(accounts[0], players[0]); assert.equal(accounts[1], players[1]); assert.equal(accounts[2], players[2]); assert.equal(3, players.length); }); it('requires a min amount of ether to enter', async () => { try { await lottery.enter({ from: accounts[0], value: 0, }); assert(false); } catch (err) { assert(err); } }); it('only manager can call pickWinner', async () => { try { await lottery.pickWinner({ from: accounts[1], }); assert(false); } catch (err) { assert(err); } }); it('sends money to the winner and resets the players', async () => { await lottery.enter({ from: accounts[0], value: web3.utils.toWei('2', 'ether'), }); const initialBal = await web3.eth.getBalance(accounts[0]); await lottery.pickWinner({ from: accounts[0], }); const finalBal = await web3.eth.getBalance(accounts[0]); const difference = finalBal - initialBal; console.log(difference); assert(difference > web3.utils.toWei('1.8', 'ether')); }); }); });

I am having this error, please tell me how to do the conversion.我有这个错误,请告诉我如何进行转换。 It shows the last test is not passing, please help I am a beginner.它表明最后一个测试没有通过,请帮助我是初学者。 错误

I am a beginner too, but i guess you should use web3.utils.BN when receiving balances.我也是初学者,但我想你应该在接收余额时使用 web3.utils.BN。 More info here: about BN in Web3.js docs.更多信息在这里:关于 Web3.js 文档中的 BN。

Your truffle.config.js has no connection to ganache.您的 truffle.config.js 与 ganache 没有任何关系。

add this to your truffle-config.js将此添加到您的 truffle-config.js

networks: {
    development: {
      host: "127.0.0.1", 
      // default ganache gui listens to 7545
      port: 7545, 
      network_id: "*", 
    },

Make sure your ganache is running and run the test.确保您的 ganache 正在运行并运行测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM