Skip to content

Instantly share code, notes, and snippets.

@Turupawn
Created April 29, 2022 20:39
Show Gist options
  • Save Turupawn/f50e1e10a143b4a191049fe41b825e17 to your computer and use it in GitHub Desktop.
Save Turupawn/f50e1e10a143b4a191049fe41b825e17 to your computer and use it in GitHub Desktop.
Mai Staking
// SPDX-License-Identifier: MIT
pragma solidity 0.8.13;
contract MaiStaking
{
uint public genesisTimestamp;
uint public auctionDuration = 15 minutes;
constructor() {
genesisTimestamp = block.timestamp;
}
struct Auction {
uint price;
address bidder;
bool is_claimed;
uint bid_history_amount;
}
struct Bid {
uint price;
address bidder;
}
mapping(uint => Auction) public auctions;
mapping(uint => mapping(uint => Bid)) public bidHistory;
function bid(uint auction_id, uint mai_amount) public {
require(isActive(auction_id), "Auction must be active.");
require(mai_amount > auctions[auction_id].price, "Mai amount is less than current price.");
bidHistory[auction_id][auctions[auction_id].bid_history_amount].price = mai_amount;
bidHistory[auction_id][auctions[auction_id].bid_history_amount].bidder = msg.sender;
auctions[auction_id].bidder = msg.sender;
auctions[auction_id].price = mai_amount;
auctions[auction_id].bid_history_amount += 1;
}
function claim(uint auction_id) public {
require(block.timestamp > genesisTimestamp + ((auction_id + 1) * auctionDuration), "Can't claim before auction to end");
require(!auctions[auction_id].is_claimed, "This NFT was already claimed");
require(msg.sender == auctions[auction_id].bidder, "Only winner can claim");
auctions[auction_id].is_claimed = true;
}
function getCurrentAuctionId() public view returns(uint)
{
return (block.timestamp - genesisTimestamp) / auctionDuration;
}
function isActive(uint auction_id) public view returns(bool)
{
return block.timestamp > genesisTimestamp + (auction_id * auctionDuration)
&& block.timestamp < genesisTimestamp + ((auction_id + 1) * auctionDuration);
}
}
[
{
"inputs": [],
"stateMutability": "nonpayable",
"type": "constructor"
},
{
"inputs": [],
"name": "auctionDuration",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "auctions",
"outputs": [
{
"internalType": "uint256",
"name": "price",
"type": "uint256"
},
{
"internalType": "address",
"name": "bidder",
"type": "address"
},
{
"internalType": "bool",
"name": "is_claimed",
"type": "bool"
},
{
"internalType": "uint256",
"name": "bid_history_amount",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "auction_id",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "mai_amount",
"type": "uint256"
}
],
"name": "bid",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "bidHistory",
"outputs": [
{
"internalType": "uint256",
"name": "price",
"type": "uint256"
},
{
"internalType": "address",
"name": "bidder",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "auction_id",
"type": "uint256"
}
],
"name": "claim",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "genesisTimestamp",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getCurrentAuctionId",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "auction_id",
"type": "uint256"
}
],
"name": "isActive",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
}
]
@Turupawn
Copy link
Author

Rinkeby address:
0xcf9eea9e11f7ad18a14a8670C757f09043A524Bf

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment