Skip to content

Commit 2398d1e

Browse files
committed
Update PoS - reorg
bring back smart contracts directory
1 parent afac95a commit 2398d1e

File tree

8 files changed

+1132
-0
lines changed

8 files changed

+1132
-0
lines changed

docs/pos/how-to/smart-contracts/alchemy.md

Lines changed: 398 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
!!! warning "Third-party content"
2+
3+
Polygon technical documentation may contain third-party content, including websites, products, and services, that are provided for informational purposes only.
4+
5+
Polygon Labs does not endorse, warrant, or make any representations regarding the accuracy, quality, reliability, or legality of any third-party websites, products, or services. If you decide to access any third-party content, you do so entirely at your own risk and subject to the terms and conditions of use for such websites. Polygon Labs reserves the right to withdraw such references and links without notice.
6+
7+
Polygon technical documentation serves as an industry public good and is made available under the [MIT License](https://opensource.org/license/mit/). In addition, please view the official [Polygon Labs Terms of Use](https://polygon.technology/terms-of-use).
8+
9+
[ChainIDE](https://chainide.com/) is a chain agnostic, cloud-based IDE for creating decentralized applications. It enhances development cycle through pre-configured plugins that save users' time and effort. This is a beginner guide on creating and deploying a simple ERC-721 smart contract on the Polygon Mumbai Testnet.
10+
11+
## Prerequisites
12+
13+
1. ChainIDE
14+
2. MetaMask
15+
3. Solidity
16+
17+
## What you will do
18+
19+
The following are general steps for deploying an ERC-721 smart contract:
20+
21+
1. Set up a wallet
22+
2. Write down an ERC-721 smart contract
23+
3. Compile an ERC-721 Smart Contract
24+
4. Deploy an ERC-721 Smart Contract
25+
5. Create a Flattened File using Flattener Library
26+
6. Verify a Smart Contract
27+
7. NFT Minting
28+
29+
## Setting up a Wallet
30+
31+
### Install MetaMask
32+
33+
A gas fee must be paid when deploying a smart contract on the blockchain or making a transaction to a deployed smart contract, and we must do so using a crypto wallet, such as MetaMask. To download MetaMask, click [here](https://metamask.io/).
34+
35+
### Adding the Mumbai testnet
36+
37+
After installing MetaMask, you need to add the Polygon Mumbai Testnet to MetaMask. In order to add Mumbai Testnet, check out the configuration guide for [MetaMask](https://docs.polygon.technology/docs/tools/wallets/metamask/config-polygon-on-metamask).
38+
39+
<div style={{ display: 'flex', justifyContent: 'center', alignItems: 'center' }}>
40+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image.png" />
41+
</div>
42+
43+
### Obtaining MATIC tokens on Testnet
44+
45+
After adding the Mumbai testnet on MetaMask, visit the [Polygon Faucet](https://faucet.polygon.technology/) to request testnet tokens. These tokens are required to pay gas fees in order to deploy and interact with the smart contract. On the faucet page, select **Mumbai** as the network, **MATIC** as the token, and paste your MetaMask wallet address. Then, click **Submit** and the faucet will send you some test MATIC within a minute.
46+
47+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/Polygon_PR_get_tokens.png" width="100%" height="100%" />
48+
49+
## Write an ERC-721 Smart Contract
50+
51+
You need to write down all the required functions that you want to implement in your ERC-721 smart contract. A general ERC-721 smart contract has the following functions:
52+
53+
- `balanceOf()`: returns the number of NFTs held by the owner.
54+
- `ownerOf()`: returns the address of the token holder.
55+
- `approve()`: approves the permission to transfer tokens on the owner’s behalf. Approval event needs to be triggered after the method is successful.
56+
- `setApprovalForAll()`: Sets the approval of a given operator, and the `approvalforall` event needs to be triggered after success.
57+
- `getApproved()` : Get the approved address for a single NFT.
58+
- `isApprovedForAll()`: Query if an address is an authorized operator for another address.
59+
- `safeTransferFrom()`: To transfer the ownership of an NFT, a successful transfer operation must initiate a transfer event.
60+
- `transferFrom()`: Used to transfer NFTs. After the method succeeds, it needs to trigger the transfer event. The caller confirms he can receive NFT normally, otherwise, this NFT will be lost. When this function is implemented, it needs to check whether it meets the judgment conditions.
61+
62+
The ChainIDE team has prepared a complete ERC-721 template contract that includes all the required functions; you may use this built-in template and add/delete functions according to your requirements.
63+
64+
Visit the [ChainIDE site](https://chainide.com/) and click on **Try Now**.
65+
66+
<img src="https://3869740696-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-MYy-lqJKjq1m0yBAX4r%2Fuploads%2Fnpdf7fg51675wYmFcL6b%2Fimage.png?alt=media&token=353fc876-a319-49cb-92d5-1ed23c39aa90" width="100%" height="100%" />
67+
68+
Then, click on **New Project** and select **Polygon**. You will be presented with a list of available public templates. Select **ERC721 Showcase**.
69+
70+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/Using+ChainIDE+polygon/select+polygon+showcase.png" width="100%" height="100%" />
71+
72+
Now, you can see the template contract, `Creature.sol`, that includes all the required functions.
73+
74+
After creating the project, click on the **Unconnected** button in the upper right corner, select the **Injected Web3 Provider** button, and then click on MetaMask to connect your wallet (Polygon Mainnet is the main network, and Mumbai is the test network - connect to Mumbai).
75+
76+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/Using+ChainIDE+polygon/connect+mumbai.png" width="100%" height="100%" />
77+
78+
## Compile an ERC-721 Smart Contract
79+
80+
After you have completed your smart contract, it is time to compile it. To compile, navigate to "compile the module", choose an appropriate compiler according to your source code, and press the "compile" button. An ABI and bytecode for the source code are generated upon successful compilation. If there are any errors in your source code, they will be displayed in the "Logger module" under the output panel. You may need to carefully read the error, resolve it, and recompile the contract.
81+
82+
!!! note
83+
84+
Note down the **compiler version** and the **license** for your source code. It would be needed when you **Verify** your smart contract on the **Polygon Mumbai Testnet**.
85+
86+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(1).png" width="100%" height="100%" />
87+
88+
## Deploy an ERC-721 Smart Contract
89+
90+
After successful compilation, it's time to deploy your compiled ERC-721 smart contract to the Polygon Mumbai Testnet. Before you deploy, you need to have MetaMask installed, the Mumbai test network added to your wallet, and some testnet MATIC tokens to pay for the transaction fees.
91+
92+
Navigate to the **Deploy & Interaction** module and choose among the compiled smart contract. Select the smart contract you want to deploy and click the **Deploy** button. For this tutorial, the `GameItem` smart contract will be deployed.
93+
94+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(2).png" width="100%" height="100%" />
95+
96+
### Check Functions from the Deployed Contract
97+
98+
After successful deployment, an output message should state that your smart contract was deployed successfully. You can now verify the deployed contract. All the functions in the deployed smart contract can be seen in the **INTERACT** panel.
99+
100+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(3).png" width="100%" height="100%" />
101+
102+
## Create a Flattened File using Flattener Library
103+
104+
To verify a smart contract that imports other smart contracts, we need to create a flattened file. A flattened file includes all the source codes of imported contracts in a single file. To create a flattened file, you need to add a **Flattener** plug-in.
105+
106+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(7).png" width="100%" height="100%" />
107+
108+
Once the **Flattener** plug-in is activated, you'll be able to access it as a separate module as shown in the figure below. Choose the compiled file, and click on the **Flatten** button to create a flattened file. Once the flattened file is created, it will be automatically copied to the clipboard. You may paste it into a file and save it for later usage.
109+
110+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(8).png" width="100%" height="100%" />
111+
112+
If you want to save the flattened file, click the **Save** button, and a flattened file will be saved in the current repository.
113+
114+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(9).png" width="100%" height="100%" />
115+
116+
The saved flattened file can be accessed under the explorer module.
117+
118+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(10).png" width="100%" height="100%" />
119+
120+
## Verify a Smart Contract
121+
122+
To verify a smart contract, you need to visit [Mumbai Polygonscan](https://mumbai.polygonscan.com/), and search for the deployed smart contract using the contract address.
123+
124+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(11).png" width="100%" height="100%" />
125+
126+
Click on the **Verify and Publish** link shown under the contract section.
127+
128+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(12).png" width="100%" height="100%" />
129+
130+
Once you click on the verify and publish link, you will be asked for the following:
131+
132+
- Contract Address: The address of a deployed smart contract that you want to verify
133+
- Compiler Type: Either you want to verify a single file or multiple files
134+
- Compiler Version: The compiler version that you used to compile the smart contract
135+
- License: Open-source license type that you used for your source code
136+
137+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(13).png" width="100%" height="100%" />
138+
139+
After that, you need to paste the flattened file that you created in step 5, and your smart contract will be verified.
140+
141+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(14).png" width="100%" height="100%" />
142+
143+
If there are no issues with your smart contract, it would be verified, and you'll be able to see an image similar to the one that is shown below.
144+
145+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(15).png" width="100%" height="100%" />
146+
147+
## Minting NFT
148+
149+
To mint an NFT, you need to use the **Award Item** function, the wallet address of someone to whom you want to award an NFT, and the link of the photo uploaded to IPFS to be pasted in the token URL input field.
150+
151+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(4).png" width="100%" height="100%" />
152+
153+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(5).png" width="100%" height="100%" />
154+
155+
After successful minting, you can check the minted NFT on the OpenSea NFT marketplace. Visit [OpenSea Testnet](https://testnets.opensea.io/), connect your MetaMask wallet and make sure the selected network is Polygon Mumbai Testnet, and you'll be able to see and trade the minted NFT on the OpenSea NFT marketplace.
156+
157+
**Congratulations! You have successfully minted an NFT on Polygon using ChainIDE.**
158+
159+
<img src="https://d3gvnlbntpm4ho.cloudfront.net/ERC+721+Deployment+on++Mumbai/image+(6).png" width="100%" height="100%" />
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
!!! warning "Third-party content"
2+
3+
Polygon technical documentation may contain third-party content, including websites, products, and services, that are provided for informational purposes only.
4+
5+
Polygon Labs does not endorse, warrant, or make any representations regarding the accuracy, quality, reliability, or legality of any third-party websites, products, or services. If you decide to access any third-party content, you do so entirely at your own risk and subject to the terms and conditions of use for such websites. Polygon Labs reserves the right to withdraw such references and links without notice.
6+
7+
Polygon technical documentation serves as an industry public good and is made available under the [MIT License](https://opensource.org/license/mit/). In addition, please view the official [Polygon Labs Terms of Use](https://polygon.technology/terms-of-use).
8+
9+
This section guides you through deploying a Hello World contract using [Chainstack](https://chainstack.com/build-better-with-polygon/) and [Foundry](https://github.com/gakonst/foundry/) on the Polygon Mumbai testnet.
10+
11+
Chainstack provides infrastructure for Ethereum-based applications and other blockchains. They maintain nodes and guarantee their connection to the network and also offer an interface to interact with mainnet and testnets.
12+
13+
Foundry is a fast toolkit for Ethereum application development written in Rust. It provides testing, interaction with EVM smart contracts, sending transactions, and blockchain data retrieval.
14+
15+
!!! tip
16+
17+
If you have any questions, reach out in the [Chainstack Discord](https://discord.com/invite/Cymtg2f7pX) server.
18+
19+
## What you will learn
20+
21+
Create a Hello World contract, using Chainstack to deploy a Polygon node and Foundry to deploy the contract.
22+
23+
## What you will do
24+
25+
1. Deploy a Polygon node using Chainstack
26+
2. Set up Foundry
27+
3. Create the smart contract
28+
4. Deploy the smart contract.
29+
30+
## Deploy a Polygon Mumbai Node
31+
32+
You need a node to deploy a smart contract to the blockchain network. Follow the steps below to get your node up and running:
33+
34+
**Step 1 &rarr;** Sign up with [Chainstack](https://console.chainstack.com/user/account/create)
35+
36+
![img](../../../img/pos/sign-up.png)
37+
38+
**Step 2 &rarr;** Follow the instructions on how to [deploy a Mumbai node](https://docs.chainstack.com/platform/join-a-public-network#join-a-polygon-pos-network)
39+
40+
![img](../../../img/pos/join-network.png)
41+
42+
**Step 3 &rarr;** Get the [deployed node’s HTTPS endpoint](https://docs.chainstack.com/platform/view-node-access-and-credentials)
43+
44+
## Install Foundry
45+
46+
Foundry is a development toolkit to work with smart contracts. To begin working with it, you need to install the Rust coding language first.
47+
48+
1. [Install Rust](https://www.rust-lang.org/tools/install).
49+
1. [Install Foundry](https://github.com/gakonst/foundry/).
50+
51+
## Initialize with Foundry
52+
53+
To create a boilerplate project, navigate to your working directory and run:
54+
55+
```
56+
forge init PROJECT_NAME
57+
// PROJECT_NAME - name of project
58+
```
59+
60+
## Fund Your Account
61+
62+
You will need a wallet account to deploy the smart contract. You can use [Metamask](https://metamask.io/) for that. You also need to pay gas on the network to deploy the contract. Just copy your wallet address and get Mumbai MATIC token [through the faucet](https://faucet.polygon.technology/).
63+
64+
## Create the Hello World contract
65+
66+
In the initialized Foundry project in `src/`, create `HelloWorld.sol`:
67+
68+
```
69+
// SPDX-License-Identifier: None
70+
71+
// Specifies the version of Solidity, using semantic versioning.
72+
// Learn more: https://solidity.readthedocs.io/en/v0.5.10/layout-of-source-files.html#pragma
73+
pragma solidity >=0.8.9;
74+
75+
// Defines a contract named `HelloWorld`.
76+
// A contract is a collection of functions and data (its state). Once deployed, a contract resides at a specific address on the Ethereum blockchain. Learn more: https://solidity.readthedocs.io/en/v0.5.10/structure-of-a-contract.html
77+
contract HelloWorld {
78+
79+
//Emitted when update function is called
80+
//Smart contract events are a way for your contract to communicate that something happened on the blockchain to your app front-end, which can be 'listening' for certain events and take action when they happen.
81+
event UpdatedMessages(string oldStr, string newStr);
82+
83+
// Declares a state variable `message` of type `string`.
84+
// State variables are variables whose values are permanently stored in contract storage. The keyword `public` makes variables accessible from outside a contract and creates a function that other contracts or clients can call to access the value.
85+
string public message;
86+
87+
// Similar to many class-based object-oriented languages, a constructor is a special function that is only executed upon contract creation.
88+
// Constructors are used to initialize the contract's data. Learn more:https://solidity.readthedocs.io/en/v0.5.10/contracts.html#constructors
89+
constructor(string memory initMessage) {
90+
91+
// Accepts a string argument `initMessage` and sets the value into the contract's `message` storage variable).
92+
message = initMessage;
93+
}
94+
95+
// A public function that accepts a string argument and updates the `message` storage variable.
96+
function update(string memory newMessage) public {
97+
string memory oldMsg = message;
98+
message = newMessage;
99+
emit UpdatedMessages(oldMsg, newMessage);
100+
}
101+
}
102+
```
103+
104+
## Deploy the Contract
105+
106+
At this point, you are ready to deploy your contract:
107+
108+
* You have your own node on the Polygon Mumbai network through which you will deploy the contract.
109+
* You have Foundry that you will use to deploy the contract.
110+
* You have a funded account that will deploy the contract.
111+
112+
To deploy the contract, run:
113+
114+
```bash
115+
forge create HelloWorld --constructor-args "Hello" --contracts CONTRACT_PATH --private-key PRIVATE_KEY --rpc-url HTTPS_ENDPOINT
116+
```
117+
118+
Here,
119+
120+
* CONTRACT_PATH — path to your `HelloWorld.sol` file.
121+
* PRIVATE_KEY — the private key from your account.
122+
* HTTPS_ENDPOINT — [your node's endpoint](https://docs.chainstack.com/platform/view-node-access-and-credentials).
123+
124+
Example:
125+
126+
``` sh
127+
forge create HelloWorld --constructor-args "Hello" --contracts /root/foundry/src/HelloWorld.sol --private-key d8936f6eae35c73a14ea7c1aabb8d068e16889a7f516c8abc482ba4e1489f4cd --rpc-url https://nd-123-456-789.p2pify.com/3c6e0b8a9c15224a8228b9a98ca1531d
128+
```
129+
130+
!!! tip
131+
132+
You can always check the contract's deployment on [Mumbai Polygonscan](https://mumbai.polygonscan.com/) using the newly-generated hash from the last step.
133+
134+
## Test the Contract
135+
136+
There is a `forge test` command in case you need to check whether the contract is working fine. Foundry provides many [options](https://book.getfoundry.sh/reference/forge/forge-test) (flags) for more specific tests. Learn more about writing tests, advanced testing and other features at [Foundry's documentation](https://book.getfoundry.sh/forge/tests).
137+
138+
**Congratulations! You have deployed your Hello World smart contract on Polygon.**
139+
140+
See also Chainstack docs for more Polygon-related [tutorials](https://docs.chainstack.com/tutorials/polygon/) and [tools](https://docs.chainstack.com/operations/polygon/tools).

0 commit comments

Comments
 (0)