Last active
March 7, 2021 16:24
-
-
Save tpae/b49cc392cd32b762147f01dc92b30b44 to your computer and use it in GitHub Desktop.
Library to help manage multiple offer bids for tokens
This file contains hidden or 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: Unlicense | |
pragma solidity ^0.7.0; | |
import "@openzeppelin/contracts/utils/Counters.sol"; | |
import "@openzeppelin/contracts/utils/EnumerableSet.sol"; | |
library OfferManager { | |
using EnumerableSet for EnumerableSet.UintSet; | |
using EnumerableSet for EnumerableSet.AddressSet; | |
using Counters for Counters.Counter; | |
struct Offer { | |
Counters.Counter _bidId; | |
mapping(address => EnumerableSet.UintSet) _bidderForBids; | |
mapping(uint256 => EnumerableSet.UintSet) _tokenIdForBids; | |
mapping(uint256 => address) _bidForBidder; | |
mapping(uint256 => uint256) _bidForAmount; | |
} | |
function addBid( | |
Offer storage offer, | |
address bidder, | |
uint256 tokenId, | |
uint256 amount | |
) internal returns (uint256) { | |
offer._bidId.increment(); | |
uint256 bidId = offer._bidId.current(); | |
offer._bidderForBids[bidder].add(bidId); | |
offer._tokenIdForBids[tokenId].add(bidId); | |
offer._bidForBidder[bidId] = bidder; | |
offer._bidForAmount[bidId] = amount; | |
return bidId; | |
} | |
function removeBid( | |
Offer storage offer, | |
address bidder, | |
uint256 tokenId, | |
uint256 bidId | |
) internal returns (bool) { | |
if (offer._bidderForBids[bidder].contains(bidId)) { | |
offer._bidderForBids[bidder].remove(bidId); | |
offer._tokenIdForBids[tokenId].remove(bidId); | |
delete offer._bidForBidder[bidId]; | |
delete offer._bidForAmount[bidId]; | |
return true; | |
} | |
return false; | |
} | |
function totalBids(Offer storage offer, uint256 tokenId) | |
internal | |
view | |
returns (uint256) | |
{ | |
return offer._tokenIdForBids[tokenId].length(); | |
} | |
function bidAt( | |
Offer storage offer, | |
uint256 tokenId, | |
uint256 index | |
) internal view returns (uint256) { | |
return offer._tokenIdForBids[tokenId].at(index); | |
} | |
function bidAmount(Offer storage offer, uint256 bidId) | |
internal | |
view | |
returns (uint256) | |
{ | |
return offer._bidForAmount[bidId]; | |
} | |
function bidderAddress(Offer storage offer, uint256 bidId) | |
internal | |
view | |
returns (address) | |
{ | |
return offer._bidForBidder[bidId]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment