|
|
|
|
|
|
|
pragma solidity 0.8.4;
|
|
|
|
interface IQueryableErc20 {
|
|
function totalSupply() external view returns (uint256);
|
|
function balanceOf(address addr) external view returns (uint256);
|
|
function allowance(address owner, address spender) external view returns (uint256);
|
|
function decimals() external view returns (uint8);
|
|
|
|
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
event Approval(address indexed owner, address indexed spender, uint256 value);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ReentrancyGuard {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256 private constant _NOT_ENTERED = 1;
|
|
uint256 private constant _ENTERED = 2;
|
|
|
|
uint256 private _status;
|
|
|
|
constructor() {
|
|
_status = _NOT_ENTERED;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
modifier nonReentrant() {
|
|
|
|
require(_status != _ENTERED, "ReentrancyGuard: reentrant call");
|
|
|
|
|
|
_status = _ENTERED;
|
|
|
|
_;
|
|
|
|
|
|
|
|
_status = _NOT_ENTERED;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
contract CustomInitializable {
|
|
bool private _wasInitialized;
|
|
|
|
|
|
|
|
|
|
modifier ifInitialized () {
|
|
require(_wasInitialized, "Not initialized yet");
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
modifier ifNotInitialized () {
|
|
require(!_wasInitialized, "Already initialized");
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function _initializationCompleted () internal ifNotInitialized {
|
|
_wasInitialized = true;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract CustomOwnable {
|
|
|
|
address internal _owner;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event OnOwnershipTransferred (address previousOwner, address newOwner);
|
|
|
|
|
|
|
|
|
|
|
|
modifier onlyOwner() {
|
|
require(_owner == msg.sender, "Only owner");
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transferOwnership (address addr) external virtual onlyOwner {
|
|
require(addr != address(0), "non-zero address required");
|
|
emit OnOwnershipTransferred(_owner, addr);
|
|
_owner = addr;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function owner () external virtual view returns (address) {
|
|
return _owner;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC20Strict is IQueryableErc20 {
|
|
function transfer(address to, uint256 value) external returns (bool);
|
|
function transferFrom(address from, address to, uint256 value) external returns (bool);
|
|
function approve(address spender, uint256 value) external returns (bool);
|
|
}
|
|
|
|
interface ITwapHook {
|
|
function newSyntheticPairDeployed (address sellingTokenAddress, address buyingTokenAddress, address newContractAddress) external;
|
|
function newOrderCreated (address sellingTokenAddress, address buyingTokenAddress, uint256 newDeadline, uint256 targetQty) external;
|
|
}
|
|
|
|
interface ITwapQuery {
|
|
function getOrderMetrics () external view returns (uint256 pStartedOn, uint256 pDeadline, uint256 pSpent, uint256 pFilled, uint256 pTradeSize, address srcToken, address dstToken, uint8 pState, bool pAlive);
|
|
}
|
|
|
|
interface IParaSwapAugustus {
|
|
function getTokenTransferProxy() external view returns (address);
|
|
}
|
|
|
|
contract TwapOrder is ITwapQuery, CustomOwnable, CustomInitializable, ReentrancyGuard {
|
|
address private constant AUGUSTUS_SWAPPER_ADDR = 0xDEF171Fe48CF0115B1d80b88dc8eAB59176FEe57;
|
|
|
|
uint8 private constant STATE_ACTIVE = 1;
|
|
uint8 private constant STATE_FINISHED = 2;
|
|
uint8 private constant STATE_CANCELLED = 3;
|
|
|
|
uint256 internal _startedOn;
|
|
uint256 internal _deadline;
|
|
uint256 internal _spent;
|
|
uint256 internal _filled;
|
|
uint256 internal _tradeSize;
|
|
uint256 internal _priceLimit;
|
|
address public sellingTokenAddress;
|
|
address public buyingTokenAddress;
|
|
address public traderAddress;
|
|
address public depositorAddress;
|
|
address public hookAddress;
|
|
|
|
uint8 internal _currentState;
|
|
bool internal _orderAlive;
|
|
|
|
event OnTraderChanged (address newAddr);
|
|
event OnDepositorChanged (address newAddr);
|
|
event OnCompletion ();
|
|
event OnCancel ();
|
|
event OnClose ();
|
|
event OnOpen ();
|
|
event OnSwap (address fromToken, uint256 fromAmount, address toToken, uint256 toAmount);
|
|
|
|
|
|
constructor () {
|
|
_owner = msg.sender;
|
|
}
|
|
|
|
modifier onlyTrader() {
|
|
require(traderAddress == msg.sender, "Only trader");
|
|
_;
|
|
}
|
|
|
|
modifier onlyDepositor() {
|
|
require(depositorAddress == msg.sender, "Only depositor");
|
|
_;
|
|
}
|
|
|
|
modifier ifCanCloseOrder () {
|
|
require(_orderAlive, "Current order is not live");
|
|
require(
|
|
(_currentState == STATE_FINISHED || _currentState == STATE_CANCELLED) ||
|
|
(_currentState == STATE_ACTIVE && block.timestamp > _deadline)
|
|
, "Cannot close order yet");
|
|
_;
|
|
}
|
|
|
|
function initialize (address traderAddr, address depositorAddr, IERC20Strict sellingToken, IERC20Strict buyingToken, ITwapHook hook) external onlyOwner ifNotInitialized {
|
|
require(address(sellingToken) != address(buyingToken), "Invalid pair");
|
|
require(address(hook) != address(0), "Invalid hook");
|
|
|
|
traderAddress = traderAddr;
|
|
depositorAddress = depositorAddr;
|
|
sellingTokenAddress = address(sellingToken);
|
|
buyingTokenAddress = address(buyingToken);
|
|
hookAddress = address(hook);
|
|
|
|
_initializationCompleted();
|
|
}
|
|
|
|
function switchTrader (address traderAddr) external onlyOwner ifInitialized {
|
|
require(traderAddr != address(0), "Invalid trader");
|
|
require(traderAddr != traderAddress, "Trader already set");
|
|
require(!_orderAlive, "Current order still alive");
|
|
|
|
traderAddress = traderAddr;
|
|
emit OnTraderChanged(traderAddr);
|
|
}
|
|
|
|
function switchDepositor (address depositorAddr) external onlyOwner ifInitialized {
|
|
require(depositorAddr != address(0), "Invalid depositor");
|
|
require(depositorAddr != depositorAddress, "Depositor already set");
|
|
require(!_orderAlive, "Current order still alive");
|
|
|
|
depositorAddress = depositorAddr;
|
|
emit OnDepositorChanged(depositorAddr);
|
|
}
|
|
|
|
function openOrder (uint256 newDeadline, uint256 targetQty, uint256 maxPriceLimit) external onlyDepositor ifInitialized {
|
|
require(newDeadline > block.timestamp, "Invalid deadline");
|
|
require(targetQty > 0, "Invalid trade size");
|
|
require(maxPriceLimit > 0, "Invalid price limit");
|
|
require(!_orderAlive, "Current order still alive");
|
|
|
|
_startedOn = block.timestamp;
|
|
_deadline = newDeadline;
|
|
_tradeSize = targetQty;
|
|
_priceLimit = maxPriceLimit;
|
|
_filled = 0;
|
|
_spent = 0;
|
|
_orderAlive = true;
|
|
_currentState = STATE_ACTIVE;
|
|
|
|
ITwapHook(hookAddress).newOrderCreated(sellingTokenAddress, buyingTokenAddress, newDeadline, targetQty);
|
|
|
|
_approveProxy();
|
|
emit OnOpen();
|
|
}
|
|
|
|
function deposit (uint256 depositAmount) external onlyDepositor ifInitialized {
|
|
require(IERC20Strict(sellingTokenAddress).transferFrom(msg.sender, address(this), depositAmount), "Deposit failed");
|
|
}
|
|
|
|
function swap (uint256 sellQty, uint256 buyQty, bytes memory payload) external nonReentrant onlyTrader ifInitialized {
|
|
require(_currentState == STATE_ACTIVE, "Invalid state");
|
|
require(_deadline > block.timestamp, "Deadline expired");
|
|
require(sellQty <= _priceLimit, "Price limit reached");
|
|
|
|
IERC20Strict sellingToken = IERC20Strict(sellingTokenAddress);
|
|
uint256 sellingTokenBefore = sellingToken.balanceOf(address(this));
|
|
require(sellingTokenBefore > 0, "Insufficient balance");
|
|
|
|
IERC20Strict buyingToken = IERC20Strict(buyingTokenAddress);
|
|
uint256 buyingTokenBefore = buyingToken.balanceOf(address(this));
|
|
|
|
|
|
(bool success,) = AUGUSTUS_SWAPPER_ADDR.call(payload);
|
|
require(success, "Swap failed");
|
|
|
|
uint256 sellingTokenAfter = sellingToken.balanceOf(address(this));
|
|
uint256 buyingTokenAfter = buyingToken.balanceOf(address(this));
|
|
require(buyingTokenAfter > buyingTokenBefore, "Invalid swap: Buy");
|
|
require(sellingTokenBefore > sellingTokenAfter, "Invalid swap: Sell");
|
|
|
|
|
|
uint256 tokensReceived = buyingTokenAfter - buyingTokenBefore;
|
|
require(tokensReceived >= buyQty, "Invalid amount received");
|
|
_filled += tokensReceived;
|
|
|
|
|
|
uint256 tokensSold = sellingTokenBefore - sellingTokenAfter;
|
|
require(tokensSold <= sellQty, "Invalid amount spent");
|
|
_spent += tokensSold;
|
|
|
|
emit OnSwap(sellingTokenAddress, tokensSold, buyingTokenAddress, tokensReceived);
|
|
|
|
if (buyingTokenAfter >= _tradeSize) {
|
|
_currentState = STATE_FINISHED;
|
|
emit OnCompletion();
|
|
}
|
|
}
|
|
|
|
function cancelOrder () external nonReentrant onlyDepositor ifInitialized {
|
|
require(_currentState == STATE_ACTIVE, "Invalid state");
|
|
|
|
_currentState = STATE_CANCELLED;
|
|
emit OnCancel();
|
|
|
|
_closeOrder();
|
|
}
|
|
|
|
function closeOrder () external nonReentrant onlyDepositor ifInitialized {
|
|
_closeOrder();
|
|
}
|
|
|
|
function _closeOrder () private ifCanCloseOrder {
|
|
_orderAlive = false;
|
|
|
|
IERC20Strict sellingToken = IERC20Strict(sellingTokenAddress);
|
|
IERC20Strict buyingToken = IERC20Strict(buyingTokenAddress);
|
|
uint256 sellingTokenBalance = sellingToken.balanceOf(address(this));
|
|
uint256 buyingTokenBalance = buyingToken.balanceOf(address(this));
|
|
|
|
if (sellingTokenBalance > 0) require(sellingToken.transfer(depositorAddress, sellingTokenBalance), "Transfer failed: sell");
|
|
if (buyingTokenBalance > 0) require(buyingToken.transfer(depositorAddress, buyingTokenBalance), "Transfer failed: buy");
|
|
_revokeProxy();
|
|
|
|
emit OnClose();
|
|
}
|
|
|
|
function _approveProxy () private {
|
|
IERC20Strict token = IERC20Strict(sellingTokenAddress);
|
|
uint256 currentBalance = token.balanceOf(address(this));
|
|
address proxyAddr = IParaSwapAugustus(AUGUSTUS_SWAPPER_ADDR).getTokenTransferProxy();
|
|
if (token.allowance(address(this), proxyAddr) < currentBalance) {
|
|
require(token.approve(proxyAddr, currentBalance), "Token approval failed");
|
|
}
|
|
}
|
|
|
|
function _revokeProxy () private {
|
|
IERC20Strict token = IERC20Strict(sellingTokenAddress);
|
|
address proxyAddr = IParaSwapAugustus(AUGUSTUS_SWAPPER_ADDR).getTokenTransferProxy();
|
|
if (token.allowance(address(this), proxyAddr) > 0) {
|
|
require(token.approve(proxyAddr, 0), "Token approval failed");
|
|
}
|
|
}
|
|
|
|
function getOrderMetrics () external view override returns (uint256 pStartedOn, uint256 pDeadline, uint256 pSpent, uint256 pFilled, uint256 pTradeSize, address srcToken, address dstToken, uint8 pState, bool pAlive) {
|
|
pDeadline = _deadline;
|
|
pSpent = _spent;
|
|
pFilled = _filled;
|
|
pStartedOn = _startedOn;
|
|
pTradeSize = _tradeSize;
|
|
srcToken = sellingTokenAddress;
|
|
dstToken = buyingTokenAddress;
|
|
pState = _currentState;
|
|
pAlive = _orderAlive;
|
|
}
|
|
}
|
|
|
|
interface ITwapDeployer {
|
|
function deployTwap (IERC20Strict sellingToken, IERC20Strict buyingToken, address twapOwnerAddr, address traderAddr, address depositorAddr) external;
|
|
}
|
|
|
|
contract TwapDeployer is ITwapDeployer, CustomOwnable, CustomInitializable {
|
|
address public authorizedDeployer;
|
|
address public hookAddress;
|
|
|
|
constructor () {
|
|
_owner = msg.sender;
|
|
}
|
|
|
|
modifier onlyAuthorizedDeployer() {
|
|
require(authorizedDeployer == msg.sender, "Deployer Unauthorized");
|
|
_;
|
|
}
|
|
|
|
function initialize (ITwapHook hook, address authorizedDeployerAddr) external onlyOwner ifNotInitialized {
|
|
require(address(hook) != address(0), "Invalid hook");
|
|
require(authorizedDeployerAddr != address(0), "Invalid deployer");
|
|
|
|
hookAddress = address(hook);
|
|
authorizedDeployer = authorizedDeployerAddr;
|
|
_initializationCompleted();
|
|
}
|
|
|
|
function deployTwap (IERC20Strict sellingToken, IERC20Strict buyingToken, address twapOwnerAddr, address traderAddr, address depositorAddr) external override ifInitialized onlyAuthorizedDeployer {
|
|
TwapOrder instance = new TwapOrder();
|
|
instance.initialize(traderAddr, depositorAddr, sellingToken, buyingToken, ITwapHook(hookAddress));
|
|
instance.transferOwnership(twapOwnerAddr);
|
|
|
|
ITwapHook(hookAddress).newSyntheticPairDeployed(address(sellingToken), address(buyingToken), address(instance));
|
|
}
|
|
} |