You need to interface playerContract
with MyToken
.
contract MyToken{
...
}
contract playerContract{
function play (uint256 tokens) public {
// store values for each participant.
if (condition) {
MyToekn instance = MyToken('address of Mytoken');
instance.transfer('winner address', ntokens);
}
}
}
The idea is that the players are sending tokens to playerContract
Address. For the contract to be able to transfer these tokens to the winner it needs to use the function transfer
in MyToken
, so you define an instance to the contact and now playerContract
can use the transfer function.
Hope this helps
EDIT: using a single transaction.
This can be achieved with approveAndCall
. The ERC20 contract must have implemented it and something like this is in the standard:
contract ApproveAndCallFallBack {
function receiveApproval(address from, uint256 tokens, address token, bytes data) public;
}
In you contract the function receive approval
must be implemented. You will be getting the information of who is sending the tokens, how much, from which token contract and to whom, you also get a variable data
in case you need them to pass data, but this is not the case.
contract MyToken{
...
}
contract playerContract{
function play (uint256 tokens, address from) internal {
// store values for each participant.
if (condition) {
MyToekn instance = MyToken('address of Mytoken');
instance.transfer('winner address', ntokens);
}
}
function receiveApproval(address from, uint256 tokens, address token, bytes data) public{
require(token=='your token address');
delete data;
MyToekn instance = MyToken(token);
instance.transferFrom(from,address(this), tokens)
play(tokens, from);
}
}
The require(token=='your token address');
is there because people may try to pay you with a token different than yours.
Hope this helps.
Note: do not forget that if you have some profit in tokens these will be linked to the contract and you will need to implement a function to move then like in the play
function.