Deploy Your Smart Contract to Anvil

Noah Hein
InstructorNoah Hein

Share this video with your friends

Send Tweet
Published 6 months ago
Updated 4 months ago

We deploy our smart contract to the local Anvil server running in the background on our machine. After we deploy the contract, we will look over some of the logging that Anvil produced for us.

Noah Hein: [0:00] To deploy our smart contract, we are going to use Forge. Forge will deploy our contract on to our Anvil server that's running in the background. [0:09] To do that, we're going to do forge c. C is shorthand for create. Then we're going to need to pass in the private key that we copied over here, so make sure you have this copied. You can do forge c, and you will pass in the path to your contract. We'll do src/Contract.sol and then a colon and the name of the contract that you want to deploy.

[0:40] You can only deploy one contract at a time using forge create. If you had multiple smart contracts within your Solidity file, you need to suffix it with this colon and then the name of the contract.

[0:55] After that, you need to give it a private key. This private key will be signing the traction, that is deploying this contract up to our chain. We'll be paying all of the gas fees that are associated with that.

[1:09] The other thing that we'll need to do is since our contract has a constructor, here you can see that we've got this constructor. That is not optional. We do need to pass that. That's another flag. We'll say -constructor-args and we'll pass 10 as the value.

[1:32] Awesome. You can see that we successfully deployed our contract. You can see that the deployer was the private key that we passed in. If you take a look here, you can see that our private key was f2ff80. Over here you can see that it is f2ff80. It's the same private key. That private key matches to this deployer, which is like the public key that matches our private key.

[2:14] You can see here that we used this to sign it, but it winds up being this b92266. It's what you see on chain. You can see a little bit of your private key and public key, and how they relate to each other there. Then, you can see that our contract was deployed to this address.

[2:35] It also gives us the transaction hash. You can see all this happening over here on Anvil. You can see that when we originally booted it up. There it is. You can see it runs a bunch of different commands here. Then it finally runs an eth_sendRawTransaction. You can see this transaction matches the same hash over here. You see it ends in 27b. Here you can see it ends in 27b as well.

[3:02] This is the contract that was created along with the block hash and everything. You can see that it is now successfully deployed up to the chain. We can begin interacting with it.