Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/foundry desired me tokens received #50

Merged
merged 5 commits into from
Nov 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 51 additions & 0 deletions contracts/Foundry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -407,4 +407,55 @@ contract Foundry is IFoundry, Ownable, Initializable {
);
}
}

function calculateTokensDeposited(
// TODO: can we just pass in hubId instead of _meToken for first argument?
address _meToken,
uint256 _desiredMeTokensReturned
) public view returns (uint256 tokensDeposited) {
Details.MeToken memory meToken_ = meTokenRegistry.getDetails(_meToken);
Details.Hub memory hub_ = hub.getDetails(meToken_.hubId);
// gas savings
uint256 totalSupply_ = IERC20(_meToken).totalSupply();

// Calculate return assuming update is not happening
tokensDeposited = ICurve(hub_.curve).calculateTokensDeposited(
_desiredMeTokensReturned,
meToken_.hubId,
totalSupply_,
meToken_.balancePooled
);
// Logic for if we're switching to a new curve type // updating curveDetails
if (
(hub_.updating && (hub_.targetCurve != address(0))) ||
(hub_.reconfigure)
) {
uint256 targetTokensDeposited;
if (hub_.targetCurve != address(0)) {
// Means we are updating to a new curve type
targetTokensDeposited = ICurve(hub_.targetCurve)
.calculateTokensDeposited(
_desiredMeTokensReturned,
meToken_.hubId,
totalSupply_,
meToken_.balancePooled
);
} else {
// Must mean we're updating curveDetails
targetTokensDeposited = ICurve(hub_.curve)
.calculateTargetTokensDeposited(
_desiredMeTokensReturned,
meToken_.hubId,
totalSupply_,
meToken_.balancePooled
);
}
tokensDeposited = WeightedAverage.calculate(
tokensDeposited,
targetTokensDeposited,
hub_.startTime,
hub_.endTime
);
}
}
}
115 changes: 84 additions & 31 deletions contracts/curves/BancorZeroCurve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ contract BancorZeroCurve is ICurve {
using ABDKMathQuad for bytes16;

bytes16 private immutable _baseX = uint256(1 ether).fromUInt();
// uint256 public BASE_X = uint256(1 ether);
uint32 public maxWeight = 1000000;
bytes16 private immutable _one = (uint256(1)).fromUInt();

Expand Down Expand Up @@ -52,20 +51,20 @@ contract BancorZeroCurve is ICurve {
// TODO: access control

uint32 targetReserveWeight = abi.decode(_encodedDetails, (uint32));
Details.Bancor storage bancorDetails = _bancors[_hubId];
Details.Bancor storage bancor_ = _bancors[_hubId];

require(targetReserveWeight > 0, "!reserveWeight");
require(
targetReserveWeight != bancorDetails.reserveWeight,
targetReserveWeight != bancor_.reserveWeight,
"targetWeight!=Weight"
);

// targetBaseX = (old baseY * oldR) / newR
uint256 targetBaseY = (bancorDetails.baseY *
bancorDetails.reserveWeight) / targetReserveWeight;
uint256 targetBaseY = (bancor_.baseY * bancor_.reserveWeight) /
targetReserveWeight;

bancorDetails.targetBaseY = targetBaseY;
bancorDetails.targetReserveWeight = targetReserveWeight;
bancor_.targetBaseY = targetBaseY;
bancor_.targetReserveWeight = targetReserveWeight;
}

function finishReconfigure(uint256 _hubId) external override {
Expand Down Expand Up @@ -164,6 +163,52 @@ contract BancorZeroCurve is ICurve {
);
}

function calculateTokensDeposited(
uint256 _desiredMeTokens,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view override returns (uint256 tokensDeposited) {
Details.Bancor memory bancor_ = _bancors[_hubId];
if (_supply > 0) {
tokensDeposited = _calculateTokensDeposited(
_desiredMeTokens,
bancor_.reserveWeight,
bancor_.baseY,
_balancePooled
);
} else {
tokensDeposited = _calculateTokensDepositedFromZero(
_desiredMeTokens,
bancor_.reserveWeight,
bancor_.baseY
);
}
}

function calculateTargetTokensDeposited(
uint256 _desiredMeTokens,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view override returns (uint256 tokensDeposited) {
Details.Bancor memory bancor_ = _bancors[_hubId];
if (_supply > 0) {
tokensDeposited = _calculateTokensDeposited(
_desiredMeTokens,
bancor_.targetReserveWeight,
bancor_.targetBaseY,
_balancePooled
);
} else {
tokensDeposited = _calculateTokensDepositedFromZero(
_desiredMeTokens,
bancor_.targetReserveWeight,
bancor_.targetBaseY
);
}
}

/// @notice Given a deposit (in the connector token), reserve weight, meToken supply and
/// balance pooled, calculate the return for a given conversion (in the meToken)
/// @dev _supply * ((1 + _tokensDeposited / _balancePooled) ^ (_reserveWeight / 1000000) - 1)
Expand Down Expand Up @@ -196,6 +241,8 @@ contract BancorZeroCurve is ICurve {
bytes16 exponent = uint256(_reserveWeight).fromUInt().div(
uint256(maxWeight).fromUInt()
);
// 1 + balanceDeposited/connectorBalance
// TODO: name for `part1`?
bytes16 part1 = _one.add(
_tokensDeposited.fromUInt().div(_balancePooled.fromUInt())
);
Expand Down Expand Up @@ -224,39 +271,16 @@ contract BancorZeroCurve is ICurve {
bytes16 numerator = _tokensDeposited.fromUInt().mul(
_baseY.fromUInt().ln().mul(_one.div(reserveWeight)).exp()
);
// console.log("### numerator:%s", numerator.toUInt());

// as baseY == 1ether and we want to result to be in ether too we simply remove
// as baseY == 1 ether and we want to result to be in ether too we simply remove
// the multiplication by baseX
bytes16 denominator = reserveWeight.mul(_baseY.fromUInt());
// console.log("### denominator:%s", denominator.toUInt());
// Instead of calculating "x ^ exp", we calculate "e ^ (log(x) * exp)".
// (numerator/denominator) ^ (reserveWeight )
// bytes16 division = numerator.div(denominator);
// console.log("### division:%s", division.toUInt());
bytes16 res = (numerator.div(denominator))
.ln()
.mul(reserveWeight)
.exp();
return res.toUInt();
// (MAX_WEIGHT/reserveWeight -1)
/* bytes16 exponent = uint256(maxWeight)
.fromUInt()
.div(_reserveWeight.fromUInt())
.sub(_one);
// Instead of calculating "x ^ exp", we calculate "e ^ (log(x) * exp)".
// _baseX ^ (MAX_WEIGHT/reserveWeight )
bytes16 denominator_denominator = (_baseX.ln().mul(exponent)).exp();
bytes16 denominator = _reserveWeight.fromUInt().mul(_baseX).mul(
_baseY.fromUInt()
);
// tokensDeposited / (reserveWeight * baseX * baseY) / baseX ^ (MAX_WEIGHT/reserveWeight)
bytes16 base = _tokensDeposited.fromUInt().div(denominator).div(
denominator_denominator
);
// [tokensDeposited / (reserveWeight * baseX * baseY) / baseX ^ (MAX_WEIGHT/reserveWeight)] ^ reserveWeight
bytes16 res = (base.ln().mul(_reserveWeight.fromUInt()).exp());
return res.toUInt(); */
}

/// @notice Given an amount of meTokens to burn, connector weight, supply and collateral pooled,
Expand Down Expand Up @@ -310,4 +334,33 @@ contract BancorZeroCurve is ICurve {
);
return res.toUInt();
}

// (baseY * desiredMeTokens^2 * reserveWeight) / baseX
// Or (baseY * reserveWeight) / baseX * desiredMeTokens^2
function _calculateTokensDepositedFromZero(
uint256 _desiredMeTokens,
uint256 _reserveWeight,
uint256 _baseY
) private view returns (uint256) {
bytes16 reserveWeight = _reserveWeight.fromUInt().div(
uint256(maxWeight).fromUInt()
);
bytes16 numerator = _baseY.fromUInt().mul(reserveWeight);
// Instead of calculating s ^ exp, we calculate e ^ (log(s) * exp).
bytes16 squared = _desiredMeTokens
.fromUInt()
.ln()
.mul(uint256(2).fromUInt())
.exp();
bytes16 res = numerator.mul(squared).div(_baseX);
return res.toUInt();
}

// (baseY * (supply + desiredMeTokens)^2 * reserveWeight / baseX - pooledBalance
function _calculateTokensDeposited(
uint256 _desiredMeTokens,
uint256 _reserveWeight,
uint256 _supply,
uint256 _balancePooled
) private view returns (uint256) {}
}
14 changes: 14 additions & 0 deletions contracts/interfaces/ICurve.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,19 @@ interface ICurve {
uint256 _balancePooled
) external view returns (uint256 tokensReturned);

function calculateTokensDeposited(
uint256 _desiredMeTokensReturned,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view returns (uint256 tokensDeposited);

function calculateTargetTokensDeposited(
uint256 _desiredMeTokensReturned,
uint256 _hubId,
uint256 _supply,
uint256 _balancePooled
) external view returns (uint256 tokensDeposited);

function finishReconfigure(uint256 id) external;
}
24 changes: 12 additions & 12 deletions deployment/script-rinkeby.json
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
{
"network": "rinkeby",
"Hub Contract Address": "0x914bd0af1e158E55d159d78Ad19D624E25696De4",
"VaultRegistry Contract Address": "0x5065Cb3bC3B1881e70b11B2724d5399C077616bd",
"Migration Registry Contract Address": "0xfCd47dCCEc344f6F0c785Be919e3507408D044d6",
"SingleAsset Vault Contract Address": "0x05c4C8698cf351129585ebc376Fa1E10B3F6bdA1",
"Fee Contract Address": "0x29D8F397777B9d84c3dd5c2bcea8398A669d20e5",
"Curve Registry Contract Address": "0xcD86Ebc45c8f02B06C213e057ABc56BECe243Ee7",
"Bancor Curve Contract Address": "0xaC533dd9CfA342391C3f70f4B8521104f7Ca5E0a",
"Foundry Contract Address": "0x08216B04f79b6476F189179432A9EA9932824A01",
"MeToken Factory Contract Address": "0x149727Ff69d94fc8CF170806612D67101e77f27E",
"MeToken Registry Contract Address": "0x8B52A0D675FE76545B86526877D04E0c28fa3156",
"WeightedAverage Contract Address": "0x5Cb52183BB317992728f8AF25078B5EA9AF86316",
"Block Number": "9625170"
"Hub Contract Address": "0x709A55fa9F1C23Bb8D3c8db390cc82Eb7c10E526",
"VaultRegistry Contract Address": "0xd43801bEeaBae6FAb6eca9De8334Eac0ea992396",
"Migration Registry Contract Address": "0xabd68C360735cBF3C20937539E15bA6565FFA045",
"SingleAsset Vault Contract Address": "0x14DEF3F8a64B4e4bD5C08cC74ab6a4eD1B443479",
"Fee Contract Address": "0x1Fd9bd4354b0bC276Cdb963Fa7FC67e67851d13f",
"Curve Registry Contract Address": "0xfc3872474D1Ab36a914a4E6d411065FE2ef9225F",
"Bancor Curve Contract Address": "0x2E060E9549F29de0e1C20fC533E5cB461a8da9c5",
"Foundry Contract Address": "0x2B12c10407421cE8eB7520cbe3e17Ac68a60b8DF",
"MeToken Factory Contract Address": "0xf019aC5E337489a0aF0c909e5B73BAF1342ad7DB",
"MeToken Registry Contract Address": "0x9F1a8e2F32705f0F584552F8545d8fF0CDD82BF1",
"WeightedAverage Contract Address": "0x52A9054858A4E95b6d58078A6627b28a4Ca3Ff79",
"Block Number": "9621499"
}