Skip to content
This repository has been archived by the owner on Feb 11, 2019. It is now read-only.

[WIP] Refactor to protocol #22

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions SlavasCurve.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
pragma solidity ^0.4.18;

import "zeppelin-solidity/contracts/token/ERC20/StandardToken.sol";
import "zeppelin-solidity/contracts/ownership/Ownable.sol";
import "./BancorFormula.sol";


/**
* @title Bonding Curve
* @dev Bonding curve contract based on Bacor formula
* inspired by bancor protocol and simondlr
* https://github.com/bancorprotocol/contracts
* https://github.com/ConsenSys/curationmarkets/blob/master/CurationMarkets.sol
*/
contract BondingCurve is StandardToken, BancorFormula, Ownable {
/**
* @dev Available balance of reserve token in contract
*/
uint256 public poolBalance;

/*
* @dev reserve ratio, represented in ppm, 1-1000000
* 1/3 corresponds to y= multiple * x^2
* 1/2 corresponds to y= multiple * x
* 2/3 corresponds to y= multiple * x^1/2
* multiple will depends on contract initialization,
* specificallytotalAmount and poolBalance parameters
* we might want to add an 'initialize' function that will allow
* the owner to send ether to the contract and mint a given amount of tokens
*/
uint32 public reserveRatio;

/*
* - Front-running attacks are currently mitigated by the following mechanisms:
* TODO - minimum return argument for each conversion provides a way to define a minimum/maximum price for the transaction
* - gas price limit prevents users from having control over the order of execution
*/
uint256 public gasPrice = 0 wei; // maximum gas price for bancor transactions

/**
* @dev default function
* gas ~ 91645
*/
function() public payable {
buy();
}

/**
* @dev Buy tokens
* gas ~ 77825
* TODO implement maxAmount that helps prevent miner front-running
*/
function buy() validGasPrice public payable returns(bool) {
require(msg.value > 0);
uint256 tokensToMint = calculatePurchaseReturn(totalSupply_, poolBalance, reserveRatio, msg.value);
totalSupply_ = totalSupply_.add(tokensToMint);
balances[msg.sender] = balances[msg.sender].add(tokensToMint);
poolBalance = poolBalance.add(msg.value);
LogMint(tokensToMint, msg.value);
return true;
}

/**
* @dev Sell tokens
* gas ~ 86936
* @param sellAmount Amount of tokens to withdraw
* TODO implement maxAmount that helps prevent miner front-running
*/
function sell(uint256 sellAmount) validGasPrice public returns(bool) {
require(sellAmount > 0 && balances[msg.sender] >= sellAmount);
uint256 ethAmount = calculateSaleReturn(totalSupply_, poolBalance, reserveRatio, sellAmount);
msg.sender.transfer(ethAmount);
poolBalance = poolBalance.sub(ethAmount);
balances[msg.sender] = balances[msg.sender].sub(sellAmount);
totalSupply_ = totalSupply_.sub(sellAmount);
LogWithdraw(sellAmount, ethAmount);
return true;
}

// verifies that the gas price is lower than the universal limit
modifier validGasPrice() {
assert(tx.gasprice <= gasPrice);
_;
}

/**
* @dev Allows the owner to update the gas price limit
* @param _gasPrice The new gas price limit
*/
function setGasPrice(uint256 _gasPrice) onlyOwner public {
require(_gasPrice > 0);
gasPrice = _gasPrice;
}

event LogMint(uint256 amountMinted, uint256 totalCost);
event LogWithdraw(uint256 amountWithdrawn, uint256 reward);
event LogBondingCurve(string logString, uint256 value);
}

Empty file removed arc-ts/index.ts
Empty file.
29 changes: 29 additions & 0 deletions contracts/Bancor/BancorAdaptor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
pragma solidity ^0.4.24;

import "./BancorFormula.sol";

contract BancorAdaptor {
// function price(uint256 forTokens)
// public view returns (uint256 thePrice)
// {
// return
// }

// function reward(uint256 forTokens)
// public view returns (uint256 theReward)
// {

// }

// function exponent()
// public view returns (uint256 exponent)
// {

// }

// function currentPrice()
// public view returns (uint256 theCurrentPrice)
// {
// return reserve / (totalSupply() * reserveRatio);
// }
}
67 changes: 67 additions & 0 deletions contracts/Bancor/UsingBancor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
pragma solidity ^0.4.24;

import "openzeppelin-eth/contracts/math/SafeMath.sol";
import "openzeppelin-eth/contracts/token/ERC20/ERC20.sol";
import "openzeppelin-eth/contracts/token/ERC20/ERC20Detailed.sol";
import "zos-lib/contracts/Initializable.sol";

import "./BancorFormula.sol";

contract UsingBancor is Initializable, ERC20, ERC20Detailed, BancorFormula {
using SafeMath for uint256;

address public reserveAsset;
uint256 public reserve;
uint32 public reserveRatio;

function initialize(string name, string symbol, uint8 decimals, uint32 _reserveRatio)
initializer public
{
ERC20Detailed.initialize(name, symbol, decimals);
reserveRatio = _reserveRatio;
_mint(msg.sender, 10 * 10 ** uint256(decimals));
}

function addReserve(uint256 howMuch)
payable public
{
require(howMuch == msg.value);
reserve = reserve.add(howMuch);
}

function buy(uint256 amountToPay)
public payable returns (uint256 newTokens)
{
if (reserveAsset == address(0x0)) {
require(msg.value >= amountToPay);
newTokens = calculatePurchaseReturn(totalSupply(), reserve, reserveRatio, amountToPay);

reserve = reserve.add(amountToPay);
_mint(msg.sender, newTokens);

if (msg.value > amountToPay) {
msg.sender.transfer(msg.value.sub(amountToPay));
}

emit CurveBuy(newTokens, amountToPay, msg.sender);
}
}

function sell(uint256 tokens)
public returns (uint256 rewarded)
{
require(tokens > 0);
require(balanceOf(msg.sender) >= tokens);

rewarded = calculateSaleReturn(totalSupply(), reserve, reserveRatio, tokens);

reserve = reserve.sub(rewarded);
_burn(msg.sender, tokens);
msg.sender.transfer(rewarded);

emit CurveSell(tokens, rewarded, msg.sender);
}

event CurveBuy(uint256 amount, uint256 paid, uint256 indexed buyer);
event CurveSell(uint256 amount, uint256 rewarded, uint256 indexed seller);
}
9 changes: 0 additions & 9 deletions contracts/BancorAdaptor.sol

This file was deleted.

42 changes: 0 additions & 42 deletions contracts/Curves/Exponential.sol

This file was deleted.

55 changes: 0 additions & 55 deletions contracts/Curves/Linear.sol

This file was deleted.

10 changes: 0 additions & 10 deletions contracts/Curves/Linear_2.sol

This file was deleted.

26 changes: 0 additions & 26 deletions contracts/Curves/Polynomial.sol

This file was deleted.

50 changes: 0 additions & 50 deletions contracts/Curves/SquareRoot.sol

This file was deleted.

Loading