equivalent of msg.sender for contracts ?

DjocOBFa4bAm9lDjocOBFa4bAm9l Member Posts: 6
edited January 2016 in Solidity
Hey,

I'm trying to find a way to have my contract effectively act as the msg.sender when calling function from other contract internally.

If that's not clear a quick example: let say i have a token contract like the eth example and an escrow contract.
contract token { 
    mapping (address => uint) public coinBalanceOf;
    event CoinTransfer(address sender, address receiver, uint amount);

  /* Initializes contract with initial supply tokens to the creator of the contract */
  function token(uint supply) {
        if (supply == 0) supply = 10000;
        coinBalanceOf[msg.sender] = supply;
    }

  /* Very simple trade function */
    function sendCoin(address receiver, uint amount) returns(bool sufficient) {
        if (coinBalanceOf[msg.sender] < amount) return false;
        coinBalanceOf[msg.sender] -= amount;
        coinBalanceOf[receiver] += amount;
        CoinTransfer(msg.sender, receiver, amount);
        return true;
    }
}
It's easy to see how i could simple use the sendCoin() function to send coin to my Escrow contract account, basically adding coin to the balance of the address of the Escrow contract in the Token contract.

Now what if i want to have my token contract send back the coin at some point ?

only way i could think of would be something like this :
  function payCoin(address receiver,address a_o_contract, uint amount, uint coin) returns(bool successful) {
    balances[a_o_contract][coin] -= amount;
    balances[receiver][coin] += amount;
    return true;
  }
And once my contract is instantiated in the Escrow contract i can effectively send back coins from the contract to the user at will by using "this" as the address of the contract so... something like this inside my 2nd contract :
		Token t = Token(tokenContractAddress);
		m.payCoin(receiver,this, amount);
But the problem is pretty obvious... :smiley: Now anybody can effectively call this method and fill it with any address and steal everybody's precious tokens at will. :(

My original goal was to make that method private so that it's not available to front end user but only to one of my other function under precise circonstance. Wouldn't be pretty , but better than nothing. Only issue is i'm calling the method from 1st contract on the second contract , and it's impossible with private. So i'm back to square one.

Is there a way to do that nicely , ideally something like this ? (i'm using msg.sender because the contract is the one calling the other contract but it's obviously not the right term but didn't really know how to call that):
      if msg.sender == contract.adress
        transfer funds
     else 
        false
Sign In or Register to comment.