Search code examples
tokenblockchainsolidityerc20

How to enable Claim Tokens in Solidity Smart Contract?


I am currently using the Open Zapeline smart contract for my Dapp, I wanted to know if there is a way where users can claim my tokens (i.e transfer from owner wallet to current user) I know a method with hardcoding the private keys but is there any way wherein the Smart Contract I can set msg.Sender as Owner or Transfer tokens from Owner account to the user without any signatures?


Solution

  • You can use the internal function _transfer() (GitHub link) that only validates is the from param is the actual owner of the token. But it doesn't validate if the msg.sender is the token owner.

    Note that this function has internal visibility, so it's only executable from inside of your contract.

    pragma solidity ^0.8;
    
    import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
    import "@openzeppelin/contracts/access/Ownable.sol";
    
    contract MyCollection is ERC721, Ownable {
    
        constructor() ERC721("MyCollection", "MyC") {
            _mint(owner(), 1);
        }
    
        function claim() external {
            require(ownerOf(1) == owner(), "Already claimed");
            _transfer(owner(), msg.sender, 1);
        }
    }