@@ -13,6 +13,8 @@ import {IERC721} from "../dependencies/openzeppelin/contracts/IERC721.sol";
1313import {IERC20 , SafeERC20} from "../dependencies/openzeppelin/contracts/SafeERC20.sol " ;
1414import {PercentageMath} from "../protocol/libraries/math/PercentageMath.sol " ;
1515import {SignatureChecker} from "../dependencies/looksrare/contracts/libraries/SignatureChecker.sol " ;
16+ import {Errors} from "../protocol/libraries/helpers/Errors.sol " ;
17+ import {IDelegateRegistry} from "../dependencies/delegation/IDelegateRegistry.sol " ;
1618
1719contract P2PPairStaking is
1820 Initializable ,
@@ -47,6 +49,7 @@ contract P2PPairStaking is
4749 address internal immutable apeCoin;
4850 address internal immutable cApe;
4951 ApeCoinStaking internal immutable apeCoinStaking;
52+ address internal immutable delegateRegistry;
5053
5154 bytes32 internal DOMAIN_SEPARATOR;
5255 mapping (bytes32 => ListingOrderStatus) public listingOrderStatus;
@@ -58,6 +61,8 @@ contract P2PPairStaking is
5861 uint256 private baycMatchedCap;
5962 uint256 private maycMatchedCap;
6063 uint256 private bakcMatchedCap;
64+ //asset => tokenId => delegateTo
65+ mapping (address => mapping (uint256 => address )) tokenDelegations;
6166
6267 constructor (
6368 address _bayc ,
@@ -68,7 +73,8 @@ contract P2PPairStaking is
6873 address _nBakc ,
6974 address _apeCoin ,
7075 address _cApe ,
71- address _apeCoinStaking
76+ address _apeCoinStaking ,
77+ address _delegateRegistry
7278 ) {
7379 bayc = _bayc;
7480 mayc = _mayc;
@@ -79,6 +85,7 @@ contract P2PPairStaking is
7985 apeCoin = _apeCoin;
8086 cApe = _cApe;
8187 apeCoinStaking = ApeCoinStaking (_apeCoinStaking);
88+ delegateRegistry = _delegateRegistry;
8289 }
8390
8491 function initialize () public initializer {
@@ -117,6 +124,78 @@ contract P2PPairStaking is
117124 }
118125 }
119126
127+ function clearDelegation (address nft , uint256 tokenId ) external {
128+ require (
129+ msg .sender == nBayc || msg .sender == nMayc || msg .sender == nBakc,
130+ Errors.INVALID_CALLER
131+ );
132+ _clearDelegation (nft, tokenId);
133+ }
134+
135+ function _clearDelegation (address nft , uint256 tokenId ) internal {
136+ bool delegated = (tokenDelegations[nft][tokenId] != address (0 ));
137+ if (delegated) {
138+ _updateTokenDelegation (
139+ nft,
140+ tokenId,
141+ tokenDelegations[nft][tokenId],
142+ false
143+ );
144+ }
145+ }
146+
147+ function delegateForToken (
148+ address nft ,
149+ uint256 [] calldata tokenIds ,
150+ address delegateTo ,
151+ bool value
152+ ) external nonReentrant {
153+ address nTokenAddress;
154+ if (nft == bayc) {
155+ nTokenAddress = nBayc;
156+ } else if (nft == mayc) {
157+ nTokenAddress = nMayc;
158+ } else if (nft == bakc) {
159+ nTokenAddress = nBakc;
160+ } else {
161+ revert ("unsupported " );
162+ }
163+ IERC721 nToken = IERC721 (nTokenAddress);
164+ for (uint256 index = 0 ; index < tokenIds.length ; index++ ) {
165+ require (
166+ msg .sender == nToken.ownerOf (tokenIds[index]),
167+ Errors.INVALID_CALLER
168+ );
169+
170+ bool isDelegated = (tokenDelegations[nft][tokenIds[index]] !=
171+ address (0 ));
172+ require (value != isDelegated, Errors.TOKEN_ALREADY_DELEGATED);
173+
174+ _updateTokenDelegation (nft, tokenIds[index], delegateTo, value);
175+ }
176+ }
177+
178+ function _updateTokenDelegation (
179+ address asset ,
180+ uint256 tokenId ,
181+ address delegateTo ,
182+ bool value
183+ ) internal {
184+ if (value) {
185+ tokenDelegations[asset][tokenId] = delegateTo;
186+ } else {
187+ delete tokenDelegations[asset][tokenId];
188+ }
189+
190+ IDelegateRegistry (delegateRegistry).delegateERC721 (
191+ delegateTo,
192+ asset,
193+ tokenId,
194+ "" ,
195+ value
196+ );
197+ }
198+
120199 function cancelListing (
121200 ListingOrder calldata listingOrder
122201 ) external nonReentrant {
@@ -326,7 +405,7 @@ contract P2PPairStaking is
326405 apeCoinStaking.withdrawBAKC (_otherPairs, _nfts);
327406 }
328407 }
329- //5 transfer token
408+ //5 transfer token and clear delegation
330409 uint256 matchedCount = apeMatchedCount[order.apeToken][
331410 order.apeTokenId
332411 ];
@@ -336,6 +415,7 @@ contract P2PPairStaking is
336415 apeNToken,
337416 order.apeTokenId
338417 );
418+ _clearDelegation (order.apeToken, order.apeTokenId);
339419 }
340420 apeMatchedCount[order.apeToken][order.apeTokenId] = matchedCount - 1 ;
341421
@@ -349,6 +429,7 @@ contract P2PPairStaking is
349429 nBakc,
350430 order.bakcTokenId
351431 );
432+ _clearDelegation (bakc, order.bakcTokenId);
352433 }
353434
354435 //6 reset ape coin listing order status
0 commit comments