Last active
November 12, 2023 14:45
-
-
Save Nasah-Kuma/427819528616e8a0a5a8dd8ecfd63b2a to your computer and use it in GitHub Desktop.
A smart contract that allows the deposit of a token created, and users can only withdraw it after 70 blocks from when it was deposited.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
contract MyToken { | |
string public name; | |
string public symbol; | |
uint8 public decimals; | |
uint256 public totalSupply; | |
mapping(address => uint256) public balanceOf; | |
mapping(address => mapping(address => uint256)) public allowance; | |
event Transfer(address indexed from, address indexed to, uint256 value); | |
event Approval(address indexed owner, address indexed spender, uint256 value); | |
constructor(string memory _name, string memory _symbol, uint8 _decimals, uint256 _totalSupply) { | |
name = _name; | |
symbol = _symbol; | |
decimals = _decimals; | |
totalSupply = _totalSupply; | |
balanceOf[msg.sender] = _totalSupply; | |
} | |
function transfer(address _to, uint256 _value) public returns (bool) { | |
require(balanceOf[msg.sender] >= _value, "Insufficient balance"); | |
balanceOf[msg.sender] -= _value; | |
balanceOf[_to] += _value; | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
function approve(address _spender, uint256 _value) public returns (bool) { | |
allowance[msg.sender][_spender] = _value; | |
emit Approval(msg.sender, _spender, _value); | |
return true; | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public returns (bool) { | |
require(balanceOf[_from] >= _value, "Insufficient balance"); | |
require(allowance[_from][msg.sender] >= _value, "Not allowed to transfer"); | |
balanceOf[_from] -= _value; | |
balanceOf[_to] += _value; | |
allowance[_from][msg.sender] -= _value; | |
emit Transfer(_from, _to, _value); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8.0; | |
import "./atoken.sol"; | |
contract TokenDeposit { | |
MyToken public token; | |
mapping(address => uint256) public depositedAtBlock; | |
event TokenDeposited(address indexed depositor, uint256 amount); | |
event TokenWithdrawn(address indexed withdrawer, uint256 amount); | |
constructor(address _tokenAddress) { | |
token = MyToken(_tokenAddress); | |
} | |
function deposit(uint256 _amount) public { | |
require(token.transferFrom(msg.sender, address(this), _amount), "Transfer failed"); | |
depositedAtBlock[msg.sender] = block.number; | |
emit TokenDeposited(msg.sender, _amount); | |
} | |
function withdraw() public { | |
uint256 depositedBlock = depositedAtBlock[msg.sender]; | |
require(depositedBlock > 0, "No tokens deposited"); | |
require(block.number >= depositedBlock + 70, "Cannot withdraw yet"); | |
uint256 amount = token.balanceOf(address(this)); | |
require(amount > 0, "No tokens available for withdrawal"); | |
require(token.transfer(msg.sender, amount), "Transfer failed"); | |
delete depositedAtBlock[msg.sender]; | |
emit TokenWithdrawn(msg.sender, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment