|
|
|
|
|
|
|
|
|
|
|
pragma solidity ^0.8.12;
|
|
pragma experimental ABIEncoderV2;
|
|
|
|
|
|
|
|
|
|
|
|
interface IFeedRegistry {
|
|
function getFeed(address, address) external view returns (address);
|
|
function latestRoundData(address, address) external view returns (
|
|
uint80 roundId,
|
|
int256 answer,
|
|
uint256 startedAt,
|
|
uint256 updatedAt,
|
|
uint80 answeredInRound
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC20 {
|
|
|
|
|
|
|
|
function totalSupply() external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
function balanceOf(address account) external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transfer(address recipient, uint256 amount) external returns (bool);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function allowance(address owner, address spender) external view returns (uint256);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function approve(address spender, uint256 amount) external returns (bool);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transferFrom(
|
|
address sender,
|
|
address recipient,
|
|
uint256 amount
|
|
) external returns (bool);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
|
|
|
|
|
|
|
|
|
|
event Approval(address indexed owner, address indexed spender, uint256 value);
|
|
}
|
|
|
|
|
|
interface IERC20Extended is IERC20 {
|
|
function decimals() external view returns (uint8);
|
|
|
|
function name() external view returns (string memory);
|
|
|
|
function symbol() external view returns (string memory);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface ITripod{
|
|
function pool() external view returns(address);
|
|
function tokenA() external view returns (address);
|
|
function balanceOfA() external view returns(uint256);
|
|
function tokenB() external view returns (address);
|
|
function balanceOfB() external view returns(uint256);
|
|
function tokenC() external view returns (address);
|
|
function balanceOfC() external view returns(uint256);
|
|
function invested(address) external view returns(uint256);
|
|
function totalLpBalance() external view returns(uint256);
|
|
function investedWeight(address)external view returns(uint256);
|
|
function quote(address, address, uint256) external view returns(uint256);
|
|
function usingReference() external view returns(bool);
|
|
function referenceToken() external view returns(address);
|
|
function balanceOfTokensInLP() external view returns(uint256, uint256, uint256);
|
|
function getRewardTokens() external view returns(address[] memory);
|
|
function pendingRewards() external view returns(uint256[] memory);
|
|
}
|
|
|
|
interface IBalancerTripod is ITripod{
|
|
|
|
struct PoolInfo {
|
|
address token;
|
|
address bbPool;
|
|
bytes32 poolId;
|
|
}
|
|
function poolInfo(uint256) external view returns(PoolInfo memory);
|
|
function curveIndex(address) external view returns(int128);
|
|
function poolId() external view returns(bytes32);
|
|
function toSwapToIndex() external view returns(uint256);
|
|
function toSwapToPoolId() external view returns(bytes32);
|
|
}
|
|
|
|
|
|
|
|
library TripodMath {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
struct RebalanceInfo {
|
|
uint256 precisionA;
|
|
uint256 a0;
|
|
uint256 a1;
|
|
uint256 b0;
|
|
uint256 b1;
|
|
uint256 eOfB;
|
|
uint256 precisionB;
|
|
uint256 c0;
|
|
uint256 c1;
|
|
uint256 eOfC;
|
|
uint256 precisionC;
|
|
}
|
|
|
|
struct Tokens {
|
|
address tokenA;
|
|
uint256 ratioA;
|
|
address tokenB;
|
|
uint256 ratioB;
|
|
address tokenC;
|
|
uint256 ratioC;
|
|
}
|
|
|
|
uint256 private constant RATIO_PRECISION = 1e18;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getNandP(RebalanceInfo memory info) public pure returns(uint256 n, uint256 p) {
|
|
p = getP(info);
|
|
n = getN(info, p);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getP(RebalanceInfo memory info) public pure returns (uint256 p) {
|
|
|
|
|
|
|
|
|
|
|
|
unchecked {
|
|
|
|
|
|
uint256 var1 = info.a0 * info.b1 * info.eOfC / info.precisionA;
|
|
|
|
uint256 var2 = info.a1 * info.b0 * info.eOfC / info.precisionA;
|
|
|
|
uint256 numerator = var2 + (info.b0 * info.c1) - (info.b1 * info.c0) - var1;
|
|
|
|
uint256 denominator =
|
|
(info.a1 * info.c0 * info.eOfB / info.precisionA) +
|
|
var2 -
|
|
(info.a0 * info.c1 * info.eOfB / info.precisionA) -
|
|
var1;
|
|
|
|
p = numerator * 1e18 / denominator;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getN(RebalanceInfo memory info, uint256 p) public pure returns(uint256) {
|
|
|
|
|
|
|
|
|
|
|
|
unchecked{
|
|
uint256 numerator =
|
|
(info.a1 * info.b0) -
|
|
(info.a0 * info.b1);
|
|
|
|
uint256 denominator =
|
|
(info.b0 * 1e18) +
|
|
(info.eOfB * info.a0 / info.precisionA * p);
|
|
|
|
return numerator * 1e18 / denominator;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getNbAndNc(RebalanceInfo memory info) public pure returns(uint256 nb, uint256 nc) {
|
|
|
|
|
|
|
|
|
|
|
|
unchecked {
|
|
uint256 numeratorB =
|
|
(info.a0 * info.b1) +
|
|
(info.c0 * info.eOfC * info.b1 / info.precisionC) -
|
|
(info.a1 * info.b0) -
|
|
(info.c1 * info.eOfC * info.b0 / info.precisionC);
|
|
|
|
uint256 numeratorC =
|
|
(info.a0 * info.c1) +
|
|
(info.b0 * info.eOfB * info.c1 / info.precisionB) -
|
|
(info.a1 * info.c0) -
|
|
(info.b1 * info.eOfB * info.c0 / info.precisionB);
|
|
|
|
uint256 denominator =
|
|
info.a0 +
|
|
(info.eOfC * info.c0 / info.precisionC) +
|
|
(info.b0 * info.eOfB / info.precisionB);
|
|
|
|
nb = numeratorB / denominator;
|
|
nc = numeratorC / denominator;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getRatios(
|
|
uint256 startingA,
|
|
uint256 currentA,
|
|
uint256 startingB,
|
|
uint256 currentB,
|
|
uint256 startingC,
|
|
uint256 currentC
|
|
) public pure returns (uint256 _a, uint256 _b, uint256 _c) {
|
|
unchecked {
|
|
_a = (currentA * RATIO_PRECISION) / startingA;
|
|
_b = (currentB * RATIO_PRECISION) / startingB;
|
|
_c = (currentC * RATIO_PRECISION) / startingC;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getWeights(
|
|
uint256 investedA,
|
|
uint256 investedB,
|
|
uint256 investedC
|
|
) public view returns (uint256 wA, uint256 wB, uint256 wC) {
|
|
ITripod tripod = ITripod(address(this));
|
|
unchecked {
|
|
uint256 adjustedA = getOraclePrice(tripod.tokenA(), investedA);
|
|
uint256 adjustedB = getOraclePrice(tripod.tokenB(), investedB);
|
|
uint256 adjustedC = getOraclePrice(tripod.tokenC(), investedC);
|
|
uint256 total = adjustedA + adjustedB + adjustedC;
|
|
|
|
wA = adjustedA * RATIO_PRECISION / total;
|
|
wB = adjustedB * RATIO_PRECISION / total;
|
|
wC = adjustedC * RATIO_PRECISION / total;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function getOraclePrice(address _token, uint256 _amount) public view returns(uint256) {
|
|
address token = _token;
|
|
|
|
if(_token == 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2) token = 0xEeeeeEeeeEeEeeEeEeEeeEEEeeeeEeeeeeeeEEeE;
|
|
if(_token == 0x2260FAC5E5542a773Aa44fBCfeDf7C193bc2C599) token = 0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB;
|
|
|
|
(uint80 roundId, int256 price,, uint256 updateTime, uint80 answeredInRound) = IFeedRegistry(0x47Fb2585D2C56Fe188D0E6ec628a38b74fCeeeDf).latestRoundData(
|
|
token,
|
|
address(0x0000000000000000000000000000000000000348) // USD
|
|
);
|
|
|
|
require(price > 0 && updateTime != 0 && answeredInRound >= roundId);
|
|
|
|
return uint256(price) * _amount / (10 ** IERC20Extended(_token).decimals());
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function rebalance() public view returns(uint8, address, address, address){
|
|
ITripod tripod = ITripod(address(this));
|
|
|
|
Tokens memory tokens = Tokens(tripod.tokenA(), 0, tripod.tokenB(), 0, tripod.tokenC(), 0);
|
|
|
|
(tokens.ratioA, tokens.ratioB, tokens.ratioC) = getRatios(
|
|
tripod.invested(tokens.tokenA),
|
|
tripod.balanceOfA(),
|
|
tripod.invested(tokens.tokenB),
|
|
tripod.balanceOfB(),
|
|
tripod.invested(tokens.tokenC),
|
|
tripod.balanceOfC()
|
|
);
|
|
|
|
|
|
if(isCloseEnough(tokens.ratioA, tokens.ratioB) && isCloseEnough(tokens.ratioB, tokens.ratioC)) {
|
|
|
|
return(0, tokens.tokenA, tokens.tokenB, tokens.tokenC);
|
|
}
|
|
|
|
uint256 avgRatio;
|
|
unchecked{
|
|
avgRatio = (tokens.ratioA * tripod.investedWeight(tokens.tokenA) +
|
|
tokens.ratioB * tripod.investedWeight(tokens.tokenB) +
|
|
tokens.ratioC * tripod.investedWeight(tokens.tokenC)) /
|
|
RATIO_PRECISION;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(tokens.ratioA > avgRatio) {
|
|
|
|
if (tokens.ratioB > avgRatio) {
|
|
|
|
return(2, tokens.tokenA, tokens.tokenB, tokens.tokenC);
|
|
} else if (tokens.ratioC > avgRatio) {
|
|
|
|
return(2, tokens.tokenA, tokens.tokenC, tokens.tokenB);
|
|
} else {
|
|
|
|
return(1, tokens.tokenA, tokens.tokenB, tokens.tokenC);
|
|
}
|
|
|
|
} else if (tokens.ratioB > avgRatio) {
|
|
|
|
if (tokens.ratioC > avgRatio) {
|
|
|
|
return(2, tokens.tokenB, tokens.tokenC, tokens.tokenA);
|
|
} else {
|
|
|
|
return(1, tokens.tokenB, tokens.tokenA, tokens.tokenC);
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
return(1, tokens.tokenC, tokens.tokenA, tokens.tokenB);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function estimatedTotalAssetsAfterBalance()
|
|
public
|
|
view
|
|
returns (uint256, uint256, uint256)
|
|
{
|
|
ITripod tripod = ITripod(address(this));
|
|
|
|
(uint256 _aBalance, uint256 _bBalance, uint256 _cBalance) = tripod.balanceOfTokensInLP();
|
|
|
|
|
|
unchecked{
|
|
_aBalance += tripod.balanceOfA();
|
|
_bBalance += tripod.balanceOfB();
|
|
_cBalance += tripod.balanceOfC();
|
|
}
|
|
|
|
|
|
uint256[] memory _rewardsPending = tripod.pendingRewards();
|
|
address[] memory _rewardTokens = tripod.getRewardTokens();
|
|
address reward;
|
|
for (uint256 i; i < _rewardsPending.length; ++i) {
|
|
reward = _rewardTokens[i];
|
|
if (reward == tripod.tokenA()) {
|
|
_aBalance += _rewardsPending[i];
|
|
} else if (reward == tripod.tokenB()) {
|
|
_bBalance += _rewardsPending[i];
|
|
} else if (reward == tripod.tokenC()) {
|
|
_cBalance += _rewardsPending[i];
|
|
} else if (_rewardsPending[i] != 0) {
|
|
|
|
address swapTo = tripod.usingReference() ? tripod.referenceToken() : tripod.tokenA();
|
|
uint256 outAmount = tripod.quote(
|
|
reward,
|
|
swapTo,
|
|
_rewardsPending[i]
|
|
);
|
|
|
|
if (swapTo == tripod.tokenA()) {
|
|
_aBalance += outAmount;
|
|
} else if (swapTo == tripod.tokenB()) {
|
|
_bBalance += outAmount;
|
|
} else if (swapTo == tripod.tokenC()) {
|
|
_cBalance += outAmount;
|
|
}
|
|
}
|
|
}
|
|
return quoteRebalance(_aBalance, _bBalance, _cBalance);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function quoteRebalance(
|
|
uint256 startingA,
|
|
uint256 startingB,
|
|
uint256 startingC
|
|
) public view returns(uint256, uint256, uint256) {
|
|
ITripod tripod = ITripod(address(this));
|
|
|
|
Tokens memory tokens = Tokens(tripod.tokenA(), 0, tripod.tokenB(), 0, tripod.tokenC(), 0);
|
|
|
|
|
|
if(tripod.invested(tokens.tokenA) == 0 || tripod.invested(tokens.tokenB) == 0 || tripod.invested(tokens.tokenC) == 0) {
|
|
return (startingA, startingB, startingC);
|
|
}
|
|
|
|
(tokens.ratioA, tokens.ratioB, tokens.ratioC) = getRatios(
|
|
tripod.invested(tokens.tokenA),
|
|
startingA,
|
|
tripod.invested(tokens.tokenB),
|
|
startingB,
|
|
tripod.invested(tokens.tokenC),
|
|
startingC
|
|
);
|
|
|
|
|
|
if(isCloseEnough(tokens.ratioA, tokens.ratioB) && isCloseEnough(tokens.ratioB, tokens.ratioC)) {
|
|
return(startingA, startingB, startingC);
|
|
}
|
|
|
|
uint256 avgRatio;
|
|
unchecked{
|
|
avgRatio = (tokens.ratioA * tripod.investedWeight(tokens.tokenA) +
|
|
tokens.ratioB * tripod.investedWeight(tokens.tokenB) +
|
|
tokens.ratioC * tripod.investedWeight(tokens.tokenC)) /
|
|
RATIO_PRECISION;
|
|
}
|
|
|
|
uint256 change0;
|
|
uint256 change1;
|
|
uint256 change2;
|
|
RebalanceInfo memory info;
|
|
|
|
if(tokens.ratioA > avgRatio) {
|
|
if (tokens.ratioB > avgRatio) {
|
|
|
|
info = RebalanceInfo(0, 0, startingC, 0, startingA, 0, 0, 0, startingB, 0, 0);
|
|
(change0, change1, change2) =
|
|
_quoteSwapTwoToOne(tripod, info, tokens.tokenA, tokens.tokenB, tokens.tokenC);
|
|
return ((startingA - change0),
|
|
(startingB - change1),
|
|
(startingC + change2));
|
|
} else if (tokens.ratioC > avgRatio) {
|
|
|
|
info = RebalanceInfo(0, 0, startingB, 0, startingA, 0, 0, 0, startingC, 0, 0);
|
|
(change0, change1, change2) =
|
|
_quoteSwapTwoToOne(tripod, info, tokens.tokenA, tokens.tokenC, tokens.tokenB);
|
|
return ((startingA - change0),
|
|
(startingB + change2),
|
|
(startingC - change1));
|
|
} else {
|
|
|
|
info = RebalanceInfo(0, 0, startingA, 0, startingB, 0, 0, 0, startingC, 0, 0);
|
|
(change0, change1, change2) =
|
|
_quoteSwapOneToTwo(tripod, info, tokens.tokenA, tokens.tokenB, tokens.tokenC);
|
|
return ((startingA - change0),
|
|
(startingB + change1),
|
|
(startingC + change2));
|
|
}
|
|
} else if (tokens.ratioB > avgRatio) {
|
|
|
|
if (tokens.ratioC > avgRatio) {
|
|
|
|
info = RebalanceInfo(0, 0, startingA, 0, startingB, 0, 0, 0, startingC, 0, 0);
|
|
(change0, change1, change2) =
|
|
_quoteSwapTwoToOne(tripod, info, tokens.tokenB, tokens.tokenC, tokens.tokenA);
|
|
return ((startingA + change2),
|
|
(startingB - change0),
|
|
(startingC - change1));
|
|
} else {
|
|
|
|
info = RebalanceInfo(0, 0, startingB, 0, startingA, 0, 0, 0, startingC, 0, 0);
|
|
(change0, change1, change2) =
|
|
_quoteSwapOneToTwo(tripod, info, tokens.tokenB, tokens.tokenA, tokens.tokenC);
|
|
return ((startingA + change1),
|
|
(startingB - change0),
|
|
(startingC + change2));
|
|
}
|
|
} else {
|
|
|
|
|
|
info = RebalanceInfo(0, 0, startingC, 0, startingA, 0, 0, 0, startingB, 0, 0);
|
|
(change0, change1, change2) =
|
|
_quoteSwapOneToTwo(tripod, info, tokens.tokenC, tokens.tokenA, tokens.tokenB);
|
|
return ((startingA + change1),
|
|
(startingB + change2),
|
|
(startingC - change0));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _quoteSwapOneToTwo(
|
|
ITripod tripod,
|
|
RebalanceInfo memory info,
|
|
address toSwapFrom,
|
|
address toSwapTo0,
|
|
address toSwapTo1
|
|
) internal view returns (uint256 n, uint256 amountOut, uint256 amountOut2) {
|
|
uint256 swapTo0;
|
|
uint256 swapTo1;
|
|
|
|
unchecked {
|
|
uint256 precisionA = 10 ** IERC20Extended(toSwapFrom).decimals();
|
|
|
|
uint256 p;
|
|
|
|
(n, p) = getNandP(RebalanceInfo({
|
|
precisionA : precisionA,
|
|
a0 : tripod.invested(toSwapFrom),
|
|
a1 : info.a1,
|
|
b0 : tripod.invested(toSwapTo0),
|
|
b1 : info.b1,
|
|
eOfB : tripod.quote(toSwapFrom, toSwapTo0, precisionA),
|
|
precisionB : 0, //Not needed for this calculation
|
|
c0 : tripod.invested(toSwapTo1),
|
|
c1 : info.c1,
|
|
eOfC : tripod.quote(toSwapFrom, toSwapTo1, precisionA),
|
|
precisionC : 0 // Not needed
|
|
}));
|
|
|
|
swapTo0 = n * p / RATIO_PRECISION;
|
|
|
|
swapTo1 = n - swapTo0;
|
|
}
|
|
|
|
amountOut = tripod.quote(
|
|
toSwapFrom,
|
|
toSwapTo0,
|
|
swapTo0
|
|
);
|
|
|
|
amountOut2 = tripod.quote(
|
|
toSwapFrom,
|
|
toSwapTo1,
|
|
swapTo1
|
|
);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _quoteSwapTwoToOne(
|
|
ITripod tripod,
|
|
RebalanceInfo memory info,
|
|
address token0Address,
|
|
address token1Address,
|
|
address toTokenAddress
|
|
) internal view returns(uint256, uint256, uint256) {
|
|
|
|
(uint256 toSwapFrom0, uint256 toSwapFrom1) =
|
|
getNbAndNc(RebalanceInfo({
|
|
precisionA : 0, //Not needed
|
|
a0 : tripod.invested(toTokenAddress),
|
|
a1 : info.a1,
|
|
b0 : tripod.invested(token0Address),
|
|
b1 : info.b1,
|
|
eOfB : tripod.quote(token0Address, toTokenAddress, 10 ** IERC20Extended(token0Address).decimals()),
|
|
precisionB : 10 ** IERC20Extended(token0Address).decimals(),
|
|
c0 : tripod.invested(token1Address),
|
|
c1 : info.c1,
|
|
eOfC : tripod.quote(token1Address, toTokenAddress, 10 ** IERC20Extended(token1Address).decimals()),
|
|
precisionC : 10 ** IERC20Extended(token1Address).decimals()
|
|
}));
|
|
|
|
uint256 amountOut = tripod.quote(
|
|
token0Address,
|
|
toTokenAddress,
|
|
toSwapFrom0
|
|
);
|
|
|
|
uint256 amountOut2 = tripod.quote(
|
|
token1Address,
|
|
toTokenAddress,
|
|
toSwapFrom1
|
|
);
|
|
|
|
return (toSwapFrom0, toSwapFrom1, (amountOut + amountOut2));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function isCloseEnough(uint256 ratio0, uint256 ratio1) public pure returns(bool) {
|
|
if(ratio0 == 0 && ratio1 == 0) return true;
|
|
|
|
uint256 delta = ratio0 > ratio1 ? ratio0 - ratio1 : ratio1 - ratio0;
|
|
|
|
uint256 maxRelDelta = ratio1 / 10_000;
|
|
|
|
if (delta < maxRelDelta) return true;
|
|
}
|
|
|
|
} |