0

Please help me make following process feasible

situation: A user wants to buy service(or product) at certain dapp but A has no token(even ETH) in his account so asks B for paying on behalf of.

  1. B call approve A to spend amount of price
  2. A call transferfrom B to Xcontract
  3. Xcontract delegatecall buy function in dapp contract
  4. dapp contract gives ownership of service to A

I put Xcontract here because dapp contract may check balance of msg.sender so X need to return as following to avoid revert:

msg.sender=A, balance is equal or greater than price

I am stucked here. If Xcontract does delegatecall then it returns msg.sender=A but 0 for balance(A has no token) right? eventually, dapp will revert. Is there any solution to return both msg.sender as A and enough balance in this case?

How can I do? is it absolutely impossible? token in this case is just erc20

1 Answer 1

0

The issue with your code logic is that transferFrom does not trigger the contract that sell the product, it just change the balances in the token smart contract.

A purchase can be done like this.

Assume that A has enough balance first.

A approve tokens to be moved by the smart contract of the dapp, then A calls the function but in the dapp. The dapp contract takes the tokes that A has approved and delivers the product/service.

If A has no tokens, B approves it for A and then A has to take the tokens from B and move it to himself. After that A can do as described above.

Note that this can be avoided by allowing directly in the dapp for an user to buy on behalf of someone else by just providing the address of another person.

Finally, the two transaction process needed to buy, as described above, can be avoided using approveAndCall function (check the ERC20 standard) the problem is that tokens that are old, do not implement this function.

Hope this helps.

2
  • actually, I want that tokens do not go through A's account
    – lagoiz
    Apr 30, 2019 at 6:05
  • Then your option is to modify the dApp contract so that B can purchase on behalf of A. A cannot buy with tokens that it doesn't have, A has the tokens only when they are reflected in the balance of A.
    – Jaime
    Apr 30, 2019 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.