Deploy Your First Smart Contract
Let's deploy a simple ERC-721 NFT contract for an in-game asset.
Install OpenZeppelin Contracts
OpenZeppelin provides secure, community-audited smart contract templates. It's a best practice to use them.
Bash
npm install @openzeppelin/contractsConfigure Hardhat for Wall Street Chain
Open the hardhat.config.ts file. You need to tell Hardhat how to connect to our Testnet. You'll also need to store your MetaMask private key securely.
First, install the dotenv package to manage your private key:
npm install dotenvCreate a file named .env in your project's root directory. IMPORTANT: Add .env to your .gitignore file to prevent accidentally sharing your key!
Inside your .env file, add your private key:
PRIVATE_KEY="YOUR_METAMASK_PRIVATE_KEY"
Now, update your hardhat.config.ts to look like this:
TypeScript
import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";
import "dotenv/config"; // Make sure to import this
// Get the private key from the .env file
const PRIVATE_KEY = process.env.PRIVATE_KEY || "";
const config: HardhatUserConfig = {
solidity: "0.8.24",
networks: {
wallStreetChainTestnet: {
url: "https://test.rpc",
chainId: TBA,
accounts: [PRIVATE_KEY],
},
},
};
export default config;Write the Smart Contract
Delete the sample Lock.sol file in the contracts/ directory. Create a new file named DegenArmor.sol and add the following code:
Solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts/access/Ownable.sol";
contract DegenArmor is ERC721, Ownable {
uint256 public nextTokenId;
constructor() ERC721("Degen Armor", "DARMOR") Ownable(msg.sender) {}
function mint(address to) public onlyOwner {
uint256 tokenId = nextTokenId++;
_safeMint(to, tokenId);
}
}Create the Deployment Script
In the scripts/ folder, rename the deploy.ts file (or create a new one) and update it with the following code to deploy your DegenArmor contract:
TypeScript
import { ethers } from "hardhat";
async function main() {
const degenArmor = await ethers.deployContract("DegenArmor");
await degenArmor.waitForDeployment();
console.log(`DegenArmor NFT deployed to: ${degenArmor.target}`);
}
main().catch((error) => {
console.error(error);
process.exitCode = 1;
});Compile and Deploy! ✅
You're ready. Run the following commands in your terminal:
First, compile your contract:
npx hardhat compileNow, run your deployment script on the WSC Testnet:
npx hardhat run scripts/deploy.ts --network wallStreetChainTestnetIf everything is configured correctly, your terminal will output the contract address where your DegenArmor NFT was deployed.
Congratulations! Your first smart contract is live on the WSC Testnet. 🎉
You can now look up the contract address on the Testnet Explorer to see it.
Last updated