-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAbstractPayer.sol
More file actions
60 lines (48 loc) · 1.97 KB
/
Copy pathAbstractPayer.sol
File metadata and controls
60 lines (48 loc) · 1.97 KB
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.0;
import '../interfaces/IPayer.sol';
import '../interfaces/IPayable.sol';
/// @title Abstract base contract for contracts needing to handle payments to the system contract or callback proxies.
abstract contract AbstractPayer is IPayer {
IPayable internal vendor;
/// @notice ACL for addresses allowed to make callbacks and/or request payment.
mapping(address => bool) senders;
constructor() {
}
/// @inheritdoc IPayer
receive() virtual external payable {
}
modifier authorizedSenderOnly() {
require(senders[msg.sender], 'Authorized sender only');
_;
}
/// @inheritdoc IPayer
function pay(uint256 amount) external authorizedSenderOnly {
_pay(payable(msg.sender), amount);
}
/// @notice Automatically cover the outstanding debt to the system contract or callback proxy, provided the contract has sufficient funds.
function coverDebt() external {
uint256 amount = vendor.debt(address(this));
_pay(payable(vendor), amount);
}
/// @notice Attempts to safely transfer the specified sum to the given address.
/// @param recipient Address of the transfer's recipient.
/// @param amount Amount to be transferred.
function _pay(address payable recipient, uint256 amount) internal {
require(address(this).balance >= amount, 'Insufficient funds');
if (amount > 0) {
(bool success,) = payable(recipient).call{value: amount}(new bytes(0));
require(success, 'Transfer failed');
}
}
/// @notice Adds the given address to the ACL.
/// @param sender Sender address to add.
function addAuthorizedSender(address sender) internal {
senders[sender] = true;
}
/// @notice Removes the given address from the ACL.
/// @param sender Sender address to remove.
function removeAuthorizedSender(address sender) internal {
senders[sender] = false;
}
}