Created
May 29, 2020 21:10
-
-
Save brianmcmichael/3e6dd40a9fcd959c6f7e79404b179052 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity 0.5.12; | |
contract Price { | |
uint256 _startPrice; | |
uint256 _endPrice; | |
uint256 _start; | |
uint256 _duration; | |
function start(uint256 startPrice, uint256 endPrice, uint256 duration) public { | |
require (startPrice >= endPrice); | |
_startPrice = startPrice; | |
_endPrice = endPrice; | |
_duration = duration; | |
_start = now; | |
} | |
uint256 constant ONE = 10 ** 27; | |
function rpow(uint x, uint n, uint b) internal pure returns (uint z) { | |
assembly { | |
switch x case 0 {switch n case 0 {z := b} default {z := 0}} | |
default { | |
switch mod(n, 2) case 0 { z := b } default { z := x } | |
let half := div(b, 2) // for rounding. | |
for { n := div(n, 2) } n { n := div(n,2) } { | |
let xx := mul(x, x) | |
if iszero(eq(div(xx, x), x)) { revert(0,0) } | |
let xxRound := add(xx, half) | |
if lt(xxRound, xx) { revert(0,0) } | |
x := div(xxRound, b) | |
if mod(n,2) { | |
let zx := mul(z, x) | |
if and(iszero(iszero(x)), iszero(eq(div(zx, x), z))) { revert(0,0) } | |
let zxRound := add(zx, half) | |
if lt(zxRound, zx) { revert(0,0) } | |
z := div(zxRound, b) | |
} | |
} | |
} | |
} | |
} | |
function rmul(uint x, uint y) internal pure returns (uint z) { | |
z = x * y; | |
require(y == 0 || z / y == x); | |
z = z / ONE; | |
} | |
function sub(uint x, uint y) internal pure returns (uint z) { | |
z = x - uint(y); | |
require(y <= 0 || z <= x); | |
require(y >= 0 || z >= x); | |
} | |
function linearPrice() public view returns (uint256) { | |
// transaction cost 27141 gas (Cost only applies when called by a contract) | |
require(now < (_start + _duration)); | |
return (_startPrice + (_startPrice - _endPrice) * (now - _start) / _duration); | |
} | |
function expPrice() public view returns (uint256) { | |
// transaction cost 26857 gas (Cost only applies when called by a contract) | |
require(now < (_start + _duration)); | |
return rmul(_startPrice, rpow(_start, sub(now, _start) / _duration, ONE)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment