编译器
0.6.12+commit.27d51765
文件 1 的 12:Address.sol
pragma solidity >=0.6.2 <0.8.0;
library Address {
function isContract(address account) internal view returns (bool) {
uint256 size;
assembly { size := extcodesize(account) }
return size > 0;
}
function sendValue(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, "Address: insufficient balance");
(bool success, ) = recipient.call{ value: amount }("");
require(success, "Address: unable to send value, recipient may have reverted");
}
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
function functionCall(address target, bytes memory data, string memory errorMessage) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
function functionCallWithValue(address target, bytes memory data, uint256 value) internal returns (bytes memory) {
return functionCallWithValue(target, data, value, "Address: low-level call with value failed");
}
function functionCallWithValue(address target, bytes memory data, uint256 value, string memory errorMessage) internal returns (bytes memory) {
require(address(this).balance >= value, "Address: insufficient balance for call");
require(isContract(target), "Address: call to non-contract");
(bool success, bytes memory returndata) = target.call{ value: value }(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
function functionStaticCall(address target, bytes memory data, string memory errorMessage) internal view returns (bytes memory) {
require(isContract(target), "Address: static call to non-contract");
(bool success, bytes memory returndata) = target.staticcall(data);
return _verifyCallResult(success, returndata, errorMessage);
}
function _verifyCallResult(bool success, bytes memory returndata, string memory errorMessage) private pure returns(bytes memory) {
if (success) {
return returndata;
} else {
if (returndata.length > 0) {
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
文件 2 的 12:AllowListVerifier.sol
pragma solidity >=0.6.8;
library AllowListVerifierHelper {
bytes4 internal constant MAGICVALUE = 0x19a05a7e;
}
interface AllowListVerifier {
function isAllowed(
address user,
uint256 auctionId,
bytes calldata callData
) external view returns (bytes4);
}
文件 3 的 12:Context.sol
pragma solidity >=0.6.0 <0.8.0;
abstract contract Context {
function _msgSender() internal view virtual returns (address payable) {
return msg.sender;
}
function _msgData() internal view virtual returns (bytes memory) {
this;
return msg.data;
}
}
文件 4 的 12:EasyAuction.sol
pragma solidity >=0.6.8;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/SafeERC20.sol";
import "./libraries/IterableOrderedOrderSet.sol";
import "@openzeppelin/contracts/math/Math.sol";
import "@openzeppelin/contracts/math/SafeMath.sol";
import "./libraries/IdToAddressBiMap.sol";
import "./libraries/SafeCast.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
import "./interfaces/AllowListVerifier.sol";
contract EasyAuction is Ownable {
using SafeERC20 for IERC20;
using SafeMath for uint64;
using SafeMath for uint96;
using SafeMath for uint256;
using SafeCast for uint256;
using IterableOrderedOrderSet for IterableOrderedOrderSet.Data;
using IterableOrderedOrderSet for bytes32;
using IdToAddressBiMap for IdToAddressBiMap.Data;
modifier atStageOrderPlacement(uint256 auctionId) {
require(
block.timestamp < auctionData[auctionId].auctionEndDate,
"no longer in order placement phase"
);
_;
}
modifier atStageOrderPlacementAndCancelation(uint256 auctionId) {
require(
block.timestamp < auctionData[auctionId].orderCancellationEndDate,
"no longer in order placement and cancelation phase"
);
_;
}
modifier atStageSolutionSubmission(uint256 auctionId) {
{
uint256 auctionEndDate = auctionData[auctionId].auctionEndDate;
require(
auctionEndDate != 0 &&
block.timestamp >= auctionEndDate &&
auctionData[auctionId].clearingPriceOrder == bytes32(0),
"Auction not in solution submission phase"
);
}
_;
}
modifier atStageFinished(uint256 auctionId) {
require(
auctionData[auctionId].clearingPriceOrder != bytes32(0),
"Auction not yet finished"
);
_;
}
event NewSellOrder(
uint256 indexed auctionId,
uint64 indexed userId,
uint96 buyAmount,
uint96 sellAmount
);
event CancellationSellOrder(
uint256 indexed auctionId,
uint64 indexed userId,
uint96 buyAmount,
uint96 sellAmount
);
event ClaimedFromOrder(
uint256 indexed auctionId,
uint64 indexed userId,
uint96 buyAmount,
uint96 sellAmount
);
event NewUser(uint64 indexed userId, address indexed userAddress);
event NewAuction(
uint256 indexed auctionId,
IERC20 indexed _auctioningToken,
IERC20 indexed _biddingToken,
uint256 orderCancellationEndDate,
uint256 auctionEndDate,
uint64 userId,
uint96 _auctionedSellAmount,
uint96 _minBuyAmount,
uint256 minimumBiddingAmountPerOrder,
uint256 minFundingThreshold,
address allowListContract,
bytes allowListData
);
event AuctionCleared(
uint256 indexed auctionId,
uint96 soldAuctioningTokens,
uint96 soldBiddingTokens,
bytes32 clearingPriceOrder
);
event UserRegistration(address indexed user, uint64 userId);
struct AuctionData {
IERC20 auctioningToken;
IERC20 biddingToken;
uint256 orderCancellationEndDate;
uint256 auctionEndDate;
bytes32 initialAuctionOrder;
uint256 minimumBiddingAmountPerOrder;
uint256 interimSumBidAmount;
bytes32 interimOrder;
bytes32 clearingPriceOrder;
uint96 volumeClearingPriceOrder;
bool minFundingThresholdNotReached;
bool isAtomicClosureAllowed;
uint256 feeNumerator;
uint256 minFundingThreshold;
}
mapping(uint256 => IterableOrderedOrderSet.Data) internal sellOrders;
mapping(uint256 => AuctionData) public auctionData;
mapping(uint256 => address) public auctionAccessManager;
mapping(uint256 => bytes) public auctionAccessData;
IdToAddressBiMap.Data private registeredUsers;
uint64 public numUsers;
uint256 public auctionCounter;
constructor() public Ownable() {}
uint256 public feeNumerator = 0;
uint256 public constant FEE_DENOMINATOR = 1000;
uint64 public feeReceiverUserId = 1;
function setFeeParameters(
uint256 newFeeNumerator,
address newfeeReceiverAddress
) public onlyOwner() {
require(
newFeeNumerator <= 15,
"Fee is not allowed to be set higher than 1.5%"
);
feeReceiverUserId = getUserId(newfeeReceiverAddress);
feeNumerator = newFeeNumerator;
}
function initiateAuction(
IERC20 _auctioningToken,
IERC20 _biddingToken,
uint256 orderCancellationEndDate,
uint256 auctionEndDate,
uint96 _auctionedSellAmount,
uint96 _minBuyAmount,
uint256 minimumBiddingAmountPerOrder,
uint256 minFundingThreshold,
bool isAtomicClosureAllowed,
address accessManagerContract,
bytes memory accessManagerContractData
) public returns (uint256) {
_auctioningToken.safeTransferFrom(
msg.sender,
address(this),
_auctionedSellAmount.mul(FEE_DENOMINATOR.add(feeNumerator)).div(
FEE_DENOMINATOR
)
);
require(_auctionedSellAmount > 0, "cannot auction zero tokens");
require(_minBuyAmount > 0, "tokens cannot be auctioned for free");
require(
minimumBiddingAmountPerOrder > 0,
"minimumBiddingAmountPerOrder is not allowed to be zero"
);
require(
orderCancellationEndDate <= auctionEndDate,
"time periods are not configured correctly"
);
require(
auctionEndDate > block.timestamp,
"auction end date must be in the future"
);
auctionCounter = auctionCounter.add(1);
sellOrders[auctionCounter].initializeEmptyList();
uint64 userId = getUserId(msg.sender);
auctionData[auctionCounter] = AuctionData(
_auctioningToken,
_biddingToken,
orderCancellationEndDate,
auctionEndDate,
IterableOrderedOrderSet.encodeOrder(
userId,
_minBuyAmount,
_auctionedSellAmount
),
minimumBiddingAmountPerOrder,
0,
IterableOrderedOrderSet.QUEUE_START,
bytes32(0),
0,
false,
isAtomicClosureAllowed,
feeNumerator,
minFundingThreshold
);
auctionAccessManager[auctionCounter] = accessManagerContract;
auctionAccessData[auctionCounter] = accessManagerContractData;
emit NewAuction(
auctionCounter,
_auctioningToken,
_biddingToken,
orderCancellationEndDate,
auctionEndDate,
userId,
_auctionedSellAmount,
_minBuyAmount,
minimumBiddingAmountPerOrder,
minFundingThreshold,
accessManagerContract,
accessManagerContractData
);
return auctionCounter;
}
function placeSellOrders(
uint256 auctionId,
uint96[] memory _minBuyAmounts,
uint96[] memory _sellAmounts,
bytes32[] memory _prevSellOrders,
bytes calldata allowListCallData
) external atStageOrderPlacement(auctionId) returns (uint64 userId) {
return
_placeSellOrders(
auctionId,
_minBuyAmounts,
_sellAmounts,
_prevSellOrders,
allowListCallData,
msg.sender
);
}
function placeSellOrdersOnBehalf(
uint256 auctionId,
uint96[] memory _minBuyAmounts,
uint96[] memory _sellAmounts,
bytes32[] memory _prevSellOrders,
bytes calldata allowListCallData,
address orderSubmitter
) external atStageOrderPlacement(auctionId) returns (uint64 userId) {
return
_placeSellOrders(
auctionId,
_minBuyAmounts,
_sellAmounts,
_prevSellOrders,
allowListCallData,
orderSubmitter
);
}
function _placeSellOrders(
uint256 auctionId,
uint96[] memory _minBuyAmounts,
uint96[] memory _sellAmounts,
bytes32[] memory _prevSellOrders,
bytes calldata allowListCallData,
address orderSubmitter
) internal returns (uint64 userId) {
{
address allowListManger = auctionAccessManager[auctionId];
if (allowListManger != address(0)) {
require(
AllowListVerifier(allowListManger).isAllowed(
orderSubmitter,
auctionId,
allowListCallData
) == AllowListVerifierHelper.MAGICVALUE,
"user not allowed to place order"
);
}
}
{
(
,
uint96 buyAmountOfInitialAuctionOrder,
uint96 sellAmountOfInitialAuctionOrder
) = auctionData[auctionId].initialAuctionOrder.decodeOrder();
for (uint256 i = 0; i < _minBuyAmounts.length; i++) {
require(
_minBuyAmounts[i].mul(buyAmountOfInitialAuctionOrder) <
sellAmountOfInitialAuctionOrder.mul(_sellAmounts[i]),
"limit price not better than mimimal offer"
);
}
}
uint256 sumOfSellAmounts = 0;
userId = getUserId(orderSubmitter);
uint256 minimumBiddingAmountPerOrder =
auctionData[auctionId].minimumBiddingAmountPerOrder;
for (uint256 i = 0; i < _minBuyAmounts.length; i++) {
require(
_minBuyAmounts[i] > 0,
"_minBuyAmounts must be greater than 0"
);
require(
_sellAmounts[i] > minimumBiddingAmountPerOrder,
"order too small"
);
if (
sellOrders[auctionId].insert(
IterableOrderedOrderSet.encodeOrder(
userId,
_minBuyAmounts[i],
_sellAmounts[i]
),
_prevSellOrders[i]
)
) {
sumOfSellAmounts = sumOfSellAmounts.add(_sellAmounts[i]);
emit NewSellOrder(
auctionId,
userId,
_minBuyAmounts[i],
_sellAmounts[i]
);
}
}
auctionData[auctionId].biddingToken.safeTransferFrom(
msg.sender,
address(this),
sumOfSellAmounts
);
}
function cancelSellOrders(uint256 auctionId, bytes32[] memory _sellOrders)
public
atStageOrderPlacementAndCancelation(auctionId)
{
uint64 userId = getUserId(msg.sender);
uint256 claimableAmount = 0;
for (uint256 i = 0; i < _sellOrders.length; i++) {
bool success =
sellOrders[auctionId].removeKeepHistory(_sellOrders[i]);
if (success) {
(
uint64 userIdOfIter,
uint96 buyAmountOfIter,
uint96 sellAmountOfIter
) = _sellOrders[i].decodeOrder();
require(
userIdOfIter == userId,
"Only the user can cancel his orders"
);
claimableAmount = claimableAmount.add(sellAmountOfIter);
emit CancellationSellOrder(
auctionId,
userId,
buyAmountOfIter,
sellAmountOfIter
);
}
}
auctionData[auctionId].biddingToken.safeTransfer(
msg.sender,
claimableAmount
);
}
function precalculateSellAmountSum(
uint256 auctionId,
uint256 iterationSteps
) public atStageSolutionSubmission(auctionId) {
(, , uint96 auctioneerSellAmount) =
auctionData[auctionId].initialAuctionOrder.decodeOrder();
uint256 sumBidAmount = auctionData[auctionId].interimSumBidAmount;
bytes32 iterOrder = auctionData[auctionId].interimOrder;
for (uint256 i = 0; i < iterationSteps; i++) {
iterOrder = sellOrders[auctionId].next(iterOrder);
(, , uint96 sellAmountOfIter) = iterOrder.decodeOrder();
sumBidAmount = sumBidAmount.add(sellAmountOfIter);
}
require(
iterOrder != IterableOrderedOrderSet.QUEUE_END,
"reached end of order list"
);
(, uint96 buyAmountOfIter, uint96 sellAmountOfIter) =
iterOrder.decodeOrder();
require(
sumBidAmount.mul(buyAmountOfIter) <
auctioneerSellAmount.mul(sellAmountOfIter),
"too many orders summed up"
);
auctionData[auctionId].interimSumBidAmount = sumBidAmount;
auctionData[auctionId].interimOrder = iterOrder;
}
function settleAuctionAtomically(
uint256 auctionId,
uint96[] memory _minBuyAmount,
uint96[] memory _sellAmount,
bytes32[] memory _prevSellOrder,
bytes calldata allowListCallData
) public atStageSolutionSubmission(auctionId) {
require(
auctionData[auctionId].isAtomicClosureAllowed,
"not allowed to settle auction atomically"
);
require(
_minBuyAmount.length == 1 && _sellAmount.length == 1,
"Only one order can be placed atomically"
);
uint64 userId = getUserId(msg.sender);
require(
auctionData[auctionId].interimOrder.smallerThan(
IterableOrderedOrderSet.encodeOrder(
userId,
_minBuyAmount[0],
_sellAmount[0]
)
),
"precalculateSellAmountSum is already too advanced"
);
_placeSellOrders(
auctionId,
_minBuyAmount,
_sellAmount,
_prevSellOrder,
allowListCallData,
msg.sender
);
settleAuction(auctionId);
}
function settleAuction(uint256 auctionId)
public
atStageSolutionSubmission(auctionId)
returns (bytes32 clearingOrder)
{
(
uint64 auctioneerId,
uint96 minAuctionedBuyAmount,
uint96 fullAuctionedAmount
) = auctionData[auctionId].initialAuctionOrder.decodeOrder();
uint256 currentBidSum = auctionData[auctionId].interimSumBidAmount;
bytes32 currentOrder = auctionData[auctionId].interimOrder;
uint256 buyAmountOfIter;
uint256 sellAmountOfIter;
uint96 fillVolumeOfAuctioneerOrder = fullAuctionedAmount;
do {
bytes32 nextOrder = sellOrders[auctionId].next(currentOrder);
if (nextOrder == IterableOrderedOrderSet.QUEUE_END) {
break;
}
currentOrder = nextOrder;
(, buyAmountOfIter, sellAmountOfIter) = currentOrder.decodeOrder();
currentBidSum = currentBidSum.add(sellAmountOfIter);
} while (
currentBidSum.mul(buyAmountOfIter) <
fullAuctionedAmount.mul(sellAmountOfIter)
);
if (
currentBidSum > 0 &&
currentBidSum.mul(buyAmountOfIter) >=
fullAuctionedAmount.mul(sellAmountOfIter)
) {
uint256 uncoveredBids =
currentBidSum.sub(
fullAuctionedAmount.mul(sellAmountOfIter).div(
buyAmountOfIter
)
);
if (sellAmountOfIter >= uncoveredBids) {
uint256 sellAmountClearingOrder =
sellAmountOfIter.sub(uncoveredBids);
auctionData[auctionId]
.volumeClearingPriceOrder = sellAmountClearingOrder
.toUint96();
currentBidSum = currentBidSum.sub(uncoveredBids);
clearingOrder = currentOrder;
} else {
currentBidSum = currentBidSum.sub(sellAmountOfIter);
clearingOrder = IterableOrderedOrderSet.encodeOrder(
0,
fullAuctionedAmount,
currentBidSum.toUint96()
);
}
} else {
if (currentBidSum > minAuctionedBuyAmount) {
clearingOrder = IterableOrderedOrderSet.encodeOrder(
0,
fullAuctionedAmount,
currentBidSum.toUint96()
);
} else {
clearingOrder = IterableOrderedOrderSet.encodeOrder(
0,
fullAuctionedAmount,
minAuctionedBuyAmount
);
fillVolumeOfAuctioneerOrder = currentBidSum
.mul(fullAuctionedAmount)
.div(minAuctionedBuyAmount)
.toUint96();
}
}
auctionData[auctionId].clearingPriceOrder = clearingOrder;
if (auctionData[auctionId].minFundingThreshold > currentBidSum) {
auctionData[auctionId].minFundingThresholdNotReached = true;
}
processFeesAndAuctioneerFunds(
auctionId,
fillVolumeOfAuctioneerOrder,
auctioneerId,
fullAuctionedAmount
);
emit AuctionCleared(
auctionId,
fillVolumeOfAuctioneerOrder,
uint96(currentBidSum),
clearingOrder
);
auctionAccessManager[auctionId] = address(0);
delete auctionAccessData[auctionId];
auctionData[auctionId].initialAuctionOrder = bytes32(0);
auctionData[auctionId].interimOrder = bytes32(0);
auctionData[auctionId].interimSumBidAmount = uint256(0);
auctionData[auctionId].minimumBiddingAmountPerOrder = uint256(0);
}
function claimFromParticipantOrder(
uint256 auctionId,
bytes32[] memory orders
)
public
atStageFinished(auctionId)
returns (
uint256 sumAuctioningTokenAmount,
uint256 sumBiddingTokenAmount
)
{
for (uint256 i = 0; i < orders.length; i++) {
require(
sellOrders[auctionId].remove(orders[i]),
"order is no longer claimable"
);
}
AuctionData memory auction = auctionData[auctionId];
(, uint96 priceNumerator, uint96 priceDenominator) =
auction.clearingPriceOrder.decodeOrder();
(uint64 userId, , ) = orders[0].decodeOrder();
bool minFundingThresholdNotReached =
auctionData[auctionId].minFundingThresholdNotReached;
for (uint256 i = 0; i < orders.length; i++) {
(uint64 userIdOrder, uint96 buyAmount, uint96 sellAmount) =
orders[i].decodeOrder();
require(
userIdOrder == userId,
"only allowed to claim for same user"
);
if (minFundingThresholdNotReached) {
sumBiddingTokenAmount = sumBiddingTokenAmount.add(sellAmount);
} else {
if (orders[i] == auction.clearingPriceOrder) {
sumAuctioningTokenAmount = sumAuctioningTokenAmount.add(
auction
.volumeClearingPriceOrder
.mul(priceNumerator)
.div(priceDenominator)
);
sumBiddingTokenAmount = sumBiddingTokenAmount.add(
sellAmount.sub(auction.volumeClearingPriceOrder)
);
} else {
if (orders[i].smallerThan(auction.clearingPriceOrder)) {
sumAuctioningTokenAmount = sumAuctioningTokenAmount.add(
sellAmount.mul(priceNumerator).div(priceDenominator)
);
} else {
sumBiddingTokenAmount = sumBiddingTokenAmount.add(
sellAmount
);
}
}
}
emit ClaimedFromOrder(auctionId, userId, buyAmount, sellAmount);
}
sendOutTokens(
auctionId,
sumAuctioningTokenAmount,
sumBiddingTokenAmount,
userId
);
}
function processFeesAndAuctioneerFunds(
uint256 auctionId,
uint256 fillVolumeOfAuctioneerOrder,
uint64 auctioneerId,
uint96 fullAuctionedAmount
) internal {
uint256 feeAmount =
fullAuctionedAmount.mul(auctionData[auctionId].feeNumerator).div(
FEE_DENOMINATOR
);
if (auctionData[auctionId].minFundingThresholdNotReached) {
sendOutTokens(
auctionId,
fullAuctionedAmount.add(feeAmount),
0,
auctioneerId
);
} else {
(, uint96 priceNumerator, uint96 priceDenominator) =
auctionData[auctionId].clearingPriceOrder.decodeOrder();
uint256 unsettledAuctionTokens =
fullAuctionedAmount.sub(fillVolumeOfAuctioneerOrder);
uint256 auctioningTokenAmount =
unsettledAuctionTokens.add(
feeAmount.mul(unsettledAuctionTokens).div(
fullAuctionedAmount
)
);
uint256 biddingTokenAmount =
fillVolumeOfAuctioneerOrder.mul(priceDenominator).div(
priceNumerator
);
sendOutTokens(
auctionId,
auctioningTokenAmount,
biddingTokenAmount,
auctioneerId
);
sendOutTokens(
auctionId,
feeAmount.mul(fillVolumeOfAuctioneerOrder).div(
fullAuctionedAmount
),
0,
feeReceiverUserId
);
}
}
function sendOutTokens(
uint256 auctionId,
uint256 auctioningTokenAmount,
uint256 biddingTokenAmount,
uint64 userId
) internal {
address userAddress = registeredUsers.getAddressAt(userId);
if (auctioningTokenAmount > 0) {
auctionData[auctionId].auctioningToken.safeTransfer(
userAddress,
auctioningTokenAmount
);
}
if (biddingTokenAmount > 0) {
auctionData[auctionId].biddingToken.safeTransfer(
userAddress,
biddingTokenAmount
);
}
}
function registerUser(address user) public returns (uint64 userId) {
numUsers = numUsers.add(1).toUint64();
require(
registeredUsers.insert(numUsers, user),
"User already registered"
);
userId = numUsers;
emit UserRegistration(user, userId);
}
function getUserId(address user) public returns (uint64 userId) {
if (registeredUsers.hasAddress(user)) {
userId = registeredUsers.getId(user);
} else {
userId = registerUser(user);
emit NewUser(userId, user);
}
}
function getSecondsRemainingInBatch(uint256 auctionId)
public
view
returns (uint256)
{
if (auctionData[auctionId].auctionEndDate < block.timestamp) {
return 0;
}
return auctionData[auctionId].auctionEndDate.sub(block.timestamp);
}
function containsOrder(uint256 auctionId, bytes32 order)
public
view
returns (bool)
{
return sellOrders[auctionId].contains(order);
}
}
文件 5 的 12:IERC20.sol
pragma solidity >=0.6.0 <0.8.0;
interface IERC20 {
function totalSupply() external view returns (uint256);
function balanceOf(address account) external view returns (uint256);
function transfer(address recipient, uint256 amount) external returns (bool);
function allowance(address owner, address spender) external view returns (uint256);
function approve(address spender, uint256 amount) external returns (bool);
function transferFrom(address sender, address recipient, uint256 amount) external returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
}
文件 6 的 12:IdToAddressBiMap.sol
pragma solidity ^0.6.0;
library IdToAddressBiMap {
struct Data {
mapping(uint64 => address) idToAddress;
mapping(address => uint64) addressToId;
}
function hasId(Data storage self, uint64 id) internal view returns (bool) {
return self.idToAddress[id + 1] != address(0);
}
function hasAddress(Data storage self, address addr)
internal
view
returns (bool)
{
return self.addressToId[addr] != 0;
}
function getAddressAt(Data storage self, uint64 id)
internal
view
returns (address)
{
require(hasId(self, id), "Must have ID to get Address");
return self.idToAddress[id + 1];
}
function getId(Data storage self, address addr)
internal
view
returns (uint64)
{
require(hasAddress(self, addr), "Must have Address to get ID");
return self.addressToId[addr] - 1;
}
function insert(
Data storage self,
uint64 id,
address addr
) internal returns (bool) {
require(addr != address(0), "Cannot insert zero address");
require(id != uint64(-1), "Cannot insert max uint64");
if (
self.addressToId[addr] != 0 ||
self.idToAddress[id + 1] != address(0)
) {
return false;
}
self.idToAddress[id + 1] = addr;
self.addressToId[addr] = id + 1;
return true;
}
}
文件 7 的 12:IterableOrderedOrderSet.sol
pragma solidity >=0.6.8;
import "@openzeppelin/contracts/math/SafeMath.sol";
library IterableOrderedOrderSet {
using SafeMath for uint96;
using IterableOrderedOrderSet for bytes32;
bytes32 internal constant QUEUE_START =
0x0000000000000000000000000000000000000000000000000000000000000001;
bytes32 internal constant QUEUE_END =
0xffffffffffffffffffffffffffffffffffffffff000000000000000000000001;
struct Data {
mapping(bytes32 => bytes32) nextMap;
mapping(bytes32 => bytes32) prevMap;
}
struct Order {
uint64 owner;
uint96 buyAmount;
uint96 sellAmount;
}
function initializeEmptyList(Data storage self) internal {
self.nextMap[QUEUE_START] = QUEUE_END;
self.prevMap[QUEUE_END] = QUEUE_START;
}
function isEmpty(Data storage self) internal view returns (bool) {
return self.nextMap[QUEUE_START] == QUEUE_END;
}
function insert(
Data storage self,
bytes32 elementToInsert,
bytes32 elementBeforeNewOne
) internal returns (bool) {
(, , uint96 denominator) = decodeOrder(elementToInsert);
require(denominator != uint96(0), "Inserting zero is not supported");
require(
elementToInsert != QUEUE_START && elementToInsert != QUEUE_END,
"Inserting element is not valid"
);
if (contains(self, elementToInsert)) {
return false;
}
if (
elementBeforeNewOne != QUEUE_START &&
self.prevMap[elementBeforeNewOne] == bytes32(0)
) {
return false;
}
if (!elementBeforeNewOne.smallerThan(elementToInsert)) {
return false;
}
while (self.nextMap[elementBeforeNewOne] == bytes32(0)) {
elementBeforeNewOne = self.prevMap[elementBeforeNewOne];
}
bytes32 previous;
bytes32 current = elementBeforeNewOne;
do {
previous = current;
current = self.nextMap[current];
} while (current.smallerThan(elementToInsert));
self.nextMap[previous] = elementToInsert;
self.prevMap[current] = elementToInsert;
self.prevMap[elementToInsert] = previous;
self.nextMap[elementToInsert] = current;
return true;
}
function removeKeepHistory(Data storage self, bytes32 elementToRemove)
internal
returns (bool)
{
if (!contains(self, elementToRemove)) {
return false;
}
bytes32 previousElement = self.prevMap[elementToRemove];
bytes32 nextElement = self.nextMap[elementToRemove];
self.nextMap[previousElement] = nextElement;
self.prevMap[nextElement] = previousElement;
self.nextMap[elementToRemove] = bytes32(0);
return true;
}
function remove(Data storage self, bytes32 elementToRemove)
internal
returns (bool)
{
bool result = removeKeepHistory(self, elementToRemove);
if (result) {
self.prevMap[elementToRemove] = bytes32(0);
}
return result;
}
function contains(Data storage self, bytes32 value)
internal
view
returns (bool)
{
if (value == QUEUE_START) {
return false;
}
return self.nextMap[value] != bytes32(0);
}
function smallerThan(bytes32 orderLeft, bytes32 orderRight)
internal
pure
returns (bool)
{
(
uint64 userIdLeft,
uint96 priceNumeratorLeft,
uint96 priceDenominatorLeft
) = decodeOrder(orderLeft);
(
uint64 userIdRight,
uint96 priceNumeratorRight,
uint96 priceDenominatorRight
) = decodeOrder(orderRight);
if (
priceNumeratorLeft.mul(priceDenominatorRight) <
priceNumeratorRight.mul(priceDenominatorLeft)
) return true;
if (
priceNumeratorLeft.mul(priceDenominatorRight) >
priceNumeratorRight.mul(priceDenominatorLeft)
) return false;
if (priceNumeratorLeft < priceNumeratorRight) return true;
if (priceNumeratorLeft > priceNumeratorRight) return false;
require(
userIdLeft != userIdRight,
"user is not allowed to place same order twice"
);
if (userIdLeft < userIdRight) {
return true;
}
return false;
}
function first(Data storage self) internal view returns (bytes32) {
require(!isEmpty(self), "Trying to get first from empty set");
return self.nextMap[QUEUE_START];
}
function next(Data storage self, bytes32 value)
internal
view
returns (bytes32)
{
require(value != QUEUE_END, "Trying to get next of last element");
bytes32 nextElement = self.nextMap[value];
require(
nextElement != bytes32(0),
"Trying to get next of non-existent element"
);
return nextElement;
}
function decodeOrder(bytes32 _orderData)
internal
pure
returns (
uint64 userId,
uint96 buyAmount,
uint96 sellAmount
)
{
userId = uint64(uint256(_orderData) >> 192);
buyAmount = uint96(uint256(_orderData) >> 96);
sellAmount = uint96(uint256(_orderData));
}
function encodeOrder(
uint64 userId,
uint96 buyAmount,
uint96 sellAmount
) internal pure returns (bytes32) {
return
bytes32(
(uint256(userId) << 192) +
(uint256(buyAmount) << 96) +
uint256(sellAmount)
);
}
}
文件 8 的 12:Math.sol
pragma solidity >=0.6.0 <0.8.0;
library Math {
function max(uint256 a, uint256 b) internal pure returns (uint256) {
return a >= b ? a : b;
}
function min(uint256 a, uint256 b) internal pure returns (uint256) {
return a < b ? a : b;
}
function average(uint256 a, uint256 b) internal pure returns (uint256) {
return (a / 2) + (b / 2) + ((a % 2 + b % 2) / 2);
}
}
文件 9 的 12:Ownable.sol
pragma solidity >=0.6.0 <0.8.0;
import "../GSN/Context.sol";
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
constructor () internal {
address msgSender = _msgSender();
_owner = msgSender;
emit OwnershipTransferred(address(0), msgSender);
}
function owner() public view returns (address) {
return _owner;
}
modifier onlyOwner() {
require(_owner == _msgSender(), "Ownable: caller is not the owner");
_;
}
function renounceOwnership() public virtual onlyOwner {
emit OwnershipTransferred(_owner, address(0));
_owner = address(0);
}
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
emit OwnershipTransferred(_owner, newOwner);
_owner = newOwner;
}
}
文件 10 的 12:SafeCast.sol
pragma solidity >=0.6.0 <0.8.0;
library SafeCast {
function toUint96(uint256 value) internal pure returns (uint96) {
require(value < 2**96, "SafeCast: value doesn't fit in 96 bits");
return uint96(value);
}
function toUint64(uint256 value) internal pure returns (uint64) {
require(value < 2**64, "SafeCast: value doesn't fit in 64 bits");
return uint64(value);
}
}
文件 11 的 12:SafeERC20.sol
pragma solidity >=0.6.0 <0.8.0;
import "./IERC20.sol";
import "../../math/SafeMath.sol";
import "../../utils/Address.sol";
library SafeERC20 {
using SafeMath for uint256;
using Address for address;
function safeTransfer(IERC20 token, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(IERC20 token, address from, address to, uint256 value) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
function safeApprove(IERC20 token, address spender, uint256 value) internal {
require((value == 0) || (token.allowance(address(this), spender) == 0),
"SafeERC20: approve from non-zero to non-zero allowance"
);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, value));
}
function safeIncreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).add(value);
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(IERC20 token, address spender, uint256 value) internal {
uint256 newAllowance = token.allowance(address(this), spender).sub(value, "SafeERC20: decreased allowance below zero");
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function _callOptionalReturn(IERC20 token, bytes memory data) private {
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
文件 12 的 12:SafeMath.sol
pragma solidity >=0.6.0 <0.8.0;
library SafeMath {
function add(uint256 a, uint256 b) internal pure returns (uint256) {
uint256 c = a + b;
require(c >= a, "SafeMath: addition overflow");
return c;
}
function sub(uint256 a, uint256 b) internal pure returns (uint256) {
return sub(a, b, "SafeMath: subtraction overflow");
}
function sub(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b <= a, errorMessage);
uint256 c = a - b;
return c;
}
function mul(uint256 a, uint256 b) internal pure returns (uint256) {
if (a == 0) {
return 0;
}
uint256 c = a * b;
require(c / a == b, "SafeMath: multiplication overflow");
return c;
}
function div(uint256 a, uint256 b) internal pure returns (uint256) {
return div(a, b, "SafeMath: division by zero");
}
function div(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b > 0, errorMessage);
uint256 c = a / b;
return c;
}
function mod(uint256 a, uint256 b) internal pure returns (uint256) {
return mod(a, b, "SafeMath: modulo by zero");
}
function mod(uint256 a, uint256 b, string memory errorMessage) internal pure returns (uint256) {
require(b != 0, errorMessage);
return a % b;
}
}
{
"compilationTarget": {
"contracts/EasyAuction.sol": "EasyAuction"
},
"evmVersion": "istanbul",
"libraries": {},
"metadata": {
"bytecodeHash": "ipfs",
"useLiteralContent": true
},
"optimizer": {
"enabled": false,
"runs": 200
},
"remappings": []
}
[{"inputs":[],"stateMutability":"nonpayable","type":"constructor"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"auctionId","type":"uint256"},{"indexed":false,"internalType":"uint96","name":"soldAuctioningTokens","type":"uint96"},{"indexed":false,"internalType":"uint96","name":"soldBiddingTokens","type":"uint96"},{"indexed":false,"internalType":"bytes32","name":"clearingPriceOrder","type":"bytes32"}],"name":"AuctionCleared","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"auctionId","type":"uint256"},{"indexed":true,"internalType":"uint64","name":"userId","type":"uint64"},{"indexed":false,"internalType":"uint96","name":"buyAmount","type":"uint96"},{"indexed":false,"internalType":"uint96","name":"sellAmount","type":"uint96"}],"name":"CancellationSellOrder","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"auctionId","type":"uint256"},{"indexed":true,"internalType":"uint64","name":"userId","type":"uint64"},{"indexed":false,"internalType":"uint96","name":"buyAmount","type":"uint96"},{"indexed":false,"internalType":"uint96","name":"sellAmount","type":"uint96"}],"name":"ClaimedFromOrder","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"auctionId","type":"uint256"},{"indexed":true,"internalType":"contract IERC20","name":"_auctioningToken","type":"address"},{"indexed":true,"internalType":"contract IERC20","name":"_biddingToken","type":"address"},{"indexed":false,"internalType":"uint256","name":"orderCancellationEndDate","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"auctionEndDate","type":"uint256"},{"indexed":false,"internalType":"uint64","name":"userId","type":"uint64"},{"indexed":false,"internalType":"uint96","name":"_auctionedSellAmount","type":"uint96"},{"indexed":false,"internalType":"uint96","name":"_minBuyAmount","type":"uint96"},{"indexed":false,"internalType":"uint256","name":"minimumBiddingAmountPerOrder","type":"uint256"},{"indexed":false,"internalType":"uint256","name":"minFundingThreshold","type":"uint256"},{"indexed":false,"internalType":"address","name":"allowListContract","type":"address"},{"indexed":false,"internalType":"bytes","name":"allowListData","type":"bytes"}],"name":"NewAuction","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint256","name":"auctionId","type":"uint256"},{"indexed":true,"internalType":"uint64","name":"userId","type":"uint64"},{"indexed":false,"internalType":"uint96","name":"buyAmount","type":"uint96"},{"indexed":false,"internalType":"uint96","name":"sellAmount","type":"uint96"}],"name":"NewSellOrder","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"uint64","name":"userId","type":"uint64"},{"indexed":true,"internalType":"address","name":"userAddress","type":"address"}],"name":"NewUser","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"previousOwner","type":"address"},{"indexed":true,"internalType":"address","name":"newOwner","type":"address"}],"name":"OwnershipTransferred","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"internalType":"address","name":"user","type":"address"},{"indexed":false,"internalType":"uint64","name":"userId","type":"uint64"}],"name":"UserRegistration","type":"event"},{"inputs":[],"name":"FEE_DENOMINATOR","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"auctionAccessData","outputs":[{"internalType":"bytes","name":"","type":"bytes"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"auctionAccessManager","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"auctionCounter","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"","type":"uint256"}],"name":"auctionData","outputs":[{"internalType":"contract IERC20","name":"auctioningToken","type":"address"},{"internalType":"contract IERC20","name":"biddingToken","type":"address"},{"internalType":"uint256","name":"orderCancellationEndDate","type":"uint256"},{"internalType":"uint256","name":"auctionEndDate","type":"uint256"},{"internalType":"bytes32","name":"initialAuctionOrder","type":"bytes32"},{"internalType":"uint256","name":"minimumBiddingAmountPerOrder","type":"uint256"},{"internalType":"uint256","name":"interimSumBidAmount","type":"uint256"},{"internalType":"bytes32","name":"interimOrder","type":"bytes32"},{"internalType":"bytes32","name":"clearingPriceOrder","type":"bytes32"},{"internalType":"uint96","name":"volumeClearingPriceOrder","type":"uint96"},{"internalType":"bool","name":"minFundingThresholdNotReached","type":"bool"},{"internalType":"bool","name":"isAtomicClosureAllowed","type":"bool"},{"internalType":"uint256","name":"feeNumerator","type":"uint256"},{"internalType":"uint256","name":"minFundingThreshold","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"},{"internalType":"bytes32[]","name":"_sellOrders","type":"bytes32[]"}],"name":"cancelSellOrders","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"},{"internalType":"bytes32[]","name":"orders","type":"bytes32[]"}],"name":"claimFromParticipantOrder","outputs":[{"internalType":"uint256","name":"sumAuctioningTokenAmount","type":"uint256"},{"internalType":"uint256","name":"sumBiddingTokenAmount","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"},{"internalType":"bytes32","name":"order","type":"bytes32"}],"name":"containsOrder","outputs":[{"internalType":"bool","name":"","type":"bool"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeNumerator","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"feeReceiverUserId","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"}],"name":"getSecondsRemainingInBatch","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"getUserId","outputs":[{"internalType":"uint64","name":"userId","type":"uint64"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"contract IERC20","name":"_auctioningToken","type":"address"},{"internalType":"contract IERC20","name":"_biddingToken","type":"address"},{"internalType":"uint256","name":"orderCancellationEndDate","type":"uint256"},{"internalType":"uint256","name":"auctionEndDate","type":"uint256"},{"internalType":"uint96","name":"_auctionedSellAmount","type":"uint96"},{"internalType":"uint96","name":"_minBuyAmount","type":"uint96"},{"internalType":"uint256","name":"minimumBiddingAmountPerOrder","type":"uint256"},{"internalType":"uint256","name":"minFundingThreshold","type":"uint256"},{"internalType":"bool","name":"isAtomicClosureAllowed","type":"bool"},{"internalType":"address","name":"accessManagerContract","type":"address"},{"internalType":"bytes","name":"accessManagerContractData","type":"bytes"}],"name":"initiateAuction","outputs":[{"internalType":"uint256","name":"","type":"uint256"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"numUsers","outputs":[{"internalType":"uint64","name":"","type":"uint64"}],"stateMutability":"view","type":"function"},{"inputs":[],"name":"owner","outputs":[{"internalType":"address","name":"","type":"address"}],"stateMutability":"view","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"},{"internalType":"uint96[]","name":"_minBuyAmounts","type":"uint96[]"},{"internalType":"uint96[]","name":"_sellAmounts","type":"uint96[]"},{"internalType":"bytes32[]","name":"_prevSellOrders","type":"bytes32[]"},{"internalType":"bytes","name":"allowListCallData","type":"bytes"}],"name":"placeSellOrders","outputs":[{"internalType":"uint64","name":"userId","type":"uint64"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"},{"internalType":"uint96[]","name":"_minBuyAmounts","type":"uint96[]"},{"internalType":"uint96[]","name":"_sellAmounts","type":"uint96[]"},{"internalType":"bytes32[]","name":"_prevSellOrders","type":"bytes32[]"},{"internalType":"bytes","name":"allowListCallData","type":"bytes"},{"internalType":"address","name":"orderSubmitter","type":"address"}],"name":"placeSellOrdersOnBehalf","outputs":[{"internalType":"uint64","name":"userId","type":"uint64"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"},{"internalType":"uint256","name":"iterationSteps","type":"uint256"}],"name":"precalculateSellAmountSum","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"user","type":"address"}],"name":"registerUser","outputs":[{"internalType":"uint64","name":"userId","type":"uint64"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[],"name":"renounceOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"newFeeNumerator","type":"uint256"},{"internalType":"address","name":"newfeeReceiverAddress","type":"address"}],"name":"setFeeParameters","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"}],"name":"settleAuction","outputs":[{"internalType":"bytes32","name":"clearingOrder","type":"bytes32"}],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"uint256","name":"auctionId","type":"uint256"},{"internalType":"uint96[]","name":"_minBuyAmount","type":"uint96[]"},{"internalType":"uint96[]","name":"_sellAmount","type":"uint96[]"},{"internalType":"bytes32[]","name":"_prevSellOrder","type":"bytes32[]"},{"internalType":"bytes","name":"allowListCallData","type":"bytes"}],"name":"settleAuctionAtomically","outputs":[],"stateMutability":"nonpayable","type":"function"},{"inputs":[{"internalType":"address","name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"stateMutability":"nonpayable","type":"function"}]