Verified contractExactMIT
LldexUsdtPool
Address0x7b687a431db098a4b51c62179c74a8dab959bfaa
- Balance
0 ETH
- Transactions
6
- Compiler
solc 0.8.24
- Same code
Only this contract
Details
- Compiler
- solc 0.8.24+commit.e11b9ed9
- Language
- solidity
- Optimization
- Yes, 200 runs
- EVM version
- cancun
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,498,972
- Creation transaction
- 0xea8c9c5d52…7cc0b8897a
src/LldexUsdtPool.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.24;
/// @title lldex USDT pool, and its lldexPOOL receipt token.
/// @notice The pool holds only USDT and sets no price. Swaps between USDT and
/// AR or AO go through the lldex node, which prices them with its own AR/USDT
/// and AO/USDT pools and pays and receives the AR and AO; the pool holds the
/// swap's USDT and takes the only fee (fee bps, 2/3 to liquidity providers,
/// 1/3 protocol). Liquidity providers' USDT is never swapped.
///
/// One node address (the lldex node's Ethereum wallet, which also holds the
/// USDT of its AR/USDT and AO/USDT pools): only it sends completeSwap,
/// refundSwap and payout; it receives a completed swap's USDT and funds
/// payouts, approving each payout's amount first.
///
/// lldexPOOL is 1:1 with deposited USDT: deposit N USDT, get N lldexPOOL;
/// send back N, get back N. Fees accrue per unit held as they are earned and
/// are claimed with claim(); they are never added to the principal, so one
/// lldexPOOL is always one USDT of the pool; a holder's share is their
/// balance over totalSupply.
///
/// Books (every unit of the pool's USDT balance belongs to one of these):
/// principal liquidity providers' USDT, always equal to totalSupply
/// escrowed USDT -> AR/AO swaps waiting for the node (amount incl. fee)
/// lpUnclaimed fees given to liquidity providers, not yet claimed
/// protocolClaimable the protocol's fees, not yet claimed
///
/// Swaps (asset 1 = AR, 2 = AO):
/// USDT -> AR or AO: the trader calls swap (after approve). The pool holds
/// the USDT. The node pays the AR or AO, then calls
/// completeSwap: the pool counts the fee and sends the rest to the
/// node. The node may refund instead (refundSwap: all of it back to
/// the trader). If it does neither within swapTimeout, the trader may
/// cancelSwap and take it all back.
/// AR or AO -> USDT: the node received AR or AO; it approves the amount
/// and calls payout: the pool takes the USDT from the node, keeps the fee and pays the rest to the trader, once per
/// deposit (ref: the AR transaction or AO message).
contract LldexUsdtPool {
string public constant name = "lldexPOOL";
string public constant symbol = "LLDEXPOOL";
uint8 public constant decimals = 6;
uint256 private constant SCALE = 1e30;
uint8 private constant OPEN = 1;
uint8 private constant DONE = 2;
uint8 private constant REFUNDED = 3;
uint8 private constant CANCELLED = 4;
uint8 public constant ASSET_AR = 1;
uint8 public constant ASSET_AO = 2;
address public immutable usdt;
address public immutable node;
address public immutable protocol;
uint256 public immutable feeBps;
uint256 public immutable holdSeconds;
uint256 public immutable swapTimeout;
address public owner;
bool public paused;
uint256 public totalSupply;
mapping(address => uint256) public balanceOf;
mapping(address => mapping(address => uint256)) public allowance;
uint256 public escrowed;
uint256 public lpUnclaimed;
uint256 public protocolClaimable;
uint256 public accPerShare;
mapping(address => uint256) private accAt;
mapping(address => uint256) private earned;
mapping(address => uint256) public lockedUntil;
struct Swap {
address user;
uint64 time;
uint8 status;
uint8 asset;
uint256 amount;
uint256 fee;
bytes32 recipient;
uint256 minOut;
}
Swap[] private swaps;
mapping(bytes32 => bool) public paidRefs;
uint256 private locked = 1;
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event LiquidityAdded(address indexed provider, uint256 amount);
event LiquidityRemoved(address indexed provider, uint256 amount);
event Claimed(address indexed to, uint256 amount);
event ProtocolClaimed(address indexed to, uint256 amount);
event SwapOpened(uint256 indexed id, address indexed user, uint256 amount, uint256 fee, uint8 asset, bytes32 recipient, uint256 minOut);
event SwapDone(uint256 indexed id, bytes32 txid, uint256 quantity);
event SwapRefunded(uint256 indexed id, string reason);
event SwapCancelled(uint256 indexed id);
event Payout(bytes32 indexed ref, uint8 asset, address indexed to, uint256 amount, uint256 fee);
event PausedSet(bool paused);
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
error NotOwner();
error NotNode();
error IsPaused();
error Locked(uint256 until);
error BadAmount();
error BadAddress();
error BadSwap();
error TooEarly(uint256 at);
error AlreadyPaid();
error Insufficient();
error TokenCallFailed();
error Reentrant();
modifier onlyOwner() {
if (msg.sender != owner) revert NotOwner();
_;
}
modifier onlyNode() {
if (msg.sender != node) revert NotNode();
_;
}
modifier whenNotPaused() {
if (paused) revert IsPaused();
_;
}
modifier nonReentrant() {
if (locked != 1) revert Reentrant();
locked = 2;
_;
locked = 1;
}
constructor(
address usdt_,
address node_,
address protocol_,
uint256 feeBps_,
uint256 holdSeconds_,
uint256 swapTimeout_
) {
if (usdt_ == address(0) || node_ == address(0) || protocol_ == address(0)) revert BadAddress();
if (protocol_ == node_) revert BadAddress();
if (feeBps_ >= 10000) revert BadAmount();
if (swapTimeout_ < 1 hours) revert BadAmount();
usdt = usdt_;
node = node_;
protocol = protocol_;
feeBps = feeBps_;
holdSeconds = holdSeconds_;
swapTimeout = swapTimeout_;
owner = msg.sender;
emit OwnershipTransferred(address(0), msg.sender);
}
// ------------------------------------------------------------------ USDT
// USDT on Ethereum returns nothing from transfer and transferFrom.
function _call(bytes memory data) private {
(bool ok, bytes memory ret) = usdt.call(data);
if (!ok || (ret.length != 0 && !abi.decode(ret, (bool)))) revert TokenCallFailed();
}
// The pool must receive exactly `amount` (USDT can charge a transfer fee
// if its owner turns it on; the books would no longer add up).
function _pull(address from, uint256 amount) private {
uint256 before = _balance();
_call(abi.encodeWithSelector(0x23b872dd, from, address(this), amount));
if (_balance() != before + amount) revert TokenCallFailed();
}
function _balance() private view returns (uint256) {
(bool ok, bytes memory ret) = usdt.staticcall(abi.encodeWithSelector(0x70a08231, address(this)));
if (!ok || ret.length < 32) revert TokenCallFailed();
return abi.decode(ret, (uint256));
}
function _send(address to, uint256 amount) private {
if (amount > 0) _call(abi.encodeWithSelector(0xa9059cbb, to, amount));
}
// ------------------------------------------------------------------ fees
// Bring an account's fees up to date before its holding changes.
function _touch(address a) private {
uint256 bal = balanceOf[a];
if (bal > 0) earned[a] += (bal * (accPerShare - accAt[a])) / SCALE;
accAt[a] = accPerShare;
}
// Split a fee: 2/3 to liquidity providers (per unit held), 1/3 protocol.
// With no liquidity providers, all of it goes to the protocol.
function _addFee(uint256 fee) private {
uint256 lp = (fee * 2) / 3;
if (totalSupply == 0) lp = 0;
uint256 proto = fee - lp;
if (lp > 0) {
accPerShare += (lp * SCALE) / totalSupply;
lpUnclaimed += lp;
}
protocolClaimable += proto;
}
function feeOf(uint256 amount) public view returns (uint256) {
return (amount * feeBps) / 10000;
}
/// @notice Fees an account can claim now.
function claimable(address a) public view returns (uint256) {
return earned[a] + (balanceOf[a] * (accPerShare - accAt[a])) / SCALE;
}
/// @notice Claim your fees (any amount; nothing happens below one unit).
function claim() external nonReentrant returns (uint256 amount) {
_touch(msg.sender);
amount = earned[msg.sender];
if (amount == 0) return 0;
earned[msg.sender] = 0;
lpUnclaimed -= amount;
_send(msg.sender, amount);
emit Claimed(msg.sender, amount);
}
/// @notice The protocol address claims the protocol's fees.
function claimProtocol() external nonReentrant returns (uint256 amount) {
if (msg.sender != protocol) revert BadAddress();
amount = protocolClaimable;
protocolClaimable = 0;
_send(protocol, amount);
emit ProtocolClaimed(protocol, amount);
}
// ------------------------------------------------------------------ liquidity
/// @notice Deposit USDT (approve first), get the same amount of lldexPOOL.
/// It can be removed after holdSeconds.
function addLiquidity(uint256 amount) external nonReentrant whenNotPaused {
if (amount == 0) revert BadAmount();
_pull(msg.sender, amount);
_touch(msg.sender);
totalSupply += amount;
balanceOf[msg.sender] += amount;
uint256 until = block.timestamp + holdSeconds;
if (until > lockedUntil[msg.sender]) lockedUntil[msg.sender] = until;
emit Transfer(address(0), msg.sender, amount);
emit LiquidityAdded(msg.sender, amount);
}
/// @notice Burn lldexPOOL, get back the same amount of USDT (also while paused).
function removeLiquidity(uint256 amount) external nonReentrant {
if (amount == 0 || amount > balanceOf[msg.sender]) revert BadAmount();
if (block.timestamp < lockedUntil[msg.sender]) revert Locked(lockedUntil[msg.sender]);
_touch(msg.sender);
balanceOf[msg.sender] -= amount;
totalSupply -= amount;
emit Transfer(msg.sender, address(0), amount);
_send(msg.sender, amount);
emit LiquidityRemoved(msg.sender, amount);
}
/// @notice The principal: USDT that belongs to liquidity providers.
function principal() external view returns (uint256) {
return totalSupply;
}
// ------------------------------------------------------------------ lldexPOOL (ERC-20)
function transfer(address to, uint256 value) external returns (bool) {
_transfer(msg.sender, to, value);
return true;
}
function transferFrom(address from, address to, uint256 value) external returns (bool) {
uint256 a = allowance[from][msg.sender];
if (a != type(uint256).max) {
if (a < value) revert Insufficient();
allowance[from][msg.sender] = a - value;
}
_transfer(from, to, value);
return true;
}
function approve(address spender, uint256 value) external returns (bool) {
allowance[msg.sender][spender] = value;
emit Approval(msg.sender, spender, value);
return true;
}
// Fees stay with whoever held the units while they accrued; the lock moves
// with the units, so a transfer cannot shorten it.
function _transfer(address from, address to, uint256 value) private {
if (to == address(0) || to == address(this)) revert BadAddress();
if (value > balanceOf[from]) revert Insufficient();
_touch(from);
_touch(to);
balanceOf[from] -= value;
balanceOf[to] += value;
if (lockedUntil[from] > lockedUntil[to]) lockedUntil[to] = lockedUntil[from];
emit Transfer(from, to, value);
}
// ------------------------------------------------------------------ USDT -> AR or AO
/// @notice Swap USDT (approve first) for AR (asset 1) or AO (asset 2) that
/// the node pays to `recipient`: the 32 bytes of an Arweave or AO address,
/// or a 0x AO address left-padded with zeros; at least minOut base units.
function swap(uint256 amount, uint8 asset, bytes32 recipient, uint256 minOut)
external
nonReentrant
whenNotPaused
returns (uint256 id)
{
if (amount == 0) revert BadAmount();
if (asset != ASSET_AR && asset != ASSET_AO) revert BadSwap();
if (recipient == bytes32(0)) revert BadAddress();
uint256 fee = feeOf(amount);
if (fee >= amount) revert BadAmount();
_pull(msg.sender, amount);
escrowed += amount;
id = swaps.length;
swaps.push(Swap(msg.sender, uint64(block.timestamp), OPEN, asset, amount, fee, recipient, minOut));
emit SwapOpened(id, msg.sender, amount, fee, asset, recipient, minOut);
}
function _open(uint256 id) private view returns (Swap storage s) {
if (id >= swaps.length) revert BadSwap();
s = swaps[id];
if (s.status != OPEN) revert BadSwap();
}
/// @notice The node paid the AR or AO (txid, quantity base units): count
/// the fee and send the rest of the swap's USDT to the node.
function completeSwap(uint256 id, bytes32 txid, uint256 quantity) external nonReentrant onlyNode {
Swap storage s = _open(id);
if (txid == bytes32(0) || quantity < s.minOut) revert BadSwap();
s.status = DONE;
escrowed -= s.amount;
_addFee(s.fee);
_send(node, s.amount - s.fee);
emit SwapDone(id, txid, quantity);
}
/// @notice The node does not pay this swap: all of its USDT goes back.
function refundSwap(uint256 id, string calldata reason) external nonReentrant onlyNode {
Swap storage s = _open(id);
s.status = REFUNDED;
escrowed -= s.amount;
_send(s.user, s.amount);
emit SwapRefunded(id, reason);
}
/// @notice The node neither completed nor refunded within swapTimeout: take it all back.
function cancelSwap(uint256 id) external nonReentrant {
Swap storage s = _open(id);
if (msg.sender != s.user) revert BadSwap();
uint256 at = uint256(s.time) + swapTimeout;
if (block.timestamp < at) revert TooEarly(at);
s.status = CANCELLED;
escrowed -= s.amount;
_send(s.user, s.amount);
emit SwapCancelled(id);
}
function swapCount() external view returns (uint256) {
return swaps.length;
}
function swapAt(uint256 id) external view returns (Swap memory) {
if (id >= swaps.length) revert BadSwap();
return swaps[id];
}
// ------------------------------------------------------------------ AR or AO -> USDT
/// @notice The node received AR or AO (ref: that Arweave transaction or AO
/// message) and pays `amount` USDT from its own wallet (approved
/// first) to `to`, less the pool's fee. Once per ref.
function payout(address to, uint256 amount, uint8 asset, bytes32 ref) external nonReentrant whenNotPaused onlyNode {
if (to == address(0) || to == address(this)) revert BadAddress();
if (asset != ASSET_AR && asset != ASSET_AO) revert BadSwap();
if (ref == bytes32(0)) revert BadSwap();
if (paidRefs[ref]) revert AlreadyPaid();
uint256 fee = feeOf(amount);
if (amount == 0 || fee >= amount) revert BadAmount();
paidRefs[ref] = true;
_pull(node, amount);
_addFee(fee);
_send(to, amount - fee);
emit Payout(ref, asset, to, amount - fee, fee);
}
// ------------------------------------------------------------------ administration
/// @notice Pause new deposits and swaps. Removing liquidity, claiming,
/// completing, refunding and cancelling swaps keep working.
function setPaused(bool p) external onlyOwner {
paused = p;
emit PausedSet(p);
}
/// @notice Transfer the owner; address(0) gives it up for good.
function transferOwnership(address newOwner) external onlyOwner {
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"evmVersion": "cancun"
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.