Skip to content
Permalink
Browse files

token distributor

  • Loading branch information...
Amxx committed May 24, 2019
1 parent 570dfbb commit 959b623915afb0a1d7bfd08d319cbb55fd415a47
Showing with 51 additions and 3 deletions.
  1. +2 −2 contracts/PriceOracle.sol
  2. +1 −1 contracts/PriceOracleRequester.sol
  3. +48 −0 contracts/TokenDistributor.sol
@@ -6,15 +6,15 @@ import "iexec-doracle-base/contracts/IexecDoracle.sol";

contract PriceOracle is Ownable, IexecDoracle
{
struct timedValue
struct TimedValue
{
bytes32 oracleCallID;
uint256 date;
uint256 value;
string details;
}

mapping(bytes32 => timedValue) public values;
mapping(bytes32 => TimedValue) public values;

event ValueUpdated(
bytes32 indexed id,
@@ -32,7 +32,7 @@ contract PriceOracleRequester is PriceOracle
string memory _asset2,
uint256 _decimal,
uint256 _timestamp)
public view returns (string memory)
public pure returns (string memory)
{
Buffer.buffer memory buf;
buf.init(256);
@@ -0,0 +1,48 @@
pragma solidity ^0.5.8;
pragma experimental ABIEncoderV2;

import "openzeppelin-solidity/contracts/ownership/Ownable.sol";
import "iexec-solidity/contracts/ERC20_Token/IERC20.sol";
import "iexec-solidity/contracts/Libs/SafeMath.sol";
import "./PriceOracle.sol";

contract TokenDistributor is Ownable
{
using SafeMath for uint256;

IERC20 public token;
PriceOracle public oracle;
string public descr;
uint256 public validity;

constructor(address _token, address _oracle, string memory _descr)
public
{
token = IERC20(_token);
oracle = PriceOracle(_oracle);
descr = _descr;
validity = 1 days;
}

function setValidity(uint256 _validity)
external onlyOwner
{
validity = _validity;
}

function buyTokens()
external payable
{
(,uint256 date,uint256 value,) = oracle.values(keccak256(bytes(descr)));
require(date.add(validity) > now, "value-too-old");

// cast from address to address payable is messed up
address(uint160(owner())).transfer(msg.value);

require(token.transferFrom(
owner(),
msg.sender,
value.mul(msg.value).div(10**18).mulByFraction(990000, 1000000)
));
}
}

0 comments on commit 959b623

Please sign in to comment.
You can’t perform that action at this time.