Skip to content

Commit 0c18fac

Browse files
ernestognwAmxx
andauthored
Update Solidity files in docs (#4956)
Co-authored-by: Hadrien Croubois <[email protected]>
1 parent 6ae2c17 commit 0c18fac

File tree

16 files changed

+152
-154
lines changed

16 files changed

+152
-154
lines changed

contracts/mocks/docs/MyNFT.sol

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// contracts/MyNFT.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {ERC721} from "../../token/ERC721/ERC721.sol";
6+
7+
contract MyNFT is ERC721 {
8+
constructor() ERC721("MyNFT", "MNFT") {}
9+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// contracts/AccessControlModified.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {AccessControl} from "../../../access/AccessControl.sol";
6+
7+
contract AccessControlModified is AccessControl {
8+
error AccessControlNonRevokable();
9+
10+
// Override the revokeRole function
11+
function revokeRole(bytes32, address) public pure override {
12+
revert AccessControlNonRevokable();
13+
}
14+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// contracts/AccessControlNonRevokableAdmin.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {AccessControl} from "../../../access/AccessControl.sol";
6+
7+
contract AccessControlNonRevokableAdmin is AccessControl {
8+
error AccessControlNonRevokable();
9+
10+
function revokeRole(bytes32 role, address account) public override {
11+
if (role == DEFAULT_ADMIN_ROLE) {
12+
revert AccessControlNonRevokable();
13+
}
14+
15+
super.revokeRole(role, account);
16+
}
17+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// contracts/GameItems.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {ERC1155} from "../../../../token/ERC1155/ERC1155.sol";
6+
7+
contract GameItems is ERC1155 {
8+
uint256 public constant GOLD = 0;
9+
uint256 public constant SILVER = 1;
10+
uint256 public constant THORS_HAMMER = 2;
11+
uint256 public constant SWORD = 3;
12+
uint256 public constant SHIELD = 4;
13+
14+
constructor() ERC1155("https://game.example/api/item/{id}.json") {
15+
_mint(msg.sender, GOLD, 10 ** 18, "");
16+
_mint(msg.sender, SILVER, 10 ** 27, "");
17+
_mint(msg.sender, THORS_HAMMER, 1, "");
18+
_mint(msg.sender, SWORD, 10 ** 9, "");
19+
_mint(msg.sender, SHIELD, 10 ** 9, "");
20+
}
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// contracts/MyERC115HolderContract.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {ERC1155Holder} from "../../../../token/ERC1155/utils/ERC1155Holder.sol";
6+
7+
contract MyERC115HolderContract is ERC1155Holder {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// contracts/GLDToken.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {ERC20} from "../../../../token/ERC20/ERC20.sol";
6+
7+
contract GLDToken is ERC20 {
8+
constructor(uint256 initialSupply) ERC20("Gold", "GLD") {
9+
_mint(msg.sender, initialSupply);
10+
}
11+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// contracts/GameItem.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {ERC721URIStorage, ERC721} from "../../../../token/ERC721/extensions/ERC721URIStorage.sol";
6+
7+
contract GameItem is ERC721URIStorage {
8+
uint256 private _nextTokenId;
9+
10+
constructor() ERC721("GameItem", "ITM") {}
11+
12+
function awardItem(address player, string memory tokenURI) public returns (uint256) {
13+
uint256 tokenId = _nextTokenId++;
14+
_mint(player, tokenId);
15+
_setTokenURI(tokenId, tokenURI);
16+
17+
return tokenId;
18+
}
19+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
pragma solidity ^0.8.20;
4+
5+
import {ERC721} from "../../../token/ERC721/ERC721.sol";
6+
import {Strings} from "../../../utils/Strings.sol";
7+
import {Base64} from "../../../utils/Base64.sol";
8+
9+
contract Base64NFT is ERC721 {
10+
using Strings for uint256;
11+
12+
constructor() ERC721("Base64NFT", "MTK") {}
13+
14+
// ...
15+
16+
function tokenURI(uint256 tokenId) public pure override returns (string memory) {
17+
// Equivalent to:
18+
// {
19+
// "name": "Base64NFT #1",
20+
// // Replace with extra ERC-721 Metadata properties
21+
// }
22+
// prettier-ignore
23+
string memory dataURI = string.concat("{\"name\": \"Base64NFT #", tokenId.toString(), "\"}");
24+
25+
return string.concat("data:application/json;base64,", Base64.encode(bytes(dataURI)));
26+
}
27+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// contracts/Box.sol
2+
// SPDX-License-Identifier: MIT
3+
pragma solidity ^0.8.20;
4+
5+
import {Multicall} from "../../../utils/Multicall.sol";
6+
7+
contract Box is Multicall {
8+
function foo() public {
9+
// ...
10+
}
11+
12+
function bar() public {
13+
// ...
14+
}
15+
}

docs/modules/ROOT/pages/erc1155.adoc

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,7 @@ Here's what a contract for tokenized items might look like:
3232

3333
[source,solidity]
3434
----
35-
// contracts/GameItems.sol
36-
// SPDX-License-Identifier: MIT
37-
pragma solidity ^0.8.20;
38-
39-
import {ERC1155} from "@openzeppelin/contracts/token/ERC1155/ERC1155.sol";
40-
41-
contract GameItems is ERC1155 {
42-
uint256 public constant GOLD = 0;
43-
uint256 public constant SILVER = 1;
44-
uint256 public constant THORS_HAMMER = 2;
45-
uint256 public constant SWORD = 3;
46-
uint256 public constant SHIELD = 4;
47-
48-
constructor() ERC1155("https://game.example/api/item/{id}.json") {
49-
_mint(msg.sender, GOLD, 10**18, "");
50-
_mint(msg.sender, SILVER, 10**27, "");
51-
_mint(msg.sender, THORS_HAMMER, 1, "");
52-
_mint(msg.sender, SWORD, 10**9, "");
53-
_mint(msg.sender, SHIELD, 10**9, "");
54-
}
55-
}
35+
include::api:example$token/ERC1155/GameItems.sol[]
5636
----
5737

5838
Note that for our Game Items, Gold is a fungible token whilst Thor's Hammer is a non-fungible token as we minted only one.
@@ -119,27 +99,20 @@ TIP: If you'd like to put all item information on-chain, you can extend ERC-721
11999
[[sending-to-contracts]]
120100
== Sending Tokens to Contracts
121101

122-
A key difference when using xref:api:token/ERC1155.adoc#IERC1155-safeTransferFrom-address-address-uint256-uint256-bytes-[`safeTransferFrom`] is that token transfers to other contracts may revert with the following message:
102+
A key difference when using xref:api:token/ERC1155.adoc#IERC1155-safeTransferFrom-address-address-uint256-uint256-bytes-[`safeTransferFrom`] is that token transfers to other contracts may revert with the following custom error:
123103

124104
[source,text]
125105
----
126-
ERC-1155: transfer to non ERC-1155 Receiver implementer
106+
ERC1155InvalidReceiver("<ADDRESS>")
127107
----
128108

129109
This is a good thing! It means that the recipient contract has not registered itself as aware of the ERC-1155 protocol, so transfers to it are disabled to *prevent tokens from being locked forever*. As an example, https://etherscan.io/token/0xa74476443119A942dE498590Fe1f2454d7D4aC0d?a=0xa74476443119A942dE498590Fe1f2454d7D4aC0d[the Golem contract currently holds over 350k `GNT` tokens], worth multiple tens of thousands of dollars, and lacks methods to get them out of there. This has happened to virtually every ERC20-backed project, usually due to user error.
130110

131-
In order for our contract to receive ERC-1155 tokens we can inherit from the convenience contract xref:api:token/ERC1155.adoc#ERC1155Holder[`ERC1155Holder`] which handles the registering for us. Though we need to remember to implement functionality to allow tokens to be transferred out of our contract:
111+
In order for our contract to receive ERC-1155 tokens we can inherit from the convenience contract xref:api:token/ERC1155.adoc#ERC1155Holder[`ERC1155Holder`] which handles the registering for us. Though we need to remember to implement functionality to allow tokens to be transferred out of our contract:
132112

133113
[source,solidity]
134114
----
135-
// contracts/MyContract.sol
136-
// SPDX-License-Identifier: MIT
137-
pragma solidity ^0.8.20;
138-
139-
import {ERC1155Holder} from "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol";
140-
141-
contract MyContract is ERC1155Holder {
142-
}
115+
include::api:example$token/ERC1155/MyERC115HolderContract.sol[]
143116
----
144117

145118
We can also implement more complex scenarios using the xref:api:token/ERC1155.adoc#IERC1155Receiver-onERC1155Received-address-address-uint256-uint256-bytes-[`onERC1155Received`] and xref:api:token/ERC1155.adoc#IERC1155Receiver-onERC1155BatchReceived-address-address-uint256---uint256---bytes-[`onERC1155BatchReceived`] functions.

0 commit comments

Comments
 (0)