Skip to content

Commit

Permalink
Merge pull request #20 from ripio/fix-trailing-whitespaces
Browse files Browse the repository at this point in the history
Fix lint and trailing whitespaces
  • Loading branch information
Agustin Aguilar authored Aug 13, 2019
2 parents 3be4b97 + f55db5f commit c266471
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 68 deletions.
59 changes: 26 additions & 33 deletions contracts/ConverterRamp.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,27 +11,25 @@ import "./interfaces/RateOracle.sol";
import "./safe/SafeERC20.sol";

/// @title Converter Ramp
/// @notice for conversion between different assets, use TokenConverter
/// @notice for conversion between different assets, use TokenConverter
/// contract as abstract layer for convert different assets.
/// @dev All function calls are currently implemented without side effects
contract ConverterRamp is Ownable {

using SafeMath for uint256;
using SafeERC20 for IERC20;

/// @notice address to identify operations with ETH
/// @notice address to identify operations with ETH
address public constant ETH_ADDRESS = address(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee);

event Return(address _token, address _to, uint256 _amount);
event ReadedOracle(address _oracle, uint256 _tokens, uint256 _equivalent);


/// @notice pays a loan using _fromTokens
/// @param _converter converter to use for swapping (uniswap, kyber, bancor, etc)
/// @param _fromToken token address to convert
/// @param _loanManagerAddress address of diaspore LoanManagaer
/// @param _debtEngineAddress address of diaspore LoanManagaer
/// @param _payFrom registering pay address
/// @param _debtEngineAddress address of diaspore LoanManagaer
/// @param _payFrom registering pay address
/// @param _requestId loan id to pay
/// @param _oracleData data signed by ripio oracle
function pay(
Expand All @@ -43,24 +41,23 @@ contract ConverterRamp is Ownable {
bytes32 _requestId,
bytes calldata _oracleData
) external payable {

/// load RCN IERC20, we need it to pay
IERC20 token = LoanManager(_loanManagerAddress).token();

/// get amount required, in RCN, for payment
uint256 amount = getRequiredRcnPay(
_loanManagerAddress,
_requestId,
_requestId,
_oracleData
);

/// converter using token converter
convertSafe(_converter, _loanManagerAddress, _fromToken, address(token), amount);

/// pay loan
DebtEngine debtEngine = DebtEngine(_debtEngineAddress);
require(token.safeApprove(_debtEngineAddress, amount), "error on payment approve");

uint256 prevTokenBalance = token.balanceOf(address(this));
debtEngine.pay(_requestId, amount, _payFrom, _oracleData);

Expand All @@ -72,12 +69,12 @@ contract ConverterRamp is Ownable {
/// @param _converter converter to use for swapping (uniswap, kyber, bancor, etc)
/// @param _fromToken token address to convert
/// @param _loanManagerAddress address of diaspore LoanManagaer
/// @param _lenderCosignerAddress address of diaspore Cosigner
/// @param _debtEngineAddress address of diaspore LoanManagaer
/// @param _lenderCosignerAddress address of diaspore Cosigner
/// @param _debtEngineAddress address of diaspore LoanManagaer
/// @param _requestId loan id to pay
/// @param _oracleData data signed by ripio oracle
/// @param _cosignerData cosigner data
/// @param _callbackData callback data
/// @param _callbackData callback data
function lend(
address _converter,
address _fromToken,
Expand All @@ -89,16 +86,15 @@ contract ConverterRamp is Ownable {
bytes memory _cosignerData,
bytes memory _callbackData
) public payable {

/// load RCN IERC20
IERC20 token = LoanManager(_loanManagerAddress).token();

/// get required RCN for lending the loan
uint256 amount = getRequiredRcnLend(
_loanManagerAddress,
_lenderCosignerAddress,
_requestId,
_oracleData,
_loanManagerAddress,
_lenderCosignerAddress,
_requestId,
_oracleData,
_cosignerData
);

Expand All @@ -108,14 +104,14 @@ contract ConverterRamp is Ownable {
uint256 prevTokenBalance = token.balanceOf(address(this));

LoanManager(_loanManagerAddress).lend(
_requestId,
_oracleData,
_lenderCosignerAddress,
0,
_cosignerData,
_requestId,
_oracleData,
_lenderCosignerAddress,
0,
_cosignerData,
_callbackData
);

require(token.safeApprove(_loanManagerAddress, 0), "error removing approve");
require(token.balanceOf(address(this)) == prevTokenBalance.sub(amount), "the contract balance should be the previous");

Expand All @@ -131,14 +127,12 @@ contract ConverterRamp is Ownable {
/// @param _token RCN token address
/// @return _tokenCost and _etherCost
function getCost(uint _amount, address _converter, address _fromToken, address _token) public view returns (uint256, uint256) {

TokenConverter tokenConverter = TokenConverter(_converter);
if (_fromToken == ETH_ADDRESS) {
return tokenConverter.getPrice(_token, _amount);
} else {
return tokenConverter.getPrice(_fromToken, _token, _amount);
}

}

/// @notice Converts an amount using a converter
Expand All @@ -150,7 +144,6 @@ contract ConverterRamp is Ownable {
address _token,
uint256 _amount
) internal {

(uint256 tokenCost, uint256 etherCost) = getCost(_amount, _converter, _fromToken, address(_token));
if (_fromToken == ETH_ADDRESS) {
ethConvertSafe(_converter, _fromToken, address(_token), _amount, tokenCost, etherCost);
Expand All @@ -170,11 +163,11 @@ contract ConverterRamp is Ownable {
uint256 _tokenCost,
uint256 _etherCost
) internal {

IERC20 fromToken = IERC20(_fromTokenAddress);
IERC20 toToken = IERC20(_toTokenAddress);
TokenConverter tokenConverter = TokenConverter(_converter);

/// pull tokens to convert
require(fromToken.safeTransferFrom(msg.sender, address(this), _tokenCost), "Error pulling token amount");

Expand Down Expand Up @@ -235,7 +228,7 @@ contract ConverterRamp is Ownable {
bytes memory _oracleData,
bytes memory _cosignerData
) internal returns (uint256) {

/// load loan manager and id
LoanManager loanManager = LoanManager(_loanManagerAddress);
uint256 amount = loanManager.getAmount(_requestId);
Expand All @@ -248,8 +241,8 @@ contract ConverterRamp is Ownable {
amount = amount.add(cosigner.cost(_loanManagerAddress, uint256(_requestId), _cosignerData, _oracleData));
}

/// load the Oracle rate and convert required
address oracle = loanManager.getOracle(uint256(_requestId)) ;
/// load the Oracle rate and convert required
address oracle = loanManager.getOracle(uint256(_requestId));
return getCurrencyToToken(oracle, amount, _oracleData);
}

Expand All @@ -259,7 +252,6 @@ contract ConverterRamp is Ownable {
bytes32 _requestId,
bytes memory _oracleData
) internal returns (uint256 _result) {

/// Load LoanManager and ID
LoanManager loanManager = LoanManager(_loanManagerAddress);
uint256 amount = loanManager.getAmount(_requestId);
Expand All @@ -279,6 +271,7 @@ contract ConverterRamp is Ownable {
if (_oracle == address(0)) {
return _amount;
}

(uint256 tokens, uint256 equivalent) = RateOracle(_oracle).readSample(_oracleData);

emit ReadedOracle(_oracle, tokens, equivalent);
Expand Down
2 changes: 0 additions & 2 deletions contracts/mock/diaspore/TestDebtEngine.sol
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ contract TestDebtEngine is ERC721Base {
);

_generate(uint256(id), _owner);

}

function pay(
Expand All @@ -75,7 +74,6 @@ contract TestDebtEngine is ERC721Base {
address _origin,
bytes calldata _oracleData
) external returns (uint256 paid, uint256 paidToken) {

// Pull tokens from payer
require(token.transferFrom(msg.sender, address(this), _amount), "Error pulling payment tokens");

Expand Down
56 changes: 23 additions & 33 deletions contracts/proxy/UniswapProxy.sol
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,11 @@ import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
pragma solidity 0.5.10;

/// @notice proxy between ConverterRamp and Uniswap
/// accepts tokens and ether, converts these to the desired token,
/// and makes approve calls to allow the recipient to transfer those
/// accepts tokens and ether, converts these to the desired token,
/// and makes approve calls to allow the recipient to transfer those
/// tokens from the contract.
/// @author Joaquin Pablo Gonzalez (jpgonzalezra@gmail.com)
contract UniswapProxy is TokenConverter, Ownable {

using SafeMath for uint256;
using SafeExchange for UniswapExchangeInterface;
using SafeERC20 for IERC20;
Expand All @@ -24,18 +23,18 @@ contract UniswapProxy is TokenConverter, Ownable {
event WithdrawEth(address _to, uint256 _amount);
event SetUniswap(address _uniswapFactory);

/// @notice address to identify operations with ETH
/// @notice address to identify operations with ETH
IERC20 constant internal ETH_TOKEN_ADDRESS = IERC20(0x00eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee);

/// @notice registry of ERC20 tokens that have been added to the system
/// @notice registry of ERC20 tokens that have been added to the system
/// and the exchange to which they are associated.
UniswapFactoryInterface factory;

constructor (address _uniswapFactory) public {
factory = UniswapFactoryInterface(_uniswapFactory);
emit SetUniswap(_uniswapFactory);
}

/// @notice get price to swap token to another token
/// @return _tokenCost, _etherCost, _inExchange
/// @dev internal method.
Expand All @@ -44,10 +43,8 @@ contract UniswapProxy is TokenConverter, Ownable {
address _outToken,
uint256 _amount
) internal view returns (uint256, uint256, UniswapExchangeInterface) {
UniswapExchangeInterface inExchange =
UniswapExchangeInterface(factory.getExchange(_token));
UniswapExchangeInterface outExchange =
UniswapExchangeInterface(factory.getExchange(_outToken));
UniswapExchangeInterface inExchange = UniswapExchangeInterface(factory.getExchange(_token));
UniswapExchangeInterface outExchange = UniswapExchangeInterface(factory.getExchange(_outToken));

uint256 etherCost = outExchange.getEthToTokenOutputPrice(_amount);
uint256 tokenCost = inExchange.getTokenToEthOutputPrice(etherCost);
Expand All @@ -62,8 +59,7 @@ contract UniswapProxy is TokenConverter, Ownable {
address _outToken,
uint256 _amount
) internal view returns (uint256, UniswapExchangeInterface) {
UniswapExchangeInterface exchange =
UniswapExchangeInterface(factory.getExchange(_outToken));
UniswapExchangeInterface exchange = UniswapExchangeInterface(factory.getExchange(_outToken));

return (exchange.getEthToTokenOutputPrice(_amount), exchange);
}
Expand All @@ -84,9 +80,8 @@ contract UniswapProxy is TokenConverter, Ownable {
address _outToken,
uint256 _amount
) public view returns (uint256, uint256) {

(
uint256 tokenCost,
uint256 tokenCost,
uint256 etherCost,
) = price(address(_token), address(_outToken), _amount);

Expand All @@ -99,17 +94,16 @@ contract UniswapProxy is TokenConverter, Ownable {
address _outToken,
uint256 _amount
) public view returns (uint256, uint256) {

(
uint256 etherCost,
) = price(address(_outToken), _amount);

return (0, etherCost);

}

/// @notice Converts an amount
/// a. swap the user`s ETH to IERC20 token or
/// @notice Converts an amount
/// a. swap the user`s ETH to IERC20 token or
/// b. swap the user`s IERC20 token to another IERC20 token
/// @param _inToken source token contract address
/// @param _outToken destination token contract address
Expand All @@ -118,22 +112,20 @@ contract UniswapProxy is TokenConverter, Ownable {
/// @param _etherCost amount of source _etherCost
function convert(
IERC20 _inToken,
IERC20 _outToken,
IERC20 _outToken,
uint256 _amount,
uint256 _tokenCost,
uint256 _etherCost
) external payable {

) external payable {
address sender = msg.sender;
if (_inToken == ETH_TOKEN_ADDRESS && _outToken != ETH_TOKEN_ADDRESS) {
execSwapEtherToToken(_outToken, _amount, _etherCost, sender);
} else {
require(msg.value == 0, "eth not required");
require(msg.value == 0, "eth not required");
execSwapTokenToToken(_inToken, _amount, _tokenCost, _etherCost, _outToken, sender);
}

emit Swap(msg.sender, _inToken, _outToken, _amount);

}

/// @notice Swap the user`s ETH to IERC20 token
Expand All @@ -142,18 +134,17 @@ contract UniswapProxy is TokenConverter, Ownable {
/// @param _etherCost amount of source _etherCost
/// @param _recipient address to send swapped tokens to
function execSwapEtherToToken(
IERC20 _outToken,
IERC20 _outToken,
uint _amount,
uint _etherCost,
uint _etherCost,
address _recipient
) public payable {

UniswapExchangeInterface exchange = UniswapExchangeInterface(factory.getExchange(address(_outToken)));

require(msg.value >= _etherCost, "insufficient ether sent.");
exchange.swapEther(_amount, _etherCost, block.timestamp + 1, _outToken);

require(_outToken.safeTransfer(_recipient, _amount), "error transfer tokens");
require(_outToken.safeTransfer(_recipient, _amount), "error transfer tokens");
uint256 toReturn = msg.value.sub(_etherCost);
if (toReturn > 0) {
msg.sender.transfer(toReturn);
Expand All @@ -169,11 +160,11 @@ contract UniswapProxy is TokenConverter, Ownable {
/// @param _outToken destination token contract address
/// @param _recipient address to send swapped tokens to
function execSwapTokenToToken(
IERC20 _token,
IERC20 _token,
uint256 _amount,
uint256 _tokenCost,
uint256 _etherCost,
IERC20 _outToken,
uint256 _etherCost,
IERC20 _outToken,
address _recipient
) internal {

Expand All @@ -186,9 +177,8 @@ contract UniswapProxy is TokenConverter, Ownable {

/// safe swap tokens
exchange.swapTokens(_amount, _tokenCost, _etherCost, block.timestamp + 1, _outToken);
require(_outToken.safeTransfer(_recipient, _amount), "error transfer tokens");
require(_outToken.safeTransfer(_recipient, _amount), "error transfer tokens");
}

function() external payable {}

}

0 comments on commit c266471

Please sign in to comment.