Create Polygon Token With Metadata
Step 1: Set up your environment
- Install the
polygon.py
library using pip:pip install polygon.py
- Create an account on the Polygon Dashboard and obtain API keys for the main chain.
Step 2: Prepare your smart contract code
Create a new file called Token.sol
with the following code:
// SPDX-License-Identifier: MIT pragma solidity ^0.8.4; import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; import "@openzeppelin/contracts/utils/math/SafeMath.sol"; contract MyToken is ERC20 { using SafeMath for uint256; // Token metadata string public description; string public image_url; constructor() ERC20("My Token", "MTK") { _mint(msg.sender, 100000000 * 10 ** decimals()); } // Function to mint new tokens function mint(address recipient, uint256 amount) external returns (bool success) { require(recipient != address(0), "Recipient cannot be zero"); require(amount > 0, "Amount must be greater than zero"); _mint(recipient, amount); return true; } // Function to burn tokens function burn(uint256 amount) external { require(amount > 0, "Amount must be greater than zero"); _burn(msg.sender, amount); } // Function to update metadata function updateMetadata(string memory _name, string memory _symbol) external { // Update name and symbol (if needed) // Note: ERC20 does not allow changing name and symbol after deployment } // Function to get token balance for an account function getBalance(address userAddress) public view returns (uint256) { return balanceOf(userAddress); } }
This contract code defines a basic fungible ERC20 token with dynamic metadata and allows minting and burning tokens. It also includes functions to update the token’s name and symbol.
Step 3: Deploy the smart contract on Polygon main chain
Use the polygon.py
library to deploy your contract:
import polygon # Set up API keys for Polygon Dashboard api_key = "YOUR_API_KEY" api_secret = "YOUR_API_SECRET" # Create a client instance with the provided credentials client = polygon.Client(api_key, api_secret) # Deploy the contract contract_address = client.deploy_contract("Token.sol", { 'name': 'My Token', 'symbol': 'MTK' }) print(f"Contract deployed at: {contract_address}")
This script deploys your contract on the Polygon main chain and prints out its address.
Step 4: Add liquidity to exchanges
You can now add liquidity to various decentralized exchanges (DEXs) such as Uniswap, SushiSwap, or Quickswap. You’ll need to create a pool for each DEX you want to support by providing the necessary assets (e.g., USDT and WMATIC).
Step 5: Burn tokens
You can burn your own tokens using the burn
function in the contract:
function burn(uint256 amount) external { require(amount > 0, "Amount must be greater than zero"); _burn(msg.sender, amount); }
Make sure to call this function from your contract’s burn
method.
Example usage:
// Burns 10 million tokens from the msg.sender's account contract.burn(10000000 * 10 ** decimals());
Note: Burning your own tokens will reduce their total supply and might have implications on the token price.