-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathTokenAuction2.sol
52 lines (42 loc) · 1.55 KB
/
TokenAuction2.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
pragma solidity ^0.5.8;
contract TokenAuction {
uint durationTime;
address owner;
mapping (address => uint) tokenBalances;
uint public initialSupply;
uint rateOfTokensToGivePerEth;
event Auction(address tokenHolder, uint cost, uint tokenBalance);
constructor() public payable {
uint currentBlock = block.number;
durationTime = currentBlock + 1;
owner = msg.sender;
initialSupply = 1000;
tokenBalances[owner] = initialSupply;
rateOfTokensToGivePerEth = 10;
}
modifier timeCheck() {
require(durationTime >= block.number, "Auction Time is Over");
_;
}
modifier onlyOwner() {
require(owner == msg.sender, "Only Auction Owner is authorized");
_;
}
function buyTokens() public payable timeCheck {
uint tokens = rateOfTokensToGivePerEth * (msg.value / 1 ether);
require(tokens <= initialSupply, "Token Underflow");
require(tokenBalances[msg.sender] + tokens >= tokenBalances[msg.sender], "Token Overflow");
tokenBalances[owner] -= tokens;
tokenBalances[msg.sender] += tokens;
emit Auction(msg.sender, msg.value, tokenBalances[msg.sender]);
}
function getBalance() public view returns (uint tokens) {
return tokenBalances[msg.sender];
}
function currentSupply() public view onlyOwner returns (uint tokensLeft) {
return tokenBalances[owner];
}
function auctionBalance() public view onlyOwner returns (uint profits) {
return address(this).balance;
}
}