Hello everyone. Its nice to be here.
I generated a token contract from the openzeppellin wizard.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 150000 * 10 ** decimals());
}
}
I am interested in adding 2 functions . don't know if its possible.
- a modifier function to allow only 1 transfer every 30days. i.e when an address receive the token, address owner will have to wait 30 days before he can make one transfer. will basically wait 30 days to do any transfer..
- contract owner should set addresses that should be immune to this restriction above.
I would appreciate any help in its implementation or if there is any such implementations in the past..
Thanks in Advance
You can probably achieve this by adding something like:
mapping(address => bool) private _isImmuned;
mapping(address => uint256) private _lastTransfer;
function setImmuned(address user, bool isImmuned) external onlyOwner {
_isImmuned[user] = isImmuned;
}
function _transfer(address from, address to, uint256 amount) internal override {
if (!_isImmuned[from]) {
require(_lastTransfer[from] + 30 days < block.timestamp, "transfer not allowed");
_lastTransfer[from] = block.timestamp;
}
super._transfer(from, to, amount);
}
Thanks for the code but unfortunately did not work as i wanted. However i manage to get one thats close..
I made a few changes based on @barakman suggestion. I believe it will better suit your needs.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract MyToken is ERC20, Ownable {
mapping(address => bool) private _isImmuned;
mapping(address => uint256) private _lastTransfer;
constructor() ERC20("MyToken", "MTK") {
_mint(msg.sender, 150000 * 10 ** decimals());
}
function setImmuned(address user, bool isImmuned) external onlyOwner {
_isImmuned[user] = isImmuned;
}
function _transfer(address from, address to, uint256 amount) internal override {
if (!_isImmuned[from]) {
require(_lastTransfer[from] + 30 days < block.timestamp, "transfer not allowed");
_lastTransfer[to] = block.timestamp;
}
super._transfer(from, to, amount);
}
}