Skip to content

Instantly share code, notes, and snippets.

@k06a
Created September 18, 2019 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save k06a/0a4bea077525e57a13768e171704e143 to your computer and use it in GitHub Desktop.
Save k06a/0a4bea077525e57a13768e171704e143 to your computer and use it in GitHub Desktop.
MovingValue
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