У меня есть этот код:
pragma solidity ^0.4.11;
contract Bank {
struct Balance {
address owner;
uint vault;
uint profit;
}
Balance balance;
mapping (address => uint) balances;
function Bank() {
balance.owner = msg.sender;
balance = Balance(msg.sender, 0, 0);
}
function getBalance() constant returns (uint) {
if(msg.sender == balance.owner) {
return this.balance;
}
throw;
}
function withdrawOwner() returns (bool) {
if(msg.sender == balance.owner) {
balance.vault = balance.vault - balance.profit;
bool sent = balance.owner.send(balance.profit);
balance.profit = 0;
return sent;
}
return false;
}
function withdraw() returns (bool) {
uint customerBalance = balances[msg.sender];
if(customerBalance == 0) {
throw;
}
balance.vault -= customerBalance;
balances[msg.sender] = 0;
return msg.sender.send(customerBalance);
}
function deposit() payable returns (bool) {
uint take = 100;
uint depositAmount = msg.value;
balance.vault += depositAmount;
balance.profit += take;
balances[msg.sender] = depositAmount - take;
if(msg.value < 20) {
throw;
}
return true;
}
}
Оценка газа говорит 4 700 000, что кажется высоким. Это из-за броска или отправки? Судя по этому, при цене эфира около 200 долларов это будет стоить 18 долларов?
Я умножаю 20Gwei * 4.7e6 = 94000000000000000 = 0,094 эфира
Я добавил фиктивную структуру и сопоставление с вашим примером и скомпилировал его в Remix.
pragma solidity ^0.4.11;
// mock contracts
contract c{
// mock mapping
mapping (address=>uint) balances;
// mock struct
struct foo {
uint vault;
}
function withdraw() returns (bool) {
// init struct
foo memory balance;
uint customerBalance = balances[msg.sender];
if(customerBalance == 0) {
throw;
}
balance.vault -= customerBalance;
balances[msg.sender] = 0;
return msg.sender.send(customerBalance);
}
}
Компилятор возвращается. Вы тратите свой бензин в другом месте. Пожалуйста, предоставьте полный контракт.
Стоимость сделки: 160457 газ. Стоимость исполнения: 81525 газ.
UPD: Полная стоимость контракта
Стоимость сделки: 379244 газ. Стоимость исполнения: 245228 газ.
Result: "0x0000000000000000000000000000000000000000000000000000000000000001" Transaction cost: 23673 gas. Execution cost: 17401 gas. Decoded: bool: true
для вывода в Remix
0xCrema.eth
Джастин Томас
ВинсентLg