This project demonstrates how to create a mapping between ERC721 tokens and ERC20 tokens using Solidity and Hardhat. Each ERC721 token is associated with 1000 ERC20 tokens.
- Clone the repository:
git clone <repo-url>
cd <repo-name>
- Install the required dependencies:
npm install
- Create a new file
.env
and add your Alchemy API key and a private key of your Ethereum wallet (Do not share this with anyone):
API_URL="https://eth-mainnet.alchemyapi.io/v2/YOUR_ALCHEMY_KEY"
PRIVATE_KEY="YOUR_PRIVATE_KEY"
For test networks like Rinkeby, Kovan, etc., the API_URL
should be changed accordingly.
- Compile the smart contracts:
npx hardhat compile
Before running tests, start the local Hardhat network:
npx hardhat node
To run tests:
npx hardhat test
To deploy the contracts to the Mumbai testnet, run:
npx hardhat run scripts/deploy.js --network mumbai
Remember to update the hardhat.config.js
with the correct network configuration.
The smart contracts included in this project are:
ERC20Token.sol
: An ERC20 token contract.ERC721Token.sol
: An ERC721 token contract.TokenMapping.sol
: A contract that creates a mapping between ERC721 and ERC20 tokens.
The test suite covers the following scenarios:
- Creating a new token and its mapping correctly.
- Preventing the reuse of the same ERC721 token ID.
- Allowing only the contract owner to mint new tokens and create mappings.
- Allowing the owner to transfer ERC721 tokens to another address.
- Updating ERC20 balance upon ERC721 transfer.
- This project is for demonstration and learning purposes only. It does not include many features of a production-ready contract (like a proper access control mechanism, pausability, upgradability, etc.)
- Always remember to adequately test smart contracts and get them audited by professionals before using them in production.