3

This question already has an answer here:

I have the following problem in Mist 0.8.6: When sending a transactions to the function of a contract without ether (in msg.value) the function executes and I see value changes in the contract.

When sending the same transaction with ether (in msg.value) it seems the function won't be called (at least I don't see any value changes in the debug variables of the contract...).

The transaction with ether does not show any errors, it seems to execute fine.

The complete code of the contract is quite a lot; below you fined the code of the function that I call (it's a fork of the DAO-Wars game). When I call the function without sending ether the debug variable "debug_add_creature" is set to "before if, ether_cost_ether" (as expected), variable "debug_ether_costs" is set to "2000000000000000000". When I call the funciton with sending ether nothing changes...

Note that Mist displays the warning "It seems this transaction will fail, it may consume all the gas you send."; but when I set the gas manually to 4000000 it does execute (without showing any errors in the transaction). Previously I could run the function with sending ether, this is a recent problem.

Any suggestions?

Is the problem, that the transaction takes too much gas?

      function add_creature(address brain, string species_name) returns(uint) {
uint eth_cost = (eth_deposits * eth_amount) + starting_eth;

/* convert wei to either, because the incoming msg.value is in ether! */
// Note: Can be optimized to just multiply with the 1000000000000000000 instead of ** 18
uint eth_cost_ether = eth_cost * 10 ** 18;
uint starting_eth_ether = starting_eth * 10 ** 18;

debug_add_creature = "before if, ether_cost_ether";
debug_msg_value = msg.value;
debug_ether_costs = eth_cost_ether;

if (msg.value >= eth_cost_ether) {
  debug_add_creature = "in if, before add creature";
  Creature creature = creature_builder.build_creature();
  debug_add_creature = "in if, creature";
  uint location = _random_empty_location();
  uint species_id = num_species++;

  board.add_creature(location, creature);
  this.register_creature(creature);
  /* The species should not always be created new, but creatures of a certain species
           should be added to that species upon creation (if the user is the same) */
  debug_add_creature = "in if, before new species";
  NewSpecies(species_id, species_name, creature);
  species.length++;
  species[species_id].name = species_name;
  species[species_id].creatures.push(creature);

  debug_add_creature = "in if, species done, set creature";
  creature.set_eth(starting_eth_ether);
  creature.set_brain(brain);
  creature.set_location(location);
  creature.set_hp(3);
  creature.set_board(board);
  creature.set_species(species_id);
  creature.set_creature_builder(creature_builder);
  creature.set_game(this);
  creature.set_admin(admin);

  debug_add_creature = "in if, creature done, send money...";

  test=creature.send(starting_eth_ether);
  test=board.send(msg.value - starting_eth_ether);
  board.deposit_eth(eth_deposits, eth_amount);

  debug_add_creature = "in if, money sent, return";

  /* Quick fix: we return the index-number of the creature-species that was created
           So the user can retrieve the creature's address by function all_creatures_for_species. */
  return species_id;
}

}

marked as duplicate by eth Oct 19 '16 at 22:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

2

The latest version Mist (0.8.6) uses solidity 0.4.2 and from solidity 0.4 onwards,

Functions that want to receive ether have to specify payable mofidier.

Use function add_creature (address brain, string species_name) payable returns(uint).

See full list of changes here.

  • Cool! Thank you, did not know that. Works with the suggested fix :) – dakami Oct 19 '16 at 10:31

Not the answer you're looking for? Browse other questions tagged or ask your own question.