DEV Community

Ricards Taujenis
Ricards Taujenis

Posted on

How to Deploy Your First Smart Contract 📃

Image description

As blockchain is becoming more popular and in demand with the latest ETF releases so will blockchain technologies and what better way to start then to create your first smart contract?
We will use test Network more specifically Mumbai on Polygon Network. If you don't have any test tokens I have a therow guide listed bellow. 👇

https://dev.to/mozes721/a-guide-to-mumbai-goerli-testnet-faucet-tokens-1f8k

Setting up project

To start it off be sure to have some testnet tokens in your Network of choise. We will be using Hardhat but there is as well Truffle, Remix and so forth.

mkdir deploy_smart_contract
cd deploy_smart_contract
npm install --save-dev hardhat
// Create test with
npx hardhat

Then you will be promted options I choose JS, root and to include .gitignore.

Selecting Target Network and configs

As we are using Polygon that network should be selected but you can adjust as pleased.
Next go to your hardhat.config.js

`import { HardhatUserConfig } from "hardhat/config";
import "@nomicfoundation/hardhat-toolbox";

const dotenv = require('dotenv');
dotenv.config();

const metamask_private_key = "MM_Private_key";

const etherscanAPIKey = "EtherScanAPI"

const config: HardhatUserConfig = {
solidity: "0.8.24",
networks: {
mumbai: {
url: "https://polygon-mumbai.infura.io/v2/PolygonTestNetworkAPIKey",
accounts: [metamask_private_key],
},
},
etherscan: {
apiKey: etherscanAPIKey,
},
};

export default config;`

You will need Metamask private key a guide for it can be found here for Etherscan API here and lastly for Infura here.
Deployment
Be sure to have scripts/sample-scripts.js or any other sample. 
Now lets run 👇

npx hardhat run scripts/deploy.js - network mumbai
// the output will be your contract name
Lock deployed to: 0x7ED4cC56CD...ef7081165DCd647028847

One thing to note that you can not redeploy the contract twice!

Image description

In Ethereum, once a contract is deployed to the blockchain, it stays deployed at its assigned address permanently. You cannot redeploy the same contract to the same address. 
If you need to make changes to a contract and redeploy it, you would typically deploy a new instance of the contract to a different address.
Ones all set and done you should see artifacts/build-info directory where you have a 2351..ac.json that is ABI JSON file contains the interface definition of your Solidity contract.

Testing Contract

As we have used Polygon Network(Mumbai) we should go to https://mumbai.polygonscan.com/ and choose the right Network as in mainet you wont see your contract!

Image description

And that about wraps it up! You can as well test it if you please depending on your contract. This was a test run so no .env file included. Blockchain technology is ever expanding and now with integration of AI so there is always things to learn, but it's essential to do it in measaruable pace.

Top comments (0)