Is it possible to take a contract from the main ethereum blockchain and used that on my private ethereum network with ethereumj?

What I want to do is, run a contract with some input without changing the blockchain and spending gas.

Any clues?

1 Answer 1


Ethereumj provides a class called StandAloneBlockchain.java. You can see it in use here.

StandaloneBlockchain sb = new StandaloneBlockchain().withAutoblock(true);
SolidityContract a = sb.submitNewContract(
                "contract A {" +
                    "  uint public a;" +
                    "  uint public b;" +
                    "  function A(uint a_, uint b_) {a = a_; b = b_; }" +
                    "A", 555, 777

With this you'll be able to test for expected results without actually using any real ETH/gas. Therefore you'll have an idea of how the real transaction may behave on the network.

You can do this by inspecting the balance before and after the transaction is executed. The code below asserts that an amount was sent; However you can conduct a more detailed inspection of the account balances to get an idea for how much the transaction cost.

BigInteger aliceInitBal = sb.getBlockchain().getRepository().getBalance(alice.getAddress());
BigInteger bobInitBal = sb.getBlockchain().getRepository().getBalance(bob.getAddress());
assert convert(123, ETHER).equals(bobInitBal);

sb.sendEther(alice.getAddress(), BigInteger.ONE);


assert convert(123, ETHER).compareTo(sb.getBlockchain().getRepository().getBalance(bob.getAddress())) > 0;
assert aliceInitBal.add(BigInteger.ONE).equals(sb.getBlockchain().getRepository().getBalance(alice.getAddress()));

The StandAloneBlockchain.java class is also useful for unit testing your Solidity contract.

Leave a comment if you need any clarification.

  • thanks very much for the quick reply. How do I validate that this code does not use any real ETH/gas? Nov 7, 2017 at 9:47
  • Please see my updated answer. You'll get an idea of how it works by looking at some of the preexisting code for the StandaloneBlockchain.java tests.
    – Malone
    Nov 7, 2017 at 10:40
  • Thanks @Malone for the reply. I have inspected the balance before and after the transaction but it is failing at assert convert(123, ETHER).equals(bobInitBal); when I added the smart contract but works fine without the smart contract. Nov 7, 2017 at 11:40
  • If you set the sender before making the call to the smart contract. Then the transaction will be made from that account. Allowing you to check the balance of that account after. Refer to the SolidityContract class to look at how you can interact with the contract. e.g. SolidityCallResult callFunction(String functionName, Object ... args); method. Checkout code that I've wrote relating to this: github.com/blmalone/Blockchain-Academic-Verification-Service/…
    – Malone
    Nov 7, 2017 at 11:56
  • ps. this is a separate blockchain the emulates what things would look like in the actual environment.Therefore the ETH spent on this chain is not mirror on the main net. Therefore, you are not spending 'real' ETH so to speak
    – Malone
    Nov 7, 2017 at 12:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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