1

when i am going to call approve function of erc20 contract.log prints tokenOwner and tokenSpender address same which is contract address msg.sender returns contract address. but i want tokenWner = caller address and tokenSpender = contract address please help.

THis is erc20 method

 function approve(address spender, uint256 tokens) public returns (bool success) {
    allowed[msg.sender][spender] = tokens;
   emit Approval(msg.sender, spender, tokens);

    return true;
}

And this is my contract method that is calling token method

 function approve(address token, uint256 _value) public returns (bool) {
 return ERC20Interface(token).approve(this, _value);
}
Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
Love Chawda
  • 47
  • 1
  • 8
  • post your javescript code, and maybe the approve funtion of your smart contract – Majd TL Nov 27 '19 at 11:05
  • @MajdTL here is my code in erc20 (msg.sender) become my contract address – Love Chawda Nov 27 '19 at 11:20
  • try to use delegated calls, however that is not the best praxis, has risk, and the owner of the token should call the first approve method directly, both are public functions. https://ethereum.stackexchange.com/questions/8120/how-does-the-delegatecall-method-work-to-call-to-another-contracts-method – Majd TL Nov 27 '19 at 12:14
  • Already i tried with delegate call it working fine with approve. but when i call allowance it returning me 0 approval. – Love Chawda Nov 27 '19 at 12:42
  • you call allowance with delegated call? delegate call does not provide a return value or exception – Majd TL Nov 27 '19 at 15:07
  • No i called allowance method normally with deployed contract – Love Chawda Nov 27 '19 at 15:22
  • then your delegated call did not work, as i said you cannot see an error if its happened – Majd TL Nov 27 '19 at 15:24
  • yes so how can i transfer tokens in that case. Because token contract has conditions – Love Chawda Nov 28 '19 at 04:46
  • can you upload your code somewhere in Github with a read me and I will take look at it at the weekend – Majd TL Nov 28 '19 at 08:53
  • https://github.com/Love3119/Escrow.git you can check here my code any help will be appreciated – Love Chawda Nov 28 '19 at 09:21
  • PLEASE DONT DO THIS (cause it is the worst hackable thing you can do in solidity). but the only way to do that is to change the function approve in ERC20Basic to allowed[tx.origin][delegate] = numTokens; and do not change anything in myContract. as I said dont do that and redesign your contracts and make the owner call direct the approve in Erc20Basic. – Majd TL Nov 28 '19 at 11:41
  • This is right way? ERC20Basic to allowed[tx.origin][delegate] = numTokens; i want to transfer already deployed tokens. Can i do this with my code. and how owner call direct approve function. – Love Chawda Nov 28 '19 at 11:48
  • no this will not work with normal deployed tokens cause they have msg.sender and not tx.origin. The Owner should call the method approve in the ERC20Basic directly and not through another approve method in another smart contract – Majd TL Nov 28 '19 at 12:14
  • Yay i got it. and thankyou so much.It is working with tx.origin. but i have one question. if i want to transfer any erc20 token so how can i to call approve method directly without my contract interaction. – Love Chawda Nov 28 '19 at 12:46
  • please don't use tx.origin, it is just a trick and not used in standards erc20 tokens. how the owner was going to call approve in your contract, are you using web3js to interact with the contract? – Majd TL Nov 28 '19 at 13:41
  • will use but right now i am testing it on remix – Love Chawda Nov 29 '19 at 04:53
  • how can i call other tokens approve method without my contract interaction please let me know. because i am new in solidity – Love Chawda Nov 29 '19 at 08:29
  • That has nothing with solidity to do, You can do that later with java script and web3js, you get the instance of erc20contract by its address and abi and make the sender send a transaction to call the method approve with parameters I. That contract – Majd TL Nov 29 '19 at 09:38
  • thankyou so much now i have clarity what i have to do. – Love Chawda Nov 29 '19 at 09:55

1 Answers1

1

The users who is spending tokens is the one to approve the contract.

The contract cannot appoint itself to spend tokens from someone else's wallet.

Hope it helps.

Rob Hitchens
  • 55,151
  • 11
  • 89
  • 145
  • you are right. but when i call approve function from my contract than msg.sender should be caller address but right now it is contract address. and i want to approve my contract from caller.and that caller already have tokens. – Love Chawda Nov 27 '19 at 13:24
  • The msg.sender should be your contract, in that case, which is not what you want, because msg.sender is always the inner-most caller, which is your contract. The caller needs to approve directly, not through your contract. – Rob Hitchens Nov 28 '19 at 02:00
  • in that case what should i do? – Love Chawda Nov 28 '19 at 04:44
  • 1
    I suspect maybe you misunderstand the flow. The user calls approve in the token contract and then calls a function in your contract to do something and pull the payment. The two steps on the user side can be coordinated by the user-facing UI. – Rob Hitchens Nov 28 '19 at 17:29
  • i want to use already available tokens and i don't have any code of that tokens so how can i directly call token approve without my contract interaction. please let me know. – Love Chawda Nov 29 '19 at 05:02
  • 1
    @lovechawda you have the address of that erc20 token contract and the abi is always the same cause it is a standard – Majd TL Nov 29 '19 at 09:40