3

I'm currently writing the "Trade" smart contract which is similar to "Crowdsale" smart contract. The only function that I want to add is "sell" function. In general Crowdsale smart contract is used for making people buy their token. There is no problem to implement "buy" function because of the payable. Not only it can make the Smart Contract can receive the ether but also it is able to make the transaction of particular payable function (i.e. function buy() payable public {}).

I need the Token Fallback function as ether does so I found the ERC223 which is improved based on ERC20. I'm wondering if I can use a Token Fallback Function in another Smart Contract. I mean another deployed contract. For instance, let's assume that someone transfer/send my erc20 custom tokens to "Trade" Smart Contract because he/she wants to sell some amount of tokens and get the proportional amount of ether back (using Web3 like this myContract.methods.transfer()). After the "Trade" Smart Contract received the token, Token Fallback Function in "Trade" Smart Contract is called and executed the "Sell" function in that Contract. Is that make sense?

The problem is how can I implement this?

function transfer(address _to, uint _value, bytes _data) public {
    require(_value > 0 );
    if(isContract(_to)) {
        ERC223ReceivingContract receiver = ERC223ReceivingContract(_to);
        receiver.tokenFallback(msg.sender, _value, _data);
    }
    balances[msg.sender] = balances[msg.sender].sub(_value);
    balances[_to] = balances[_to].add(_value);
    Transfer(msg.sender, _to, _value, _data);
}

(Refer to https://gist.github.com/anonymous/eb7be71f34911b013552960cc4ac0f45)

This is the ERC223 Token transfer function and the ERC223ReceivingContract is imported import 'browser/ERC223ReceivingContract.sol'; like this. I don't want to import the Contract because for the case when I have to update my Contract later. I want to remain my Token contract has only a few required functions.

I also found another function called TransferAndCall(). I can't find the detailed explanation how to use it. It seems like similar to Fallback function.

Anyway, do you guys have any solution to do this? Why I want to divide the "Trade" Smart Contract and "Token" Smart Contract is because for the case when I have to update my "Trade" Contract later like I mentioned. Please help me smart solidity experts!

0

After the "Trade" Smart Contract received the token, Token Fallback Function in "Trade" Smart Contract is called and executed the "Sell" function in that Contract.

Yes.

As you can see, in the transfer function, if the receiver is a contract, then it must have the a fallback function for the token, otherwise it won't work.

You need to implement this function in your contract.

if(isContract(_to)) {
        ERC223ReceivingContract receiver = ERC223ReceivingContract(_to);
        receiver.tokenFallback(msg.sender, _value, _data);
    }

As you can see, based on the abstract contract ERC223ReceivingContract, the signature of the function must be :

function tokenFallback(address _from, uint _value, bytes _data) public

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.