Skip to content

Commit c25a576

Browse files
committed
Add replacements for TokenApprovalRef
1 parent 99a6728 commit c25a576

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

contracts/ERC721AStorage.sol

+6-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22

33
pragma solidity ^0.8.0;
44

5-
import {ERC721AUpgradeable} from './ERC721AUpgradeable.sol';
6-
75
library ERC721AStorage {
6+
// Reference type for token approval.
7+
struct TokenApprovalRef {
8+
address value;
9+
}
10+
811
struct Layout {
912
// =============================================================
1013
// STORAGE
@@ -38,7 +41,7 @@ library ERC721AStorage {
3841
// - [192..255] `aux`
3942
mapping(address => uint256) _packedAddressData;
4043
// Mapping from token ID to approved address.
41-
mapping(uint256 => ERC721AUpgradeable.TokenApprovalRef) _tokenApprovals;
44+
mapping(uint256 => ERC721AStorage.TokenApprovalRef) _tokenApprovals;
4245
// Mapping from owner to operator approvals
4346
mapping(address => mapping(address => bool)) _operatorApprovals;
4447
}

contracts/ERC721AUpgradeable.sol

+1-5
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,6 @@ interface ERC721A__IERC721ReceiverUpgradeable {
3737
*/
3838
contract ERC721AUpgradeable is ERC721A__Initializable, IERC721AUpgradeable {
3939
using ERC721AStorage for ERC721AStorage.Layout;
40-
// Reference type for token approval.
41-
struct TokenApprovalRef {
42-
address value;
43-
}
4440

4541
// =============================================================
4642
// CONSTANTS
@@ -480,7 +476,7 @@ contract ERC721AUpgradeable is ERC721A__Initializable, IERC721AUpgradeable {
480476
view
481477
returns (uint256 approvedAddressSlot, address approvedAddress)
482478
{
483-
TokenApprovalRef storage tokenApproval = ERC721AStorage.layout()._tokenApprovals[tokenId];
479+
ERC721AStorage.TokenApprovalRef storage tokenApproval = ERC721AStorage.layout()._tokenApprovals[tokenId];
484480
// The following is equivalent to `approvedAddress = _tokenApprovals[tokenId]`.
485481
assembly {
486482
approvedAddressSlot := tokenApproval.slot

scripts/replace-imports.js

+26-5
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
const fs = require('fs');
33
const glob = require('glob');
44

5-
// rename files
5+
// Rename files.
66
fs.renameSync('contracts/Initializable.sol', 'contracts/ERC721A__Initializable.sol');
77
fs.renameSync('contracts/InitializableStorage.sol', 'contracts/ERC721A__InitializableStorage.sol');
88

9-
// loop through all files with contracts/**/*.sol pattern
9+
// Loop through all files with contracts/**/*.sol pattern.
1010
glob('contracts/**/*.sol', null, function (err, files) {
1111
files.forEach((file) => {
12-
// read file content
12+
// Read file content.
1313
const content = fs.readFileSync(file, 'utf8');
1414

1515
const updatedContent = content
@@ -20,7 +20,28 @@ glob('contracts/**/*.sol', null, function (err, files) {
2020
.replace(/onlyInitializing\s*?\{/g, 'onlyInitializingERC721A {')
2121
.replace(/Initializable/g, 'ERC721A__Initializable');
2222

23-
// write updated file
23+
// Write updated file.
2424
fs.writeFileSync(file, updatedContent);
2525
});
26-
});
26+
});
27+
28+
// Replace the TokenApprovalRef to break cyclic importing.
29+
let erc721aFilepath = 'contracts/ERC721AUpgradeable.sol';
30+
let erc721aContents = fs.readFileSync(erc721aFilepath, 'utf8');
31+
let tokenApprovalRefRe = /\/\/.*?\n\r?\s*struct TokenApprovalRef\s*\{[^\}]+\}/;
32+
let tokenApprovalRefMatch = erc721aContents.match(tokenApprovalRefRe);
33+
if (tokenApprovalRefMatch) {
34+
erc721aContents = erc721aContents
35+
.replace(tokenApprovalRefMatch[0], '')
36+
.replace(/TokenApprovalRef/g, 'ERC721AStorage.TokenApprovalRef');
37+
fs.writeFileSync(erc721aFilepath, erc721aContents);
38+
39+
let erc721aStorageFilepath = 'contracts/ERC721AStorage.sol';
40+
let erc721aStorageContents = fs.readFileSync(erc721aStorageFilepath, 'utf8');
41+
erc721aStorageContents = erc721aStorageContents
42+
.replace(/struct Layout\s*\{/, tokenApprovalRefMatch[0] + '\n\n struct Layout {')
43+
.replace(/ERC721AUpgradeable.TokenApprovalRef/g, 'ERC721AStorage.TokenApprovalRef')
44+
.replace(/import.*?\.\/ERC721AUpgradeable.sol[^;]+;/, '');
45+
46+
fs.writeFileSync(erc721aStorageFilepath, erc721aStorageContents);
47+
}

0 commit comments

Comments
 (0)