Commit cd53e47c by github-actions

Transpile 1a6bf792

parent 307d3eb0
......@@ -35,6 +35,7 @@
* `ERC20Capped`: optimize gas usage of by enforcing te check directly in `_mint`. ([#2524](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2524))
* Rename `UpgradeableProxy` to `ERC1967Proxy`. ([#2547](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2547))
* `ERC777`: Optimize the gas costs of the constructor. ([#2551](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2551))
* `ERC721URIStorage`: Add a new extension that implements the `_setTokenURI` behavior as it was available in 3.4.0. ([#2555](https://github.com/OpenZeppelin/openzeppelin-contracts/pull/2555))
### How to upgrade from 3.x
......
......@@ -18,16 +18,16 @@ contract ClonesMockUpgradeable is Initializable {
event NewInstance(address instance);
function clone(address master, bytes calldata initdata) public payable {
_initAndEmit(master.clone(), initdata);
function clone(address implementation, bytes calldata initdata) public payable {
_initAndEmit(implementation.clone(), initdata);
}
function cloneDeterministic(address master, bytes32 salt, bytes calldata initdata) public payable {
_initAndEmit(master.cloneDeterministic(salt), initdata);
function cloneDeterministic(address implementation, bytes32 salt, bytes calldata initdata) public payable {
_initAndEmit(implementation.cloneDeterministic(salt), initdata);
}
function predictDeterministicAddress(address master, bytes32 salt) public view returns (address predicted) {
return master.predictDeterministicAddress(salt);
function predictDeterministicAddress(address implementation, bytes32 salt) public view returns (address predicted) {
return implementation.predictDeterministicAddress(salt);
}
function _initAndEmit(address instance, bytes memory initdata) private {
......
......@@ -16,8 +16,20 @@ contract ERC721BurnableMockUpgradeable is Initializable, ERC721BurnableUpgradeab
function __ERC721BurnableMock_init_unchained(string memory name, string memory symbol) internal initializer { }
function exists(uint256 tokenId) public view returns (bool) {
return _exists(tokenId);
}
function mint(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
function safeMint(address to, uint256 tokenId) public {
_safeMint(to, tokenId);
}
function safeMint(address to, uint256 tokenId, bytes memory _data) public {
_safeMint(to, tokenId, _data);
}
uint256[50] private __gap;
}
......@@ -34,6 +34,10 @@ contract ERC721EnumerableMockUpgradeable is Initializable, ERC721EnumerableUpgra
return _baseURI();
}
function exists(uint256 tokenId) public view returns (bool) {
return _exists(tokenId);
}
function mint(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
......
......@@ -21,24 +21,32 @@ contract ERC721PausableMockUpgradeable is Initializable, ERC721PausableUpgradeab
function __ERC721PausableMock_init_unchained(string memory name, string memory symbol) internal initializer { }
function mint(address to, uint256 tokenId) public {
super._mint(to, tokenId);
function pause() external {
_pause();
}
function burn(uint256 tokenId) public {
super._burn(tokenId);
function unpause() external {
_unpause();
}
function exists(uint256 tokenId) public view returns (bool) {
return super._exists(tokenId);
return _exists(tokenId);
}
function pause() external {
_pause();
function mint(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
function unpause() external {
_unpause();
function safeMint(address to, uint256 tokenId) public {
_safeMint(to, tokenId);
}
function safeMint(address to, uint256 tokenId, bytes memory _data) public {
_safeMint(to, tokenId, _data);
}
function burn(uint256 tokenId) public {
_burn(tokenId);
}
uint256[50] private __gap;
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../token/ERC721/extensions/ERC721URIStorageUpgradeable.sol";
import "../utils/Initializable.sol";
/**
* @title ERC721Mock
* This mock just provides a public safeMint, mint, and burn functions for testing purposes
*/
contract ERC721URIStorageMockUpgradeable is Initializable, ERC721URIStorageUpgradeable {
string private _baseTokenURI;
function __ERC721URIStorageMock_init(string memory name, string memory symbol) internal initializer {
__Context_init_unchained();
__ERC165_init_unchained();
__ERC721_init_unchained(name, symbol);
__ERC721URIStorage_init_unchained();
__ERC721URIStorageMock_init_unchained(name, symbol);
}
function __ERC721URIStorageMock_init_unchained(string memory name, string memory symbol) internal initializer { }
function _baseURI() internal view virtual override returns (string memory) {
return _baseTokenURI;
}
function setBaseURI(string calldata newBaseTokenURI) public {
_baseTokenURI = newBaseTokenURI;
}
function baseURI() public view returns (string memory) {
return _baseURI();
}
function setTokenURI(uint256 tokenId, string memory _tokenURI) public {
_setTokenURI(tokenId, _tokenURI);
}
function exists(uint256 tokenId) public view returns (bool) {
return _exists(tokenId);
}
function mint(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
function safeMint(address to, uint256 tokenId) public {
_safeMint(to, tokenId);
}
function safeMint(address to, uint256 tokenId, bytes memory _data) public {
_safeMint(to, tokenId, _data);
}
function burn(uint256 tokenId) public {
_burn(tokenId);
}
uint256[49] private __gap;
}
......@@ -206,6 +206,13 @@ contract ERC777MockUpgradeableWithInit is ERC777MockUpgradeable {
__ERC777Mock_init(initialHolder, initialBalance, name, symbol, defaultOperators);
}
}
import "./ERC721URIStorageMockUpgradeable.sol";
contract ERC721URIStorageMockUpgradeableWithInit is ERC721URIStorageMockUpgradeable {
constructor(string memory name, string memory symbol) public payable {
__ERC721URIStorageMock_init(name, symbol);
}
}
import "./PausableMockUpgradeable.sol";
contract PausableMockUpgradeableWithInit is PausableMockUpgradeable {
......
......@@ -39,6 +39,8 @@ NOTE: This core set of contracts is designed to be unopinionated, allowing devel
{{ERC721Burnable}}
{{ERC721TokenUri}}
== Presets
These contracts are preconfigured combinations of the above features. They can be used through inheritance or as models to copy and paste their source code.
......
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "../ERC721Upgradeable.sol";
import "../../../utils/Initializable.sol";
/**
* @dev ERC721 token with storage based token uri management.
*/
abstract contract ERC721URIStorageUpgradeable is Initializable, ERC721Upgradeable {
function __ERC721URIStorage_init() internal initializer {
__Context_init_unchained();
__ERC165_init_unchained();
__ERC721URIStorage_init_unchained();
}
function __ERC721URIStorage_init_unchained() internal initializer {
}
using StringsUpgradeable for uint256;
// Optional mapping for token URIs
mapping (uint256 => string) private _tokenURIs;
/**
* @dev See {IERC721Metadata-tokenURI}.
*/
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
require(_exists(tokenId), "ERC721URIStorage: URI query for nonexistent token");
string memory _tokenURI = _tokenURIs[tokenId];
string memory base = _baseURI();
// If there is no base URI, return the token URI.
if (bytes(base).length == 0) {
return _tokenURI;
}
// If both are set, concatenate the baseURI and tokenURI (via abi.encodePacked).
if (bytes(_tokenURI).length > 0) {
return string(abi.encodePacked(base, _tokenURI));
}
return super.tokenURI(tokenId);
}
/**
* @dev Sets `_tokenURI` as the tokenURI of `tokenId`.
*
* Requirements:
*
* - `tokenId` must exist.
*/
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal virtual {
require(_exists(tokenId), "ERC721URIStorage: URI set of nonexistent token");
_tokenURIs[tokenId] = _tokenURI;
}
/**
* @dev Destroys `tokenId`.
* The approval is cleared when the token is burned.
*
* Requirements:
*
* - `tokenId` must exist.
*
* Emits a {Transfer} event.
*/
function _burn(uint256 tokenId) internal virtual override {
super._burn(tokenId);
if (bytes(_tokenURIs[tokenId]).length != 0) {
delete _tokenURIs[tokenId];
}
}
uint256[49] private __gap;
}
......@@ -11,6 +11,7 @@ for (const f of fs.readdirSync(path.join(__dirname, 'hardhat'))) {
}
const enableGasReport = !!process.env.ENABLE_GAS_REPORT;
const enableProduction = process.env.COMPILE_MODE === 'production';
/**
* @type import('hardhat/config').HardhatUserConfig
......@@ -20,7 +21,7 @@ module.exports = {
version: '0.8.0',
settings: {
optimizer: {
enabled: enableGasReport,
enabled: enableGasReport || enableProduction,
runs: 200,
},
},
......
......@@ -23,7 +23,7 @@
"lint:js:fix": "eslint --ignore-path .gitignore . --fix",
"lint:sol": "echo 'solidity linter currently disabled' # solhint --max-warnings 0 \"contracts/**/*.sol\"",
"prepublish": "rimraf build contracts/build artifacts cache",
"prepare": "npm run compile",
"prepare": "env COMPILE_MODE=production npm run compile",
"prepack": "scripts/prepack.sh",
"release": "scripts/release/release.sh",
"version": "scripts/release/version.sh",
......
......@@ -23,18 +23,15 @@ const ignorePatternsSubtrees = ignorePatterns
.concat(ignorePatterns.map(pat => path.join(pat, '**/*')))
.map(p => p.replace(/^\//, ''));
const artifactsDir = 'build/contracts';
const buildinfo = 'artifacts/build-info';
const filenames = fs.readdirSync(buildinfo);
if (filenames.length !== 1) {
throw new Error(`There should only be one file in ${buildinfo}`);
}
const solcOutput = readJSON(path.join(buildinfo, filenames[0])).output;
const artifactsDir = 'build/contracts';
let n = 0;
for (const sourcePath in solcOutput.contracts) {
for (const filename of filenames) {
const solcOutput = readJSON(path.join(buildinfo, filename)).output;
for (const sourcePath in solcOutput.contracts) {
const ignore = match.any(sourcePath, ignorePatternsSubtrees);
if (ignore) {
for (const contract in solcOutput.contracts[sourcePath]) {
......@@ -42,6 +39,7 @@ for (const sourcePath in solcOutput.contracts) {
n += 1;
}
}
}
}
console.error(`Removed ${n} mock artifacts`);
const { BN, expectRevert } = require('@openzeppelin/test-helpers');
const { expect } = require('chai');
const ERC721URIStorageMock = artifacts.require('ERC721URIStorageMock');
contract('ERC721URIStorage', function (accounts) {
const [ owner ] = accounts;
const name = 'Non Fungible Token';
const symbol = 'NFT';
const firstTokenId = new BN('5042');
const nonExistentTokenId = new BN('13');
beforeEach(async function () {
this.token = await ERC721URIStorageMock.new(name, symbol);
});
describe('token URI', function () {
beforeEach(async function () {
await this.token.mint(owner, firstTokenId);
});
const baseURI = 'https://api.com/v1/';
const sampleUri = 'mock://mytoken';
it('it is empty by default', async function () {
expect(await this.token.tokenURI(firstTokenId)).to.be.equal('');
});
it('reverts when queried for non existent token id', async function () {
await expectRevert(
this.token.tokenURI(nonExistentTokenId), 'ERC721URIStorage: URI query for nonexistent token',
);
});
it('can be set for a token id', async function () {
await this.token.setTokenURI(firstTokenId, sampleUri);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(sampleUri);
});
it('reverts when setting for non existent token id', async function () {
await expectRevert(
this.token.setTokenURI(nonExistentTokenId, sampleUri), 'ERC721URIStorage: URI set of nonexistent token',
);
});
it('base URI can be set', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.baseURI()).to.equal(baseURI);
});
it('base URI is added as a prefix to the token URI', async function () {
await this.token.setBaseURI(baseURI);
await this.token.setTokenURI(firstTokenId, sampleUri);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + sampleUri);
});
it('token URI can be changed by changing the base URI', async function () {
await this.token.setBaseURI(baseURI);
await this.token.setTokenURI(firstTokenId, sampleUri);
const newBaseURI = 'https://api.com/v2/';
await this.token.setBaseURI(newBaseURI);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(newBaseURI + sampleUri);
});
it('tokenId is appended to base URI for tokens with no URI', async function () {
await this.token.setBaseURI(baseURI);
expect(await this.token.tokenURI(firstTokenId)).to.be.equal(baseURI + firstTokenId);
});
it('tokens with URI can be burnt ', async function () {
await this.token.setTokenURI(firstTokenId, sampleUri);
await this.token.burn(firstTokenId, { from: owner });
expect(await this.token.exists(firstTokenId)).to.equal(false);
await expectRevert(
this.token.tokenURI(firstTokenId), 'ERC721URIStorage: URI query for nonexistent token',
);
});
});
});
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment