Skip to content

Commit 17127d7

Browse files
authored
Docs(update): Correcting the Modular contract getting started tutoria… (#5074)
1 parent 0dbb174 commit 17127d7

File tree

2 files changed

+477
-470
lines changed
  • apps/portal/src/app/contracts/modular-contracts/get-started

2 files changed

+477
-470
lines changed

apps/portal/src/app/contracts/modular-contracts/get-started/create-core-contract/page.mdx

Lines changed: 163 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,13 @@ Install Forge from Foundry and add the modular contract framework:
1818

1919
```bash
2020
forge init
21-
forge install https://github.com/thirdweb-dev/modular-contracts.git
22-
forge remappings > remappings.txt
21+
forge install thirdweb-dev/modular-contracts --no-commit
22+
```
23+
24+
Add the Thirdweb modular contracts to `foundry.toml` under `remappings`:
25+
26+
```toml
27+
remappings = ['@thirdweb-dev=lib/modular-contracts/']
2328
```
2429

2530
### Setup Core Contract
@@ -29,152 +34,196 @@ forge remappings > remappings.txt
2934
<Step title="Create a Core Contract">
3035
Create a new file in the `src` folder called `CounterCore.sol`, and inherit the `Core` contract.
3136

32-
```solidity
33-
// SPDX-License-Identifier: UNLICENSED
34-
pragma solidity ^0.8.20;
37+
```solidity
38+
// SPDX-License-Identifier: MIT
39+
pragma solidity ^0.8.20;
3540
36-
import {Core} from "modular-contracts/src/Core.sol";
41+
import {Core} from "@thirdweb-dev/src/Core.sol";
42+
import {BeforeIncrementCallback} from "./interface/BeforeIncrementCallback.sol";
3743
38-
contract CounterCore is Core {
44+
contract CounterCore is Core {
45+
constructor(address owner) {
46+
_initializeOwner(owner);
47+
}
48+
}
3949
40-
constructor(address owner) {
41-
_initializeOwner(owner);
42-
}
50+
```
51+
52+
> **Note**
53+
> The `Core` contract is the base contract that needs to be inherited for this contract to be recognized as a core contract.
54+
55+
</Step>
4356

44-
}
45-
```
57+
<Step title="Set Get Supported Callback Function">
58+
Implement the `getSupportedCallbackFunctions` function. The Core contract is abstract because this function is not implemented. To avoid compilation errors, declare the function with an empty body for now.
4659

47-
> **Note**
48-
> The `Core` contract is the base contract that needs to be inherited for this contract to be recognized as a core contract.
60+
```solidity
61+
// SPDX-License-Identifier: MIT
62+
pragma solidity ^0.8.20;
63+
64+
import {Core} from "@thirdweb-dev/src/Core.sol";
65+
66+
contract CounterCore is Core {
67+
constructor(address owner) {
68+
_initializeOwner(owner);
69+
}
70+
71+
function getSupportedCallbackFunctions()
72+
public
73+
pure
74+
override
75+
returns (SupportedCallbackFunction[] memory supportedCallbackFunctions)
76+
{}
77+
}
78+
```
4979

5080
</Step>
5181

5282
<Step title="Set Up Increment Function">
5383
Define a function to increment a counter.
5484

55-
```solidity
56-
// SPDX-License-Identifier: UNLICENSED
57-
pragma solidity ^0.8.20;
85+
```solidity
86+
// SPDX-License-Identifier: MIT
87+
pragma solidity ^0.8.20;
5888
59-
import {Core} from "modular-contracts/src/Core.sol";
89+
import {Core} from "@thirdweb-dev/src/Core.sol";
6090
61-
contract CounterCore is Core {
62-
uint256 public count;
91+
contract CounterCore is Core {
92+
uint256 public count;
6393
64-
constructor(address owner) {
65-
_initializeOwner(owner);
66-
}
94+
constructor(address owner) {
95+
_initializeOwner(owner);
96+
}
97+
98+
function getSupportedCallbackFunctions()
99+
public
100+
pure
101+
override
102+
returns (SupportedCallbackFunction[] memory supportedCallbackFunctions)
103+
{}
67104
68-
function increment() public {
69-
count += 1;
70-
}
71-
}
72-
```
105+
// 👇👇👇👇👇👇👇👇👇
106+
function increment() public {
107+
count += 1;
108+
}
109+
}
110+
```
73111

74112
</Step>
75113

76114
<Step title="Add a Callback Function">
77-
Introduce the `_beforeIncrement` function to use the `beforeIncrement` callback from a module.
115+
Introduce the `_beforeIncrement` function to use the `beforeIncrement` callback from a module to achieve this, we'll introduce the interface `BeforeIncrementCallback`
78116

79-
> **Note**
80-
> Callback functions are hook-like functionalities that can be used before or after the main functionality of a core contract.
81-
> In this example, the `beforeIncrement` callback is executed before the main increment functionality.
117+
> **Note**
118+
> Callback functions are hook-like functionalities that can be used before or after the main functionality of a core contract.
119+
> In this example, the `beforeIncrement` callback is executed before the main increment functionality.
82120
83-
```solidity
84-
// SPDX-License-Identifier: UNLICENSED
85-
pragma solidity ^0.8.20;
121+
```solidity
122+
// SPDX-License-Identifier: MIT
123+
pragma solidity ^0.8.20;
86124
87-
import {Core} from "modular-contracts/src/Core.sol";
125+
import {Core} from "@thirdweb-dev/src/Core.sol";
88126
89-
interface BeforeIncrementCallback {
90-
function beforeIncrement(uint256 count) external returns (uint256);
91-
}
127+
// 👇👇👇👇👇👇👇👇👇
92128
93-
contract CounterCore is Core {
94-
uint256 public count;
129+
interface BeforeIncrementCallback {
130+
function beforeIncrement(uint256 count) external returns (uint256);
131+
}
95132
96-
constructor(address owner) {
97-
_initializeOwner(owner);
98-
}
133+
contract CounterCore is Core {
134+
uint256 public count;
99135
100-
function increment() public {
101-
uint256 newCount = _beforeIncrement(count);
102-
count = newCount;
103-
}
104-
105-
function _beforeIncrement(
106-
uint256 count
107-
) internal returns (uint256 newCount) {
108-
(, bytes memory returndata) = _executeCallbackFunction(
109-
BeforeIncrementCallback.beforeIncrement.selector,
110-
abi.encodeCall(BeforeIncrementCallback.beforeIncrement, (count))
111-
);
112-
newCount = abi.decode(returndata, (uint256));
113-
}
114-
}
115-
```
136+
constructor(address owner) {
137+
_initializeOwner(owner);
138+
}
116139
117-
</Step>
140+
function getSupportedCallbackFunctions()
141+
public
142+
pure
143+
override
144+
returns (SupportedCallbackFunction[] memory supportedCallbackFunctions)
145+
{}
118146
119-
<Step title="Implement Supported Functions">
120-
Implement the `getSupportedCallbackFunctions` and `supportsInterface` functions to expose which callback functions and interfaces this core contract supports.
147+
function increment() public {
148+
count += 1;
149+
}
121150
122-
```solidity
123-
// SPDX-License-Identifier: UNLICENSED
124-
pragma solidity ^0.8.20;
151+
// 👇👇👇👇👇👇👇👇👇
125152
126-
import {Core} from "modular-contracts/src/Core.sol";
153+
function _beforeIncrement(
154+
uint256 _count
155+
) internal returns (uint256 newCount) {
156+
(, bytes memory returnData) = _executeCallbackFunction(
157+
BeforeIncrementCallback.beforeIncrement.selector,
158+
abi.encodeCall(BeforeIncrementCallback.beforeIncrement, (_count))
159+
);
127160
128-
interface BeforeIncrementCallback {
129-
function beforeIncrement(uint256 count) external returns (uint256);
130-
}
161+
newCount = abi.decode(returnData, (uint256));
162+
}
163+
}
131164
132-
contract CounterCore is Core {
133-
uint256 public count;
165+
```
134166

135-
constructor(address owner) {
136-
_initializeOwner(owner);
137-
}
167+
</Step>
138168

139-
function increment() public {
140-
uint256 newCount = _beforeIncrement(count);
141-
count = newCount;
142-
}
143-
144-
function getSupportedCallbackFunctions()
145-
public
146-
pure
147-
override
148-
returns (SupportedCallbackFunction[] memory supportedCallbackFunctions)
149-
{
150-
supportedCallbackFunctions = new SupportedCallbackFunction ;
151-
supportedCallbackFunctions[0] = SupportedCallbackFunction({
152-
selector: BeforeIncrementCallback.beforeIncrement.selector,
153-
mode: CallbackMode.REQUIRED
154-
});
155-
}
156-
157-
function supportsInterface(bytes4 interfaceId)
158-
public
159-
view
160-
override
161-
returns (bool)
162-
{
163-
return
164-
interfaceId == 0x00000001 || super.supportsInterface(interfaceId);
165-
}
166-
167-
function _beforeIncrement(
168-
uint256 count
169-
) internal returns (uint256 newCount) {
170-
(bool success, bytes memory returndata) = _executeCallbackFunction(
171-
BeforeIncrementCallback.beforeIncrement.selector,
172-
abi.encodeCall(BeforeIncrementCallback.beforeIncrement, (count))
173-
);
174-
newCount = abi.decode(returndata, (uint256));
175-
}
176-
}
177-
```
169+
<Step title="Implement Supported Functions">
170+
Implement the `getSupportedCallbackFunctions` and `supportsInterface` functions to expose which callback functions and interfaces this core contract supports.
171+
172+
```solidity
173+
// SPDX-License-Identifier: MIT
174+
pragma solidity ^0.8.20;
175+
176+
import {Core} from "@thirdweb-dev/src/Core.sol";
177+
178+
interface BeforeIncrementCallback {
179+
function beforeIncrement(uint256 count) external returns (uint256);
180+
}
181+
182+
contract CounterCore is Core {
183+
uint256 public count;
184+
185+
constructor(address owner) {
186+
_initializeOwner(owner);
187+
}
188+
189+
// 👇👇👇👇👇👇👇👇👇
190+
function getSupportedCallbackFunctions()
191+
public
192+
pure
193+
override
194+
returns (SupportedCallbackFunction[] memory supportedCallbackFunctions)
195+
{
196+
supportedCallbackFunctions = new SupportedCallbackFunction[](1);
197+
supportedCallbackFunctions[0] = SupportedCallbackFunction({
198+
selector: BeforeIncrementCallback.beforeIncrement.selector,
199+
mode: CallbackMode.OPTIONAL
200+
});
201+
}
202+
203+
function increment() public {
204+
count += 1;
205+
}
206+
207+
function _beforeIncrement(
208+
uint256 _count
209+
) internal returns (uint256 newCount) {
210+
(, bytes memory returnData) = _executeCallbackFunction(
211+
BeforeIncrementCallback.beforeIncrement.selector,
212+
abi.encodeCall(BeforeIncrementCallback.beforeIncrement, (_count))
213+
);
214+
215+
newCount = abi.decode(returnData, (uint256));
216+
}
217+
218+
// 👇👇👇👇👇👇👇👇👇
219+
function supportsInterface(
220+
bytes4 interfaceId
221+
) public view override returns (bool) {
222+
return interfaceId == 0x00000001 || super.supportsInterface(interfaceId);
223+
}
224+
}
225+
226+
```
178227

179228
</Step>
180229

@@ -183,5 +232,3 @@ forge remappings > remappings.txt
183232
---
184233

185234
This guide will help you create a core contract that can increment a counter with optional callback functions for additional modular functionality.
186-
187-

0 commit comments

Comments
 (0)