I'm creating an NFT game and I have a big question about payable.

Below I'm putting my code that I'm invoking a new Hero.

function invokeRandomHero() public payable {
    require(msg.value == 0.001 ether);
    uint randGenetic = _generateRandomGenetic();

If you notice I'm using a test with the "Ether" coin, my question is, how can I do the same charge but with my own coin? I'm going to create a contract for a new currency called for example "SLP" (example only, equal to axie).

How do I use this currency charge to perform the "summon a new hero" transaction?

1 Answer 1


The payable keyword is only useful when dealing with the blockchain's native asset/coin - in this case Ethers. You can't create own coins, you can only create your own tokens. So I assume you are meaning ERC20 tokens.

If you want to associate a token price for invoking the function, you should use the two staged transfer:

  1. User approves for the contract to withdraw X amount of his tokens, with the approve function in ERC20

  2. User notifies the contract the user wishes to buy something with tokens, and the contract withdraws the tokens from the user with the transferFrom function, and gives him his hero.

Tutorial of this two-phased token trade can be found for example here: https://ethereum.org/en/developers/tutorials/transfers-and-approval-of-erc-20-tokens-from-a-solidity-smart-contract/

  • Hi Lauri, thanks for fast answer.. I got it.. You have a code example about this? Thanks again :)
    – Crazy
    Oct 23, 2021 at 7:50
  • I remember now, I haven't seen in my studies yet a way to return the "balance" referring to the ERC20 (my token), if you have any examples I'll thank you very much!
    – Crazy
    Oct 23, 2021 at 7:52
  • Add a link to a tutorial which has all of this. But it's very easy to google for more such tutorials, if needed. This is a very common pattern Oct 23, 2021 at 7:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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