Skip to content

Commit

Permalink
refactor fixed rate exchange
Browse files Browse the repository at this point in the history
  • Loading branch information
Ahmed Ali authored and Ahmed Ali committed Aug 3, 2020
1 parent 51c3e69 commit 5fd2697
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 275 deletions.
61 changes: 2 additions & 59 deletions contracts/DTFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ pragma solidity ^0.5.7;
import './utils/Deployer.sol';
import './utils/Converter.sol';
import './interfaces/IERC20Template.sol';
import './interfaces/IFPLP.sol';

/**
* @title DTFactory contract
Expand All @@ -20,7 +19,6 @@ import './interfaces/IFPLP.sol';
*/
contract DTFactory is Deployer, Converter {
address private tokenTemplate;
address private fplpTemplate;

uint256 private currentTokenCount = 1;
// cap has max uint256 (2^256 -1)
Expand All @@ -44,25 +42,17 @@ contract DTFactory is Deployer, Converter {
string indexed blob
);

event FPLPCreated(
address indexed FPLPAddress,
address indexed basetoken,
address indexed datatoken,
uint256 ratio
);

/**
* @dev constructor
* Called on contract deployment. Could not be called with zero address parameters.
* @param _template refers to the address of a deployed DataToken contract.
*/
constructor(address _template, address _fplp) public {
constructor(address _template) public {
require(
_template != address(0) && _fplp != address(0),
_template != address(0),
'DTFactory: Invalid token factory initialization'
);
tokenTemplate = _template;
fplpTemplate = _fplp;
}

/**
Expand Down Expand Up @@ -122,51 +112,4 @@ contract DTFactory is Deployer, Converter {
function getTokenTemplate() external view returns (address) {
return tokenTemplate;
}

/**
* @dev Deploys new FPLP proxy contract.
* @param _lpAddress address that is providing liquidity (usually datatoken minter)
* @param _basetoken base token address
* @param _datatoken data token address
* @param _ratio exchange rate (IE: How many basetokens are required to get a DataToken)
* @return address of a new proxy DataToken contract
*/
function createFPLP(
address _lpAddress,
address _basetoken,
address _datatoken,
uint256 _ratio
) public returns (address FPLPAddress) {
require(_lpAddress != address(0), 'Factory: Invalid LP, zero address');
require(
_basetoken != address(0),
'Factory: Invalid basetoken, zero address'
);
require(
_datatoken != address(0),
'Factory: Invalid datatoken, zero address'
);
require(
_basetoken != _datatoken,
'Factory: Invalid datatoken, equals basetoken'
);
require(_ratio > 0, 'Factory: Invalid ratio value');

FPLPAddress = deploy(fplpTemplate);

require(
FPLPAddress != address(0),
'Factory: Failed to perform minimal deploy of a new FPLP'
);

IFPLP fplpInstance = IFPLP(FPLPAddress);
fplpInstance.initialize(_lpAddress, _basetoken, _datatoken, _ratio);

require(
fplpInstance.isInitialized(),
'Factory: Unable to initialize fplp instance'
);

emit FPLPCreated(FPLPAddress, _basetoken, _datatoken, _ratio);
}
}
167 changes: 167 additions & 0 deletions contracts/fixedRate/FixedRateExchange.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
pragma solidity ^0.5.7;
// Copyright BigchainDB GmbH and Ocean Protocol contributors
// SPDX-License-Identifier: (Apache-2.0 AND CC-BY-4.0)
// Code is Apache-2.0 and docs are CC-BY-4.0

import 'openzeppelin-solidity/contracts/ownership/Ownable.sol';
import '../interfaces/IERC20Template.sol';

/**
* @title FixedRateExchange
* @dev FixedRateExchange is a fixed rate exchange Contract
*/
contract FixedRateExchange is Ownable {

struct FixedRatePool {
address poolOwner;
address baseToken;
uint256 fixedRate;
}

// map a dataToken to a fixedRatePool
mapping(address => FixedRatePool) fixedRatePools;

// /**
// * @dev constructor
// * Called on contract deployment. Could not be called with zero address parameters or zero ratio
// * @param lpAddress address that is providing liquidity (usually datatoken minter)
// * @param basetoken base token (IE: Ocean)
// * @param datatoken DataToken address
// * @param ratio exchange rate (IE: How many basetokens are required to get a DataToken)
// */
// constructor(
// address lpAddress,
// address basetoken,
// address datatoken,
// uint256 ratio
// ) public {
// _initialize(lpAddress, basetoken, datatoken, ratio);
// }

// function initialize(
// address lpAddress,
// address basetoken,
// address datatoken,
// uint256 ratio
// ) public onlyNotInitialized returns (bool) {
// return _initialize(lpAddress, basetoken, datatoken, ratio);
// }

// function _initialize(
// address lpAddress,
// address basetoken,
// address datatoken,
// uint256 ratio
// ) private onlyNotInitialized returns (bool) {
// require(
// lpAddress != address(0),
// 'FPLPTemplate: Invalid LP, zero address'
// );
// require(
// basetoken != address(0),
// 'FPLPTemplate: Invalid basetoken, zero address'
// );
// require(
// datatoken != address(0),
// 'FPLPTemplate: Invalid datatoken, zero address'
// );
// require(
// basetoken != datatoken,
// 'PLPTemplate: Invalid datatoken, equals basetoken'
// );
// require(ratio > 0, 'FPLPTemplate: Invalid ratio value');
// _lpAddress = lpAddress;
// _basetoken = basetoken;
// _datatoken = datatoken;
// _ratio = ratio;
// }

// /**
// * @dev isInitialized
// * Function checks if the contract is initialized.
// * @return true if the contract is initialized, false if it is not.
// */

// function isInitialized() public view returns (bool) {
// return initialized;
// }

// /**
// * @dev buyDataTokens
// * Buys Datatokens using base token
// * @param dtAmount amount of DataTokens to be bought
// * @return true
// */
// function buyDataTokens(uint256 dtAmount) public
// onlyInitialized returns (bool) {
// //TO DO - This assumes that ratio is going to be always expressed in wei
// uint256 baseAmount = dtAmount * (_ratio / (10**18));
// //TO DO - should we check the reserve first or just let it fail if there is not enough DT ?
// require(
// IERC20Template(_basetoken).transfer(_lpAddress, baseAmount),
// 'ERROR: transfer failed'
// );
// require(
// IERC20Template(_datatoken).transferFrom(
// _lpAddress,
// msg.sender,
// dtAmount
// ),
// 'ERROR: transferFrom failed'
// );
// return true;
// }

// /**
// * @dev getRatio
// * Gets Ratio
// * @return uint ratio
// */
// function getRatio() public view onlyInitialized returns (uint256) {
// return (_ratio);
// }

// /**
// * @dev SetRatio
// * Sets a new Ratio
// * @return uint ratio
// */
// function setRatio(uint ratio) public
// onlyOwner onlyInitialized returns (bool) {
// require(ratio>0,'Ratio must be >0');
// uint oldratio = _ratio;
// _ratio = ratio;
// emit RatioChanged(oldratio,ratio);
// return true;

// }

// /**
// * @dev getTokens
// * Gets tokens addresses
// * @return address[] tokens
// */
// function getTokens() public view
// onlyInitialized returns (address[] memory) {
// address[] memory tokens = new address[](2);
// tokens[0] = _basetoken;
// tokens[1] = _datatoken;
// return (tokens);
// }

// /**
// * @dev getDTReserve
// * Gets amount of DT available to trade
// * @return uint amount of DT
// */
// function getDTReserve() public view onlyInitialized returns (uint256) {
// //get both balance & allowence and return the smaller one
// uint256 balance = IERC20Template(_datatoken).balanceOf(_lpAddress);
// uint256 allowance = IERC20Template(_datatoken).allowance(
// _lpAddress,
// address(this)
// );
// if (balance < allowance) return (balance);
// else return (allowance);
// }
}

0 comments on commit 5fd2697

Please sign in to comment.