Verified contractExactMIT
Ivy
Address0x685ef22e2f459e065d348e2e45e7ac6a5d3166e7
- Balance
0.004519 ETH
- Transactions
23
- Compiler
solc 0.8.26
- Same code
2 more contracts
Details
- Compiler
- solc 0.8.26+commit.8a97fa7a
- Language
- solidity
- Optimization
- No
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 25,928,654
- Creation transaction
- 0x20683b337f…e91914f5bd
- Main copy (most transactions)
- 0x60082130…51dd9787
2 files
- src/Ivy.solmain9.11 KB
- lib/solmate/src/tokens/ERC20.sol6.81 KB
src/Ivy.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.26;
import {ERC20} from "solmate/src/tokens/ERC20.sol";
contract Ivy is ERC20 {
string public constant WEBSITE = "https://ivyloop.xyz";
string public constant TELEGRAM = "https://t.me/ivyportaleth";
string public constant X = "https://x.com/ivy_evm";
uint256 internal constant WAD = 1e18;
uint256 internal constant YEAR = 365 days;
uint256 public constant BASE_RATE = 0.012e18;
uint256 public constant SLOPE_RATE = 0.145e18;
uint256 public floor;
uint256 public escrowed;
uint256 public borrowedPrincipal;
struct Position {
uint128 collateral;
uint128 principal;
uint128 debt;
uint64 updatedAt;
}
mapping(address => Position) public positions;
uint256 private _lock = 1;
error Reentrancy();
error ZeroAmount();
error InsufficientCollateral();
error InsufficientReserve();
error NothingBorrowed();
error StillIndebted();
error NotExhausted();
error TransferFailed();
error Overflow();
event FloorRaised(uint256 previousFloor, uint256 newFloor);
event Redeemed(address indexed who, uint256 ivyBurned, uint256 ethPaid);
event Borrowed(address indexed who, uint256 collateralAdded, uint256 ethDrawn, uint256 rate);
event Repaid(address indexed who, uint256 ethPaid, uint256 debtRemaining);
event CollateralWithdrawn(address indexed who, uint256 ivyReturned);
event Forfeited(address indexed who, uint256 collateralBurned, uint256 debtCleared);
modifier nonReentrant() {
if (_lock != 1) revert Reentrancy();
_lock = 2;
_;
_lock = 1;
}
constructor(uint256 supply, address recipient) payable ERC20("Ivy", "IVY", 18) {
if (supply == 0) revert ZeroAmount();
_mint(recipient, supply);
floor = (msg.value * WAD) / supply;
emit FloorRaised(0, floor);
}
receive() external payable {
sync();
}
function sync() public {
if (totalSupply == 0) return;
uint256 backed = ((address(this).balance + borrowedPrincipal) * WAD) / totalSupply;
if (backed > floor) {
emit FloorRaised(floor, backed);
floor = backed;
}
}
function redeem(uint256 amount) external nonReentrant returns (uint256 ethOut) {
if (amount == 0) revert ZeroAmount();
ethOut = (amount * floor) / WAD;
_burn(msg.sender, amount);
if (address(this).balance < ethOut) revert InsufficientReserve();
_send(msg.sender, ethOut);
emit Redeemed(msg.sender, amount, ethOut);
sync();
}
function burn(uint256 amount) external {
if (amount == 0) revert ZeroAmount();
_burn(msg.sender, amount);
sync();
}
function guaranteedValue(uint256 ivy) public view returns (uint256) {
return (ivy * floor) / WAD;
}
function utilisation() public view returns (uint256) {
uint256 capacity = address(this).balance + borrowedPrincipal;
if (capacity == 0) return 0;
return (borrowedPrincipal * WAD) / capacity;
}
function rateAt(uint256 u) public pure returns (uint256) {
if (u > WAD) u = WAD;
uint256 u2 = (u * u) / WAD;
uint256 u4 = (u2 * u2) / WAD;
return BASE_RATE + (SLOPE_RATE * u4) / WAD;
}
function currentRate() public view returns (uint256) {
return rateAt(utilisation());
}
function borrow(uint256 collateralIn, uint256 ethOut) external nonReentrant {
if (collateralIn == 0 && ethOut == 0) revert ZeroAmount();
Position memory p = _accrue(msg.sender);
if (collateralIn > 0) {
_pull(msg.sender, collateralIn);
p.collateral = _u128(uint256(p.collateral) + collateralIn);
escrowed += collateralIn;
}
uint256 rate = currentRate();
if (ethOut > 0) {
uint256 newDebt = uint256(p.debt) + ethOut;
if (newDebt > guaranteedValue(p.collateral)) revert InsufficientCollateral();
p.debt = _u128(newDebt);
p.principal = _u128(uint256(p.principal) + ethOut);
borrowedPrincipal += ethOut;
}
positions[msg.sender] = p;
if (ethOut > 0) {
if (address(this).balance < ethOut) revert InsufficientReserve();
_send(msg.sender, ethOut);
_assertSolvent();
}
emit Borrowed(msg.sender, collateralIn, ethOut, rate);
}
function repay() external payable nonReentrant {
if (msg.value == 0) revert ZeroAmount();
Position memory p = _accrue(msg.sender);
if (p.debt == 0) revert NothingBorrowed();
uint256 paid = msg.value;
uint256 refund;
if (paid > p.debt) {
refund = paid - p.debt;
paid = p.debt;
}
uint256 principalPaid = paid > p.principal ? p.principal : paid;
p.principal -= _u128(principalPaid);
borrowedPrincipal -= principalPaid;
p.debt -= _u128(paid);
positions[msg.sender] = p;
if (refund > 0) _send(msg.sender, refund);
emit Repaid(msg.sender, paid, p.debt);
sync();
}
function withdraw(uint256 amount) external nonReentrant {
if (amount == 0) revert ZeroAmount();
Position memory p = _accrue(msg.sender);
if (amount > p.collateral) revert InsufficientCollateral();
uint256 remaining = p.collateral - amount;
if (p.debt > guaranteedValue(remaining)) revert StillIndebted();
p.collateral = _u128(remaining);
positions[msg.sender] = p;
escrowed -= amount;
_push(msg.sender, amount);
emit CollateralWithdrawn(msg.sender, amount);
_assertSolvent();
}
function forfeit(address who) external nonReentrant {
Position memory p = _accrue(who);
if (p.debt == 0) revert NothingBorrowed();
if (p.debt < guaranteedValue(p.collateral)) revert NotExhausted();
uint256 collateral = p.collateral;
uint256 debt = p.debt;
borrowedPrincipal -= p.principal;
escrowed -= collateral;
delete positions[who];
_burn(address(this), collateral);
emit Forfeited(who, collateral, debt);
sync();
}
function _accrue(address who) internal returns (Position memory p) {
p = positions[who];
if (p.debt > 0) {
uint256 elapsed = block.timestamp - p.updatedAt;
if (elapsed > 0) {
uint256 interest = (uint256(p.debt) * currentRate() * elapsed) / (YEAR * WAD);
uint256 cap = guaranteedValue(p.collateral);
uint256 grown = uint256(p.debt) + interest;
p.debt = _u128(grown > cap ? cap : grown);
}
}
p.updatedAt = uint64(block.timestamp);
positions[who] = p;
}
function _assertSolvent() internal view {
uint256 redeemable = totalSupply - escrowed;
if (address(this).balance < (floor * redeemable) / WAD) revert InsufficientReserve();
}
function _send(address to, uint256 amount) internal {
(bool ok,) = to.call{value: amount}("");
if (!ok) revert TransferFailed();
}
function _pull(address from, uint256 amount) internal {
balanceOf[from] -= amount;
unchecked {
balanceOf[address(this)] += amount;
}
emit Transfer(from, address(this), amount);
}
function _push(address to, uint256 amount) internal {
balanceOf[address(this)] -= amount;
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(this), to, amount);
}
function _u128(uint256 x) internal pure returns (uint128) {
if (x > type(uint128).max) revert Overflow();
return uint128(x);
}
function debtOf(address who) public view returns (uint256) {
Position memory p = positions[who];
if (p.debt == 0) return 0;
uint256 elapsed = block.timestamp - p.updatedAt;
if (elapsed == 0) return p.debt;
uint256 interest = (uint256(p.debt) * currentRate() * elapsed) / (YEAR * WAD);
uint256 cap = guaranteedValue(p.collateral);
uint256 grown = uint256(p.debt) + interest;
return grown > cap ? cap : grown;
}
function availableToBorrow(address who) external view returns (uint256) {
uint256 cap = guaranteedValue(positions[who].collateral);
uint256 debt = debtOf(who);
if (debt >= cap) return 0;
uint256 headroom = cap - debt;
uint256 owed = (floor * (totalSupply - escrowed)) / WAD;
uint256 balance = address(this).balance;
uint256 slack = balance > owed ? balance - owed : 0;
return headroom > slack ? slack : headroom;
}
function reserve() external view returns (uint256 ethHeld, uint256 ethLent, uint256 redeemableSupply) {
return (address(this).balance, borrowedPrincipal, totalSupply - escrowed);
}
}
lib/solmate/src/tokens/ERC20.sol
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity >=0.8.0;
/// @notice Modern and gas efficient ERC20 + EIP-2612 implementation.
/// @author Solmate (https://github.com/transmissions11/solmate/blob/main/src/tokens/ERC20.sol)
/// @author Modified from Uniswap (https://github.com/Uniswap/uniswap-v2-core/blob/master/contracts/UniswapV2ERC20.sol)
/// @dev Do not manually set balances without updating totalSupply, as the sum of all user balances must not exceed it.
abstract contract ERC20 {
/*//////////////////////////////////////////////////////////////
EVENTS
//////////////////////////////////////////////////////////////*/
event Transfer(address indexed from, address indexed to, uint256 amount);
event Approval(address indexed owner, address indexed spender, uint256 amount);
/*//////////////////////////////////////////////////////////////
METADATA STORAGE
//////////////////////////////////////////////////////////////*/
string public name;
string public symbol;
uint8 public immutable decimals;
/*//////////////////////////////////////////////////////////////
ERC20 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
/*//////////////////////////////////////////////////////////////
EIP-2612 STORAGE
//////////////////////////////////////////////////////////////*/
uint256 internal immutable INITIAL_CHAIN_ID;
bytes32 internal immutable INITIAL_DOMAIN_SEPARATOR;
mapping(address => uint256) public nonces;
/*//////////////////////////////////////////////////////////////
CONSTRUCTOR
//////////////////////////////////////////////////////////////*/
constructor(
string memory _name,
string memory _symbol,
uint8 _decimals
) {
name = _name;
symbol = _symbol;
decimals = _decimals;
INITIAL_CHAIN_ID = block.chainid;
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
}
/*//////////////////////////////////////////////////////////////
ERC20 LOGIC
//////////////////////////////////////////////////////////////*/
function approve(address spender, uint256 amount) public virtual returns (bool) {
allowance[msg.sender][spender] = amount;
emit Approval(msg.sender, spender, amount);
return true;
}
function transfer(address to, uint256 amount) public virtual returns (bool) {
balanceOf[msg.sender] -= amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(msg.sender, to, amount);
return true;
}
function transferFrom(
address from,
address to,
uint256 amount
) public virtual returns (bool) {
uint256 allowed = allowance[from][msg.sender]; // Saves gas for limited approvals.
if (allowed != type(uint256).max) allowance[from][msg.sender] = allowed - amount;
balanceOf[from] -= amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(from, to, amount);
return true;
}
/*//////////////////////////////////////////////////////////////
EIP-2612 LOGIC
//////////////////////////////////////////////////////////////*/
function permit(
address owner,
address spender,
uint256 value,
uint256 deadline,
uint8 v,
bytes32 r,
bytes32 s
) public virtual {
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");
// Unchecked because the only math done is incrementing
// the owner's nonce which cannot realistically overflow.
unchecked {
address recoveredAddress = ecrecover(
keccak256(
abi.encodePacked(
"\x19\x01",
DOMAIN_SEPARATOR(),
keccak256(
abi.encode(
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
),
owner,
spender,
value,
nonces[owner]++,
deadline
)
)
)
),
v,
r,
s
);
require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");
allowance[recoveredAddress][spender] = value;
}
emit Approval(owner, spender, value);
}
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
return block.chainid == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
}
function computeDomainSeparator() internal view virtual returns (bytes32) {
return
keccak256(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256("1"),
block.chainid,
address(this)
)
);
}
/*//////////////////////////////////////////////////////////////
INTERNAL MINT/BURN LOGIC
//////////////////////////////////////////////////////////////*/
function _mint(address to, uint256 amount) internal virtual {
totalSupply += amount;
// Cannot overflow because the sum of all user
// balances can't exceed the max uint256 value.
unchecked {
balanceOf[to] += amount;
}
emit Transfer(address(0), to, amount);
}
function _burn(address from, uint256 amount) internal virtual {
balanceOf[from] -= amount;
// Cannot underflow because a user's balance
// will never be larger than the total supply.
unchecked {
totalSupply -= amount;
}
emit Transfer(from, address(0), amount);
}
}
Compiler settingsShow
{
"viaIR": false,
"metadata": {
"appendCBOR": true,
"bytecodeHash": "ipfs",
"useLiteralContent": false
},
"optimizer": {
"runs": 200,
"enabled": false
},
"evmVersion": "cancun",
"remappings": [
"forge-std/=lib/forge-std/src/",
"solmate/=lib/solmate/",
"v4-core/=lib/v4-core/src/",
"@ensdomains/=lib/v4-core/node_modules/@ensdomains/",
"@openzeppelin/=lib/v4-core/lib/openzeppelin-contracts/",
"ds-test/=lib/solmate/lib/ds-test/src/",
"erc4626-tests/=lib/v4-core/lib/openzeppelin-contracts/lib/erc4626-tests/",
"hardhat/=lib/v4-core/node_modules/hardhat/",
"openzeppelin-contracts/=lib/v4-core/lib/openzeppelin-contracts/"
]
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.