forked from calvinheath/wheat-v1-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPantherSwapBuybackAdapter.sol
107 lines (89 loc) · 3.42 KB
/
PantherSwapBuybackAdapter.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-License-Identifier: GPL-3.0-only
pragma solidity ^0.6.0;
import { ReentrancyGuard } from "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import { IExchange } from "./IExchange.sol";
import { WhitelistGuard } from "./WhitelistGuard.sol";
import { Transfers } from "./modules/Transfers.sol";
import { PantherToken } from "./interop/PantherSwap.sol";
contract PantherSwapBuybackAdapter is ReentrancyGuard, WhitelistGuard
{
address public immutable sourceToken;
address public immutable targetToken;
address public treasury;
address public buyback;
address public exchange;
uint256 public lastGulpTime;
constructor (address _sourceToken, address _targetToken,
address _treasury, address _buyback, address _exchange) public
{
require(_sourceToken != _targetToken, "invalid token");
sourceToken = _sourceToken;
targetToken = _targetToken;
treasury = _treasury;
buyback = _buyback;
exchange = _exchange;
}
function pendingSource() external view returns (uint256 _totalSource)
{
return Transfers._getBalance(sourceToken);
}
function pendingTarget() external view returns (uint256 _totalTarget)
{
require(exchange != address(0), "exchange not set");
uint256 _totalSource = Transfers._getBalance(sourceToken);
uint256 _limitSource = _calcMaxRewardTransferAmount();
if (_totalSource > _limitSource) {
_totalSource = _limitSource;
}
_totalTarget = IExchange(exchange).calcConversionFromInput(sourceToken, targetToken, _totalSource);
return _totalTarget;
}
function gulp(uint256 _minTotalTarget) external onlyEOAorWhitelist nonReentrant
{
require(exchange != address(0), "exchange not set");
uint256 _totalSource = Transfers._getBalance(sourceToken);
uint256 _limitSource = _calcMaxRewardTransferAmount();
if (_totalSource > _limitSource) {
_totalSource = _limitSource;
}
Transfers._approveFunds(sourceToken, exchange, _totalSource);
IExchange(exchange).convertFundsFromInput(sourceToken, targetToken, _totalSource, 1);
uint256 _totalTarget = Transfers._getBalance(targetToken);
require(_totalTarget >= _minTotalTarget, "high slippage");
Transfers._pushFunds(targetToken, buyback, _totalTarget);
lastGulpTime = now;
}
function recoverLostFunds(address _token) external onlyOwner nonReentrant
{
require(_token != sourceToken, "invalid token");
uint256 _balance = Transfers._getBalance(_token);
Transfers._pushFunds(_token, treasury, _balance);
}
function setBuyback(address _newBuyback) external onlyOwner nonReentrant
{
require(_newBuyback != address(0), "invalid address");
address _oldBuyback = buyback;
buyback = _newBuyback;
emit ChangeBuyback(_oldBuyback, _newBuyback);
}
function setTreasury(address _newTreasury) external onlyOwner nonReentrant
{
require(_newTreasury != address(0), "invalid address");
address _oldTreasury = treasury;
treasury = _newTreasury;
emit ChangeTreasury(_oldTreasury, _newTreasury);
}
function setExchange(address _newExchange) external onlyOwner nonReentrant
{
address _oldExchange = exchange;
exchange = _newExchange;
emit ChangeExchange(_oldExchange, _newExchange);
}
function _calcMaxRewardTransferAmount() internal view returns (uint256 _maxRewardTransferAmount)
{
return PantherToken(sourceToken).maxTransferAmount();
}
event ChangeBuyback(address _oldBuyback, address _newBuyback);
event ChangeTreasury(address _oldTreasury, address _newTreasury);
event ChangeExchange(address _oldExchange, address _newExchange);
}