Skip to content

Commit 43cb59c

Browse files
committed
Add ZKsync Permit2 support
1 parent 10e2381 commit 43cb59c

File tree

2 files changed

+16
-4
lines changed

2 files changed

+16
-4
lines changed

src/DN404.sol

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,8 @@ abstract contract DN404 {
129129
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
130130
address internal constant _PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
131131

132-
/// @dev The ZKsync deployment.
132+
/// @dev The ZKsync Permit2 deployment.
133+
/// If deploying on ZKsync or Abstract, override `_isPermit2(address)` to check against this too.
133134
/// [Etherscan](https://era.zksync.network/address/0x0000000000225e31D15943971F47aD3022F714Fa)
134135
address internal constant _ZKSYNC_PERMIT_2 = 0x0000000000225e31D15943971F47aD3022F714Fa;
135136

src/DN420.sol

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,11 @@ abstract contract DN420 {
177177
/// [Etherscan](https://etherscan.io/address/0x000000000022D473030F116dDEE9F6B43aC78BA3)
178178
address internal constant _PERMIT2 = 0x000000000022D473030F116dDEE9F6B43aC78BA3;
179179

180+
/// @dev The ZKsync Permit2 deployment.
181+
/// If deploying on ZKsync or Abstract, override `_isPermit2(address)` to check against this too.
182+
/// [Etherscan](https://era.zksync.network/address/0x0000000000225e31D15943971F47aD3022F714Fa)
183+
address internal constant _ZKSYNC_PERMIT_2 = 0x0000000000225e31D15943971F47aD3022F714Fa;
184+
180185
/*«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-*/
181186
/* STORAGE */
182187
/*-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»*/
@@ -353,7 +358,7 @@ abstract contract DN420 {
353358

354359
/// @dev Returns the amount of ERC20 tokens that `spender` can spend on behalf of `owner`.
355360
function allowance(address owner, address spender) public view returns (uint256) {
356-
if (_givePermit2DefaultInfiniteAllowance() && spender == _PERMIT2) {
361+
if (_givePermit2DefaultInfiniteAllowance() && _isPermit2(spender)) {
357362
uint8 flags = _getDN420Storage().addressData[owner].flags;
358363
if ((flags & _ADDRESS_DATA_OVERRIDE_PERMIT2_FLAG) == uint256(0)) {
359364
return type(uint256).max;
@@ -410,7 +415,7 @@ abstract contract DN420 {
410415
function transferFrom(address from, address to, uint256 amount) public virtual returns (bool) {
411416
Uint256Ref storage a = _ref(_getDN420Storage().allowance, from, msg.sender);
412417

413-
uint256 allowed = _givePermit2DefaultInfiniteAllowance() && msg.sender == _PERMIT2
418+
uint256 allowed = _givePermit2DefaultInfiniteAllowance() && _isPermit2(msg.sender)
414419
&& (_getDN420Storage().addressData[from].flags & _ADDRESS_DATA_OVERRIDE_PERMIT2_FLAG)
415420
== uint256(0) ? type(uint256).max : a.value;
416421

@@ -435,6 +440,12 @@ abstract contract DN420 {
435440
return false;
436441
}
437442

443+
/// @dev Returns checks if `sender` is the canonical Permit2 address.
444+
/// If on ZKsync, override this function to check against `_ZKSYNC_PERMIT_2` as well.
445+
function _isPermit2(address sender) internal view virtual returns (bool) {
446+
return sender == _PERMIT2;
447+
}
448+
438449
/*«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-«-*/
439450
/* INTERNAL MINT FUNCTIONS */
440451
/*-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»-»*/
@@ -931,7 +942,7 @@ abstract contract DN420 {
931942
///
932943
/// Emits a {Approval} event.
933944
function _approve(address owner, address spender, uint256 amount) internal virtual {
934-
if (_givePermit2DefaultInfiniteAllowance() && spender == _PERMIT2) {
945+
if (_givePermit2DefaultInfiniteAllowance() && _isPermit2(spender)) {
935946
_getDN420Storage().addressData[owner].flags |= _ADDRESS_DATA_OVERRIDE_PERMIT2_FLAG;
936947
}
937948
_ref(_getDN420Storage().allowance, owner, spender).value = amount;

0 commit comments

Comments
 (0)