[Youtube] Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course Lesson3

IT/Blockchain|2023. 3. 28. 21:44

Learn Blockchain, Solidity, and Full Stack Web3 Development with JavaScript – 32-Hour Course Lesson3

https://www.youtube.com/watch?v=gyMwXuJrbJQ 


The contract can deploy another contract and interact with that contract 

This lesson includes : 

importing / inheritance 

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;


contract InitialContract{
    function initialContract() public{
        //blach blach
        // contract content
    }
}

contract CallOtherContract{

    InitialContract public initialContract;


    function allOtherContract() public{

        initialContract = InitialContract(); // deploying simple storage contract by "new" keyword
    }
}

but better way :

 

use import 

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;


import "./SimpleStorage.sol";

contract StrageFactory{

    SimpleStorage public simpleStorage;

    function createSimpleStorageContract() public{

        simpleStorage = new SimpleStorage(); // deploying simple storage contract by "new" keyword
    }
}

 

 

Initializing address=>  0X0000... 

after you call the first function then 

you will have the address : 

 

StorageFactory = manager

SimpleStorage = all the works 

 

//SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;


import "./SimpleStorage.sol";

contract StrageFactory{

    SimpleStorage[] public simpleStorageArray;

    function createSimpleStorageContract() public{
        SimpleStorage simpleStorage = new SimpleStorage();
        simpleStorageArray.push(simpleStorage);

    }

    function sfStore(uint256 _simpleStorageIndex, uint256 _simpleStorageNumber) public{
        //to interact with contract you alway need
        // address of contract
        // ABI - Application Binary Interface
        // if you import on the top you dont need ABI 

        SimpleStorage simpleStorage = simpleStorageArray[_simpleStorageIndex];
        simpleStorage.store(_simpleStorageNumber); // Store the simple storage number to it 

    }

    function sfGet(uint256 _simpleStorageIndex) public view returns(uint256) {
        SimpleStorage simpleStorage= simpleStorageArray[_simpleStorageIndex];
        return simpleStorage.retrieve();
    } // read data from the simple storage
}

 

Inheritacne / override :

 

to use override you have to put "virtual" keyword in the original store function 

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.8;

import "./SimpleStorage.sol";


// inherit all the functionality from simple storage contract
contract EnxtraStorage is SimpleStorage{


    // change store function add +5 instead of +1
    // overriding 
    // virtual + override
    function store(uint256 _favoriteNumber) public override {
        favoriteNumber= _favoriteNumber + 5;
    }
}

 

 

 

반응형

댓글()