Call a function on behalf of another address?

Hello everyone. I want to call approve function in contract B from contract A.
The function in B is like this --> approve(spender, amount)
The approve function in B receives the owner variable in itself with msg.sender.
As a result, since A -> B, msg.sender = address A.

But I want to call this operation on behalf of another address, even if it is within the A contract. So I want msg.sender to be another address (a wallet address). I already give the spender and amount values as parameters.

Actually, my goal is to automate the approve function in a function within my contract.

Is it possible? Thanks in advance..

So you want to be able to approve a transfer from a random wallet address in your contract?
With that logic you would also be able to transfer any coins from any wallet to wherever you want. I hope you see the problem with that logic.

1 Like

It is not.

As @CryptoWorld explains, this is a key element of smart contract security. If you could impersonate an address, then everything would just break.

As per my understanding you want in your contract (A) to make a call to contract (B) and the msg.sender will be a random user's address (that you're not owner of)
if so, this is not possible

Thank you all for your attention. It is not possible to approve on behalf of another address. Thats why I changed my algorithm.
I added a function inside contract A, "callApprove". So I can properly call the corresponding approve function from contract A.
And then I added one more function callSwap. (Actually this is what exactly I want to design; Swapping tokens in a contract with "uniswap-v3. Only contract owner can do it." ).

So first I call the "callApprove" from owner wallet of contract A. And than I call "callSwap". It worked well.

But I am wondering If I can do approvement inside the "callSwap" function. Just like this:

function callSwap(address denomAsset, address spenderForApprove, uint256 amountIn) public onlyOwner {
        // Approvement
        this.callApprove(denomAsset, spenderForApprove, amountIn);

        // Swapping
        IMyUni myUniContract = IMyUni(myUniContractAddress);
        myUniContract.swapExactInputSingle(amountIn);
    }

It is a bit unclear what you are trying to do. What is the callApprove function is supposed to do?

If callApprove is for allowing someone to transfer tokens from your contract to someone else then it makes sense, however why is it called callApprove and not just approve as a normal erc20 token?

Or are you using the callApprove to authorize your contract to make the swap on uniswap?
In the last situation you can also call it in your other function.

However that wallet would still have had to make an earlier call to allow your contract to use/receive the tokens.

I have a contract A and it has an owner. What I want to do is to swap the tokens inside contract A only by its owner. I also have one more contract for swapping, contract Swap. We need to approve before swap function inside Swap contract.

So the owner send a request callSwap function inside contract A, inside the function we should call callApprove then swap function. And that's it. The owner made the contract A swapping successfully.

First, I was trying to make the owner send a request to the contract Swap for swapping. that's why I was trying to do approvement inside contract Swap on behalf of contract A. It was wrong algorithm.

I apologize for asking incompletely or incomprehensibly what I want to do. I can't ask the right questions because some issues don't make sense for me.