Skip to content
31 changes: 17 additions & 14 deletions contracts/core/diaspore/cosigner/Collateral.sol
Original file line number Diff line number Diff line change
Expand Up @@ -790,28 +790,31 @@ contract Collateral is Ownable, Cosigner, ERC721Base {
_oracleData
);

emit ConvertPay(
_entryId,
sold,
bought,
_oracleData
);

if (paidTokens < tokensToPay) {
// Buy back extra collateral
sold = tokensToPay - paidTokens;
bought = converter.safeConvertFrom(
// TODO: Remove convertback, replace by raw RCN
uint256 remainder = tokensToPay - paidTokens;
uint256 rebought = converter.safeConvertFrom(
loanManagerToken,
token,
sold,
remainder,
0
);
emit Rebuy(_entryId, sold, bought);
} else {
bought = 0;

sold = sold.sub(rebought);
bought = bought.sub(remainder);

emit Rebuy(_entryId, remainder, rebought);
}

entry.amount = entry.amount.sub(sold).add(bought);
emit ConvertPay(
_entryId,
sold,
bought,
_oracleData
);

entry.amount = entry.amount.sub(sold);
}

/**
Expand Down
12 changes: 12 additions & 0 deletions contracts/test_utils/diaspore/TestModel.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ contract TestModel is ERC165, BytesUtils, Ownable {
event SetEngine(address _engine);
event SetErrorFlag(bytes32 _id, uint256 _flag);
event SetGlobalErrorFlag(uint256 _flag);
event SetTotal(bytes32 _id, uint256 _total);
event SetDueTime(bytes32 _id, uint256 _time);

mapping(bytes4 => bool) private _supportedInterface;

Expand Down Expand Up @@ -84,6 +86,16 @@ contract TestModel is ERC165, BytesUtils, Ownable {
emit SetErrorFlag(_id, _flag);
}

function setTotal(bytes32 _id, uint128 _total) external onlyOwner {
registry[_id].total = _total;
emit SetTotal(_id, _total);
}

function setDueTime(bytes32 _id, uint64 _time) external onlyOwner {
registry[_id].dueTime = _time;
emit SetDueTime(_id, _time);
}

function setEngine(address _engine) external onlyOwner {
engine = _engine;
emit SetEngine(_engine);
Expand Down
63 changes: 39 additions & 24 deletions contracts/utils/SafeTokenConverter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,23 @@ library SafeTokenConverter {
uint256 _fromAmount,
uint256 _minReceive
) internal returns (uint256 received) {
require(_fromToken.safeApprove(address(_converter), _fromAmount), "error approving converter");
uint256 prevToBalance = _toToken.balanceOf(address(this));

_converter.convertFrom(
_fromToken,
_toToken,
_fromAmount,
_minReceive
);

require(_fromToken.clearApprove(address(_converter)), "error clearing approve");
received = _toToken.balanceOf(address(this)).sub(prevToBalance);
if (address(_fromToken) == address(_toToken)) {
received = _fromAmount;
} else {
require(_fromToken.safeApprove(address(_converter), _fromAmount), "error approving converter");
uint256 prevToBalance = _toToken.balanceOf(address(this));

_converter.convertFrom(
_fromToken,
_toToken,
_fromAmount,
_minReceive
);

require(_fromToken.clearApprove(address(_converter)), "error clearing approve");
received = _toToken.balanceOf(address(this)).sub(prevToBalance);
}

require(received >= _minReceive, "_minReceived not reached");
}

Expand All @@ -40,22 +45,27 @@ library SafeTokenConverter {
uint256 _toAmount,
uint256 _maxSpend
) internal returns (uint256 spend) {
require(_fromToken.safeApprove(address(_converter), _maxSpend), "error approving converter");
if (address(_fromToken) == address(_toToken)) {
spend = _toAmount;
} else {
require(_fromToken.safeApprove(address(_converter), _maxSpend), "error approving converter");

uint256 prevFromBalance = _fromToken.balanceOf(address(this));
uint256 prevToBalance = _toToken.balanceOf(address(this));
uint256 prevFromBalance = _fromToken.balanceOf(address(this));
uint256 prevToBalance = _toToken.balanceOf(address(this));

_converter.convertTo(
_fromToken,
_toToken,
_toAmount,
_maxSpend
);
_converter.convertTo(
_fromToken,
_toToken,
_toAmount,
_maxSpend
);

require(_fromToken.clearApprove(address(_converter)), "error clearing approve");
spend = prevFromBalance.sub(_fromToken.balanceOf(address(this)));
require(_toToken.balanceOf(address(this)).sub(prevToBalance) >= _toAmount, "_toAmount not received");
}

require(_fromToken.clearApprove(address(_converter)), "error clearing approve");
spend = prevFromBalance.sub(_fromToken.balanceOf(address(this)));
require(spend <= _maxSpend, "_maxSpend exceeded");
require(_toToken.balanceOf(address(this)).sub(prevToBalance) >= _toAmount, "_toAmount not received");
}

function safeConvertToMax(
Expand All @@ -65,6 +75,11 @@ library SafeTokenConverter {
uint256 _toAmount,
uint256 _maxSpend
) internal returns (uint256 received, uint256 spend) {
if (address(_fromToken) == address(_toToken)) {
uint256 min = _maxSpend < _toAmount ? _maxSpend : _toAmount;
return (min, min);
}

uint256 maxReceive = _converter.getPriceConvertFrom(_fromToken, _toToken, _maxSpend);

if (maxReceive < _toAmount) {
Expand Down
Loading