1/5
_You can follow along with the video course from here._ ### Introduction In this lesson, the `StorageFactory` contract will be upgraded to _keep track_ of all deployed `SimpleStorage` contracts. This will allow us also to _interact_ with each deployed contract individually. ### Storing the deployed contracts In the current `StorageFactory` version, every time `createSimpleStorageContract` is called, a new `SimpleStorage` contract is deployed and **overridden** inside the variable `SimpleStorage`. Past deployments are not being tracked down. To solve this issue we can create a variable `listOfSimpleStorageContracts`, which is an array of `SimpleStorage` contracts. In this way, whenever a contract is created, it gets added to a dynamic array. ```solidity SimpleStorage[] public listOfSimpleStorageContracts; ``` We can then modify the function `createSimpleStorageContract`, pushing the newly deployed contract to this variable. ```solidity function createSimpleStorageContract() public { SimpleStorage simpleStorageContractVariable = new SimpleStorage(); listOfSimpleStorageContracts.push(simpleStorageContractVariable); } ``` In Remix, you can access `listOfSimpleStorageContracts` via the index of type `uint256`, which refers to the **position** of the deployed contract inside the dynamic array. ### Simple Storage interaction `StorageFactory` can interact with the deployed contracts by calling their `store` function. To do this we need to create a **function** `sfStore`: ```solidity function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public { //SimpleStorage store function will be called here } ``` > đâ**IMPORTANT**:br > Every time you have to interact with another contract, you need: > > 1. the contract **address** > 2. the contract **ABI (Application Binary Interface)**: a standardized way for interacting with the binary version of a smart contract deployed on the blockchain. It specifies the functions, their parameters, and the structure of the data that can be used to interact with the contract. It's generated by the compiler. > đď¸ **NOTE**:br > If you do not have the full ABI available, a function selector will suffice (see later in the course). If you go to Solidity's compile tab, you will find a button that lets you copy the ABI to the clipboard. ::image{src='/solidity/remix/lesson-3/interacting/interacting-contract1.png' style='width: 100%; height: auto;'} > đď¸ **NOTE**:br > In Solidity, it's possible to **type cast** an _address_ to a type _contract_ We can now proceed to store a new number on a `SimpleStorage` contract: ```solidity function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public { listOfSimpleStorageContracts[_simpleStorageIndex].store( _simpleStorageNumber); } ``` We can then retrieve the stored value with a _get_ function: ```solidity function sfGet(uint256 _simpleStorageIndex) public view returns (uint256) { // return SimpleStorage(address(simpleStorageArray[_simpleStorageIndex])).retrieve(); return listOfSimpleStorageContracts[_simpleStorageIndex].retrieve(); } ``` ### Conclusion The `StorageFactory` contract was able to create a list of `SimpleStorage` contracts, store a variable in each of them and read it back. ### đ§âđť Test yourself 1. đ What do you need to interact with an external contract? 2. đ§âđť Deploy 3 instances of the `SimpleStorage` contract through the `StorageFactory`. Then store some numbers via `sfStore` and retrieve all of them via `sfGet`.
This lesson teaches how to keep track of contract addresses when deploying new contracts using Solidity's 'new' keyword. It introduces the concept of ABI (Application Binary Interface) for contract interaction and demonstrates how to interact with contracts using ABI and address in Solidity.
Previous lesson
Previous
Next lesson
Next
Give us feedback
Course Overview
About the course
Blockchain developer fundamentals
Introduction to blockchain gas
Transaction signatures
Smart contract development
Solidity gas optimization techniques
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on November 25, 2024
Solidity Developer
Solidity Smart Contract DevelopmentDuration: 1h 46min
Duration: 38min
Duration: 2h
Duration: 23min
Course Overview
About the course
Blockchain developer fundamentals
Introduction to blockchain gas
Transaction signatures
Smart contract development
Solidity gas optimization techniques
Smart Contract Auditor
$100,000 - $200,000 (avg. salary)
Smart Contract Engineer
$100,000 - $150,000 (avg. salary)
Web3 Developer Relations
$85,000 - $125,000 (avg. salary)
Web3 developer
$60,000 - $150,000 (avg. salary)
Web3 engineer, educator, and Cyfrin co-founder. Patrick's smart contract development and security courses have helped hundreds of thousands of engineers kickstarting their careers into web3.
Last updated on November 25, 2024
Testimonials
Read what our students have to say about this course.
Chainlink
Chainlink
Gustavo Gonzalez
Solutions Engineer at OpenZeppelin
Francesco Andreoli
Lead Devrel at Metamask
Albert Hu
DeForm Founding Engineer
Radek
Senior Developer Advocate at Ceramic
Boidushya
WalletConnect
Idris
Developer Relations Engineer at Axelar