I'm trying to write a test suite in Solidity for a few contracts contained in a single script. I can't alter the code in the contracts - they're not written by me, and I don't have the rights. I would like to be able to simulate a transaction between two contracts, but I can't figure out if there's a way for me to fund a contract from thin air (for testing) using Solidity. I'm assuming it would look something like:

contract testUser {
  constructor () {
    address(this).balance = 1 ether;

Right now this is giving me an error in Remix (Expression has to be an lvalue), which I think is because this needs to be a storage thing, and this is trying to write it in memory, but adding storage to the line where I'm trying to fund the contract yields a syntax error. Is there a way to do this?

2 Answers 2


First, you can't just give your contract 1 ether money. The only way the contract receives ether is to implement fallback function.

If you're using truffle it's quite easy to test your contracts. Please refer to the documentation.

Test code that sends ether could be as following:


pragma solidity ^0.4.24;

contract MyContract {
    address public owner;

    constructor() {
        owner = msg.sender;

    function() external payable {
        owner.transfer(msg.value); // transfer funds to owner acc



let MyContract = artifacts.require("./MyContract.sol");
module.exports = function (deployer) {


let MyContract = artifacts.require("./MyContract.sol");

contract("MyContract", accounts => {

it("Test: receive payment", () => {
    let owner = accounts[0];
    let ownerInitialBalance;
    let sender = accounts[1];
    let amount = 100;

    return MyContract.deployed()
        .then(it => {
            ownerInitialBalance = Number(web3.eth.getBalance(owner));
            return it.sendTransaction({from: sender, value: amount});
        .then(receipt => {
            console.log("Transaction Receipt = ", receipt);
            assert.equal(Number(web3.eth.getBalance(owner)), ownerInitialBalance + amount, "Owner balance check");
  • I'd been hoping to be able to handle it all inside a Solidity script, but I guess that's a no. This is a great answer, though - thanks! Jul 23, 2018 at 12:12

You can either send some ETH along with the deployment transaction, or do it later with this web3 command. In both cases, the function needs to be payable. ETH cannot be created from thin air, you have to transfer some from your accounts in ganache or truffle develop or Remix's JS-based VM which all give you lots of test ether and fast function execution.

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.