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
    May 23, 2019 at 4:53
  • C is a kind of dapp. B need to send tokens to C directly to save time.
    – ikaop
    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
    May 23, 2019 at 5:21
  • I meant "C receive tokens in B and provide service to A"
    – ikaop
    May 23, 2019 at 5:23
  • In that case, of course this is possible.
    – user19510
    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
    }
}
8
  • Thanks! I will try!
    – ikaop
    May 23, 2019 at 7:03
  • 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
    May 23, 2019 at 10:20
  • If C checks the balance of msg.sender, it will be checking the balance of B.
    – user19510
    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
    May 24, 2019 at 5:34
  • It's just a regular call. delegatecall wouldn't make sense here.
    – user19510
    May 24, 2019 at 13:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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