-
Notifications
You must be signed in to change notification settings - Fork 0
/
BatchInbox.sol
68 lines (55 loc) · 1.87 KB
/
BatchInbox.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
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
61
62
63
64
65
66
67
68
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.24;
contract BatchInbox {
StorageContract public immutable esStorageContract;
mapping(address => uint256) public balances;
error BalanceNotEnough();
constructor(address _esStorageContract) {
esStorageContract = StorageContract(_esStorageContract);
}
function store() internal {
uint256 i = 0;
uint256 payment = 0;
bytes32 h;
do {
h = blobhash(i);
if (h == bytes32(0)) break;
if (payment == 0) payment = esStorageContract.upfrontPayment();
esStorageContract.putBlob{value: payment}(h, i, 4096 * 32);
unchecked {
i++;
}
} while (true);
// deduct fee from balance
if (i == 0) return;
uint256 toDeduct = payment * i;
uint256 balance = balances[msg.sender];
if (balance < toDeduct) revert BalanceNotEnough();
balances[msg.sender] = balance - toDeduct;
}
fallback() external payable {
_deposit(msg.sender, msg.value);
store();
}
receive() external payable {
_deposit(msg.sender, msg.value);
store();
}
function deposit(address _to) external payable {
_deposit(_to, msg.value);
}
function _deposit(address _to_to, uint256 _amount) internal {
if (_amount == 0) return;
balances[_to_to] += _amount;
}
function withdraw(address _to, uint256 _amount) external {
uint256 balance = balances[msg.sender];
if (balance < _amount) revert BalanceNotEnough();
balances[msg.sender] = balance - _amount;
payable(_to).transfer(_amount);
}
}
interface StorageContract {
function putBlob(bytes32 key, uint256 blobIdx, uint256 length) external payable;
function upfrontPayment() external view returns (uint256);
}