Zero to One Full-Stack DApp Ethereum Development based on Foundry, NextJS, Typescript - 4 Deploying the smart contract to a local chain
Deploying the smart contract to a local chain
We remember the Foundry includes forge
, cast
, and anvil
.
We will use forge
to deploy our smart contracts. Then we will try to call our smart contract on cast
Deploying
The first important thing is that we need to keep running the local chain on ganache
.
We will run like below command to deploy a smart contract
forge create --rpc-url <your_rpc_url> --private-key <your_private_key> src/MyContract.sol:MyContract
This your_rpc_url
can be found here.
This your_private_key
is your deploy wallet private key. It can be found here.
We fill our value to the command, my command likes:
1 | # on ./DApp-Demo |
Because ganchce
runs an old version chain so we need to add --legacy
.
We will get like this:
1 | forge create --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c src/Counter.sol:Counter --json --legacy | jq |
The deployedTo value 0x77D4DD041918061A42759fe1ECDe60fC67d28317
is the smart contract address.
Now, our smart contract already has been deployed successfully.
So easy! Right?
One more thing, because we use ganacha
, we can check the smart contract deploy information on GUI too.
cast
call our smart contract
We will use send
and call
.
Call
cast call
- Perform a call on an account without publishing a transaction. More Detail
It is Ethereum JSON-RPC’s eth_call
Transaction
cast send
- Sign and publish a transaction. More Detail
It is Ethereum JSON-RPC’s eth_sendTransaction
The difference between a call and a transaction is the following:
transactions are created by your client, signed, and broadcasted to the network. They will eventually alter the state of the blockchain, for example, by manipulating balances or values in smart contracts.
calls are transactions executed locally on the user’s local machine which alone evaluates the result. These are read-only and fast. They can’t change the blockchain in any way because they are never sent to the network. Some examples “read-only/dry run/practice”.
Use cast call
We will run like below command to get a smart contract’s a public variablecast call your_smart_contract_address "public_variable_name()" --rpc-url http://127.0.0.1:8545
First, we need to find the smart contract address 0x77D4DD041918061A42759fe1ECDe60fC67d28317
.
Second, our smart contract code is (chain_end/src/Counter.sol)
1 | // SPDX-License-Identifier: UNLICENSED |
So public_variable_name
is number
.
We could get this command
1 | cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545 |
Use cast send
We will run like below command to call a smart contract’s a function
cast send your_smart_contract_address "function_name(function_args_type)(function_return_type)" --rpc-url http://127.0.0.1:8545 --private-key your_private_key --legacy --json | jq
Based on before content, we know the your_smart_contract_address
and can find the your_private_key
.
The increment
function
1 | function increment() public { |
uses function_name(function_args_type)(function_return_type)
method will convert to increment()
The setNumber
function
1 | function setNumber(uint256 newNumber) public { |
uses function_name(function_args_type)(function_return_type)
will convert to setNumber(uint256)
1 | cast send 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "increment()" --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c --legacy --json | jq |
Next, we will repeat multiple times to call our smart contract function.
1 | cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545 |
1 | cast send 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "increment()" --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c --legacy --json | jq |
1 | cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545 |
1 | cast send 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "setNumber(uint256)" 100 --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c --legacy --json | jq |
1 | cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545 |
So far so good.
Now we can deploy the smart contract on the local chain and call its function successfully.
P.S.
This article is very subjective. If you do not feel comfortable viewing it, please close it as soon as possible.
If you think my article can help you, you can subscribe to this site by using RSS.
Referrals
Photo by GuerrillaBuzz Crypto PR on Unsplash
Zero to One Full-Stack DApp Ethereum Development based on Foundry, NextJS, Typescript - 4 Deploying the smart contract to a local chain