Skip to content

Commit 4ef4f60

Browse files
committed
wip draft
1 parent 43755e1 commit 4ef4f60

File tree

6 files changed

+700
-2
lines changed

6 files changed

+700
-2
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ docs/
1212

1313
# Dotenv file
1414
.env
15+
16+
.DS_STORE

src/core/TheCompactCore.sol

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
// SPDX-License-Identifier: MIT
2+
pragma solidity ^0.8.27;
3+
4+
import { ERC6909 } from "solady/tokens/ERC6909.sol";
5+
import { Scope } from "../types/Scope.sol";
6+
import { ResetPeriod } from "../types/ResetPeriod.sol";
7+
import { Deposit } from "./lib/Deposit.sol";
8+
import { ITheCompactCore } from "../interfaces/ITheCompactCore.sol";
9+
10+
contract TheCompactCore is ERC6909, Deposit {
11+
12+
error InvalidToken();
13+
14+
function deposit(address allocator, Scope scope, ResetPeriod resetPeriod, address recipient) external payable returns (uint256 id) {
15+
return _deposit(address(0), msg.value, allocator, scope, resetPeriod, recipient);
16+
}
17+
18+
function deposit(address token, uint256 amount, address allocator, ResetPeriod resetPeriod, Scope scope, address recipient) external returns (uint256) {
19+
// Collects the tokens from the sender, reverts if the token is zero address. Returns the actual received amount
20+
amount = _collect(token, amount, msg.sender);
21+
return _deposit(token, amount, allocator, scope, resetPeriod, recipient);
22+
}
23+
24+
function register(ITheCompactCore.Compact calldata compact) external {
25+
bytes32 digest = _compactDigest(compact);
26+
_register(msg.sender, compact.sponsor, digest, compact.expires);
27+
}
28+
29+
function registerWithWitness(ITheCompactCore.Compact calldata compact, bytes32 witness, string calldata typeString) external {
30+
bytes32 digest = _compactDigestWitness(compact, witness, typeString);
31+
_register(msg.sender, compact.sponsor, digest, compact.expires);
32+
}
33+
34+
function transfer(address to, uint256 id, uint256 amount) public payable override returns (bool) {
35+
_ensureAttested(msg.sender, to, id, amount);
36+
return super.transfer(to, id, amount);
37+
}
38+
39+
function transferFrom(address from, address to, uint256 id, uint256 amount) public payable override returns (bool) {
40+
_ensureAttested(from, to, id, amount);
41+
return super.transferFrom(from, to, id, amount);
42+
}
43+
44+
function allocatedTransfer(ITheCompactCore.Transfer calldata transfer_, bytes calldata allocatorSignature) external returns (bool) {
45+
uint256 length = _ensureBatchAttested(msg.sender, transfer_, allocatorSignature);
46+
for(uint256 i = 0; i < length; ++i) {
47+
_transfer(address(0), msg.sender, _castToAddress(transfer_.recipients[i].recipient), transfer_.recipients[i].id, transfer_.recipients[i].amount);
48+
}
49+
return true;
50+
}
51+
52+
function allocatedTransferFrom(ITheCompactCore.DelegatedTransfer calldata delegatedTransfer, bytes calldata allocatorSignature) external returns (bool) {
53+
uint256 length = _ensureBatchAttested(msg.sender, delegatedTransfer.transfer, allocatorSignature);
54+
for(uint256 i = 0; i < length; ++i) {
55+
_transfer(msg.sender, delegatedTransfer.from, _castToAddress(delegatedTransfer.transfer.recipients[i].recipient), delegatedTransfer.transfer.recipients[i].id, delegatedTransfer.transfer.recipients[i].amount);
56+
}
57+
return true;
58+
}
59+
60+
// @notice Flexible withdrawal of tokens
61+
// @dev Works for server based allocators and on chain allocators
62+
// @dev On chain allocators can supply an empty bytes for the allocatorSignature
63+
function withdrawal(ITheCompactCore.Transfer calldata withdrawal_, bytes calldata allocatorSignature) external returns (bool) {
64+
uint256 length = _ensureBatchAttested(msg.sender, withdrawal_, allocatorSignature);
65+
for(uint256 i = 0; i < length; ++i) {
66+
_burn(msg.sender, withdrawal_.recipients[i].id, withdrawal_.recipients[i].amount); // reverts if insufficient balance
67+
_distribute(withdrawal_.recipients[i].id, withdrawal_.recipients[i].amount, _castToAddress(withdrawal_.recipients[i].recipient));
68+
}
69+
return true;
70+
}
71+
72+
// @notice Flexible withdrawal of tokens delegated by a sponsor
73+
// @dev Works for server based allocators and on chain allocators
74+
// @dev Requires an approval from the sender
75+
function withdrawalFrom(ITheCompactCore.DelegatedTransfer calldata delegatedWithdrawal, bytes calldata sponsorSignature) external returns (bool) {
76+
uint256 length = _ensureBatchAttested(msg.sender, delegatedWithdrawal.transfer, sponsorSignature);
77+
for(uint256 i = 0; i < length; ++i) {
78+
_checkApproval(msg.sender, delegatedWithdrawal.from, delegatedWithdrawal.transfer.recipients[i].id, delegatedWithdrawal.transfer.recipients[i].amount);
79+
_burn(delegatedWithdrawal.from, delegatedWithdrawal.transfer.recipients[i].id, delegatedWithdrawal.transfer.recipients[i].amount);
80+
_distribute(delegatedWithdrawal.transfer.recipients[i].id, delegatedWithdrawal.transfer.recipients[i].amount, _castToAddress(delegatedWithdrawal.transfer.recipients[i].recipient));
81+
}
82+
return true;
83+
}
84+
85+
function claim(ITheCompactCore.Claim calldata claim_, bool withdraw) external returns (bool) {
86+
(address allocator, ITheCompactCore.Compact memory compact) = _verifyClaim(claim_);
87+
_verifySignatures(_compactDigest(compact), claim_.compact.sponsor, claim_.sponsorSignature, allocator, claim_.allocatorSignature);
88+
uint256 length = compact.inputs.length;
89+
for(uint256 i = 0; i < length; ++i) {
90+
if(withdraw) {
91+
_burn(claim_.compact.sponsor, claim_.compact.inputs[i].id, claim_.compact.inputs[i].amount);
92+
_distribute(claim_.compact.inputs[i].id, claim_.compact.inputs[i].amount, _castToAddress(claim_.compact.inputs[i].recipient));
93+
} else {
94+
_rebalance(claim_.compact.sponsor, _castToAddress(claim_.compact.inputs[i].recipient), claim_.compact.inputs[i].id, claim_.compact.inputs[i].amount, false);
95+
// TODO: add event
96+
}
97+
}
98+
return true;
99+
}
100+
101+
102+
/// @dev Returns the symbol for token `id`.
103+
function name(uint256) public view virtual override returns (string memory) {
104+
return "";
105+
}
106+
107+
/// @dev Returns the symbol for token `id`.
108+
function symbol(uint256) public view virtual override returns (string memory) {
109+
return "";
110+
}
111+
112+
/// @dev Returns the Uniform Resource Identifier (URI) for token `id`.
113+
function tokenURI(uint256) public view virtual override returns (string memory) {
114+
return "";
115+
}
116+
}

0 commit comments

Comments
 (0)