0

This is my first question on StackOverflow.

I am having trouble sending Ether from an account to the contract. The DApp I am creating is separated into a Data & App contract, where the Data contract is supposed to function as a database.

 pragma solidity ^0.5.8;

 import "./FlightSuretyData.sol";
 import "../node_modules/openzeppelin-solidity/contracts/math/SafeMath.sol";


address private contractOwner;          // Account used to deploy contract

uint constant AIRLINE_REGISTRATION_FEE = 10 ether;

address payable public dataAddress;
FlightSuretyData data;

/**
* @dev Contract constructor
*
*/
constructor (address payable _data, address payable _firstAirline) payable public
{
    contractOwner = msg.sender;
    dataAddress = _data;
    data = FlightSuretyData(_data);

    data.registerAirline("Default", _firstAirline);

}

************************* PROBLEM AREA **********************************
*************************************************************************

/**
* @dev Deposit money to the smart contract
*
*/
function deposit(uint amount) public payable {
   require(msg.value == amount, "Amount not the same!");
   // nothing else to do!

 }

function fund() public payable {

  // make sure enough ether
  require (msg.value >= AIRLINE_REGISTRATION_FEE, "Not enough ether to deposit");

  // transfer money from caller of function to THIS contract
  // PROBLEM
  deposit(msg.value);

  // transfer money from THIS contract to DATA contract
  require(address(this).balance >= AIRLINE_REGISTRATION_FEE, "do not have enough to pay");

  dataAddress.transfer(msg.value);

  require(dataAddress.balance >= AIRLINE_REGISTRATION_FEE, "data contract did not get ether");

  data.fund();
}

  *************************************************************************

I have intentionally given minimal code so that the reader doesn't get sucked into the project details. Also after much debugging, I have figured out the problem is with the deposit function. It simply doesn't deposit (I checked this with the require statement right after).

Indeed, when I run the test below, I get Error: Returned error: VM Exception while processing transaction: revert :

    await config.flightSuretyApp.fund({from: accounts[3], value: 
    web3.utils.toWei("10", "ether")});

Let me know if more code is needed!

function() external payable { fund(); }

flightSuretyData.fund() is: function fund () public { require (airlineDB[tx.origin].isRegistered, "Can only fund registered airlines"); airlineDB[msg.sender].isFunded = true; isAirline[msg.sender] = true; }

  • What's REGISTRATION_FEE? I don't see it defined in the code... is it possible it's more than AIRLINE_REGISTRATION_FEE? If you just have deposit(msg.value) and then require(address(this).balance >= msg.value);, does that work? If so, work your way back up to find out which line is causing the problem. My guess would be either dataAddress.transfer() (perhaps if that address is a contract) or data.fund(). – user19510 Sep 25 '19 at 3:43
  • Or a difference between REGISTRATION_FEE and AIRLINE_REGISTRATION_FEE, but I'm guessing that's just an error introduced in copy/pasting your code. – user19510 Sep 25 '19 at 3:44
  • So uncommenting everything except deposit(msg.value) and require(address(this).balance >= REGISTRATION_FEE, "do not have enough to pay"); makes the test pass!!! Thank you! I have tried data.fund() separately and that works, so I guess the issue is with dataAddress.transfer(), since that address indeed is a contract. Is there a special way to transfer ether from this contract to that one? – Raghav Batra Sep 25 '19 at 7:54
  • Does the contract at dataAddress have a payable fallback function? If not, it will refuse to accept ether transfers. If it does, then make sure the code in that function (if any) is not reverting and that it can succeed with only 2300 gas. – user19510 Sep 25 '19 at 7:58
  • Updated post. Not sure if it uses more than 2300 gas. What do I do? Could I add the gas as a parameter? – Raghav Batra Sep 25 '19 at 8:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.