Zero to One Full-Stack DApp Ethereum Development based on Foundry, NextJS, Typescript - 4 Deploying the smart contract to a local chain

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.
image.png

This your_private_key is your deploy wallet private key. It can be found here.

image.png
image.png

We fill our value to the command, my command likes:

1
2
3
4
5
# on ./DApp-Demo
cd chain_end
forge create --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c src/Counter.sol:Counter --legacy
# or we can install https://github.com/stedolan/jq. Then we can format the output. It is option
forge create --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c src/Counter.sol:Counter --json --legacy | jq

Because ganchce runs an old version chain so we need to add --legacy.

We will get like this:

1
2
3
4
5
6
forge create --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c src/Counter.sol:Counter --json --legacy | jq
{
"deployedTo": "0x77D4DD041918061A42759fe1ECDe60fC67d28317",
"deployer": "0xDd7Eaf8eFF926aD81f5d4bBd68568CD1Fe4B1869",
"transactionHash": "0x41d17867b64fdca61bd6182ba6548b78b867d8d81530bed82d988646b3259d39"
}

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.

image.png

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:

  1. 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.

  2. 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 variable
cast 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
2
3
4
5
6
7
8
9
10
11
12
13
14
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;

contract Counter {
uint256 public number;

function setNumber(uint256 newNumber) public {
number = newNumber;
}

function increment() public {
number++;
}
}

So public_variable_name is number.

We could get this command

1
2
3
cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545

0x0000000000000000000000000000000000000000000000000000000000000000

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
2
3
function increment() public {
number++;
}

uses function_name(function_args_type)(function_return_type) method will convert to increment()

The setNumber function

1
2
3
function setNumber(uint256 newNumber) public {
number = newNumber;
}

uses function_name(function_args_type)(function_return_type) will convert to setNumber(uint256)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cast send 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "increment()" --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c --legacy --json | jq

{
"blockHash": "0x627958c966a6cae190d0dadd513cfdd5f53eaa048932394bd343cbb0fe3add00",
"blockNumber": "0x15",
"contractAddress": null,
"cumulativeGasUsed": "0xa478",
"from": "0xdd7eaf8eff926ad81f5d4bbd68568cd1fe4b1869",
"gasUsed": "0xa478",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1",
"to": "0x77D4DD041918061A42759fe1ECDe60fC67d28317",
"transactionHash": "0x9ee54c94adf35f3c9d4c18525266ff20e2210cd410cd3fa7d8ac682a4456b021",
"transactionIndex": "0x0"
}

Next, we will repeat multiple times to call our smart contract function.

1
2
3
cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545

0x0000000000000000000000000000000000000000000000000000000000000001
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cast send 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "increment()" --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c --legacy --json | jq

{
"blockHash": "0x6c3e9052c3e898083184c2409915a403dc73f17ee5ebed7d58b5a021ae38b8c2",
"blockNumber": "0x16",
"contractAddress": null,
"cumulativeGasUsed": "0x69e0",
"from": "0xdd7eaf8eff926ad81f5d4bbd68568cd1fe4b1869",
"gasUsed": "0x69e0",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1",
"to": "0x77D4DD041918061A42759fe1ECDe60fC67d28317",
"transactionHash": "0xcb506d3895e30c2cbcfb2f49063e1d598994499d980fb4eb7821d5edcabca3fa",
"transactionIndex": "0x0"
}
1
2
3
cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545

0x0000000000000000000000000000000000000000000000000000000000000002
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
cast send 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "setNumber(uint256)" 100 --rpc-url http://127.0.0.1:8545 --private-key 1430bfebd9c4cf85cd2c9ccb43b7b1f6e3aa3449cd98451ad23ac31f518c3f7c --legacy --json | jq

{
"blockHash": "0x5950ceaadc6177b0552396c208af2064d1b9272c796d8e4209252c8e048bbf83",
"blockNumber": "0x17",
"contractAddress": null,
"cumulativeGasUsed": "0x671a",
"from": "0xdd7eaf8eff926ad81f5d4bbd68568cd1fe4b1869",
"gasUsed": "0x671a",
"logs": [],
"logsBloom": "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
"status": "0x1",
"to": "0x77D4DD041918061A42759fe1ECDe60fC67d28317",
"transactionHash": "0x5acc3e10d0e7a92cd5117d77810f0ffe31c0ee8e3b831577d6d0c7733d36452c",
"transactionIndex": "0x0"
}
1
2
3
4
5
6
7
cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()" --rpc-url http://127.0.0.1:8545

0x0000000000000000000000000000000000000000000000000000000000000064

cast call 0x77D4DD041918061A42759fe1ECDe60fC67d28317 "number()(uint256)" --rpc-url http://127.0.0.1:8545

100

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

Deploying - Foundry Book

What is the difference between a transaction and a call?

Zero to One Full-Stack DApp Ethereum Development based on Foundry, NextJS, Typescript - 4 Deploying the smart contract to a local chain

https://iiiyu.com/2022/10/09/Zero-to-One-Full-Stack-DApp-Ethereum-Development-based-on-Foundry-NextJS-Typescript-4-Deploying-the-smart-contract-to-a-local-chain/

Author

Ewan Xiao

Posted on

October 9th 2022

Updated on

May 17th 2024

Licensed under

Comments