0

I have a token contract, when transfer is called it calls other contract's transfer with data. Other contract checks msg.sender and allows only the first contract to pass and performs some important job with the address sent from the parameter.

I heard that delegatecall is gas cheaper. If I change the code to delegatecall I can't get properly msg.sender because it changes caller from contract to the user wallet address. In that case I can't check if the other contract is called only by the first one and this allows attacker to call the other contract without the first one and that leads to one giant glitch.

Is it possible to make delegatecall callable only by specific contract?

Here is a chunk of the code. The reason of splitting this to two contract is that the other contract is large and it's much more than a mere token. The first one is just a basic token with call in transfer.

First contract:

function transferWorker(
    address pFrom,
    address pTo,
    uint pValue
)
    private
{
    require(pTo != address(0));
    balances[pFrom] = balances[pFrom].sub(pValue);
    balances[pTo] = balances[pTo].add(pValue);
    otherContract.processTransfer(pFrom, pTo, pValue);
    emit Transfer(pFrom, pTo, pValue);
}

Other contract:

function processTransfer(
    address pAddressFrom,
    address pAddressTo,
    uint pValue
)
    external
{
    require(msg.sender == allowedAddress);
    transferWorker(
        pAddressFrom, 
        pAddressTo, 
        pValue
    );
}

function transferWorker(
    address pFrom,
    address pTo,
    uint pValue
)
    private
{
    require(msg.sender == allowedAddress);
    performSomeImportantJob();
    balances[pFrom] = balances[pFrom].sub(pValue);
    balances[pTo] = balances[pTo].add(pValue);
}
  • in your code, the balances are updated and then you call another function in the other contract to update the balances again, why?, why do you think delegate call will use less gas? – Jaime Jan 6 at 14:23
  • Because the code of the other one is large and more complex, it's much more than just a transferable token. My idea is to release source code on the etherscan for the first contract only to let people know that token has a safe code. Why do I think that delegate would use less gas? I don't really know from my own experience, but I read from some articles and people described it to be cheaper. Maybe it's not true, I don't know. – Radeg90 Jan 6 at 14:33

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.