0

I'm a newbie in blockchain. I am trying to repeat DAO attack using two different contracts, in my case DAO and Hacker. I want the 2 contracts belongs to 2 separated account, so I can see that the ethereum goes from DAO contract holder to Hacker contract holder in Genache. However, I got stuck since I can only migrate my contract using the same account based on command truffle --migrate -reset

Here are the contracts for both DAO and Hacker:

pragma solidity ^0.4.24;

contract Dao {
    struct User {
        address addr;
        string name;
        uint balence;
        uint8 flag;
    }

    //Fetch User
    mapping(address=>User) users; // create HashArray<address,User>, which can be random access

    uint public userCount;

    constructor() public {
        //user = "hello"; just for testing
        //addUser("wei");
        //addUser("alex");
    }

    function addUser (address _addr, string _name, uint _balence, uint _flag) private {
        users[_addr] = User(_addr,_name,0,1);
        userCount++;
    }

    function addToBalence(string name) public payable{
        if(users[msg.sender].flag != 1) addUser(msg.sender,name,0,1);
        users[msg.sender].balence = users[msg.sender].balence + msg.value;
    }

    function showBalence(address) public returns (uint) {
        return users[msg.sender].balence;
    }

    function withdraw(uint amount) public payable{

        if(users[msg.sender].flag != 1) revert("user does not exist");
        else if (users[msg.sender].balence >= amount){
            msg.sender.call.value(amount)();
            users[msg.sender].balence -= amount;
        }
        else return false;
    }
    }
}

contract Hacker {
    // the address of the user, so the money can be transfered
    Dao dao;
    address userAddress;
    string userName;

    constructor(string name, address daoAddr) public {
        dao = Dao(daoAddr);
        userAddress = msg.sender;
        userName = name;
    }

    function payout(uint amount) public payable{
        dao.withdraw(amount);
    }
}

2 Answers 2

1

In your Truffle migration file, you are able to specify the from account which actually deploys the contract. Details are in their documentation here.

Assuming you are using a tool like Ganache for your local blockchain simulator, you should be provided with 10 accounts which you can access as shown below:

var MyContract1 = artifacts.require("MyContract1");
var MyContract2 = artifacts.require("MyContract2");

module.exports = function(deployer) {
  deployer.deploy(MyContract1, {from: web3.eth.accounts[0]});
  deployer.deploy(MyContract2, {from: web3.eth.accounts[1]});
};

Let me know if this helps.

1
  • THANK YOU! I successfully deployed because of your help! For those who suffer the same problem, here is the link to the project repo. Currently, I'm working on the contracts and migration folders, where my question post arise from. Thanks again @Shawn, couldn't really figure it out without your help
    – Wei
    Commented Dec 27, 2018 at 13:57
0

I'll post the detail solution here, in case someone stumble on this. Note that all the credit goes to @Shawn Tabrizi

here is the structure of my project folder for both contract and migration

\contracts
 **MyContracts.sol**
\migrations
 1_initial_migration.js
 **2_deploy_contracts.js**

I have added *symple to the files written by me.

MyContracts.sol looks like this

pragma solidity ^0.4.24;
contract MyContract1 {...}
contract MyContract2 {...}

2_deploy_contracts.js looks like this

var MyContract1 = artifacts.require("MyContract1");
var MyContract1 = artifacts.require("MyContract2");
module.exports = function(deployer) {
deployer.deploy(MyContract1, {from: "0x2e...")); // copy paste from Genache GUI provided account
deployer.deploy(MyContract2, {from: "0xfa..."});// copy paste from Genache GUI provided account
};

As a result, I was able to migrate my contracts to local blockchain setup by Ganache and using truffle migrate --reset.

4
  • Glad it worked out. BTW, I think you can generalize the addresses like I showed in my example. That way, if you generate new addresses with Ganache, the same migration file will work. Commented Dec 28, 2018 at 8:31
  • I can directly use web3 library without any declaration, or I have do something like "import 'web3' "?
    – Wei
    Commented Dec 28, 2018 at 9:21
  • I believe truffle takes care of that for you. Commented Dec 28, 2018 at 9:24
  • ok, I see! Thanks for all the help and wish you a happy new year & merry X'mas!
    – Wei
    Commented Dec 28, 2018 at 9:33

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.