One of the best prototypes of ERC20 token implementation is the OpenZeppelin's ERC20 implementation.
Follow it's guidelines and you'll archive to do it.
Related with your problem, the error that gives the other answer comes from the ERC20 implementation , if You've overridden the transfer() method and set only one parameter, you won't be able to do it.
Another problem is that msg.value
is in wei, so as fas as a wei is indivisible, that may be causing errors too.
So doing it fast it'll be something like that:
contract MyToken {
// ERC20 token implementation
}
contract SomeContract {
address tokenAddress;
MyToken token;
token.approve(this( which is adress of SomeContract), X amount of tokens is able to donate has to be more than 1000 that is what you send)
// Run this first, to tell the contract the address
// of your token smart contract
function setTokenAddress(address _tokenAddress) public {
tokenAddress = _tokenAddress;
token = MyToken(_tokenAddress);
}
function GiveMeTokens() public payable {
if (msg.value > 1) {
token.transfer(msg.sender, 1000);
}
}
}
Use a standard ERC20 implementation and try again your method.