0

Lets suppose here are user A and smartcontract B and C. A has no token but little ETH for gas, but B has enough tokens(Erc20).

  1. A send TX to B
  2. B send delegatecall to C
  3. C receive tokens in B and give ownership to A

Is this logic possible? If not, how can I make it? Pls correct me.

8
  • Why do you need contract C? Why doesn't contract B just transfer its tokens to user A?
    – user19510
    Commented May 23, 2019 at 4:53
  • C is a kind of dapp. B need to send tokens to C directly to save time.
    – ikaop
    Commented May 23, 2019 at 5:17
  • Then I don't understand the question. Maybe when you said "and give ownership to A" you're talking about ownership of something other than the tokens?
    – user19510
    Commented May 23, 2019 at 5:21
  • I meant "C receive tokens in B and provide service to A"
    – ikaop
    Commented May 23, 2019 at 5:23
  • In that case, of course this is possible.
    – user19510
    Commented May 23, 2019 at 5:24

1 Answer 1

0

Here's some code doing this:

contract C {
    ERC20 token = ERC20(0x...);
    address ownerOfFoo;

    function buyFoo(address owner) external {
        require(ownerOfFoo == address(0), "already owned");
        require(token.transferFrom(msg.sender, address(this), 1000), "foo costs 1,000 token units");

        ownerOfFoo = owner;
    }
}

contract B {
    ERC20 token = ERC20(0x...);
    C c = C(0x...);

    function helpMeBuyFoo() external {
        // probably you want to allow only A to do this?
        // require(...);

        token.approve(c, 1000); // tokens will come from B
        c.buyFoo(msg.sender);   // tell C who should get the service
    }
}
7
  • I have question. If contract C(sort of dapp) check balance of msg.sender while executing function buyfoo, then it could be failed because there is no token in A's account. Isn't it?
    – ikaop
    Commented May 23, 2019 at 10:20
  • If C checks the balance of msg.sender, it will be checking the balance of B.
    – user19510
    Commented May 23, 2019 at 15:21
  • Is contract B using delegatecall or just call? I am still confused.. because if it's delegatecall, msg.sender must be A I think.
    – ikaop
    Commented May 24, 2019 at 5:34
  • It's just a regular call. delegatecall wouldn't make sense here.
    – user19510
    Commented May 24, 2019 at 13:35
  • In case of regular call, msg.sender in C must be contract B not A right? So, contract C can receives tokens from B but also gives service to B NOT A. Please correct me @smarx
    – ikaop
    Commented May 28, 2019 at 8:46

Your Answer

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

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