[Ethereum] Cannot send funds to a smart contract using web3j

web3j

I'm trying to send funds to a smart contract using the following code, however, the balance is always 0.

TransactionReceipt transactionReceipt = Transfer.sendFunds(
            web3j, credentials, recipient,
            amount, Convert.Unit.WEI)
            .sendAsync().get();
EthGetBalance contractGetBalance = web3j.ethGetBalance(recipient, DefaultBlockParameterName.LATEST)
              .sendAsync().get();
    BigInteger contractBalance = contractGetBalance.getBalance();

Why doesn't it go through? The smart contract code is:

pragma solidity ^0.4.7;

contract ReceiveContract {
  address public owner;

  function ReceiveContract() public {
    owner = msg.sender;
  }

  function () payable public {
    owner.transfer(msg.value);
  }

}

Best Answer

So I found a solution, and it was to change my send code to use raw transactions

    EthGetTransactionCount ethGetTransactionCount = web3j.ethGetTransactionCount(
            credentials.getAddress(), DefaultBlockParameterName.LATEST).sendAsync().get();
    BigInteger nonce = ethGetTransactionCount.getTransactionCount();
    RawTransaction rawTransaction  = RawTransaction.createEtherTransaction(
             nonce, ManagedTransaction.GAS_PRICE, Contract.GAS_LIMIT, recipient, amount);
    byte[] signedMessage = TransactionEncoder.signMessage(rawTransaction, credentials);
    String hexValue = Numeric.toHexString(signedMessage);
    EthSendTransaction ethSendTransaction = web3j.ethSendRawTransaction(hexValue).sendAsync().get();
Related Topic