logoAcademy

Build Basic Smart Contract

Learn about Solidity

Let's create and interact with the basic smart contract. Create a new solidity file called NumberStorage.sol and fill it with the contract below:

// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

contract NumberStorage {
 
    uint256 num;
  
    function getNum() public view returns(uint) {
        return num; 
    }
  
    function setNum(uint _num) public {
        num = _num;
    }
  
}

Now let's deploy it:

forge create --rpc-url local-c --private-key $PK src/my-contracts/NumberStorage.sol:NumberStorage 

The result should look something like this:

[⠒] Compiling...
[⠢] Compiling 1 files with 0.8.18
[⠆] Solc 0.8.18 finished in 14.67ms
Compiler run successful!
Deployer: 0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
Deployed to: 0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25
Transaction hash: 0x5717e501e12f40d644c60030bc0ab9569ddb9f4cba968546ab597fb516eae09b

Next, let's store a number:

cast send --rpc-url local-c --private-key $PK 0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25 "setNum(uint)" 42

Since we are now writing to and not just reading from the blockchain we will see the transaction details:

blockHash               0x02eb13d317a43976ea8ba21a76e5deb6d02d257a0b98c1a84734e0609b8a6fec
blockNumber             3
contractAddress         
cumulativeGasUsed       43516
effectiveGasPrice       28000000000
from                    0x8db97C7cEcE249c2b98bDC0226Cc4C2A57BF52FC
gasUsed                 43516
logs                    []
logsBloom               0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000
root                    
status                  1
transactionHash         0xd382101a4955f05f8a96e4ab4b62457700697930dc6ed84246a346e51d41d3cb
transactionIndex        0
type                    2
to                      0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25

Next, let's get the number from the storage:

cast call --rpc-url local-c 0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25 "getNum()(uint)"

As a result we should get the number we have stored earlier:

@martineckardt /workspaces/avalanche-starter-kit (main) $ cast call --rpc-url local-c 0x17aB05351fC94a1a67Bf3f56DdbB941aE6c63E25 "getNum()(uint)"
42

On this page

No Headings