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; }
REGISTRATION_FEE
? I don't see it defined in the code... is it possible it's more thanAIRLINE_REGISTRATION_FEE
? If you just havedeposit(msg.value)
and thenrequire(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 eitherdataAddress.transfer()
(perhaps if that address is a contract) ordata.fund()
.REGISTRATION_FEE
andAIRLINE_REGISTRATION_FEE
, but I'm guessing that's just an error introduced in copy/pasting your code.deposit(msg.value)
andrequire(address(this).balance >= REGISTRATION_FEE, "do not have enough to pay");
makes the test pass!!! Thank you! I have trieddata.fund()
separately and that works, so I guess the issue is withdataAddress.transfer()
, since that address indeed is a contract. Is there a special way to transfer ether from this contract to that one?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.