Verified contractPartialMIT
Interaction
Address0xdec10aaff57196fa38c837b9c4931048ad8e4656
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.15
- Same code
Only this contract
Details
- Compiler
- solc 0.8.15+commit.e14f2714
- Language
- solidity
- Optimization
- Yes, 100 runs
- EVM version
- london
- License
- MIT
- Match
- Partialcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 17,476,147
- Creation transaction
- 0x7411e42924…acc319206b
- Libraries
- contracts/libraries/AuctionProxy.sol:AuctionProxy0x1a80…bd5d88
22 files
- contracts/Interaction.solmain22.83 KB
- @openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol5.67 KB
- @openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol2.78 KB
- @openzeppelin/contracts-upgradeable/token/ERC20/extensions/draft-IERC20PermitUpgradeable.sol2.26 KB
- @openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol4.59 KB
- @openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol7.37 KB
- @openzeppelin/contracts/utils/structs/EnumerableSet.sol12.18 KB
- contracts/ceros/interfaces/IDavosProvider.sol1.01 KB
- contracts/ceros/interfaces/IInteraction.sol1.96 KB
- contracts/dMath.sol1.17 KB
- contracts/interfaces/ClipperLike.sol1.16 KB
- contracts/interfaces/DavosJoinLike.sol203 B
- contracts/interfaces/DogLike.sol1.57 KB
- contracts/interfaces/GemJoinLike.sol281 B
- contracts/interfaces/GemLike.sol251 B
- contracts/interfaces/IRewards.sol604 B
- contracts/interfaces/JugLike.sol386 B
- contracts/interfaces/PipLike.sol143 B
- contracts/interfaces/SpotLike.sol340 B
- contracts/interfaces/VatLike.sol1.34 KB
- contracts/libraries/AuctionProxy.sol4.61 KB
- contracts/oracle/libraries/FullMath.sol1.26 KB
contracts/Interaction.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol";
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "@openzeppelin/contracts/utils/structs/EnumerableSet.sol";
import "./dMath.sol";
import "./oracle/libraries/FullMath.sol";
import "./interfaces/VatLike.sol";
import "./interfaces/DavosJoinLike.sol";
import "./interfaces/GemJoinLike.sol";
import "./interfaces/JugLike.sol";
import "./interfaces/DogLike.sol";
import "./interfaces/PipLike.sol";
import "./interfaces/SpotLike.sol";
import "./interfaces/IRewards.sol";
import "./ceros/interfaces/IDavosProvider.sol";
import "./ceros/interfaces/IInteraction.sol";
import "./libraries/AuctionProxy.sol";
uint256 constant WAD = 10 ** 18;
uint256 constant RAD = 10 ** 45;
uint256 constant YEAR = 31556952; //seconds in year (365.2425 * 24 * 3600)
contract Interaction is Initializable, IInteraction {
mapping(address => uint) public wards;
function rely(address usr) external auth {wards[usr] = 1;}
function deny(address usr) external auth {wards[usr] = 0;}
modifier auth {
require(wards[msg.sender] == 1, "Interaction/not-authorized");
_;
}
VatLike public vat;
SpotLike public spotter;
IERC20Upgradeable public davos;
DavosJoinLike public davosJoin;
JugLike public jug;
address public dog;
IRewards public dgtRewards;
mapping(address => uint256) public deposits;
mapping(address => CollateralType) public collaterals;
using SafeERC20Upgradeable for IERC20Upgradeable;
using EnumerableSet for EnumerableSet.AddressSet;
mapping(address => address) public davosProviders; // e.g. Auction purchase from ceamaticc to amaticc
uint256 public whitelistMode;
address public whitelistOperator;
mapping(address => uint) public whitelist;
function enableWhitelist() external auth {whitelistMode = 1;}
function disableWhitelist() external auth {whitelistMode = 0;}
function setWhitelistOperator(address usr) external auth {
whitelistOperator = usr;
}
function addToWhitelist(address[] memory usrs) external operatorOrWard {
for(uint256 i = 0; i < usrs.length; i++) {
whitelist[usrs[i]] = 1;
emit AddedToWhitelist(usrs[i]);
}
}
function removeFromWhitelist(address[] memory usrs) external operatorOrWard {
for(uint256 i = 0; i < usrs.length; i++) {
whitelist[usrs[i]] = 0;
emit RemovedFromWhitelist(usrs[i]);
}
}
modifier whitelisted(address participant) {
if (whitelistMode == 1)
require(whitelist[participant] == 1, "Interaction/not-in-whitelist");
_;
}
modifier operatorOrWard {
require(msg.sender == whitelistOperator || wards[msg.sender] == 1, "Interaction/not-operator-or-ward");
_;
}
/// @custom:oz-upgrades-unsafe-allow constructor
// --- Constructor ---
constructor() { _disableInitializers(); }
function initialize(
address vat_,
address spot_,
address davos_,
address davosJoin_,
address jug_,
address dog_,
address rewards_
) external initializer {
wards[msg.sender] = 1;
vat = VatLike(vat_);
spotter = SpotLike(spot_);
davos = IERC20Upgradeable(davos_);
davosJoin = DavosJoinLike(davosJoin_);
jug = JugLike(jug_);
dog = dog_;
dgtRewards = IRewards(rewards_);
vat.hope(davosJoin_);
davos.approve(davosJoin_, type(uint256).max);
}
function setCores(address vat_, address spot_, address davosJoin_,
address jug_) public auth {
// Reset previous approval
davos.approve(address(davosJoin), 0);
vat = VatLike(vat_);
spotter = SpotLike(spot_);
davosJoin = DavosJoinLike(davosJoin_);
jug = JugLike(jug_);
vat.hope(davosJoin_);
davos.approve(davosJoin_, type(uint256).max);
}
function setDavosApprove() public auth {
davos.approve(address(davosJoin), type(uint256).max);
}
function setCollateralType(
address token,
address gemJoin,
bytes32 ilk,
address clip,
uint256 mat
) external auth {
require(collaterals[token].live == 0, "Interaction/token-already-init");
require(ilk != bytes32(0), "Interaction/empty-ilk");
vat.init(ilk);
jug.init(ilk);
spotter.file(ilk, "mat", mat);
collaterals[token] = CollateralType(GemJoinLike(gemJoin), ilk, 1, clip);
IERC20Upgradeable(token).safeApprove(gemJoin, type(uint256).max);
vat.rely(gemJoin);
emit CollateralEnabled(token, ilk);
}
function setCollateralDuty(address token, uint data) external auth {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
jug.drip(collateralType.ilk);
jug.file(collateralType.ilk, "duty", data);
}
function setDavosProvider(address token, address davosProvider) external auth {
require(davosProvider != address(0));
davosProviders[token] = davosProvider;
emit ChangeDavosProvider(davosProvider);
}
function removeCollateralType(address token) external auth {
require(collaterals[token].live != 0, "Interaction/token-not-init");
collaterals[token].live = 2; //STOPPED
address gemJoin = address(collaterals[token].gem);
vat.deny(gemJoin);
IERC20Upgradeable(token).safeApprove(gemJoin, 0);
emit CollateralDisabled(token, collaterals[token].ilk);
}
function reenableCollateralType(address token) external auth {
collaterals[token].live = 1;
address gemJoin = address(collaterals[token].gem);
vat.rely(gemJoin);
IERC20Upgradeable(token).safeApprove(gemJoin, type(uint256).max);
emit CollateralEnabled(token, collaterals[token].ilk);
}
function stringToBytes32(string memory source) external pure returns (bytes32 result) {
bytes memory tempEmptyStringTest = bytes(source);
if (tempEmptyStringTest.length == 0) {
return 0x0;
}
assembly {
result := mload(add(source, 32))
}
}
function deposit(
address participant,
address token,
uint256 dink
) external whitelisted(participant) returns (uint256) {
CollateralType memory collateralType = collaterals[token];
require(collateralType.live == 1, "Interaction/inactive-collateral");
if (davosProviders[token] != address(0)) {
require(
msg.sender == davosProviders[token],
"Interaction/only davos provider can deposit for this token"
);
}
require(dink <= uint256(type(int256).max), "Interaction/too-much-requested");
drip(token);
uint256 preBalance = IERC20Upgradeable(token).balanceOf(address(this));
IERC20Upgradeable(token).safeTransferFrom(msg.sender, address(this), dink);
uint256 postBalance = IERC20Upgradeable(token).balanceOf(address(this));
require(preBalance + dink == postBalance, "Interaction/deposit-deflated");
collateralType.gem.join(participant, dink);
vat.behalf(participant, address(this));
vat.frob(collateralType.ilk, participant, participant, participant, int256(dink), 0);
deposits[token] += dink;
emit Deposit(participant, token, dink, locked(token, participant));
return dink;
}
function borrow(address token, uint256 davosAmount) external returns (uint256) {
CollateralType memory collateralType = collaterals[token];
require(collateralType.live == 1, "Interaction/inactive-collateral");
require(davosAmount > 0,"Interaction/invalid-davosAmount");
drip(token);
dropRewards(token, msg.sender);
(, uint256 rate, , ,) = vat.ilks(collateralType.ilk);
int256 dart = int256(davosAmount * RAY / rate);
require(dart >= 0, "Interaction/too-much-requested");
if (uint256(dart) * rate < davosAmount * RAY) {
dart += 1; //ceiling
}
vat.frob(collateralType.ilk, msg.sender, msg.sender, msg.sender, 0, dart);
vat.move(msg.sender, address(this), davosAmount * RAY);
davosJoin.exit(msg.sender, davosAmount);
(uint256 ink, uint256 art) = vat.urns(collateralType.ilk, msg.sender);
uint256 liqPrice = liquidationPriceForDebt(collateralType.ilk, ink, art);
emit Borrow(msg.sender, token, ink, davosAmount, liqPrice);
return uint256(dart);
}
function dropRewards(address token, address usr) public {
dgtRewards.drop(token, usr);
}
// Burn user's DAVOS.
// N.B. User collateral stays the same.
function payback(address token, uint256 davosAmount) external returns (int256) {
require(davosAmount > 0,"Interaction/invalid-davosAmount");
CollateralType memory collateralType = collaterals[token];
// _checkIsLive(collateralType.live); Checking in the `drip` function
(,uint256 rate,,,) = vat.ilks(collateralType.ilk);
(,uint256 art) = vat.urns(collateralType.ilk, msg.sender);
int256 dart;
uint256 realAmount = davosAmount;
uint256 debt = rate * art;
if (realAmount * RAY >= debt) { // Close CDP
dart = int(art);
realAmount = debt / RAY;
realAmount = realAmount * RAY == debt ? realAmount : realAmount + 1;
} else { // Less/Greater than dust
dart = int256(FullMath.mulDiv(realAmount, RAY, rate));
}
IERC20Upgradeable(davos).safeTransferFrom(msg.sender, address(this), realAmount);
davosJoin.join(msg.sender, realAmount);
require(dart >= 0, "Interaction/too-much-requested");
vat.frob(collateralType.ilk, msg.sender, msg.sender, msg.sender, 0, - dart);
dropRewards(token, msg.sender);
drip(token);
(uint256 ink, uint256 userDebt) = vat.urns(collateralType.ilk, msg.sender);
uint256 liqPrice = liquidationPriceForDebt(collateralType.ilk, ink, userDebt);
emit Payback(msg.sender, token, realAmount, userDebt, liqPrice);
return dart;
}
// Unlock and transfer to the user `dink` amount of aMATICc
function withdraw(
address participant,
address token,
uint256 dink
) external returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
if (davosProviders[token] != address(0)) {
require(
msg.sender == davosProviders[token],
"Interaction/Only davos provider can call this function for this token"
);
} else {
require(
msg.sender == participant,
"Interaction/Caller must be the same address as participant"
);
}
uint256 unlocked = free(token, participant);
if (unlocked < dink) {
int256 diff = int256(dink) - int256(unlocked);
vat.frob(collateralType.ilk, participant, participant, participant, - diff, 0);
vat.flux(collateralType.ilk, participant, address(this), uint256(diff));
}
// Collateral is actually transferred back to user inside `exit` operation.
// See GemJoin.exit()
collateralType.gem.exit(msg.sender, dink);
deposits[token] -= dink;
emit Withdraw(participant, dink);
return dink;
}
function drip(address token) public {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
jug.drip(collateralType.ilk);
}
function poke(address token) public {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
spotter.poke(collateralType.ilk);
}
function setRewards(address rewards) external auth {
dgtRewards = IRewards(rewards);
emit ChangeRewards(rewards);
}
// /////////////////////////////////
// //// VIEW ////
// /////////////////////////////////
// Price of the collateral asset(aMATICc) from Oracle
function collateralPrice(address token) public view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(PipLike pip,) = spotter.ilks(collateralType.ilk);
(bytes32 price, bool has) = pip.peek();
if (has) {
return uint256(price);
} else {
return 0;
}
}
// Returns the DAVOS price in $
function davosPrice(address token) external view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(, uint256 rate,,,) = vat.ilks(collateralType.ilk);
return rate / 10 ** 9;
}
// Returns the collateral ratio in percents with 18 decimals
function collateralRate(address token) external view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(,uint256 mat) = spotter.ilks(collateralType.ilk);
require(mat != 0, "Interaction/spot-not-init");
return 10 ** 45 / mat;
}
// Total aMATICc deposited nominated in $
function depositTVL(address token) external view returns (uint256) {
return deposits[token] * collateralPrice(token) / WAD;
}
// Total DAVOS borrowed by all users
function collateralTVL(address token) external view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 Art, uint256 rate,,,) = vat.ilks(collateralType.ilk);
return FullMath.mulDiv(Art, rate, RAY);
}
// Not locked user balance in aMATICc
function free(address token, address usr) public view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
return vat.gem(collateralType.ilk, usr);
}
// User collateral in aMATICc
function locked(address token, address usr) public view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 ink,) = vat.urns(collateralType.ilk, usr);
return ink;
}
// Total borrowed DAVOS
function borrowed(address token, address usr) external view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(,uint256 rate,,,) = vat.ilks(collateralType.ilk);
(, uint256 art) = vat.urns(collateralType.ilk, usr);
// 100 Wei is added as a ceiling to help close CDP in repay()
if ((art * rate) / RAY != 0) {
return ((art * rate) / RAY) + 100;
}
else {
return 0;
}
}
// Collateral minus borrowed. Basically free collateral (nominated in DAVOS)
function availableToBorrow(address token, address usr) external view returns (int256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 ink, uint256 art) = vat.urns(collateralType.ilk, usr);
(, uint256 rate, uint256 spot,,) = vat.ilks(collateralType.ilk);
uint256 collateral = ink * spot;
uint256 debt = rate * art;
return (int256(collateral) - int256(debt)) / 1e27;
}
// Collateral + `amount` minus borrowed. Basically free collateral (nominated in DAVOS)
// Returns how much davos you can borrow if provide additional `amount` of collateral
function willBorrow(address token, address usr, int256 amount) external view returns (int256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 ink, uint256 art) = vat.urns(collateralType.ilk, usr);
(, uint256 rate, uint256 spot,,) = vat.ilks(collateralType.ilk);
require(amount >= - (int256(ink)), "Cannot withdraw more than current amount");
if (amount < 0) {
ink = uint256(int256(ink) + amount);
} else {
ink += uint256(amount);
}
uint256 collateral = ink * spot;
uint256 debt = rate * art;
return (int256(collateral) - int256(debt)) / 1e27;
}
function liquidationPriceForDebt(bytes32 ilk, uint256 ink, uint256 art) internal view returns (uint256) {
if (ink == 0) {
return 0; // no meaningful price if user has no debt
}
(, uint256 rate,,,) = vat.ilks(ilk);
(,uint256 mat) = spotter.ilks(ilk);
uint256 backedDebt = (art * rate / 10 ** 36) * mat;
return backedDebt / ink;
}
// Price of aMATICc when user will be liquidated
function currentLiquidationPrice(address token, address usr) external view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 ink, uint256 art) = vat.urns(collateralType.ilk, usr);
return liquidationPriceForDebt(collateralType.ilk, ink, art);
}
// Price of aMATICc when user will be liquidated with additional amount of aMATICc deposited/withdraw
function estimatedLiquidationPrice(address token, address usr, int256 amount) external view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 ink, uint256 art) = vat.urns(collateralType.ilk, usr);
require(amount >= - (int256(ink)), "Cannot withdraw more than current amount");
if (amount < 0) {
ink = uint256(int256(ink) + amount);
} else {
ink += uint256(amount);
}
return liquidationPriceForDebt(collateralType.ilk, ink, art);
}
// Price of aMATICc when user will be liquidated with additional amount of DAVOS borrowed/payback
//positive amount mean DAVOSs are being borrowed. So art(debt) will increase
function estimatedLiquidationPriceDAVOS(address token, address usr, int256 amount) external view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 ink, uint256 art) = vat.urns(collateralType.ilk, usr);
require(amount >= - (int256(art)), "Cannot withdraw more than current amount");
(, uint256 rate,,,) = vat.ilks(collateralType.ilk);
(,uint256 mat) = spotter.ilks(collateralType.ilk);
uint256 backedDebt = FullMath.mulDiv(art, rate, 10 ** 36);
if (amount < 0) {
backedDebt = uint256(int256(backedDebt) + amount);
} else {
backedDebt += uint256(amount);
}
return FullMath.mulDiv(backedDebt, mat, ink) / 10 ** 9;
}
// Returns borrow APR with 20 decimals.
// I.e. 10% == 10 ethers
function borrowApr(address token) public view returns (uint256) {
CollateralType memory collateralType = collaterals[token];
_checkIsLive(collateralType.live);
(uint256 duty,) = jug.ilks(collateralType.ilk);
uint256 principal = dMath.rpow((jug.base() + duty), YEAR, RAY);
return (principal - RAY) / (10 ** 7);
}
function startAuction(
address token,
address user,
address keeper
) external returns (uint256) {
dropRewards(token, user);
CollateralType memory collateral = collaterals[token];
(uint256 ink,) = vat.urns(collateral.ilk, user);
IDavosProvider provider = IDavosProvider(davosProviders[token]);
uint256 auctionAmount = AuctionProxy.startAuction(
user,
keeper,
davos,
davosJoin,
vat,
DogLike(dog),
provider,
collateral
);
emit AuctionStarted(token, user, ink, collateralPrice(token));
return auctionAmount;
}
function buyFromAuction(
address token,
uint256 auctionId,
uint256 collateralAmount,
uint256 maxPrice,
address receiverAddress
) external {
CollateralType memory collateral = collaterals[token];
IDavosProvider davosProvider = IDavosProvider(davosProviders[token]);
uint256 leftover = AuctionProxy.buyFromAuction(
auctionId,
collateralAmount,
maxPrice,
receiverAddress,
davos,
davosJoin,
vat,
davosProvider,
collateral
);
address urn = ClipperLike(collateral.clip).sales(auctionId).usr; // Liquidated address
dropRewards(address(davos), urn);
emit Liquidation(urn, token, collateralAmount, leftover);
}
function getAuctionStatus(address token, uint256 auctionId) external view returns(bool, uint256, uint256, uint256) {
return ClipperLike(collaterals[token].clip).getStatus(auctionId);
}
function upchostClipper(address token) external {
ClipperLike(collaterals[token].clip).upchost();
}
function getAllActiveAuctionsForToken(address token) external view returns (Sale[] memory sales) {
return AuctionProxy.getAllActiveAuctionsForClip(ClipperLike(collaterals[token].clip));
}
function resetAuction(address token, uint256 auctionId, address keeper) external {
AuctionProxy.resetAuction(auctionId, keeper, davos, davosJoin, vat, collaterals[token]);
}
function totalPegLiquidity() external view returns (uint256) {
return IERC20Upgradeable(davos).totalSupply();
}
function _checkIsLive(uint256 live) internal pure {
require(live != 0, "Interaction/inactive collateral");
}
}@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (proxy/utils/Initializable.sol)
pragma solidity ^0.8.2;
import "../../utils/AddressUpgradeable.sol";
/**
* @dev This is a base contract to aid in writing upgradeable contracts, or any kind of contract that will be deployed
* behind a proxy. Since proxied contracts do not make use of a constructor, it's common to move constructor logic to an
* external initializer function, usually called `initialize`. It then becomes necessary to protect this initializer
* function so it can only be called once. The {initializer} modifier provided by this contract will have this effect.
*
* The initialization functions use a version number. Once a version number is used, it is consumed and cannot be
* reused. This mechanism prevents re-execution of each "step" but allows the creation of new initialization steps in
* case an upgrade adds a module that needs to be initialized.
*
* For example:
*
* [.hljs-theme-light.nopadding]
* ```
* contract MyToken is ERC20Upgradeable {
* function initialize() initializer public {
* __ERC20_init("MyToken", "MTK");
* }
* }
* contract MyTokenV2 is MyToken, ERC20PermitUpgradeable {
* function initializeV2() reinitializer(2) public {
* __ERC20Permit_init("MyToken");
* }
* }
* ```
*
* TIP: To avoid leaving the proxy in an uninitialized state, the initializer function should be called as early as
* possible by providing the encoded function call as the `_data` argument to {ERC1967Proxy-constructor}.
*
* CAUTION: When used with inheritance, manual care must be taken to not invoke a parent initializer twice, or to ensure
* that all initializers are idempotent. This is not verified automatically as constructors are by Solidity.
*
* [CAUTION]
* ====
* Avoid leaving a contract uninitialized.
*
* An uninitialized contract can be taken over by an attacker. This applies to both a proxy and its implementation
* contract, which may impact the proxy. To prevent the implementation contract from being used, you should invoke
* the {_disableInitializers} function in the constructor to automatically lock it when it is deployed:
*
* [.hljs-theme-light.nopadding]
* ```
* /// @custom:oz-upgrades-unsafe-allow constructor
* constructor() {
* _disableInitializers();
* }
* ```
* ====
*/
abstract contract Initializable {
/**
* @dev Indicates that the contract has been initialized.
* @custom:oz-retyped-from bool
*/
uint8 private _initialized;
/**
* @dev Indicates that the contract is in the process of being initialized.
*/
bool private _initializing;
/**
* @dev Triggered when the contract has been initialized or reinitialized.
*/
event Initialized(uint8 version);
/**
* @dev A modifier that defines a protected initializer function that can be invoked at most once. In its scope,
* `onlyInitializing` functions can be used to initialize parent contracts. Equivalent to `reinitializer(1)`.
*/
modifier initializer() {
bool isTopLevelCall = !_initializing;
require(
(isTopLevelCall && _initialized < 1) || (!AddressUpgradeable.isContract(address(this)) && _initialized == 1),
"Initializable: contract is already initialized"
);
_initialized = 1;
if (isTopLevelCall) {
_initializing = true;
}
_;
if (isTopLevelCall) {
_initializing = false;
emit Initialized(1);
}
}
/**
* @dev A modifier that defines a protected reinitializer function that can be invoked at most once, and only if the
* contract hasn't been initialized to a greater version before. In its scope, `onlyInitializing` functions can be
* used to initialize parent contracts.
*
* `initializer` is equivalent to `reinitializer(1)`, so a reinitializer may be used after the original
* initialization step. This is essential to configure modules that are added through upgrades and that require
* initialization.
*
* Note that versions can jump in increments greater than 1; this implies that if multiple reinitializers coexist in
* a contract, executing them in the right order is up to the developer or operator.
*/
modifier reinitializer(uint8 version) {
require(!_initializing && _initialized < version, "Initializable: contract is already initialized");
_initialized = version;
_initializing = true;
_;
_initializing = false;
emit Initialized(version);
}
/**
* @dev Modifier to protect an initialization function so that it can only be invoked by functions with the
* {initializer} and {reinitializer} modifiers, directly or indirectly.
*/
modifier onlyInitializing() {
require(_initializing, "Initializable: contract is not initializing");
_;
}
/**
* @dev Locks the contract, preventing any future reinitialization. This cannot be part of an initializer call.
* Calling this in the constructor of a contract will prevent that contract from being initialized or reinitialized
* to any version. It is recommended to use this to lock implementation contracts that are designed to be called
* through proxies.
*/
function _disableInitializers() internal virtual {
require(!_initializing, "Initializable: contract is initializing");
if (_initialized < type(uint8).max) {
_initialized = type(uint8).max;
emit Initialized(type(uint8).max);
}
}
}
@openzeppelin/contracts-upgradeable/token/ERC20/IERC20Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20Upgradeable {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
function totalSupply() external view returns (uint256);
/**
* @dev Returns the amount of tokens owned by `account`.
*/
function balanceOf(address account) external view returns (uint256);
/**
* @dev Moves `amount` tokens from the caller's account to `to`.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transfer(address to, uint256 amount) external returns (bool);
/**
* @dev Returns the remaining number of tokens that `spender` will be
* allowed to spend on behalf of `owner` through {transferFrom}. This is
* zero by default.
*
* This value changes when {approve} or {transferFrom} are called.
*/
function allowance(address owner, address spender) external view returns (uint256);
/**
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* IMPORTANT: Beware that changing an allowance with this method brings the risk
* that someone may use both the old and the new allowance by unfortunate
* transaction ordering. One possible solution to mitigate this race
* condition is to first reduce the spender's allowance to 0 and set the
* desired value afterwards:
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
*
* Emits an {Approval} event.
*/
function approve(address spender, uint256 amount) external returns (bool);
/**
* @dev Moves `amount` tokens from `from` to `to` using the
* allowance mechanism. `amount` is then deducted from the caller's
* allowance.
*
* Returns a boolean value indicating whether the operation succeeded.
*
* Emits a {Transfer} event.
*/
function transferFrom(
address from,
address to,
uint256 amount
) external returns (bool);
}
@openzeppelin/contracts-upgradeable/token/ERC20/extensions/draft-IERC20PermitUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/draft-IERC20Permit.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 Permit extension allowing approvals to be made via signatures, as defined in
* https://eips.ethereum.org/EIPS/eip-2612[EIP-2612].
*
* Adds the {permit} method, which can be used to change an account's ERC20 allowance (see {IERC20-allowance}) by
* presenting a message signed by the account. By not relying on {IERC20-approve}, the token holder account doesn't
* need to send a transaction, and thus is not required to hold Ether at all.
*/
interface IERC20PermitUpgradeable {
/**
* @dev Sets `value` as the allowance of `spender` over ``owner``'s tokens,
* given ``owner``'s signed approval.
*
* IMPORTANT: The same issues {IERC20-approve} has related to transaction
* ordering also apply here.
*
* Emits an {Approval} event.
*
* Requirements:
*
* - `spender` cannot be the zero address.
* - `deadline` must be a timestamp in the future.
* - `v`, `r` and `s` must be a valid `secp256k1` signature from `owner`
* over the EIP712-formatted function arguments.
* - the signature must use ``owner``'s current nonce (see {nonces}).
*
* For more information on the signature format, see the
* https://eips.ethereum.org/EIPS/eip-2612#specification[relevant EIP
* section].
*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) external;
/**
* @dev Returns the current nonce for `owner`. This value must be
* included whenever a signature is generated for {permit}.
*
* Every successful call to {permit} increases ``owner``'s nonce by one. This
* prevents a signature from being used multiple times.
*/
function nonces(address owner) external view returns (uint256);
/**
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
*/
// solhint-disable-next-line func-name-mixedcase
function DOMAIN_SEPARATOR() external view returns (bytes32);
}
@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (token/ERC20/utils/SafeERC20.sol)
pragma solidity ^0.8.0;
import "../IERC20Upgradeable.sol";
import "../extensions/draft-IERC20PermitUpgradeable.sol";
import "../../../utils/AddressUpgradeable.sol";
/**
* @title SafeERC20
* @dev Wrappers around ERC20 operations that throw on failure (when the token
* contract returns false). Tokens that return no value (and instead revert or
* throw on failure) are also supported, non-reverting calls are assumed to be
* successful.
* To use this library you can add a `using SafeERC20 for IERC20;` statement to your contract,
* which allows you to call the safe operations as `token.safeTransfer(...)`, etc.
*/
library SafeERC20Upgradeable {
using AddressUpgradeable for address;
function safeTransfer(
IERC20Upgradeable token,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transfer.selector, to, value));
}
function safeTransferFrom(
IERC20Upgradeable token,
address from,
address to,
uint256 value
) internal {
_callOptionalReturn(token, abi.encodeWithSelector(token.transferFrom.selector, from, to, value));
}
/**
* @dev Deprecated. This function has issues similar to the ones found in
* {IERC20-approve}, and its usage is discouraged.
*
* Whenever possible, use {safeIncreaseAllowance} and
* {safeDecreaseAllowance} instead.
*/
function safeApprove(
IERC20Upgradeable token,
address spender,
uint256 value
) internal {
// safeApprove should only be called when setting an initial allowance,
// or when resetting it to zero. To increase and decrease it, use
// 'safeIncreaseAllowance' and 'safeDecreaseAllowance'
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(
IERC20Upgradeable token,
address spender,
uint256 value
) internal {
uint256 newAllowance = token.allowance(address(this), spender) + value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
function safeDecreaseAllowance(
IERC20Upgradeable token,
address spender,
uint256 value
) internal {
unchecked {
uint256 oldAllowance = token.allowance(address(this), spender);
require(oldAllowance >= value, "SafeERC20: decreased allowance below zero");
uint256 newAllowance = oldAllowance - value;
_callOptionalReturn(token, abi.encodeWithSelector(token.approve.selector, spender, newAllowance));
}
}
function safePermit(
IERC20PermitUpgradeable token,
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) internal {
uint256 nonceBefore = token.nonces(owner);
token.permit(owner, spender, value, deadline, v, r, s);
uint256 nonceAfter = token.nonces(owner);
require(nonceAfter == nonceBefore + 1, "SafeERC20: permit did not succeed");
}
/**
* @dev Imitates a Solidity high-level call (i.e. a regular function call to a contract), relaxing the requirement
* on the return value: the return value is optional (but if data is returned, it must not be false).
* @param token The token targeted by the call.
* @param data The call data (encoded using abi.encode or one of its variants).
*/
function _callOptionalReturn(IERC20Upgradeable token, bytes memory data) private {
// We need to perform a low level call here, to bypass Solidity's return data size checking mechanism, since
// we're implementing it ourselves. We use {Address.functionCall} to perform this call, which verifies that
// the target address contains contract code and also asserts for success in the low-level call.
bytes memory returndata = address(token).functionCall(data, "SafeERC20: low-level call failed");
if (returndata.length > 0) {
// Return data is optional
require(abi.decode(returndata, (bool)), "SafeERC20: ERC20 operation did not succeed");
}
}
}
@openzeppelin/contracts-upgradeable/utils/AddressUpgradeable.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Address.sol)
pragma solidity ^0.8.1;
/**
* @dev Collection of functions related to the address type
*/
library AddressUpgradeable {
/**
* @dev Returns true if `account` is a contract.
*
* [IMPORTANT]
* ====
* It is unsafe to assume that an address for which this function returns
* false is an externally-owned account (EOA) and not a contract.
*
* Among others, `isContract` will return false for the following
* types of addresses:
*
* - an externally-owned account
* - a contract in construction
* - an address where a contract will be created
* - an address where a contract lived, but was destroyed
* ====
*
* [IMPORTANT]
* ====
* You shouldn't rely on `isContract` to protect against flash loan attacks!
*
* Preventing calls from contracts is highly discouraged. It breaks composability, breaks support for smart wallets
* like Gnosis Safe, and does not provide security since it can be circumvented by calling from a contract
* constructor.
* ====
*/
function isContract(address account) internal view returns (bool) {
// This method relies on extcodesize/address.code.length, which returns 0
// for contracts in construction, since the code is only stored at the end
// of the constructor execution.
return account.code.length > 0;
}
/**
* @dev Replacement for Solidity's `transfer`: sends `amount` wei to
* `recipient`, forwarding all available gas and reverting on errors.
*
* https://eips.ethereum.org/EIPS/eip-1884[EIP1884] increases the gas cost
* of certain opcodes, possibly making contracts go over the 2300 gas limit
* imposed by `transfer`, making them unable to receive funds via
* `transfer`. {sendValue} removes this limitation.
*
* https://diligence.consensys.net/posts/2019/09/stop-using-soliditys-transfer-now/[Learn more].
*
* IMPORTANT: because control is transferred to `recipient`, care must be
* taken to not create reentrancy vulnerabilities. Consider using
* {ReentrancyGuard} or the
* https://solidity.readthedocs.io/en/v0.5.11/security-considerations.html#use-the-checks-effects-interactions-pattern[checks-effects-interactions pattern].
*/
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");
}
/**
* @dev Performs a Solidity function call using a low level `call`. A
* plain `call` is an unsafe replacement for a function call: use this
* function instead.
*
* If `target` reverts with a revert reason, it is bubbled up by this
* function (like regular Solidity function calls).
*
* Returns the raw returned data. To convert to the expected return value,
* use https://solidity.readthedocs.io/en/latest/units-and-global-variables.html?highlight=abi.decode#abi-encoding-and-decoding-functions[`abi.decode`].
*
* Requirements:
*
* - `target` must be a contract.
* - calling `target` with `data` must not revert.
*
* _Available since v3.1._
*/
function functionCall(address target, bytes memory data) internal returns (bytes memory) {
return functionCall(target, data, "Address: low-level call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`], but with
* `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
function functionCall(
address target,
bytes memory data,
string memory errorMessage
) internal returns (bytes memory) {
return functionCallWithValue(target, data, 0, errorMessage);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but also transferring `value` wei to `target`.
*
* Requirements:
*
* - the calling contract must have an ETH balance of at least `value`.
* - the called Solidity function must be `payable`.
*
* _Available since v3.1._
*/
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");
}
/**
* @dev Same as {xref-Address-functionCallWithValue-address-bytes-uint256-}[`functionCallWithValue`], but
* with `errorMessage` as a fallback revert reason when `target` reverts.
*
* _Available since v3.1._
*/
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);
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
function functionStaticCall(address target, bytes memory data) internal view returns (bytes memory) {
return functionStaticCall(target, data, "Address: low-level static call failed");
}
/**
* @dev Same as {xref-Address-functionCall-address-bytes-string-}[`functionCall`],
* but performing a static call.
*
* _Available since v3.3._
*/
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);
}
/**
* @dev Tool to verifies that a low level call was successful, and revert if it wasn't, either by bubbling the
* revert reason using the provided one.
*
* _Available since v4.3._
*/
function verifyCallResult(
bool success,
bytes memory returndata,
string memory errorMessage
) internal pure returns (bytes memory) {
if (success) {
return returndata;
} else {
// Look for revert reason and bubble it up if present
if (returndata.length > 0) {
// The easiest way to bubble the revert reason is using memory via assembly
/// @solidity memory-safe-assembly
assembly {
let returndata_size := mload(returndata)
revert(add(32, returndata), returndata_size)
}
} else {
revert(errorMessage);
}
}
}
}
@openzeppelin/contracts/utils/structs/EnumerableSet.sol
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (utils/structs/EnumerableSet.sol)
pragma solidity ^0.8.0;
/**
* @dev Library for managing
* https://en.wikipedia.org/wiki/Set_(abstract_data_type)[sets] of primitive
* types.
*
* Sets have the following properties:
*
* - Elements are added, removed, and checked for existence in constant time
* (O(1)).
* - Elements are enumerated in O(n). No guarantees are made on the ordering.
*
* ```
* contract Example {
* // Add the library methods
* using EnumerableSet for EnumerableSet.AddressSet;
*
* // Declare a set state variable
* EnumerableSet.AddressSet private mySet;
* }
* ```
*
* As of v3.3.0, sets of type `bytes32` (`Bytes32Set`), `address` (`AddressSet`)
* and `uint256` (`UintSet`) are supported.
*/
library EnumerableSet {
// To implement this library for multiple types with as little code
// repetition as possible, we write it in terms of a generic Set type with
// bytes32 values.
// The Set implementation uses private functions, and user-facing
// implementations (such as AddressSet) are just wrappers around the
// underlying Set.
// This means that we can only create new EnumerableSets for types that fit
// in bytes32.
struct Set {
// Storage of set values
bytes32[] _values;
// Position of the value in the `values` array, plus 1 because index 0
// means a value is not in the set.
mapping(bytes32 => uint256) _indexes;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function _add(Set storage set, bytes32 value) private returns (bool) {
if (!_contains(set, value)) {
set._values.push(value);
// The value is stored at length-1, but we add 1 to all indexes
// and use 0 as a sentinel value
set._indexes[value] = set._values.length;
return true;
} else {
return false;
}
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function _remove(Set storage set, bytes32 value) private returns (bool) {
// We read and store the value's index to prevent multiple reads from the same storage slot
uint256 valueIndex = set._indexes[value];
if (valueIndex != 0) {
// Equivalent to contains(set, value)
// To delete an element from the _values array in O(1), we swap the element to delete with the last one in
// the array, and then remove the last element (sometimes called as 'swap and pop').
// This modifies the order of the array, as noted in {at}.
uint256 toDeleteIndex = valueIndex - 1;
uint256 lastIndex = set._values.length - 1;
if (lastIndex != toDeleteIndex) {
bytes32 lastvalue = set._values[lastIndex];
// Move the last value to the index where the value to delete is
set._values[toDeleteIndex] = lastvalue;
// Update the index for the moved value
set._indexes[lastvalue] = valueIndex; // Replace lastvalue's index to valueIndex
}
// Delete the slot where the moved value was stored
set._values.pop();
// Delete the index for the deleted slot
delete set._indexes[value];
return true;
} else {
return false;
}
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function _contains(Set storage set, bytes32 value) private view returns (bool) {
return set._indexes[value] != 0;
}
/**
* @dev Returns the number of values on the set. O(1).
*/
function _length(Set storage set) private view returns (uint256) {
return set._values.length;
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function _at(Set storage set, uint256 index) private view returns (bytes32) {
return set._values[index];
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function _values(Set storage set) private view returns (bytes32[] memory) {
return set._values;
}
// Bytes32Set
struct Bytes32Set {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _add(set._inner, value);
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(Bytes32Set storage set, bytes32 value) internal returns (bool) {
return _remove(set._inner, value);
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(Bytes32Set storage set, bytes32 value) internal view returns (bool) {
return _contains(set._inner, value);
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(Bytes32Set storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function at(Bytes32Set storage set, uint256 index) internal view returns (bytes32) {
return _at(set._inner, index);
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(Bytes32Set storage set) internal view returns (bytes32[] memory) {
return _values(set._inner);
}
// AddressSet
struct AddressSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(AddressSet storage set, address value) internal returns (bool) {
return _add(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(AddressSet storage set, address value) internal returns (bool) {
return _remove(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(AddressSet storage set, address value) internal view returns (bool) {
return _contains(set._inner, bytes32(uint256(uint160(value))));
}
/**
* @dev Returns the number of values in the set. O(1).
*/
function length(AddressSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function at(AddressSet storage set, uint256 index) internal view returns (address) {
return address(uint160(uint256(_at(set._inner, index))));
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(AddressSet storage set) internal view returns (address[] memory) {
bytes32[] memory store = _values(set._inner);
address[] memory result;
assembly {
result := store
}
return result;
}
// UintSet
struct UintSet {
Set _inner;
}
/**
* @dev Add a value to a set. O(1).
*
* Returns true if the value was added to the set, that is if it was not
* already present.
*/
function add(UintSet storage set, uint256 value) internal returns (bool) {
return _add(set._inner, bytes32(value));
}
/**
* @dev Removes a value from a set. O(1).
*
* Returns true if the value was removed from the set, that is if it was
* present.
*/
function remove(UintSet storage set, uint256 value) internal returns (bool) {
return _remove(set._inner, bytes32(value));
}
/**
* @dev Returns true if the value is in the set. O(1).
*/
function contains(UintSet storage set, uint256 value) internal view returns (bool) {
return _contains(set._inner, bytes32(value));
}
/**
* @dev Returns the number of values on the set. O(1).
*/
function length(UintSet storage set) internal view returns (uint256) {
return _length(set._inner);
}
/**
* @dev Returns the value stored at position `index` in the set. O(1).
*
* Note that there are no guarantees on the ordering of values inside the
* array, and it may change when more values are added or removed.
*
* Requirements:
*
* - `index` must be strictly less than {length}.
*/
function at(UintSet storage set, uint256 index) internal view returns (uint256) {
return uint256(_at(set._inner, index));
}
/**
* @dev Return the entire set in an array
*
* WARNING: This operation will copy the entire storage to memory, which can be quite expensive. This is designed
* to mostly be used by view accessors that are queried without any gas fees. Developers should keep in mind that
* this function has an unbounded cost, and using it as part of a state-changing function may render the function
* uncallable if the set grows to a point where copying to memory consumes too much gas to fit in a block.
*/
function values(UintSet storage set) internal view returns (uint256[] memory) {
bytes32[] memory store = _values(set._inner);
uint256[] memory result;
assembly {
result := store
}
return result;
}
}
contracts/ceros/interfaces/IDavosProvider.sol
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.0;
interface IDavosProvider {
// --- Events ---
event Deposit(address indexed _account, uint256 _amount);
event Withdrawal(address indexed _owner, address indexed _recipient, uint256 _amount);
event CollateralChanged(address _collateral);
event CollateralDerivativeChanged(address _collateralDerivative);
event MasterVaultChanged(address _masterVault);
event InteractionChanged(address _interaction);
event UnderlyingChanged(address _matic);
event NativeStatusChanged(bool _isNative);
// --- Functions ---
function provide(uint256 _amount) external payable returns (uint256 value);
function release(address _recipient, uint256 _amount) external returns (uint256 realAmount);
function liquidation(address _recipient, uint256 _amount) external;
function daoBurn(address _account, uint256 _value) external;
function daoMint(address _account, uint256 _value) external;
}contracts/ceros/interfaces/IInteraction.sol
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.8.0;
import "../../interfaces/GemJoinLike.sol";
struct CollateralType {
GemJoinLike gem;
bytes32 ilk;
uint32 live; //0 - inactive, 1 - started, 2 - stopped
address clip;
}
interface IInteraction {
event Deposit(address indexed user, address collateral, uint256 amount, uint256 totalAmount);
event Borrow(address indexed user, address collateral, uint256 collateralAmount, uint256 amount, uint256 liquidationPrice);
event Payback(address indexed user, address collateral, uint256 amount, uint256 debt, uint256 liquidationPrice);
event Withdraw(address indexed user, uint256 amount);
event CollateralEnabled(address token, bytes32 ilk);
event CollateralDisabled(address token, bytes32 ilk);
event CollateralEnable(address token, bytes32 ilk);
event AuctionStarted(address indexed token, address user, uint256 amount, uint256 price);
event AuctionFinished(address indexed token, address keeper, uint256 amount);
event Liquidation(address indexed user, address indexed collateral, uint256 amount, uint256 leftover);
event AddedToWhitelist(address indexed user);
event RemovedFromWhitelist(address indexed user);
event ChangeRewards(address rewards);
event ChangeDavosProvider(address davosProvider);
function deposit(
address participant,
address token,
uint256 dink
) external returns (uint256);
function withdraw(
address participant,
address token,
uint256 dink
) external returns (uint256);
function dropRewards(address token, address usr) external;
function buyFromAuction(address token, uint256 auctionId, uint256 collateralAmount, uint256 maxPrice, address receiverAddress) external;
function collaterals(address) external view returns(GemJoinLike gem, bytes32 ilk, uint32 live, address clip);
}contracts/dMath.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
library dMath {
uint256 constant ONE = 10 ** 27;
function rpow(uint x, uint n, uint b) internal pure returns (uint z) {
assembly {
switch x case 0 {switch n case 0 {z := b} default {z := 0}}
default {
switch mod(n, 2) case 0 { z := b } default { z := x }
let half := div(b, 2) // for rounding.
for { n := div(n, 2) } n { n := div(n,2) } {
let xx := mul(x, x)
if iszero(eq(div(xx, x), x)) { revert(0,0) }
let xxRound := add(xx, half)
if lt(xxRound, xx) { revert(0,0) }
x := div(xxRound, b)
if mod(n,2) {
let zx := mul(z, x)
if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) { revert(0,0) }
let zxRound := add(zx, half)
if lt(zxRound, zx) { revert(0,0) }
z := div(zxRound, b)
}
}
}
}
}
}contracts/interfaces/ClipperLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
struct Sale {
uint256 pos; // Index in active array
uint256 tab; // Davos to raise [rad]
uint256 lot; // collateral to sell [wad]
address usr; // Liquidated CDP
uint96 tic; // Auction start time
uint256 top; // Starting price [ray]
}
interface ClipperLike {
function ilk() external view returns (bytes32);
function kick(
uint256 tab,
uint256 lot,
address usr,
address kpr
) external returns (uint256);
function take(
uint256 id,
uint256 amt,
uint256 max,
address who,
bytes calldata data
) external;
function redo(uint256 id, address kpr) external;
function upchost() external;
function getStatus(uint256 id) external view returns (bool, uint256, uint256, uint256);
function kicks() external view returns (uint256);
function count() external view returns (uint256);
function list() external view returns (uint256[] memory);
function sales(uint256 auctionId) external view returns (Sale memory);
}contracts/interfaces/DavosJoinLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
interface DavosJoinLike {
function join(address usr, uint256 wad) external;
function exit(address usr, uint256 wad) external;
}
contracts/interfaces/DogLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
interface DogLike {
// --- Administration ---
function file(bytes32 what, address data) external;
function file(bytes32 what, uint256 data) external;
function file(bytes32 ilk, bytes32 what, uint256 data) external;
function file(bytes32 ilk, bytes32 what, address clip) external;
function chop(bytes32 ilk) external view returns (uint256);
// --- CDP Liquidation: all bark and no bite ---
//
// Liquidate a Vault and start a Dutch auction to sell its collateral for DAVOS.
//
// The third argument is the address that will receive the liquidation reward, if any.
//
// The entire Vault will be liquidated except when the target amount of DAVOS to be raised in
// the resulting auction (debt of Vault + liquidation penalty) causes either Dirt to exceed
// Hole or ilk.dirt to exceed ilk.hole by an economically significant amount. In that
// case, a partial liquidation is performed to respect the global and per-ilk limits on
// outstanding DAVOS target. The one exception is if the resulting auction would likely
// have too little collateral to be interesting to Keepers (debt taken from Vault < ilk.dust),
// in which case the function reverts. Please refer to the code and comments within if
// more detail is desired.
function bark(bytes32 ilk, address urn, address kpr) external returns (uint256 id);
function digs(bytes32 ilk, uint256 rad) external;
function cage() external;
}contracts/interfaces/GemJoinLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import "./GemLike.sol";
interface GemJoinLike {
function join(address usr, uint256 wad) external;
function exit(address usr, uint256 wad) external;
function gem() external view returns (GemLike);
}contracts/interfaces/GemLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
interface GemLike is IERC20Upgradeable {
function decimals() external view returns (uint);
}contracts/interfaces/IRewards.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
interface IRewards {
/**
* Events
*/
event PoolInited(address token, uint256 rate);
event DgtTokenChanged(address newToken);
event DgtOracleChanged(address newOracle);
event RateChanged(address token, uint256 newRate);
event RewardsLimitChanged(uint256 newLimit);
event Uncage(address user);
event Cage(address user);
event Claimed(address indexed user, uint256 amount);
/**
* Methods
*/
function drop(address token, address usr) external;
}contracts/interfaces/JugLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
interface JugLike {
function drip(bytes32 ilk) external returns (uint256);
function ilks(bytes32) external view returns (uint256, uint256);
function base() external view returns (uint256);
function init(bytes32 ilk) external;
function file(bytes32 ilk, bytes32 what, uint data) external;
}
contracts/interfaces/PipLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
interface PipLike {
function peek() external view returns (bytes32, bool);
}contracts/interfaces/SpotLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import "./PipLike.sol";
interface SpotLike {
function ilks(bytes32) external view returns (PipLike, uint256);
function poke(bytes32) external;
function file(bytes32 ilk, bytes32 what, uint data) external;
function par() external returns (uint256);
}contracts/interfaces/VatLike.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
interface VatLike {
function init(bytes32 ilk) external;
function hope(address usr) external;
function nope(address usr) external;
function rely(address usr) external;
function deny(address usr) external;
function move(address src, address dst, uint256 rad) external;
function behalf(address bit, address usr) external;
function frob(bytes32 i, address u, address v, address w, int dink, int dart) external;
function flux(bytes32 ilk, address src, address dst, uint256 wad) external;
function ilks(bytes32) external view returns (uint256, uint256, uint256, uint256, uint256);
function fold(bytes32 i, address u, int rate) external;
function gem(bytes32, address) external view returns (uint256);
function davos(address) external view returns (uint256);
function urns(bytes32, address) external view returns (uint256, uint256);
function file(bytes32, bytes32, uint) external;
function sin(address) external view returns (uint256);
function heal(uint rad) external;
function suck(address u, address v, uint rad) external;
function grab(bytes32,address,address,address,int256,int256) external;
function slip(bytes32,address,int) external;
}contracts/libraries/AuctionProxy.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.10;
import "@openzeppelin/contracts-upgradeable/token/ERC20/utils/SafeERC20Upgradeable.sol";
import "../interfaces/ClipperLike.sol";
import "../interfaces/GemJoinLike.sol";
import "../interfaces/DavosJoinLike.sol";
import "../interfaces/DogLike.sol";
import "../interfaces/VatLike.sol";
import "../ceros/interfaces/IDavosProvider.sol";
import "../oracle/libraries/FullMath.sol";
import { CollateralType } from "../ceros/interfaces/IInteraction.sol";
uint256 constant RAY = 10**27;
library AuctionProxy {
using SafeERC20Upgradeable for IERC20Upgradeable;
using SafeERC20Upgradeable for GemLike;
function startAuction(
address user,
address keeper,
IERC20Upgradeable davos,
DavosJoinLike davosJoin,
VatLike vat,
DogLike dog,
IDavosProvider davosProvider,
CollateralType calldata collateral
) public returns (uint256 id) {
ClipperLike _clip = ClipperLike(collateral.clip);
_clip.upchost();
uint256 davosBal = davos.balanceOf(address(this));
id = dog.bark(collateral.ilk, user, address(this));
davosJoin.exit(address(this), vat.davos(address(this)) / RAY);
davosBal = davos.balanceOf(address(this)) - davosBal;
davos.transfer(keeper, davosBal);
// Burn any derivative token (dMATIC incase of ceaMATICc collateral)
if (address(davosProvider) != address(0)) {
davosProvider.daoBurn(user, _clip.sales(id).lot);
}
}
function resetAuction(
uint auctionId,
address keeper,
IERC20Upgradeable davos,
DavosJoinLike davosJoin,
VatLike vat,
CollateralType calldata collateral
) public {
ClipperLike _clip = ClipperLike(collateral.clip);
uint256 davosBal = davos.balanceOf(address(this));
_clip.redo(auctionId, keeper);
davosJoin.exit(address(this), vat.davos(address(this)) / RAY);
davosBal = davos.balanceOf(address(this)) - davosBal;
davos.transfer(keeper, davosBal);
}
// Returns lefover from auction
function buyFromAuction(
uint256 auctionId,
uint256 collateralAmount,
uint256 maxPrice,
address receiverAddress,
IERC20Upgradeable davos,
DavosJoinLike davosJoin,
VatLike vat,
IDavosProvider davosProvider,
CollateralType calldata collateral
) public returns (uint256 leftover) {
// Balances before
uint256 davosBal = davos.balanceOf(address(this));
uint256 gemBal = collateral.gem.gem().balanceOf(address(this));
uint256 davosMaxAmount = FullMath.mulDiv(maxPrice, collateralAmount, RAY);
davos.transferFrom(msg.sender, address(this), davosMaxAmount);
davosJoin.join(address(this), davosMaxAmount);
vat.hope(address(collateral.clip));
address urn = ClipperLike(collateral.clip).sales(auctionId).usr; // Liquidated address
leftover = vat.gem(collateral.ilk, urn); // userGemBalanceBefore
ClipperLike(collateral.clip).take(auctionId, collateralAmount, maxPrice, address(this), "");
leftover = vat.gem(collateral.ilk, urn) - leftover; // leftover
collateral.gem.exit(address(this), vat.gem(collateral.ilk, address(this)));
davosJoin.exit(address(this), vat.davos(address(this)) / RAY);
// Balances rest
davosBal = davos.balanceOf(address(this)) - davosBal;
gemBal = collateral.gem.gem().balanceOf(address(this)) - gemBal;
davos.transfer(receiverAddress, davosBal);
vat.nope(address(collateral.clip));
if (address(davosProvider) != address(0)) {
IERC20Upgradeable(collateral.gem.gem()).safeTransfer(address(davosProvider), gemBal);
davosProvider.liquidation(receiverAddress, gemBal); // Burn router ceToken and mint amaticc to receiver
if (leftover != 0) {
// Auction ended with leftover
vat.flux(collateral.ilk, urn, address(this), leftover);
collateral.gem.exit(address(davosProvider), leftover); // Router (disc) gets the remaining ceamaticc
davosProvider.liquidation(urn, leftover); // Router burns them and gives amaticc remaining
}
} else {
IERC20Upgradeable(collateral.gem.gem()).safeTransfer(receiverAddress, gemBal);
}
}
function getAllActiveAuctionsForClip(ClipperLike clip)
external
view
returns (Sale[] memory sales)
{
uint256[] memory auctionIds = clip.list();
uint256 auctionsCount = auctionIds.length;
sales = new Sale[](auctionsCount);
for (uint256 i = 0; i < auctionsCount; i++) {
sales[i] = clip.sales(auctionIds[i]);
}
}
}
contracts/oracle/libraries/FullMath.sol
// SPDX-License-Identifier: CC-BY-4.0
pragma solidity ^0.8.10;
// taken from https://medium.com/coinmonks/math-in-solidity-part-3-percents-and-proportions-4db014e080b1
// license is CC-BY-4.0
library FullMath {
function fullMul(uint256 x, uint256 y) internal pure returns (uint256 l, uint256 h) {
unchecked{
uint256 mm = mulmod(x, y, type(uint256).max);
l = x * y;
h = mm - l;
if (mm < l) h -= 1;
}
}
function fullDiv(
uint256 l,
uint256 h,
uint256 d
) private pure returns (uint256) {
unchecked {
uint256 pow2 = d & (~d + 1);
d /= pow2;
l /= pow2;
l += h * ((~pow2 + 1) / pow2 + 1);
uint256 r = 1;
r *= 2 - d * r;
r *= 2 - d * r;
r *= 2 - d * r;
r *= 2 - d * r;
r *= 2 - d * r;
r *= 2 - d * r;
r *= 2 - d * r;
r *= 2 - d * r;
return l * r;
}
}
function mulDiv(
uint256 x,
uint256 y,
uint256 d
) internal pure returns (uint256) {
(uint256 l, uint256 h) = fullMul(x, y);
unchecked {
uint256 mm = mulmod(x, y, d);
if (mm > l) h -= 1;
l -= mm;
if (h == 0) return l / d;
require(h < d, "FullMath: FULLDIV_OVERFLOW");
return fullDiv(l, h, d);
}
}
}
Compiler settingsShow
{
"metadata": {
"bytecodeHash": "ipfs"
},
"libraries": {
"contracts/libraries/AuctionProxy.sol": {
"AuctionProxy": "0x1a80b0512580791dda042fef0083e6ce7cbd5d88"
}
},
"optimizer": {
"runs": 100,
"enabled": true
},
"evmVersion": "london",
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.