Created
September 18, 2019 13:24
-
-
Save k06a/0a4bea077525e57a13768e171704e143 to your computer and use it in GitHub Desktop.
MovingValue
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.0; | |
import "github.com/openzeppelin/openzeppelin-contracts/contracts/math/SafeMath.sol"; | |
library MovingValue { | |
using SafeMath for uint256; | |
struct Data { | |
uint40 time; | |
uint216 speed; | |
uint256 origin; | |
} | |
function update(Data storage self, uint256 newSpeed) internal { | |
uint256 newOrigin = value(self); | |
self.time = uint40(now); | |
self.speed = uint216(newSpeed); | |
self.origin = newOrigin; | |
} | |
function value(Data storage self) internal view returns(uint256) { | |
return valueMemory(self); | |
} | |
function valueMemory(Data memory self) internal view returns(uint256) { | |
return uint256(self.origin).add( | |
now.sub(self.time).mul(self.speed).div(365 days) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment