I've got a question that I cannot seem to find an answer for, hoping the community can help (and this is my first post here so my apologies if this is the wrong question to ask in the first place).
I am trying to create a contract with a Payable function that only accepts a specific token, not ETH, as payment. I own both the token contract and this contract which is specifically created to accept payment for for that particular token.
Is this even possible? I found this as a possible solution:
Source: How do I create a crowdsale that only accepts a specific erc20 token?
contract Crowdsale {
address owner;
mapping(address => uint256) balances;
function Crowdsale(){
owner = msg.sender;
}
function acceptOnlyMyToken(address _yourTokenAddress, uint256 amount){
address user = msg.sender;
YourToken token = YourToken(_yourTokenAddress);
//get the user's balance
uint256 userBalance = token.balanceOf(user);
//check user's balance
if(userBalance >= amount){
token.transferFrom(user, owner, amount);
}
}
}
Is it safe to assume the following with the code above?
- Replacing
YourToken token = YourToken(_yourTokenAddress);
withYourToken token = YourToken(address(0xABCD...));
with "0xABCD" being the address of my token which is in a separate contract should work. - Specifying
require(msg.value == 100
under theacceptOnlyMyToken(...)
function would allow me to set the specific payment amount required.
Also, I don't see this function being called as "payable", does this matter? The reason is because I want to pass on variables as part of the function to do XYZ work as part of the contract logic, for which I receive a payment for in a specific amount of that token.
Any thoughts?