Skip to content

Commit

Permalink
getCDPValue
Browse files Browse the repository at this point in the history
  • Loading branch information
MAIN authored and MAIN committed Jan 6, 2021
1 parent a6a1856 commit ca1f0e0
Show file tree
Hide file tree
Showing 17 changed files with 621 additions and 87 deletions.
4 changes: 2 additions & 2 deletions artifacts/Bond.json

Large diffs are not rendered by default.

122 changes: 120 additions & 2 deletions artifacts/Dao.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions artifacts/DaoVault.json

Large diffs are not rendered by default.

17 changes: 15 additions & 2 deletions artifacts/Pool.json

Large diffs are not rendered by default.

23 changes: 21 additions & 2 deletions artifacts/Router.json

Large diffs are not rendered by default.

111 changes: 107 additions & 4 deletions artifacts/Synth.json

Large diffs are not rendered by default.

146 changes: 141 additions & 5 deletions artifacts/Utils.json

Large diffs are not rendered by default.

70 changes: 70 additions & 0 deletions artifacts/iROUTER.json
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,76 @@
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "changeArrayFeeSize",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "changeMaxTrades",
"outputs": [
{
"internalType": "bool",
"name": "",
"type": "bool"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"name": "getCuratedPool",
"outputs": [
{
"internalType": "address",
"name": "",
"type": "address"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "getCuratedPoolsLength",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [
{
Expand Down
2 changes: 1 addition & 1 deletion artifacts/iSYNTH.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
},
{
"inputs": [],
"name": "totalCollateral",
"name": "totalCollateralValue",
"outputs": [
{
"internalType": "uint256",
Expand Down
11 changes: 8 additions & 3 deletions artifacts/iUTILS.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,21 @@
"inputs": [
{
"internalType": "uint256",
"name": "",
"name": "units",
"type": "uint256"
},
{
"internalType": "uint256",
"name": "amount",
"type": "uint256"
},
{
"internalType": "address",
"name": "",
"name": "synth",
"type": "address"
}
],
"name": "calcCDPPart",
"name": "calcCDPShare",
"outputs": [
{
"internalType": "uint256",
Expand Down
41 changes: 37 additions & 4 deletions artifacts/synthRouter.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions contracts/IContracts.sol
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ interface iUTILS {
function getDepth(address pool) external view returns (uint depth);
function calcAsymmetricShare(address token, address member) external view returns(uint amount);
function calcSwapValueInTokenWithPool(address pool, uint amount) external view returns (uint _output);
function calcCDPPart(uint, address) external view returns (uint unitSynths);
function calcCDPShare(uint units, uint amount, address synth) external view returns (uint unitSynths);

}
interface iDAO {
Expand Down Expand Up @@ -154,7 +154,7 @@ interface iSYNTH {
function transferTo(address, uint) external returns (bool);
function genesis() external view returns(uint);
function totalDebt() external view returns(uint);
function totalCollateral() external view returns(uint);
function totalCollateralValue() external view returns(uint);
}

interface iDAOVAULT {
Expand Down
19 changes: 17 additions & 2 deletions contracts/UtilsV2.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,10 @@ contract Utils {
address synthAddress;
uint genesis;
uint totalDebt;
uint totalCollateral;
uint totalCollateralValue;
}



// Only Deployer can execute
modifier onlyDeployer() {
Expand Down Expand Up @@ -266,7 +268,8 @@ contract Utils {
synthData.tokenAddress = token;
synthData.genesis = iSYNTH(synth).genesis();
synthData.totalDebt = iSYNTH(synth).totalDebt();
synthData.totalCollateral = iSYNTH(synth).totalCollateral();
synthData.totalCollateralValue = calcCDPValue(token);

return synthData;
}

Expand Down Expand Up @@ -426,5 +429,17 @@ contract Utils {
share = baseAmount.add(tokenSwapped);
return share;
}
function calcCDPValue(address token) public view returns (uint cdpValue){
address synth = getSynth(token);
address [] memory getCuratedPools = allCuratedPools();
for(uint i=0;i<getCuratedPools.length;i++){
uint lpTokenBalance = iBEP20(getCuratedPools[i]).balanceOf(synth);
uint baseAmount = calcShare(lpTokenBalance, iBEP20(getCuratedPools[i]).totalSupply(), iPOOL(getCuratedPools[i]).baseAmount());
uint tokenAmount = calcShare(lpTokenBalance, iBEP20(getCuratedPools[i]).totalSupply(), iPOOL(getCuratedPools[i]).tokenAmount());
uint tokenSwapped = calcSwapValueInBaseWithPool(getCuratedPools[i], tokenAmount);
cdpValue = cdpValue.add(baseAmount.add(tokenSwapped));
}
return cdpValue;
}

}
24 changes: 18 additions & 6 deletions contracts/poolFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ contract Pool is iBEP20 {

uint public genesis;
uint public baseAmount;
uint public unitsAmount;
uint public tokenAmount;
uint public baseAmountPooled;
uint public tokenAmountPooled;
Expand Down Expand Up @@ -121,7 +122,9 @@ contract Pool is iBEP20 {
function sync() public {
baseAmount = iBEP20(BASE).balanceOf(address(this));
tokenAmount = iBEP20(TOKEN).balanceOf(address(this));
unitsAmount = balanceOf(address(this));
}


// Add liquidity for self
function addLiquidity() public returns(uint liquidityUnits){
Expand All @@ -147,18 +150,27 @@ contract Pool is iBEP20 {

// Remove Liquidity for a member
function removeLiquidityForMember(address member) public returns (uint outputBase, uint outputToken) {
uint units = balanceOf(member);
transferTo(address(this), units); // this needs testing
outputBase = iUTILS(_DAO().UTILS()).calcLiquidityShare(units, BASE, address(this), member);
outputToken = iUTILS(_DAO().UTILS()).calcLiquidityShare(units, TOKEN, address(this), member);
uint256 _actualInputUnits = _getAddedUnitsAmount();
outputBase = iUTILS(_DAO().UTILS()).calcLiquidityShare(_actualInputUnits, BASE, address(this), member);
outputToken = iUTILS(_DAO().UTILS()).calcLiquidityShare(_actualInputUnits, TOKEN, address(this), member);
_decrementPoolBalances(outputBase, outputToken);
_burn(address(this), units);
_burn(address(this), _actualInputUnits);
iBEP20(BASE).transfer(member, outputBase);
iBEP20(TOKEN).transfer(member, outputToken);
emit RemoveLiquidity(member, outputBase, outputToken, units);
emit RemoveLiquidity(member, outputBase, outputToken, _actualInputUnits);
return (outputBase, outputToken);
}

function _getAddedUnitsAmount() internal view returns(uint256 _actual){
uint _unitsBalance = balanceOf(address(this));
if(_unitsBalance > unitsAmount){
_actual = _unitsBalance.sub(unitsAmount);
} else {
_actual = 0;
}
return _actual;
}

function swap(address token) public returns (uint outputAmount, uint fee){
(outputAmount, fee) = swapTo(token, msg.sender);
return (outputAmount, fee);
Expand Down
6 changes: 3 additions & 3 deletions contracts/poolRouter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -148,10 +148,10 @@ contract Router {
// Remove an exact qty of units
function removeLiquidityExact(uint units, address token) public returns (uint outputBase, uint outputToken) {
address _pool = getPool(token);
require(isPool[_pool] = true, 'must be a pool');
address _member = msg.sender;
(outputBase, outputToken) = Pool(_pool).removeLiquidity();
_handleTransferOut(token, outputToken, _member);
_handleTransferOut(BASE, outputBase, _member);
_handleTransferIn(_pool, units, _pool);
(outputBase, outputToken) = Pool(_pool).removeLiquidityForMember(_member);
totalPooled = totalPooled.sub(outputBase);
removeLiquidityTx += 1;
return (outputBase, outputToken);
Expand Down
73 changes: 41 additions & 32 deletions contracts/synthFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ contract Synth is iBEP20 {
address public TOKEN;
uint public genesis;
uint public totalDebt;
uint public synthsAmount;

// ERC-20 Parameters
string _name; string _symbol;
Expand All @@ -27,6 +28,7 @@ contract Synth is iBEP20 {


event AddLPCollateral(address member, uint inputLPToken, uint synthsIssued, address collateralType);
event RemoveCollateral(address member, uint outputLPToken, uint synthsBurnt, address collateralType);

function _DAO() internal view returns(iDAO) {
return iBASE(BASE).DAO();
Expand All @@ -41,8 +43,6 @@ contract Synth is iBEP20 {
_symbol = string(abi.encodePacked(synthSymbol, iBEP20(_token).symbol()));
decimals = 18;
genesis = now;


}

//========================================iBEP20=========================================//
Expand Down Expand Up @@ -116,29 +116,22 @@ contract Synth is iBEP20 {
function swapSynth()public payable returns(uint256 outputAmount, uint256 fee){

}
function sync() public {
synthsAmount = balanceOf(address(this));
}

// Add collateral for self
function addCollateral(address lpToken) public returns(uint synths){
synths = addCollateralForMember(lpToken, msg.sender);
return synths;
}
function _getAddedCollateralAmount(address _lptoken) internal view returns(uint256 _actual){
uint _lpCollateralBalance = iBEP20(_lptoken).balanceOf(address(this));
if(_lpCollateralBalance > totalCollateral){
_actual = _lpCollateralBalance.sub(totalCollateral);
} else {
_actual = 0;
}
return _actual;
}

// add collateral for member
function addCollateralForMember(address lptoken, address member) public returns(uint synths){
uint256 _actualInputCollateral = _getAddedCollateralAmount(lptoken);// get the added collateral to LP CDP
uint _baseAmount = iPOOL(lptoken).baseAmount(); //used to calc assymetricalShare
uint _lpTotalSupply = iPOOL(lptoken).baseAmount(); //used to calc assymetricalShare
uint baseValue = iUTILS(_DAO().UTILS()).calcAsymmetricShare(lptoken, member);//get asym share in sparta
synths = iUTILS(_DAO().UTILS()).calcSwapValueInTokenWithPool(lptoken, baseValue); //get swap value with sparta
_incrementCDP(synths, _actualInputCollateral, lptoken); //update CDP
_incrementCDP(synths, _actualInputCollateral, lptoken); //update CDP details
collateralAmount[member][lptoken] = collateralAmount[member][lptoken].add(_actualInputCollateral);//member collateral lptoken > amount
debtAmount[member]= debtAmount[member].add(synths); //member debt lptoken > amount
_mint(member, synths); // mint synth to member
Expand All @@ -147,32 +140,48 @@ contract Synth is iBEP20 {
}

// Remove Collateral
function removeCollateral(uint basisPoints) public returns (uint outputToken) {
outputToken = removeCollateralForMember(basisPoints, msg.sender);
return outputToken;
function removeCollateral(address lptoken) public returns (uint outputCollateral, uint burntDebt) {
(outputCollateral, burntDebt )= removeCollateralForMember(lptoken,msg.sender);
return (outputCollateral, burntDebt);
}

// Remove Collateral for a member
function removeCollateralForMember(address lptoken, uint basisPoints, address member) public returns (uint outputCollateral) {
require((basisPoints > 0 && basisPoints <= 10000), "InputErr");
uint _synths = iUTILS(_DAO().UTILS()).calcPart(basisPoints, iBEP20(address(this)).balanceOf(member));
transferTo(address(this), _synths); // get synth from user
outputCollateral = iUTILS(_DAO().UTILS()).calcCDPShare(_synths, totalCollateral, address(this));
_decrementCDP(outputCollateral, _synths);
collateralAmount[member][lptoken] = collateralAmount[member][lptoken].add(_actualInputCollateral);//member collateral lptoken > amount
debtAmount[member]= debtAmount[member].add(synths); //member debt lptoken > amount
_burn(address(this), _synths);
iBEP20(BASE).transfer(member, outputBase);


emit RemoveCollateral(member, outputBase, outputToken, units);
return (outputBase, outputToken);
function removeCollateralForMember(address lptoken, address member) public returns (uint outputCollateral, uint debtBurnt) {
uint256 _actualInputSynths = _getAddedSynthsAmount();
outputCollateral = iUTILS(_DAO().UTILS()).calcCDPShare(_actualInputSynths, totalCollateral[lptoken], address(this));
_decrementCDP(_actualInputSynths, outputCollateral, lptoken); // update cdp details
collateralAmount[member][lptoken] = collateralAmount[member][lptoken].sub(outputCollateral);//member collateral lptoken > amount
debtAmount[member] = debtAmount[member].sub(_actualInputSynths); //member debt lptoken > amount
_burn(address(this), _actualInputSynths);
iBEP20(BASE).transfer(member, outputCollateral); // return their collateral
emit RemoveCollateral(member, outputCollateral, _actualInputSynths, lptoken);
return (outputCollateral, _actualInputSynths);
}

function _getAddedSynthsAmount() internal view returns(uint256 _actual){
uint _synthsBalance = balanceOf(address(this));
if(_synthsBalance > synthsAmount){
_actual = _synthsBalance.sub(synthsAmount);
} else {
_actual = 0;
}
return _actual;
}
function _getAddedCollateralAmount(address _lptoken) internal view returns(uint256 _actual){
uint _lpCollateralBalance = iBEP20(_lptoken).balanceOf(address(this));
if(_lpCollateralBalance > totalCollateral[_lptoken]){
_actual = _lpCollateralBalance.sub(totalCollateral[_lptoken]);
} else {
_actual = 0;
}
return _actual;
}


// Decrement CDP
function _decrementCDP(uint _debt, uint _collateral, address _lpToken) internal {
totalDebt = totalDebt.sub(_debt); // map total debt
totalCollateral[_lpToken] = totalCollateral[_lpToken].sub(_collateral); // map total collateral per LPTOKEN
totalCollateral[_lpToken] = totalCollateral[_lpToken].sub(_collateral); // map total collateral per LPTOKEN
}
// Increment CDP
function _incrementCDP(uint _debt, uint _collateral, address _lpToken) internal {
Expand Down
Loading

0 comments on commit ca1f0e0

Please sign in to comment.