-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathOldAccountHook.sol
162 lines (144 loc) · 4.88 KB
/
OldAccountHook.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.19;
import {IAxiomV1Query} from "@axiom-contracts/contracts/interfaces/IAxiomV1Query.sol";
import {Ownable} from "@openzeppelin/contracts/access/Ownable.sol";
import {IPoolManager} from "@uniswap/v4-core/contracts/interfaces/IPoolManager.sol";
import {Hooks} from "@uniswap/v4-core/contracts/libraries/Hooks.sol";
import {BaseHook} from "./BaseHook.sol";
///
///
/// Only old accounts can use the pool.
/// Old is subjective :p It's the hook owner's job to define "old".
///
///
contract OldAccount is BaseHook, Ownable {
address public axiomQueryAddress;
uint256 public ageThreshold;
event UpdateAxiomQueryAddress(address newAddress);
event UpdateAgeThreshold(uint256 blockNumber);
event AccountAgeVerified(address account, uint32 birthBlock);
mapping(address => uint32) public birthBlocks; // Keeps track of first-tx block numbers of each account
constructor(
IPoolManager _poolManager,
address _axiomQueryAddress,
uint256 _ageThreshold
) BaseHook(_poolManager) {
require(_ageThreshold >= 7200, "cannot be THAT young"); // 7200 slots = 1 day
axiomQueryAddress = _axiomQueryAddress;
ageThreshold = _ageThreshold;
emit UpdateAgeThreshold(ageThreshold);
emit UpdateAxiomQueryAddress(_axiomQueryAddress);
}
modifier onlyPermitOldAccounts() {
require(
birthBlocks[tx.origin] != 0,
"you are not even born, bruh"
);
require(
block.number - uint256(birthBlocks[tx.origin]) >= ageThreshold,
"you not old enough, yo"
);
_;
}
function updateAgeThreshold(uint256 _ageThreshold) external onlyOwner {
require(_ageThreshold >= 7200, "cannot be THAT young"); // 7200 slots = 1 day
ageThreshold = _ageThreshold;
emit UpdateAgeThreshold(ageThreshold);
}
///
/// ====================
/// Uniswap
/// ====================
///
function getHooksCalls() public pure override returns (Hooks.Calls memory) {
return
Hooks.Calls({
beforeInitialize: false,
afterInitialize: false,
beforeModifyPosition: true,
afterModifyPosition: false,
beforeSwap: true,
afterSwap: false,
beforeDonate: false,
afterDonate: false
});
}
function beforeModifyPosition(
address,
IPoolManager.PoolKey calldata,
IPoolManager.ModifyPositionParams calldata
)
external
view
override
poolManagerOnly
onlyPermitOldAccounts
returns (bytes4)
{
return BaseHook.beforeModifyPosition.selector;
}
function beforeSwap(
address,
IPoolManager.PoolKey calldata,
IPoolManager.SwapParams calldata
)
external
view
override
poolManagerOnly
onlyPermitOldAccounts
returns (bytes4)
{
return BaseHook.beforeSwap.selector;
}
///
/// ====================
/// Axiom
/// ====================
///
function updateAxiomQueryAddress(
address _axiomQueryAddress
) external onlyOwner {
axiomQueryAddress = _axiomQueryAddress;
emit UpdateAxiomQueryAddress(_axiomQueryAddress);
}
///
/// @notice Verifies age of an account using a ZK proof, where age is the block number of the
/// first transaction of the account.
///
function verifyAge(
IAxiomV1Query.AccountResponse[] calldata accountProofs,
bytes32[3] calldata keccakResponses
) external {
require(accountProofs.length == 2, "Too many account proofs");
address account = accountProofs[0].addr;
require(account == accountProofs[1].addr, "Accounts are not the same");
require(
accountProofs[0].blockNumber + 1 == accountProofs[1].blockNumber,
"Block numbers are not consecutive"
);
require(accountProofs[0].nonce == 0, "Prev block nonce is not 0");
require(
accountProofs[1].nonce > 0,
"No account transactions in curr block"
);
uint256 addrSize;
assembly {
addrSize := extcodesize(account)
}
require(addrSize == 0, "Account is a contract");
require(
IAxiomV1Query(axiomQueryAddress).areResponsesValid(
keccakResponses[0],
keccakResponses[1],
keccakResponses[2],
new IAxiomV1Query.BlockResponse[](0),
accountProofs,
new IAxiomV1Query.StorageResponse[](0)
),
"Proof not valid"
);
birthBlocks[account] = accountProofs[0].blockNumber;
emit AccountAgeVerified(account, accountProofs[0].blockNumber);
}
}