Skip to content

Commit a90c44b

Browse files
authored
Merge pull request #149 from kaleido-io/contracts
Update to latest OpenZeppelin base contracts
2 parents a428921 + cb34958 commit a90c44b

10 files changed

Lines changed: 43 additions & 105 deletions

File tree

samples/solidity/contracts/ERC20NoData.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import '@openzeppelin/contracts/access/Ownable.sol';
1818
* This is a sample only and NOT a reference implementation.
1919
*/
2020
contract ERC20NoData is Context, Ownable, ERC20, ERC20Burnable {
21-
constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
21+
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) {}
2222

2323
function mint(address to, uint256 amount) public onlyOwner {
2424
_mint(to, amount);

samples/solidity/contracts/ERC20WithData.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ import './IERC20WithData.sol';
2525
* This is a sample only and NOT a reference implementation.
2626
*/
2727
contract ERC20WithData is Context, Ownable, ERC165, ERC20, IERC20WithData {
28-
constructor(string memory name, string memory symbol) ERC20(name, symbol) {}
28+
constructor(string memory name, string memory symbol) ERC20(name, symbol) Ownable(msg.sender) {}
2929

3030
function supportsInterface(
3131
bytes4 interfaceId

samples/solidity/contracts/ERC721NoData.sol

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
66
import '@openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol';
77
import '@openzeppelin/contracts/utils/Context.sol';
88
import '@openzeppelin/contracts/access/Ownable.sol';
9-
import '@openzeppelin/contracts/utils/Counters.sol';
109

1110
/**
1211
* Example ERC721 token with mint and burn.
@@ -19,18 +18,15 @@ import '@openzeppelin/contracts/utils/Counters.sol';
1918
* This is a sample only and NOT a reference implementation.
2019
*/
2120
contract ERC721NoData is Context, Ownable, ERC721, ERC721Burnable {
22-
using Counters for Counters.Counter;
21+
uint256 private _nextTokenId = 1;
2322

24-
Counters.Counter private _tokenIdCounter;
25-
26-
constructor(string memory name, string memory symbol) ERC721(name, symbol) {
27-
// Start counting at 1
28-
_tokenIdCounter.increment();
29-
}
23+
constructor(
24+
string memory name,
25+
string memory symbol
26+
) ERC721(name, symbol) Ownable(msg.sender) {}
3027

3128
function safeMint(address to) public onlyOwner {
32-
uint256 tokenId = _tokenIdCounter.current();
33-
_tokenIdCounter.increment();
29+
uint256 tokenId = _nextTokenId++;
3430
_safeMint(to, tokenId);
3531
}
3632

samples/solidity/contracts/ERC721WithData.sol

Lines changed: 8 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ pragma solidity ^0.8.0;
55
import '@openzeppelin/contracts/access/Ownable.sol';
66
import '@openzeppelin/contracts/utils/Context.sol';
77
import '@openzeppelin/contracts/utils/Strings.sol';
8-
import '@openzeppelin/contracts/utils/Counters.sol';
9-
import '@openzeppelin/contracts/token/ERC721/ERC721.sol';
8+
import '@openzeppelin/contracts/token/ERC721/extensions/ERC721URIStorage.sol';
109
import './IERC721WithData.sol';
1110

1211
/**
@@ -24,36 +23,28 @@ import './IERC721WithData.sol';
2423
*
2524
* This is a sample only and NOT a reference implementation.
2625
*/
27-
contract ERC721WithData is Context, Ownable, ERC721, IERC721WithData {
28-
using Counters for Counters.Counter;
29-
30-
Counters.Counter private _tokenIdCounter;
26+
contract ERC721WithData is Context, Ownable, ERC721URIStorage, IERC721WithData {
27+
uint256 private _nextTokenId = 1;
3128
string private _baseTokenURI;
3229

33-
// Optional mapping for token URIs
34-
mapping(uint256 => string) private _tokenURIs;
35-
3630
constructor(
3731
string memory name,
3832
string memory symbol,
3933
string memory baseTokenURI
40-
) ERC721(name, symbol) {
34+
) ERC721(name, symbol) Ownable(msg.sender) {
4135
_baseTokenURI = baseTokenURI;
42-
// Start counting at 1
43-
_tokenIdCounter.increment();
4436
}
4537

4638
function supportsInterface(
4739
bytes4 interfaceId
48-
) public view virtual override(ERC721, IERC165) returns (bool) {
40+
) public view virtual override(ERC721URIStorage, IERC165) returns (bool) {
4941
return
5042
interfaceId == type(IERC721WithData).interfaceId ||
5143
super.supportsInterface(interfaceId);
5244
}
5345

5446
function mintWithData(address to, bytes calldata data) public virtual onlyOwner {
55-
uint256 tokenId = _tokenIdCounter.current();
56-
_tokenIdCounter.increment();
47+
uint256 tokenId = _nextTokenId++;
5748
_safeMint(to, tokenId, data);
5849
_setTokenURI(tokenId, string(abi.encodePacked(_baseURI(), Strings.toString(tokenId))));
5950
}
@@ -63,8 +54,7 @@ contract ERC721WithData is Context, Ownable, ERC721, IERC721WithData {
6354
bytes calldata data,
6455
string memory tokenURI_
6556
) public virtual onlyOwner {
66-
uint256 tokenId = _tokenIdCounter.current();
67-
_tokenIdCounter.increment();
57+
uint256 tokenId = _nextTokenId++;
6858
_safeMint(to, tokenId, data);
6959

7060
// If there is no tokenURI passed, concatenate the tokenID to the base URI
@@ -103,24 +93,7 @@ contract ERC721WithData is Context, Ownable, ERC721, IERC721WithData {
10393
}
10494

10595
function _baseURI() internal view virtual override returns (string memory) {
106-
bytes memory tempURITest = bytes(_baseTokenURI);
107-
if (tempURITest.length == 0) {
108-
return 'firefly://token/';
109-
} else {
110-
return _baseTokenURI;
111-
}
112-
}
113-
114-
function tokenURI(uint256 tokenId) public view virtual override returns (string memory) {
115-
require(_exists(tokenId), 'ERC721WithData: Token does not exist');
116-
117-
string memory uri = _tokenURIs[tokenId];
118-
return uri;
119-
}
120-
121-
function _setTokenURI(uint256 tokenId, string memory _tokenURI) internal {
122-
require(_exists(tokenId), 'ERC721WithData: Token does not exist');
123-
_tokenURIs[tokenId] = _tokenURI;
96+
return _baseTokenURI;
12497
}
12598

12699
function baseTokenUri() public view virtual override returns (string memory) {

samples/solidity/package-lock.json

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

samples/solidity/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,6 @@
1818
"typescript": "^5.3.3"
1919
},
2020
"dependencies": {
21-
"@openzeppelin/contracts": "^4.7.3"
21+
"@openzeppelin/contracts": "^5.0.2"
2222
}
2323
}

samples/solidity/test/ERC20NoData.ts

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { SignerWithAddress } from '@nomicfoundation/hardhat-ethers/signers';
2-
import { expect } from "chai";
3-
import { ethers } from "hardhat";
2+
import { expect } from 'chai';
3+
import { ethers } from 'hardhat';
44
import { ERC20NoData } from '../typechain-types';
55

66
describe('ERC20NoData - Unit Tests', async function () {
@@ -43,9 +43,9 @@ describe('ERC20NoData - Unit Tests', async function () {
4343
it('Mint - Non-deployer of contract should not be able to mint tokens', async function () {
4444
expect(await deployedERC20NoData.balanceOf(signerB.address)).to.equal(0);
4545
// Signer B mint to Signer B (Not allowed)
46-
await expect(deployedERC20NoData.connect(signerB).mint(signerB.address, 20)).to.be.revertedWith(
47-
'Ownable: caller is not the owner',
48-
);
46+
await expect(
47+
deployedERC20NoData.connect(signerB).mint(signerB.address, 20),
48+
).to.be.revertedWithCustomError(deployedERC20NoData, 'OwnableUnauthorizedAccount');
4949

5050
expect(await deployedERC20NoData.balanceOf(signerB.address)).to.equal(0);
5151
});
@@ -162,11 +162,11 @@ describe('ERC20NoData - Unit Tests', async function () {
162162
// Signer B attempts to burn tokens from Signer A wallet (not allowed)
163163
await expect(
164164
deployedERC20NoData.connect(signerB).burnFrom(deployerSignerA.address, 10),
165-
).to.be.revertedWith('ERC20: insufficient allowance');
165+
).to.be.revertedWithCustomError(deployedERC20NoData, 'ERC20InsufficientAllowance');
166166
// Signer A attempts to burn tokens from Signer B wallet (not allowed)
167167
await expect(
168168
deployedERC20NoData.connect(deployerSignerA).burnFrom(signerB.address, 10),
169-
).to.be.revertedWith('ERC20: insufficient allowance');
169+
).to.be.revertedWithCustomError(deployedERC20NoData, 'ERC20InsufficientAllowance');
170170

171171
expect(await deployedERC20NoData.balanceOf(deployerSignerA.address)).to.equal(20);
172172
expect(await deployedERC20NoData.balanceOf(signerB.address)).to.equal(20);

samples/solidity/test/ERC20WithData.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ describe('ERC20WithData - Unit Tests', async function () {
4747
// Signer B mint to Signer B (Not allowed)
4848
await expect(
4949
deployedERC20WithData.connect(signerB).mintWithData(signerB.address, 20, '0x00'),
50-
).to.be.revertedWith('Ownable: caller is not the owner');
50+
).to.be.revertedWithCustomError(deployedERC20WithData, 'OwnableUnauthorizedAccount');
5151

5252
expect(await deployedERC20WithData.balanceOf(signerB.address)).to.equal(0);
5353
});
@@ -57,7 +57,7 @@ describe('ERC20WithData - Unit Tests', async function () {
5757
// Signer B mint to Signer B (Not allowed)
5858
await expect(
5959
deployedERC20WithData.connect(signerB).mintWithData(signerB.address, 20, '0x00'),
60-
).to.be.revertedWith('Ownable: caller is not the owner');
60+
).to.be.revertedWithCustomError(deployedERC20WithData, 'OwnableUnauthorizedAccount');
6161

6262
expect(await deployedERC20WithData.balanceOf(signerB.address)).to.equal(0);
6363
});
@@ -142,7 +142,7 @@ describe('ERC20WithData - Unit Tests', async function () {
142142
deployedERC20WithData
143143
.connect(deployerSignerA)
144144
.transferWithData(signerB.address, signerC.address, 11, '0x00'),
145-
).to.be.revertedWith('ERC20: insufficient allowance');
145+
).to.be.revertedWithCustomError(deployedERC20WithData, 'ERC20InsufficientAllowance');
146146

147147
expect(await deployedERC20WithData.balanceOf(deployerSignerA.address)).to.equal(0);
148148
expect(await deployedERC20WithData.balanceOf(signerB.address)).to.equal(20);

samples/solidity/test/ERC721NoData.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ describe('ERC721NoData - Unit Tests', async function () {
3636
// Signer B mint to Signer B (Not allowed)
3737
await expect(
3838
deployedERC721NoData.connect(signerB).safeMint(signerB.address),
39-
).to.be.revertedWith('Ownable: caller is not the owner');
39+
).to.be.revertedWithCustomError(deployedERC721NoData, 'OwnableUnauthorizedAccount');
4040

4141
expect(await deployedERC721NoData.balanceOf(signerB.address)).to.equal(0);
4242
});
@@ -56,7 +56,7 @@ describe('ERC721NoData - Unit Tests', async function () {
5656
// Signer B mint token to Signer B (Not allowed)
5757
await expect(
5858
deployedERC721NoData.connect(signerB).safeMint(signerB.address),
59-
).to.be.revertedWith('Ownable: caller is not the owner');
59+
).to.be.revertedWithCustomError(deployedERC721NoData, 'OwnableUnauthorizedAccount');
6060

6161
expect(await deployedERC721NoData.balanceOf(signerB.address)).to.equal(0);
6262
});
@@ -136,7 +136,7 @@ describe('ERC721NoData - Unit Tests', async function () {
136136
deployedERC721NoData
137137
.connect(deployerSignerA)
138138
['safeTransferFrom(address,address,uint256)'](signerB.address, signerC.address, 1),
139-
).to.be.revertedWith('ERC721: caller is not token owner or approved');
139+
).to.be.revertedWithCustomError(deployedERC721NoData, 'ERC721InsufficientApproval');
140140

141141
expect(await deployedERC721NoData.balanceOf(deployerSignerA.address)).to.equal(0);
142142
expect(await deployedERC721NoData.balanceOf(signerB.address)).to.equal(2);
@@ -213,12 +213,14 @@ describe('ERC721NoData - Unit Tests', async function () {
213213
.to.emit(deployedERC721NoData, 'Transfer')
214214
.withArgs(ZERO_ADDRESS, signerC.address, 3);
215215
// Signer B attempts to burn token from Signer A wallet (not allowed)
216-
await expect(deployedERC721NoData.connect(signerB).burn(1)).to.be.revertedWith(
217-
'ERC721: caller is not token owner or approved',
216+
await expect(deployedERC721NoData.connect(signerB).burn(1)).to.be.revertedWithCustomError(
217+
deployedERC721NoData,
218+
'ERC721InsufficientApproval',
218219
);
219220
// Signer C attempts to burn token from Signer B wallet (not allowed)
220-
await expect(deployedERC721NoData.connect(signerC).burn(2)).to.be.revertedWith(
221-
'ERC721: caller is not token owner or approved',
221+
await expect(deployedERC721NoData.connect(signerC).burn(2)).to.be.revertedWithCustomError(
222+
deployedERC721NoData,
223+
'ERC721InsufficientApproval',
222224
);
223225

224226
expect(await deployedERC721NoData.balanceOf(deployerSignerA.address)).to.equal(1);

samples/solidity/test/ERC721WithData.ts

Lines changed: 3 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ describe('ERC721WithData - Unit Tests', async function () {
3535
it('Create - Should create a new ERC721 instance with default state', async function () {
3636
expect(await deployedERC721WithData.name()).to.equal(contractName);
3737
expect(await deployedERC721WithData.symbol()).to.equal(contractSymbol);
38-
expect(await deployedERC721WithData.baseTokenUri()).to.equal('firefly://token/');
38+
expect(await deployedERC721WithData.baseTokenUri()).to.equal('');
3939
});
4040

4141
it('Mint - Should mint successfully with a custom URI', async function () {
@@ -58,7 +58,7 @@ describe('ERC721WithData - Unit Tests', async function () {
5858
// Signer B mint token to Signer B (Not allowed)
5959
await expect(
6060
deployedERC721WithData.connect(signerB).mintWithData(signerB.address, '0x00'),
61-
).to.be.revertedWith('Ownable: caller is not the owner');
61+
).to.be.revertedWithCustomError(deployedERC721WithData, 'OwnableUnauthorizedAccount');
6262

6363
expect(await deployedERC721WithData.balanceOf(signerB.address)).to.equal(0);
6464
});
@@ -73,7 +73,6 @@ describe('ERC721WithData - Unit Tests', async function () {
7373
.to.emit(deployedERC721WithData, 'Transfer')
7474
.withArgs(ZERO_ADDRESS, deployerSignerA.address, 1);
7575
expect(await deployedERC721WithData.balanceOf(deployerSignerA.address)).to.equal(1);
76-
expect(await deployedERC721WithData.tokenURI(1)).to.equal('firefly://token/1');
7776
// Signer A transfer token to Signer B
7877
await expect(
7978
deployedERC721WithData
@@ -137,7 +136,7 @@ describe('ERC721WithData - Unit Tests', async function () {
137136
deployedERC721WithData
138137
.connect(deployerSignerA)
139138
.transferWithData(signerB.address, signerC.address, 1, '0x00'),
140-
).to.be.revertedWith('ERC721: caller is not token owner or approved');
139+
).to.be.revertedWithCustomError(deployedERC721WithData, 'ERC721InsufficientApproval');
141140

142141
expect(await deployedERC721WithData.balanceOf(deployerSignerA.address)).to.equal(0);
143142
expect(await deployedERC721WithData.balanceOf(signerB.address)).to.equal(2);
@@ -248,36 +247,4 @@ describe('ERC721WithData - Unit Tests', async function () {
248247
expect(await deployedERC721WithData.balanceOf(signerB.address)).to.equal(1);
249248
expect(await deployedERC721WithData.balanceOf(signerC.address)).to.equal(1);
250249
});
251-
252-
it("URI - Minted token URIs should be 'firefly://token/<tokenId>'", async function () {
253-
expect(await deployedERC721WithData.balanceOf(deployerSignerA.address)).to.equal(0);
254-
expect(await deployedERC721WithData.balanceOf(signerB.address)).to.equal(0);
255-
expect(await deployedERC721WithData.balanceOf(signerC.address)).to.equal(0);
256-
// Signer A mints token to itself
257-
await expect(
258-
deployedERC721WithData.connect(deployerSignerA).mintWithData(deployerSignerA.address, '0x00'),
259-
)
260-
.to.emit(deployedERC721WithData, 'Transfer')
261-
.withArgs(ZERO_ADDRESS, deployerSignerA.address, 1);
262-
// Signer A mints token to Signer B
263-
await expect(
264-
deployedERC721WithData.connect(deployerSignerA).mintWithData(signerB.address, '0x00'),
265-
)
266-
.to.emit(deployedERC721WithData, 'Transfer')
267-
.withArgs(ZERO_ADDRESS, signerB.address, 2);
268-
// Signer A mints token to Signer C
269-
await expect(
270-
deployedERC721WithData.connect(deployerSignerA).mintWithData(signerC.address, '0x00'),
271-
)
272-
.to.emit(deployedERC721WithData, 'Transfer')
273-
.withArgs(ZERO_ADDRESS, signerC.address, 3);
274-
275-
expect(await deployedERC721WithData.tokenURI(1)).to.equal('firefly://token/1');
276-
expect(await deployedERC721WithData.tokenURI(2)).to.equal('firefly://token/2');
277-
expect(await deployedERC721WithData.tokenURI(3)).to.equal('firefly://token/3');
278-
279-
expect(await deployedERC721WithData.balanceOf(deployerSignerA.address)).to.equal(1);
280-
expect(await deployedERC721WithData.balanceOf(signerB.address)).to.equal(1);
281-
expect(await deployedERC721WithData.balanceOf(signerC.address)).to.equal(1);
282-
});
283250
});

0 commit comments

Comments
 (0)