so I created a token tokenA
contract tokenA is ERC20 {
address public deployer; //to save adress of the deployer
constructor() ERC20('tokenA', 'TA') { //called by the deployer (once)
_mint(msg.sender, 1000000000000 * 10 ** 10); //mint/create tokens - we have created 100000000000*10^18 tokens
deployer = msg.sender; //set the deployer
}
//total supply is fixed no more can be created ever
function burn (uint amount) external { //remove tokens by sending then to a zero address
_burn(msg.sender, amount);
}
}
and I have deployed them onto the Rinkeby (4) injected web3 environment. now I have to use these tokens as price for purchasing an NFT (which I also have to make)
so how do I use them in my code?
I can use ether like
uint price = 0.005 ether;
and the compare the transferred value with this, but if I write
uint price = 0.005 tokenA;
it gives an error, even though I have deployed tokenA and it resides in my meta mask acc
uint price = 0.005 ether;
this is correct.
But inside your function to buy an NFT you need:
payable
keyword.transferFrom
function of the ERC-20 token in order to transfer price
(0.005e18) tokens to the recipient address you specify.Keep in mind that in order to successfully call the transferFrom
function the user first need to call the approve
function.
For a more in dept explanation i suggest to read the OpenZeppelin ERC-20 docs/github repo