Skip to content
This repository has been archived by the owner on Jan 18, 2023. It is now read-only.

Commit

Permalink
Add exposed modules
Browse files Browse the repository at this point in the history
  • Loading branch information
felix2feng committed Feb 21, 2019
1 parent a2cec37 commit 5470553
Show file tree
Hide file tree
Showing 17 changed files with 211 additions and 154 deletions.
136 changes: 8 additions & 128 deletions contracts/core/extensions/CoreIssuance.sol
Expand Up @@ -21,6 +21,7 @@ import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";

import { CoreOperationState } from "./CoreOperationState.sol";
import { CoreState } from "../lib/CoreState.sol";
import { IssuanceLibrary } from "../lib/IssuanceLibrary.sol";
import { ISetToken } from "../interfaces/ISetToken.sol";


Expand Down Expand Up @@ -175,7 +176,7 @@ contract CoreIssuance is
uint256[] memory units = setToken.getUnits();

// Calculate component quantities to redeem
uint256[] memory componentQuantities = calculateTransferValues(
uint256[] memory componentQuantities = IssuanceLibrary.calculateTransferValues(
units,
naturalUnit,
_quantity
Expand All @@ -192,7 +193,7 @@ contract CoreIssuance is
(
uint256[] memory incrementTokenOwnerValues,
uint256[] memory withdrawToValues
) = calculateWithdrawAndIncrementQuantities(
) = IssuanceLibrary.calculateWithdrawAndIncrementQuantities(
componentQuantities,
_toExclude
);
Expand Down Expand Up @@ -304,7 +305,7 @@ contract CoreIssuance is
uint256[] memory units = setToken.getUnits();

// Calculate component quantities to issue
uint256[] memory requiredComponentQuantities = calculateTransferValues(
uint256[] memory requiredComponentQuantities = IssuanceLibrary.calculateTransferValues(
units,
naturalUnit,
_quantity
Expand All @@ -314,10 +315,11 @@ contract CoreIssuance is
(
uint256[] memory decrementTokenOwnerValues,
uint256[] memory depositValues
) = calculateDepositAndDecrementQuantities(
) = IssuanceLibrary.calculateDepositAndDecrementQuantities(
components,
requiredComponentQuantities,
_componentOwner
_componentOwner,
state.vault
);

// Decrement components used for issuance in vault
Expand Down Expand Up @@ -421,7 +423,7 @@ contract CoreIssuance is
// Fetch Set token properties
address[] memory components = setToken.getComponents();
uint256[] memory units = setToken.getUnits();
uint256[] memory tokenValues = calculateTransferValues(
uint256[] memory tokenValues = IssuanceLibrary.calculateTransferValues(
units,
naturalUnit,
_quantity
Expand All @@ -441,126 +443,4 @@ contract CoreIssuance is
tokenValues
);
}

/**
* Calculate the quantities required to deposit and decrement during issuance. Takes into account
* the tokens an owner already has in the vault.
*
* @param _components Addresses of components
* @param _componentQuantities Component quantities to increment and withdraw
* @param _owner Address to deposit and decrement quantities from
* @return uint256[] decrementQuantities Quantities to decrement from vault
* @return uint256[] depositQuantities Quantities to deposit into the vault
*/
function calculateDepositAndDecrementQuantities(
address[] _components,
uint256[] _componentQuantities,
address _owner
)
private
view
returns (
uint256[] /* decrementtQuantities */,
uint256[] /* depositQuantities */
)
{
uint256 componentCount = _components.length;
uint256[] memory decrementTokenOwnerValues = new uint256[](componentCount);
uint256[] memory depositQuantities = new uint256[](componentCount);

for (uint256 i = 0; i < componentCount; i++) {
// Fetch component quantity in vault
uint256 vaultBalance = state.vaultInstance.getOwnerBalance(
_components[i],
_owner
);

// If the vault holds enough components, decrement the full amount
if (vaultBalance >= _componentQuantities[i]) {
decrementTokenOwnerValues[i] = _componentQuantities[i];
} else {
// User has less than required amount, decrement the vault by full balance
if (vaultBalance > 0) {
decrementTokenOwnerValues[i] = vaultBalance;
}

depositQuantities[i] = _componentQuantities[i].sub(vaultBalance);
}
}

return (
decrementTokenOwnerValues,
depositQuantities
);
}

/**
* Calculate the quantities required to withdraw and increment during redeem and withdraw. Takes into
* account a bitmask exclusion parameter.
*
* @param _componentQuantities Component quantities to increment and withdraw
* @param _toExclude Mask of indexes of tokens to exclude from withdrawing
* @return uint256[] incrementQuantities Quantities to increment in vault
* @return uint256[] withdrawQuantities Quantities to withdraw from vault
*/
function calculateWithdrawAndIncrementQuantities(
uint256[] _componentQuantities,
uint256 _toExclude
)
private
pure
returns (
uint256[] /* incrementQuantities */,
uint256[] /* withdrawQuantities */
)
{
uint256 componentCount = _componentQuantities.length;
uint256[] memory incrementTokenOwnerValues = new uint256[](componentCount);
uint256[] memory withdrawToValues = new uint256[](componentCount);

// Loop through and decrement vault balances for the set, withdrawing if requested
for (uint256 i = 0; i < componentCount; i++) {
// Calculate bit index of current component
uint256 componentBitIndex = 2 ** i;

// Transfer to user unless component index is included in _toExclude
if ((_toExclude & componentBitIndex) != 0) {
incrementTokenOwnerValues[i] = _componentQuantities[i];
} else {
withdrawToValues[i] = _componentQuantities[i];
}
}

return (
incrementTokenOwnerValues,
withdrawToValues
);
}

/**
* Calculate the transfer values components given quantity of Set
*
* @param _componentUnits The units of the component token
* @param _naturalUnit The natural unit of the Set token
* @param _quantity The number of tokens being redeem
* @return uint256[] Transfer value in base units of the Set
*/
function calculateTransferValues(
uint256[] _componentUnits,
uint256 _naturalUnit,
uint256 _quantity
)
internal
pure
returns (uint256[])
{
uint256[] memory tokenValues = new uint256[](_componentUnits.length);

// Transfer the underlying tokens to the corresponding token balances
for (uint256 i = 0; i < _componentUnits.length; i++) {
tokenValues[i] = _quantity.mul(_componentUnits[i]).div(_naturalUnit);
}

return tokenValues;
}
}
9 changes: 6 additions & 3 deletions contracts/core/extensions/CoreModuleInteraction.sol
Expand Up @@ -37,11 +37,15 @@ contract CoreModuleInteraction is
ReentrancyGuard
{
modifier onlyModule() {
onlyModuleCallable();
_;
}

function onlyModuleCallable() internal view {
require(
state.validModules[msg.sender],
"Core: Sender not recognized module"
"Core: Not module"
);
_;
}

/**
Expand Down Expand Up @@ -89,7 +93,6 @@ contract CoreModuleInteraction is
external
onlyModule
{

batchWithdrawInternal(
_from,
_to,
Expand Down
159 changes: 159 additions & 0 deletions contracts/core/lib/IssuanceLibrary.sol
@@ -0,0 +1,159 @@
/*
Copyright 2018 Set Labs Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

pragma solidity 0.4.25;
pragma experimental "ABIEncoderV2";

import { SafeMath } from "openzeppelin-solidity/contracts/math/SafeMath.sol";

import { IVault } from "../interfaces/IVault.sol";


/**
* @title IssuanceLibrary
* @author Set Protocol
*
* This library contains functions for calculating
*/
library IssuanceLibrary {

using SafeMath for uint256;

/**
* Calculate the quantities required to deposit and decrement during issuance. Takes into account
* the tokens an owner already has in the vault.
*
* @param _components Addresses of components
* @param _componentQuantities Component quantities to increment and withdraw
* @param _owner Address to deposit and decrement quantities from
* @param _vault Address to vault
* @return uint256[] decrementQuantities Quantities to decrement from vault
* @return uint256[] depositQuantities Quantities to deposit into the vault
*/
function calculateDepositAndDecrementQuantities(
address[] _components,
uint256[] _componentQuantities,
address _owner,
address _vault
)
external
view
returns (
uint256[] /* decrementtQuantities */,
uint256[] /* depositQuantities */
)
{
uint256 componentCount = _components.length;
uint256[] memory decrementTokenOwnerValues = new uint256[](componentCount);
uint256[] memory depositQuantities = new uint256[](componentCount);

for (uint256 i = 0; i < componentCount; i++) {
// Fetch component quantity in vault
uint256 vaultBalance = IVault(_vault).getOwnerBalance(
_components[i],
_owner
);

// If the vault holds enough components, decrement the full amount
if (vaultBalance >= _componentQuantities[i]) {
decrementTokenOwnerValues[i] = _componentQuantities[i];
} else {
// User has less than required amount, decrement the vault by full balance
if (vaultBalance > 0) {
decrementTokenOwnerValues[i] = vaultBalance;
}

depositQuantities[i] = _componentQuantities[i].sub(vaultBalance);
}
}

return (
decrementTokenOwnerValues,
depositQuantities
);
}

/**
* Calculate the quantities required to withdraw and increment during redeem and withdraw. Takes into
* account a bitmask exclusion parameter.
*
* @param _componentQuantities Component quantities to increment and withdraw
* @param _toExclude Mask of indexes of tokens to exclude from withdrawing
* @return uint256[] incrementQuantities Quantities to increment in vault
* @return uint256[] withdrawQuantities Quantities to withdraw from vault
*/
function calculateWithdrawAndIncrementQuantities(
uint256[] _componentQuantities,
uint256 _toExclude
)
external
pure
returns (
uint256[] /* incrementQuantities */,
uint256[] /* withdrawQuantities */
)
{
uint256 componentCount = _componentQuantities.length;
uint256[] memory incrementTokenOwnerValues = new uint256[](componentCount);
uint256[] memory withdrawToValues = new uint256[](componentCount);

// Loop through and decrement vault balances for the set, withdrawing if requested
for (uint256 i = 0; i < componentCount; i++) {
// Calculate bit index of current component
uint256 componentBitIndex = 2 ** i;

// Transfer to user unless component index is included in _toExclude
if ((_toExclude & componentBitIndex) != 0) {
incrementTokenOwnerValues[i] = _componentQuantities[i];
} else {
withdrawToValues[i] = _componentQuantities[i];
}
}

return (
incrementTokenOwnerValues,
withdrawToValues
);
}

/**
* Calculate the transfer values components given quantity of Set
*
* @param _componentUnits The units of the component token
* @param _naturalUnit The natural unit of the Set token
* @param _quantity The number of tokens being redeem
* @return uint256[] Transfer value in base units of the Set
*/
function calculateTransferValues(
uint256[] _componentUnits,
uint256 _naturalUnit,
uint256 _quantity
)
external
pure
returns (uint256[])
{
uint256[] memory tokenValues = new uint256[](_componentUnits.length);

// Transfer the underlying tokens to the corresponding token balances
for (uint256 i = 0; i < _componentUnits.length; i++) {
tokenValues[i] = _quantity.mul(_componentUnits[i]).div(_naturalUnit);
}

return tokenValues;
}

}
1 change: 0 additions & 1 deletion contracts/supplementary/PayableExchangeIssue.sol
Expand Up @@ -83,7 +83,6 @@ contract PayableExchangeIssue is

// Commit the address and instance of Transfer Proxy to state variables
transferProxy = _transferProxy;
transferProxyInstance = ITransferProxy(transferProxy);

// Commit the address and instance of Exchange Issue Module to state variables
exchangeIssueModule = _exchangeIssueModule;
Expand Down

0 comments on commit 5470553

Please sign in to comment.