Skip to main content

Smart Contract Deployment

The code to deploy our smart contract is in scripts/deploy.js. It will deploy the smart contract to the specified network and mint the first token to the specified recipient address.

const hre = require("hardhat");

// Type your own PUBLIC key in here
const recipientAddress = ''

async function main() {
const DinoRunner = await hre.ethers.getContractFactory("DinoRunner");
const dinoRunner = await DinoRunner.deploy();

await dinoRunner.deployed();
console.log("Dino Runner contract deployed to:", dinoRunner.address);

const mintTokenTx = await dinoRunner.mint(recipientAddress);
await mintTokenTx.wait();
console.log(`Minted token to ${recipientAddress}`)
}

main()
.then(() => process.exit(0))
.catch((error) => {
console.error(error);
process.exit(1);
});

Make sure to input your own Ethereum public key in const recipientAddress before executing the code. You can easily copy this from MetaMask by clicking here.

deploy.js

// Type your own PUBLIC key in here
const recipientAddress = ''

Copy Address

When you execute the script with npx hardhat run scripts/deploy.js it will only deploy on the temporary hardhat local network. In order to deploy our smart contract to a real blockchain network or test network we need to set this up in our hardhat.config.json. We added hmvtest as one of the networks and you can add any other network you'd like here. Notice how the gasPrice is set to 0, since deploying a contract on an Oasys sub-verse doesn't require us to pay gas. If you want to deploy to most other ethereum test nets you'll first need to get funds from a fountain.

module.exports = {
solidity: "0.8.4",
defaultNetwork: "hardhat",
networks: {
hardhat: {
},
hmvtest: {
url: 'https://rpc.testnet.oasys.homeverse.games/',
chainId: 40875,
gasPrice: 0,
accounts: [
// Provide your private key here
// Remove comment out to use value from secrets.json
//PRIVATE_KEY
],
},
},
};

To be able to deploy to an actual test network we also need to provide a private key to use as the deployer. The default way of doing this with hardhat is requiring a file called secrets.json. We already prepared the code for this, however it's commented out to allow the app to run tests even without providing a secrets.json file. Stop commenting out the require part at the top and also the place where we use the Private key.

// Create secrets.json file with private key and remove comment out
const { PRIVATE_KEY } = require('./secrets.json');


accounts: [
// Provide your private key here
// Remove comment out to use value from secrets.json
PRIVATE_KEY
],

After that create a new file in the project root and call it secrets.json. In here you need to create a simple json object and pass your private key. YOU NEED TO BE VERY CAREFUL NOT TO EXPOSE YOUR PRIVATE KEY TO ANYBODY. While prototyping it's also recommended to just use a throwaway account and not your main wallet.

{
"PRIVATE_KEY": "PASTE YOUR PRIVATE KEY HERE"
}

Since secrets.json is mentioned in our .gitignore file it won't be pushed to the repository and only lives on your machine.

node_modules
.env
coverage
coverage.json
typechain

#Hardhat files
cache
artifacts

secrets.json

To use these settings when deploying we'll need to add the --network hmvtest flag.

npx hardhat run scripts/deploy.js --network hmvtest

When running the script you can see the address your contract got deployed to in the console. Make sure to copy and save this somewhere, since we'll need the contract address in the next chapter.

HMV Test