-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathSelfFunding.sol
30 lines (25 loc) · 1.13 KB
/
SelfFunding.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// SPDX-License-Identifier: Apache-2.0
pragma solidity >=0.5.0 <0.9.0;
import "./IExchangeRate.sol";
abstract contract SelfFunding {
uint256 constant TINY_PARTS_PER_WHOLE = 100_000_000;
address constant PRECOMPILE_ADDRESS = address(0x168);
function tinycentsToTinybars(uint256 tinycents) internal returns (uint256 tinybars) {
(bool success, bytes memory result) = PRECOMPILE_ADDRESS.call(
abi.encodeWithSelector(IExchangeRate.tinycentsToTinybars.selector, tinycents));
require(success);
tinybars = abi.decode(result, (uint256));
}
function tinybarsToTinycents(uint256 tinybars) internal returns (uint256 tinycents) {
(bool success, bytes memory result) = PRECOMPILE_ADDRESS.call(
abi.encodeWithSelector(IExchangeRate.tinybarsToTinycents.selector, tinybars));
require(success);
tinycents = abi.decode(result, (uint256));
}
modifier costsCents(uint256 cents) {
uint256 tinycents = cents * TINY_PARTS_PER_WHOLE;
uint256 requiredTinybars = tinycentsToTinybars(tinycents);
require(msg.value >= requiredTinybars);
_;
}
}