cyfuer

Jun 24, 2022

学习笔记:《Full Stack Blockchain Lesson 3:Remix Storage Factory》

 

Getting Started

  1. Go to Remix
  1. Paste the code from SimpleStorage.solStorageFactory.sol, and ExtraStorage.sol into a new file in Remix
  1. Hit Compile
  1. Hit Deploy for whatever contract you want.
 
SimpleStorage.sol
// SPDX-License-Identifier: MIT pragma solidity 0.8.8; // pragma solidity ^0.8.0; // pragma solidity >=0.8.0 <0.9.0; contract SimpleStorage { uint256 favoriteNumber; struct People { uint256 favoriteNumber; string name; } // uint256[] public anArray; People[] public people; mapping(string => uint256) public nameToFavoriteNumber; function store(uint256 _favoriteNumber) public virtual { favoriteNumber = _favoriteNumber; } function retrieve() public view returns (uint256){ return favoriteNumber; } function addPerson(string memory _name, uint256 _favoriteNumber) public { people.push(People(_favoriteNumber, _name)); nameToFavoriteNumber[_name] = _favoriteNumber; } }
 
 
StorageFactory.sol
pragma solidity ^0.8.0; import "./SimpleStorage.sol"; contract StorageFactory { SimpleStorage[] public simpleStorageArray; function createSimpleStorageContract() public { SimpleStorage simpleStorage = new SimpleStorage(); simpleStorageArray.push(simpleStorage); } function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public { // Address // ABI // SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).store(_simpleStorageNumber); simpleStorageArray[_simpleStorageIndex].store(_simpleStorageNumber); } function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) { // return SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).retrieve(); return simpleStorageArray[_simpleStorageIndex].retrieve(); } }
 
ExtraStorage.sol
pragma solidity 0.8.8; import "./SimpleStorage.sol"; contract ExtraStorage is SimpleStorage { function store(uint256 _favoriteNumber) public override { favoriteNumber = _favoriteNumber + 5; } }
 

Introduction

Factory Pattern

Basic Solidity: Importing Contracts into other Contracts

 

Composibility

The ability for contracts to seamlessly interact with each other is what s known as composability.
 

Solidity new keyword

SimpleStorage public simpleStorage = new simpleStorage();
 

Importing Code in solidity

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0; import "./SimpleStorage.sol"

Basic Solidity: Interacting with other Contracts

To interact, you always need: ABI + Address

function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public { // Address // ABI - Application Binary Interface simpleStorageArray[_simpleStorageIndex].store(_simpleStorageNumber); } function sfGet(uint256 _simpleStorageIndex) public view returns (uint256){ return simpleStorageArray[_simpleStorageIndex].retrieve(); }
 

ABI

notion image

Basic Solidity: Inheritance & Overrides

 

Inheritance

// SPDX-License-Identifier: MIT pragma solidity ^0.8.0 import "./SimpleStorage.sol" contract ExtraStorage is SimpleStorage { }
 

Override & Virtual Keyword

add virtual keyword to the store function that to be override in SimpleStorage.sol
function store(uint256 _favoriteNumber) public virtual { favoriteNumber = _favoriteNumber; }
 
add override to store function that override
function store(uint256 _favoriteNumber) public override { favoriteNumber = _favoriteNumber + 5; }

Copyright © 2025 cyfuer

logo