mirror of
https://github.com/logos-storage/logos-storage-contracts-eth.git
synced 2026-01-10 01:03:12 +00:00
vault: move flow accumulation calculation into VaultBase
This commit is contained in:
parent
40e6f1cdcc
commit
1d3e2fd4a3
@ -1,26 +0,0 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity 0.8.28;
|
||||
|
||||
import "./Timestamps.sol";
|
||||
|
||||
type TokensPerSecond is int256;
|
||||
|
||||
using {_negate as -} for TokensPerSecond global;
|
||||
|
||||
function _negate(TokensPerSecond rate) pure returns (TokensPerSecond) {
|
||||
return TokensPerSecond.wrap(-TokensPerSecond.unwrap(rate));
|
||||
}
|
||||
|
||||
library TokenFlows {
|
||||
function accumulated(
|
||||
TokensPerSecond rate,
|
||||
Timestamp start,
|
||||
Timestamp end
|
||||
) internal pure returns (int256) {
|
||||
if (TokensPerSecond.unwrap(rate) == 0) {
|
||||
return 0;
|
||||
}
|
||||
uint64 duration = Timestamp.unwrap(end) - Timestamp.unwrap(start);
|
||||
return TokensPerSecond.unwrap(rate) * int256(uint256(duration));
|
||||
}
|
||||
}
|
||||
12
contracts/TokensPerSecond.sol
Normal file
12
contracts/TokensPerSecond.sol
Normal file
@ -0,0 +1,12 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity 0.8.28;
|
||||
|
||||
import "./Timestamps.sol";
|
||||
|
||||
type TokensPerSecond is int256;
|
||||
|
||||
using {_negate as -} for TokensPerSecond global;
|
||||
|
||||
function _negate(TokensPerSecond rate) pure returns (TokensPerSecond) {
|
||||
return TokensPerSecond.wrap(-TokensPerSecond.unwrap(rate));
|
||||
}
|
||||
@ -4,11 +4,10 @@ pragma solidity 0.8.28;
|
||||
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
|
||||
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
|
||||
import "./Timestamps.sol";
|
||||
import "./TokenFlows.sol";
|
||||
import "./TokensPerSecond.sol";
|
||||
|
||||
using SafeERC20 for IERC20;
|
||||
using Timestamps for Timestamp;
|
||||
using TokenFlows for TokensPerSecond;
|
||||
|
||||
abstract contract VaultBase {
|
||||
IERC20 internal immutable _token;
|
||||
@ -49,15 +48,26 @@ abstract contract VaultBase {
|
||||
) internal view returns (Balance memory) {
|
||||
Balance memory balance = _balances[controller][context][recipient];
|
||||
Flow memory flow = _flows[controller][context][recipient];
|
||||
int256 flowed = flow.rate.accumulated(flow.start, Timestamps.currentTime());
|
||||
if (flowed >= 0) {
|
||||
balance.designated += uint256(flowed);
|
||||
int256 accumulated = _accumulate(flow, Timestamps.currentTime());
|
||||
if (accumulated >= 0) {
|
||||
balance.designated += uint256(accumulated);
|
||||
} else {
|
||||
balance.available -= uint256(-flowed);
|
||||
balance.available -= uint256(-accumulated);
|
||||
}
|
||||
return balance;
|
||||
}
|
||||
|
||||
function _accumulate(
|
||||
Flow memory flow,
|
||||
Timestamp end
|
||||
) private pure returns (int256) {
|
||||
if (TokensPerSecond.unwrap(flow.rate) == 0) {
|
||||
return 0;
|
||||
}
|
||||
uint64 duration = Timestamp.unwrap(end) - Timestamp.unwrap(flow.start);
|
||||
return TokensPerSecond.unwrap(flow.rate) * int256(uint256(duration));
|
||||
}
|
||||
|
||||
function _getLock(
|
||||
Controller controller,
|
||||
Context context
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user