Skip to content

Commit

Permalink
Replace Fixidity with DecimalMath in Issuance*.sol
Browse files Browse the repository at this point in the history
  • Loading branch information
uivlis committed Feb 28, 2020
1 parent e5b1d3f commit fd5b475
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 41 deletions.
18 changes: 3 additions & 15 deletions contracts/drafts/issuance/IssuanceAdvanced.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "../../token/IERC20Detailed.sol";
import "../../token/IERC20MintableDetailed.sol";
import "./../../state/StateMachine.sol";
import "../../utils/SafeCast.sol";
import "../../math/DecimalMath.sol";


/**
Expand All @@ -18,9 +19,7 @@ import "../../utils/SafeCast.sol";
contract IssuanceAdvanced is Ownable, StateMachine, ReentrancyGuard {

using SafeMath for uint256;
using FixidityLib for int256;
using SafeCast for int256;
using SafeCast for uint256;
using DecimalMath for uint256;

event IssuanceCreated();

Expand Down Expand Up @@ -102,23 +101,12 @@ contract IssuanceAdvanced is Ownable, StateMachine, ReentrancyGuard {
require(investments[msg.sender] > 0, "No investments found.");
uint256 amount = investments[msg.sender];
investments[msg.sender] = 0;
IERC20Detailed _currencyToken = IERC20Detailed(currencyToken);
IERC20MintableDetailed _issuanceToken = IERC20MintableDetailed(
issuanceToken
);
int256 investedFixed = amount.safeUintToInt().newFixed(
_currencyToken.decimals()
);
int256 issuePriceFixed = issuePrice.safeUintToInt().newFixed(
_currencyToken.decimals()
);
int256 issuanceTokensFixed = investedFixed.divide(issuePriceFixed);
uint256 issuanceTokens = issuanceTokensFixed.fromFixed(
_issuanceToken.decimals()
).safeIntToUint();
_issuanceToken.mint(
msg.sender,
issuanceTokens
amount.divd(issuePrice, _issuanceToken.decimals())
);
}

Expand Down
18 changes: 3 additions & 15 deletions contracts/issuance/Issuance.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import "../token/IERC20Detailed.sol";
import "../token/IERC20MintableDetailed.sol";
import "../state/StateMachine.sol";
import "../utils/SafeCast.sol";
import "../math/DecimalMath.sol";


/**
Expand All @@ -29,9 +30,7 @@ import "../utils/SafeCast.sol";
*/
contract Issuance is Ownable, StateMachine, ReentrancyGuard {
using SafeMath for uint256;
using FixidityLib for int256;
using SafeCast for int256;
using SafeCast for uint256;
using DecimalMath for uint256;

event IssuanceCreated();
event IssuePriceSet();
Expand Down Expand Up @@ -98,23 +97,12 @@ contract Issuance is Ownable, StateMachine, ReentrancyGuard {
);
uint256 amount = investments[msg.sender];
investments[msg.sender] = 0;
IERC20Detailed _currencyToken = IERC20Detailed(currencyToken);
IERC20MintableDetailed _issuanceToken = IERC20MintableDetailed(
issuanceToken
);
int256 investedFixed = amount.safeUintToInt().newFixed(
_currencyToken.decimals()
);
int256 issuePriceFixed = issuePrice.safeUintToInt().newFixed(
_currencyToken.decimals()
);
int256 issuanceTokensFixed = investedFixed.divide(issuePriceFixed);
uint256 issuanceTokens = issuanceTokensFixed.fromFixed(
_issuanceToken.decimals()
).safeIntToUint();
_issuanceToken.mint(
msg.sender,
issuanceTokens
amount.divd(issuePrice, _issuanceToken.decimals())
);
}

Expand Down
14 changes: 3 additions & 11 deletions contracts/issuance/IssuanceEth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import "@openzeppelin/contracts/math/SafeMath.sol";
import "@openzeppelin/contracts/utils/ReentrancyGuard.sol";
import "../token/IERC20MintableDetailed.sol";
import "../state/StateMachine.sol";
import "../utils/SafeCast.sol";
import "../math/DecimalMath.sol";


/**
Expand All @@ -27,9 +27,7 @@ import "../utils/SafeCast.sol";
*/
contract IssuanceEth is Ownable, StateMachine, ReentrancyGuard {
using SafeMath for uint256;
using FixidityLib for int256;
using SafeCast for int256;
using SafeCast for uint256;
using DecimalMath for uint256;

event IssuanceCreated();
event IssuePriceSet();
Expand Down Expand Up @@ -73,15 +71,9 @@ contract IssuanceEth is Ownable, StateMachine, ReentrancyGuard {
IERC20MintableDetailed _issuanceToken = IERC20MintableDetailed(
issuanceToken
);
int256 investedFixed = amount.safeUintToInt().newFixed(18);
int256 issuePriceFixed = issuePrice.safeUintToInt().newFixed(18);
int256 issuanceTokensFixed = investedFixed.divide(issuePriceFixed);
uint256 issuanceTokens = issuanceTokensFixed.fromFixed(
_issuanceToken.decimals()
).safeIntToUint();
_issuanceToken.mint(
msg.sender,
issuanceTokens
amount.divd(issuePrice, _issuanceToken.decimals())
);
}

Expand Down
1 change: 1 addition & 0 deletions contracts/test/math/DecimalMathMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ contract DecimalMathMock {
function muld(uint256 x, uint256 y) public pure returns (uint256) {
return x.muld(y);
}

function muld2(
uint256 x,
uint256 y,
Expand Down

0 comments on commit fd5b475

Please sign in to comment.