1

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);
  }

}
  • Do you get any error? What is the smart contract code? – Florian Castelain Jan 16 '18 at 5:50
  • I edited the original question to add the smart contract code. Also, as far as I can tell, I am not getting any error – yemista Jan 16 '18 at 13:51
1

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();

Your Answer

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

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