Skip to content

Commit cc6ba65

Browse files
fix: splitting large code in multiple files for nft tutorial
1 parent 833236b commit cc6ba65

File tree

9 files changed

+1676
-1662
lines changed

9 files changed

+1676
-1662
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
2+
For this NFT contract, you don't need to write a separate contract. Instead, you'll use an already deployed Multi-Token Contract with the following functions.
3+
4+
### 4.1 Creating an NFT Collection on MainChain
5+
6+
Open your terminal and run:
7+
8+
```bash
9+
aelf-command send JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io Create
10+
```
11+
12+
- Replace the placeholder values with your actual details.
13+
14+
:::tip
15+
ℹ️ Note: `JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE` is the contract address of the Multi-Token Contract on **aelf Testnet Mainchain**.
16+
:::
17+
18+
### 4.2 Validate TokenInfoExist on MainChain
19+
20+
```bash
21+
aelf-command send JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io ValidateTokenInfoExists
22+
```
23+
24+
:::tip
25+
ℹ️ Note: `transactionId` Note down the trasnactionId from the above transaction will be used in **step 3**.
26+
:::
27+
28+
### 4.3 Create NFT Collection on dAppChain
29+
30+
:::tip
31+
ℹ️ Note: This step cannot be executed via the command line interface (CLI).
32+
:::
33+
34+
```bash
35+
aelf-command send ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io CrossChainCreateToken
36+
```
37+
38+
```bash
39+
transactionBytes = Buffer.from(signedTx, "hex").toString("base64")
40+
```
41+
42+
:::tip
43+
ℹ️ Note: Replace the placeholder values with your actual details.
44+
:::
45+
46+
:::tip
47+
ℹ️ Note: `ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx` is the contract address of the Multi-Token Contract on **aelf Testnet dAppChain**.
48+
:::
49+
50+
### 4.4 Create NFT Token on MainChain
51+
52+
```bash
53+
aelf-command send JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io Create
54+
```
55+
56+
### 4.5 Validate Nft Token on MainChain
57+
58+
```bash
59+
aelf-command send JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io ValidateTokenInfoExists
60+
```
61+
62+
### 4.6 Create NFT Token on dAppChain
63+
64+
```bash
65+
aelf-command send ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io CrossChainCreateToken
66+
```
67+
68+
```bash
69+
transactionBytes = Buffer.from(signedTx, "hex").toString("base64")
70+
```
71+
72+
### 4.7 Issue NFT Token on dAppChain
73+
74+
```bash
75+
aelf-command send ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io Issue
76+
```
77+
78+
### 4.8 Transfer NFT
79+
80+
```bash
81+
aelf-command send ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx -a $WALLET_ADDRESS -p $WALLET_PASSWORD -e https://aelf-test-node.aelf.io Transfer
82+
```
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
## 🎯 Conclusion
2+
3+
🎊 Congratulations on successfully setting up your development environment and interacting with your deployed smart contract! 🎊 You've laid a strong foundation for creating innovative applications on the aelf blockchain. 🌟
4+
5+
**📚 What You've Learned**
6+
7+
Throughout this section, you've acquired essential skills in:
8+
9+
- **🛠️ Setting Up Your Development Environment**: You installed necessary tools like the .NET SDK, aelf contract templates, and the aelf deploy tool to prepare for smart contract development.
10+
11+
- **📦 Installing Node.js, Yarn, and aelf-command**: These tools enable efficient interaction with the aelf blockchain, facilitating wallet creation and transaction management.
12+
13+
- **💡 Getting NFT Seed**: You learned how to obtain an NFT seed from the NFT Faucet, a critical step for creating NFT collections.
14+
15+
- **🔧 Configuring Frontend Integration**: You cloned a frontend project and configured it to connect with your smart contract, allowing for seamless user interaction with your dApp.
16+
17+
**🔍 Final Output**
18+
19+
By now, you should have:
20+
21+
- 📜 Successfully set up your development environment and installed all required packages.
22+
23+
- 💻 Configured your frontend to interact with the NFT smart contract, enabling functionalities like creating and transferring NFTs.
24+
25+
**➡️ What's Next?**
26+
27+
With a solid understanding of environment setup and contract interaction, you're ready to explore more advanced aspects of blockchain development. Consider delving into:
28+
29+
- **📊 Advanced Smart Contract Logic**: Enhance your contracts with complex features and security measures.
30+
31+
- **🔒 Security Protocols**: Implement robust security protocols to safeguard your applications and smart contracts.
32+
33+
- **🌐 Cross-Chain Interoperability**: Explore how aelf facilitates communication between different blockchains, broadening your development capabilities.
34+
35+
Keep pushing the boundaries of blockchain technology with aelf. Your journey is just beginning, and the potential for innovation in decentralized applications is vast. 🚀
36+
37+
Happy coding and building on the aelf blockchain! 😊
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
### Configure Portkey Provider & Write Connect Wallet Function
2+
3+
We'll set up our Portkey provider to allow users to connect their Portkey wallets to our app and interact with the aelf smart contracts. We'll be interacting with the already deployed multi-token contract for this tutorial.
4+
5+
**Step 1. Locate the File:**
6+
7+
- Go to the `src/hooks/useNFTSmartContract.ts` file.
8+
9+
**Step 2. Fetch the Smart Contract:**
10+
11+
- Find the comment `//Step A - Function to fetch a smart contract based on the chain symbol and the contract address.`
12+
13+
- Replace the existing **`fetchContract`** function with this updated code:
14+
15+
```javascript title="useNFTSmartContract.ts"
16+
//Step A - Function to fetch a smart contract based on the chain symbol and the contract address
17+
const fetchContract = async (
18+
symbol: "AELF" | "tDVW",
19+
contractAddress: string
20+
) => {
21+
try {
22+
// If no provider is available, return null
23+
if (!provider) return null;
24+
25+
// Fetch the chain information using the provider
26+
const chain = await provider.getChain(symbol);
27+
if (!chain) throw new Error("Chain not found");
28+
29+
// Get the smart contract instance from the chain
30+
const contract = chain.getContract(contractAddress);
31+
32+
// Return the smart contract instance
33+
return contract;
34+
} catch (error) {
35+
console.error("Error in fetchContract", { symbol, contractAddress, error });
36+
}
37+
};
38+
```
39+
40+
**Explanation:**
41+
42+
- **`fetchContract`** **Function**: This function fetches a smart contract based on the given chain symbol (e.g., "AELF" or "tDVW") and the contract address.
43+
44+
- **Check Provider** : If no provider is available, the function returns null.
45+
- **Fetch Chain** : The function fetches chain information using the provider.
46+
- **Get Contract** : It retrieves the smart contract instance from the chain.
47+
- **Error Handling** : If an error occurs, it logs the error to the console.
48+
49+
**Step 3. Initialize and Fetch the Smart Contracts:**
50+
51+
- Find the comment `// Step B - Effect hook to initialize and fetch the smart contracts when the provider changes.`
52+
53+
- Replace the existing **`useEffect`** hook with this updated code:
54+
55+
```javascript title="useNFTSmartContract.ts"
56+
// Step B - Effect hook to initialize and fetch the smart contracts when the provider changes
57+
useEffect(() => {
58+
(async () => {
59+
// Fetch the MainChain Testnet Contract
60+
const mainChainContract = await fetchContract(
61+
"AELF",
62+
"JRmBduh4nXWi1aXgdUsj5gJrzeZb2LxmrAbf7W99faZSvoAaE"
63+
);
64+
setMainChainSmartContract(mainChainContract as IContract);
65+
66+
// Fetch the dAppChain Testnet Contract
67+
const sideChainContract = await fetchContract(
68+
"tDVW",
69+
"ASh2Wt7nSEmYqnGxPPzp4pnVDU4uhj1XW9Se5VeZcX2UDdyjx"
70+
);
71+
setSideChainSmartContract(sideChainContract as IContract);
72+
})();
73+
}, [provider]); // Dependency array ensures this runs when the provider changes
74+
```
75+
76+
**Explanation:**
77+
78+
- **`useEffect`** **Hook** : This hook initializes and fetches the smart contracts when the provider changes.
79+
80+
- **Check Provider** : If no provider is available, the function returns null.
81+
- **Fetch Contracts** : It fetches and sets the smart contracts for the main chain, side chain, and cross chain.
82+
- **MainChain Contract** : Fetches the MainChain Testnet Contract and sets it in the state.
83+
- **dAppChain Contract** : Fetches the dAppChain Testnet Contract and sets it in the state.
84+
85+
By following these steps, we'll configure the Portkey provider to connect users' wallets to your app and interact with the multi-token smart contract including NFT related functionalities. This setup will enable our frontend components to perform actions like `create NFTs`, `validate NFTs`, and `transfer NFTs`.
86+
87+
### Configure Connect Wallet Function
88+
89+
**Step 1: Locate the File**
90+
91+
- Go to the `src/components/layout/header/index.tsx` file.
92+
93+
**Step 2: Write the Connect Wallet Function**
94+
95+
- The `header/index.tsx` file is the header of our NFT dApp. It allows users to connect their Portkey wallet with the NFT dApp.
96+
97+
- Before users can interact with the smart contract, we need to write the `Connect Wallet` function.
98+
99+
- Find the comment `// Step C - Connect Portkey Wallet`.
100+
101+
- Replace the existing connect function with this code snippet:
102+
103+
```javascript title="header/index.tsx"
104+
const connect = async (walletProvider?: IPortkeyProvider) => {
105+
// Step C - Connect Portkey Wallet
106+
const accounts = await (walletProvider ? walletProvider : provider)?.request({
107+
method: MethodsBase.REQUEST_ACCOUNTS,
108+
});
109+
const account = accounts?.AELF && accounts?.AELF[0];
110+
if (account) {
111+
setCurrentWalletAddress(account.replace(/^ELF_/, "").replace(/_AELF$/, ""));
112+
setIsConnected(true);
113+
}
114+
!walletProvider && toast.success("Successfully connected");
115+
};
116+
```
117+
118+
**Explanation:**
119+
120+
- **`connect`** **Function** : This function connects the user's Portkey wallet with the dApp.
121+
122+
- **Fetch Accounts** : It fetches the wallet accounts using the provider.
123+
- **Log Accounts** : Logs the accounts to the console for debugging.
124+
- **Set Wallet Address** : Sets the current wallet address state variable with the fetched account.
125+
- **Update Connection Status** : Updates the state to indicate that the wallet is connected.
126+
- **User Notification** : Displays an alert to notify the user that their wallet is successfully connected.
127+
128+
In this code, we fetch the Portkey wallet account using the provider and update the wallet address state variable. An alert notifies the user that their wallet is successfully connected.
129+
130+
With the Connect Wallet function defined, we're ready to write the remaining functions in the next steps.

0 commit comments

Comments
 (0)