为什么address.transfer()
的功能只能在我的合约中发送1ether?当值大于1或小于1时将报告错误。
我的代码:
pragma solidity ^0.4.24;
contract Lottery{
uint public winn;
//抽奖者
struct lottery{
uint money; //奖金
address name; //名字(地址)
}
//庄家地址
address private bankerAddress = 0x8b3234612c7D3b805F4c9CF1Aa55Cc47B82A0769;
//得奖者数量
uint32 public count;
//得奖者集合
lottery public l;
//初始化
constructor()public {
count=0;
}
event getRandom (
uint indexed _win);
//开始抽奖的函数,返回奖金
function start() public payable{
//随机数生成
uint win= uint(keccak256(now, msg.sender, now)) % 100;
win = win % 12 +1;
winn = win;
l.money = winn;
l.name = msg.sender;
//发奖金操作
msg.sender.transfer(winn*10000000000000000);
//监听事件
getRandom(winn);
}
function () public payable{}
}
下面是JavaScript代码:
lotteryInstance.start.sendTransaction({
from: '0x8b3234612c7D3b805F4c9CF1Aa55Cc47B82A0769',
value: 10000000000000000,
gas: 210000,
gasPrice: web3.toWei('1000', 'gwei')
});
错误消息:
MetaMask - RPC错误:错误:错误:[ethjs-RPC] RPC错误与有效载荷{ “ID”:7309825988666 “jsonrpc”: “2.0”, “PARAMS”:[ “0xf8721f85e8d4a510008303345094bcd5d351e5850774d1f720328dac1c8732d68eb7872386f26fc1000084be9a6555822d45a0a07b903c493ba6dee96a54bc74344d1c668cd3d9e8a7c757fdc5daa664ff4271a01bb6799a02054ecf1c8c5be51d6748b2c086eb330091b2e0500d43e756666f69”], “方法”: “ eth_sendRawTransaction”}错误:处理事务时VM异常:还原
未捕获(在承诺)错误:错误:错误:[ethjs-RPC] RPC错误与有效载荷{ “ID”:7309825988666 “jsonrpc”: “2.0”, “PARAMS”:[ “0xf8721f85e8d4a510008303345094bcd5d351e5850774d1f720328dac1c8732d68eb7872386f26fc1000084be9a6555822d45a0a07b903c493ba6dee96a54bc74344d1c668cd3d9e8a7c757fdc5daa664ff4271a01bb6799a02054ecf1c8c5be51d6748b2c086eb330091b2e0500d43e756666f69”],“方法“:” eth_sendRawTransaction“}错误:处理事务时VM异常:还原
答案 0 :(得分:0)
在下面的JS代码中:
lotteryInstance.start.sendTransaction({
from: '0x8b3234612c7D3b805F4c9CF1Aa55Cc47B82A0769',
value: 10000000000000000, // Here is the amount of ETH you give to start fuction
gas: 210000,
gasPrice: web3.toWei('1000', 'gwei')
});
发送失败,可能是因为您给(ETH)可能没有的金额!您有value: 10000000000000000
个以太币吗?
如果否,则通过您的部分款项,因为您未对合同中的款项提供任何条件,因此您可以提供最小的款项。
PS:您的合约收到的ETH不会存储在任何地方,这可能会导致以太币损失。