Skip to content

Instantly share code, notes, and snippets.

@LuisAli22
Last active June 4, 2019 01:53
Show Gist options
  • Save LuisAli22/89671572845ea6c0060d4162a61e7ba0 to your computer and use it in GitHub Desktop.
Save LuisAli22/89671572845ea6c0060d4162a61e7ba0 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.4.26+commit.4563c3fc.js&optimize=false&gist=
pragma solidity^0.4.4;
import "./IterableMapping.sol";
contract FutureMoney {
IterableMapping.itmap tHistory;
IterableMapping.itmap futureSells;
uint256 slope;
uint256 displacement;
bool regressionParametersSeted;
uint transactionLimit;
address ownerAddress;
constructor() public {
transactionLimit = 4;
slope = 0;
displacement = 0;
regressionParametersSeted = false;
ownerAddress = msg.sender;
}
event Deposit(
address indexed _from,
uint256 price,
uint256 timestamp,
uint amount
);
event PendingFutureBuys(
uint256 price,
uint256 amount,
bool readyToBuy
);
function regression()public {
require(tHistory.size == transactionLimit);
uint256 time = 0;
uint256 price= 0;
uint256 timePrice = 0;
uint256 timeTime= 0;
for (uint i = IterableMapping.iterate_start(tHistory); IterableMapping.iterate_valid(tHistory, i); i = IterableMapping.iterate_next(tHistory, i)){
address currentContractAddress;
uint256 currentPrice;
uint256 currentTime;
uint key;
uint amount;
(key, currentContractAddress, currentPrice, currentTime, amount) = IterableMapping.iterate_get(tHistory, i);
price +=currentPrice;
timePrice += currentPrice * price;
timeTime += currentTime*currentTime;
}
slope= ((transactionLimit* timePrice) - (time * price)) / ((transactionLimit* timeTime)-(time*time));
displacement= (price - slope*time)/ transactionLimit;
regressionParametersSeted = true;
}
function calculateFutureValue(uint256 date) public view returns (uint256 price){
require(regressionParametersSeted == true);
return ((slope*date) + displacement);
}
function buyFutureMoney(uint256 date, uint amount) public payable{
require(date - now <= 90);
uint256 price = calculateFutureValue(date);
emit Deposit(address(this), price, date, amount);
IterableMapping.insert(futureSells, futureSells.size, address(this), price, date, amount);
}
function queryMyFutureBuys() public{
for (uint i = IterableMapping.iterate_start(futureSells); IterableMapping.iterate_valid(futureSells, i); i = IterableMapping.iterate_next(futureSells, i)){
address currentContractAddress;
uint256 currentPrice;
uint256 currentTime;
uint key;
uint amount;
(key, currentContractAddress, currentPrice, currentTime, amount) = IterableMapping.iterate_get(futureSells, i);
if (msg.sender == currentContractAddress){
bool readyToBuy = (currentTime <= now);
emit PendingFutureBuys(currentPrice, amount, readyToBuy);
}
}
}
function queryAllFutureBuys() public{
require(msg.sender == ownerAddress);
for (uint i = IterableMapping.iterate_start(futureSells); IterableMapping.iterate_valid(futureSells, i); i = IterableMapping.iterate_next(futureSells, i)){
address currentContractAddress;
uint256 currentPrice;
uint256 currentTime;
uint key;
uint amount;
(key, currentContractAddress, currentPrice, currentTime, amount) = IterableMapping.iterate_get(futureSells, i);
bool readyToBuy = (currentTime <= now);
emit PendingFutureBuys(currentPrice, amount, readyToBuy);
}
}
}
pragma solidity^0.4.4;
library IterableMapping{
struct itmap{
mapping(uint => IndexValue) data;
KeyFlag[] keys;
uint size;
}
struct IndexValue {
uint keyIndex;
address contractAddress;
uint256 price;
uint256 timestamp;
uint amount;
}
struct KeyFlag{
uint key;
bool deleted;
}
function insert(itmap storage self, uint key, address contractAddress, uint256 price, uint256 timestamp, uint amount) public returns (bool replaced){
uint keyIndex = self.data[key].keyIndex;
self.data[key].contractAddress = contractAddress;
self.data[key].price = price;
self.data[key].timestamp = timestamp;
self.data[key].amount = amount;
if (keyIndex > 0)
return true;
keyIndex = self.keys.length++;
self.data[key].keyIndex = keyIndex + 1;
self.keys[keyIndex].key = key;
self.size++;
return false;
}
function remove(itmap storage self, uint key) public returns (bool success){
uint keyIndex = self.data[key].keyIndex;
if (keyIndex == 0)
return false;
delete self.data[key];
self.keys[keyIndex - 1].deleted = true;
self.size --;
return true;
}
function contains(itmap storage self, uint key) public view returns (bool){
return self.data[key].keyIndex > 0;
}
function iterate_start(itmap storage self) public view returns (uint keyIndex){
return iterate_next(self, uint(-1));
}
function iterate_valid(itmap storage self, uint keyIndex) public view returns (bool){
return keyIndex < self.keys.length;
}
function iterate_next(itmap storage self, uint keyIndex) public view returns (uint r_keyIndex){
keyIndex++;
while (keyIndex < self.keys.length && self.keys[keyIndex].deleted)
keyIndex++;
return keyIndex;
}
function iterate_get(itmap storage self, uint keyIndex) public view returns (uint key, address contractAddress, uint256 price, uint256 timestamp, uint amount){
key = self.keys[keyIndex].key;
contractAddress = self.data[key].contractAddress;
price=self.data[key].price;
timestamp = self.data[key].timestamp;
amount = self.data[key].amount;
}
}
pragma solidity^0.4.4;
import "./FutureMoney.sol";
interface ERC20{
function totalSupply() external;
function balanceOf(address tokenOwner) external;
function allowance(address tokenOwner, address spender) external;
function transfer(address to, uint256 tokens) external;
function approve(address spender, uint256 tokens) external;
function transferFrom(address _from, address _to, uint256 tokens) external;
event Transfer(address indexed _from, address indexed _to, uint tokens);
event Approval(address indexed tokenOwner, address indexed spender, uint tokens);
}
contract PAT is ERC20, FutureMoney{
string public name;
uint8 public decimals;
string public symbol;
mapping (address => uint256) balances;
mapping (address => mapping (address => uint256)) allowed;
uint256 _totalSupply;
uint256 public tokenPrice;
uint256 public tokensSold;
uint256 public tokenUpdate;
event Sell(address _buyer, uint256 _amount);
constructor() public {
balances[msg.sender] = 1000; // el creador obtiene mil token iniciales
_totalSupply = 1000; // cantidad total de token
name = "Patacontoken"; // nombre del token
decimals = 18; // cantidad de decimles
symbol = "PAT"; // simbolo del token
tokenPrice = 2000; //Precio inicial del token- 1 eth = 2000 tokens
tokensSold = 0; // cantidad de tokens vendidos inicialmente (en cero)
tokenUpdate = 1000;
}
function totalSupply() public view returns (uint256) {
return _totalSupply - (balances[address(0)]);
}
function transfer(address _to, uint256 tokens) public returns (bool success) {
require(tokens > 0);
require(balances[msg.sender] >= tokens);
balances[msg.sender] -= tokens;
balances[_to] += tokens;
emit Transfer(msg.sender, _to, tokens);
return true;
}
function transferFrom(address _from, address _to, uint256 tokens) public returns (bool success) {
require(tokens >0);
require(balances[_from] >= tokens);
require(allowed[_from][msg.sender] >= tokens);
balances[_to] += tokens;
balances[_from] -= tokens;
allowed[_from][msg.sender] -= tokens;
emit Transfer(_from, _to, tokens);
return true;
}
function balanceOf(address tokenOwner) public view returns (uint256 balance) {
return balances[tokenOwner];
}
function approve(address spender, uint256 tokens) public returns (bool success) {
allowed[msg.sender][spender] = tokens;
emit Approval(msg.sender, spender, tokens);
return true;
}
function allowance(address tokenOwner, address spender) public view returns (uint256 remaining) {
return allowed[tokenOwner][spender];
}
/* Approves and then calls the receiving contract */
function approveAndCall(address _spender, uint256 _value, bytes _extraData) public returns (bool success) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
require (!_spender.call(bytes4(bytes32(keccak256("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData));
return true;
}
function insertTransactionToHistory(address contractAddress, uint256 price, uint256 timestamp, uint amount) public {
if (tHistory.size >=4){
IterableMapping.remove(tHistory, 0);
}
IterableMapping.insert(tHistory, tHistory.size, contractAddress, price, timestamp, amount);
}
function buyTokens(address _receiver) public payable {
require(msg.value > 0);
require(_receiver != address(0));
uint amount = msg.value;
uint256 tokensToBuy = amount * tokenPrice;
require(PAT.transfer(_receiver, tokensToBuy));
tokensSold += amount;
insertTransactionToHistory(this, tokenPrice, now, amount);
tokenPrice += 1 / (tokenUpdate *10**uint(decimals));
emit Sell(msg.sender, tokensToBuy);
}
function sellTokens(uint amount) public {
require(address(this).balance >= amount * tokenPrice); // verifica si el contrato tiene suficiente ether para comprar
emit Transfer(msg.sender, this, amount); // hacer la transferencia desde el vendedor hacia el contrato
msg.sender.transfer(amount * tokenPrice); // enviar ether al vendedor.
insertTransactionToHistory(address(this), tokenPrice, now, amount);
tokenPrice -= 1 / (tokenUpdate *10**uint(decimals));
}
function executeMyContracts() public payable {
for (uint i = IterableMapping.iterate_start(futureSells); IterableMapping.iterate_valid(futureSells, i); i = IterableMapping.iterate_next(futureSells, i)){
address currentContractAddress;
uint256 currentPrice;
uint256 currentTime;
uint key;
uint amount;
(key, currentContractAddress, currentPrice, currentTime, amount) = IterableMapping.iterate_get(futureSells, i);
if ((msg.sender == currentContractAddress) && (currentTime <= now)){
buyTokens(address(this));
}
}
}
function executeAllContracts() public payable{
require(msg.sender == ownerAddress);
for (uint i = IterableMapping.iterate_start(futureSells); IterableMapping.iterate_valid(futureSells, i); i = IterableMapping.iterate_next(futureSells, i)){
address currentContractAddress;
uint256 currentPrice;
uint256 currentTime;
uint key;
uint amount;
(key, currentContractAddress, currentPrice, currentTime, amount) = IterableMapping.iterate_get(futureSells, i);
if (currentTime <= now){
buyTokens(address(this));
}
}
}
}
pragma solidity >=0.4.0 <0.6.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
import "./IterableMapping.sol";
// file name has to end with '_test.sol'
contract TestIterableMapping {
IterableMapping.itmap transactionHistory;
function beforeAll() public {
IterableMapping.insert(transactionHistory, transactionHistory.size, address(0), 10, now, 2000);
}
function check_history_length() public {
// use 'Assert' to test the contract
Assert.equal(transactionHistory.size, 1, "Transaction history size should be 1");
}
function check_history_remove_item() public{
IterableMapping.remove(transactionHistory, 0);
Assert.equal(transactionHistory.size, 0, "Transaction history size should be 0");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment