Skip to content

Code and explain every Topic in separate file #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions step03_solidity_tutorial/contracts/Abstract/Abstract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

/*
Abstract Contract is one which contains at least one function without any implementation.
Such a contract is used as a base contract. Generally an abstract contract contains both implemented as well as abstract functions.
Derived contract will implement the abstract function and use the existing functions as and when required.
In case, a derived contract is not implementing the abstract function then this derived contract will be marked as abstract.
*/
//IN Case we have Parent Contract with functionA which is not implimented in that contract but child contract will implement this function
// so for that we have to mark Parent Contract as abstract and make function virtual of Parent contract

abstract contract ContractA {
function getResult() public pure returns(uint) { return 50; }

function getData() public pure virtual returns(uint) ;
}


contract ContractB is ContractA {
function getData() public pure override returns(uint){
return 12;
}



}
contract ContractC {
uint value;
function checkfunctionA() public returns(uint256) {
//ContractA a = new ContractA();// we can't deploy abstract contract
ContractB b = new ContractB();
value = b.getData();

}
function ResutlCheckfunction() public view returns(uint256) {
return value;

}
}

46 changes: 46 additions & 0 deletions step03_solidity_tutorial/contracts/Array/Array.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";
// public private Variable Scope
contract String {




// 1. Dynamic arrays
// 2. Static arrays
// 3. Array arguments and return arrays from function

//1. Dynamic Array

uint[] public myArray; //Create, Read, Update, Delete
// This is dynamic size array here we are not specifing the array size
function storageArrays() public returns ( uint[] memory ) {
myArray.push(2); //add value in array
myArray.push(3);
myArray[0] = 20; // Updating array

delete myArray[1]; // delete will not delete the value of given index
return myArray;
}

//2. Static Array
function staticArray() public pure returns (uint[] memory) {
uint[] memory statArray = new uint[](10); //Create, Read, Update, Delete
// statArray.push(10)// we can't append value using push function in static type array
statArray[0] = 20; // add value in array
statArray[1]= 30;

statArray[0] = 40; //update array

delete statArray[1];
return statArray;
}
function getIndexValue(uint _index) public view returns( uint value){
// //get value of given index from array
value = myArray[_index];
return value;
}
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";
import "./requestDemo.sol";

contract CallBackDemo{
/*
// signature function
function testFunctionCall() public {
RequestDemo rc = new RequestDemo();
rc.receivedRequest(address(this),this.receivedData.selector);
}
function receivedData(uint256 _value) public {
console.log("CallerDemo:: receivedData: value = ",_value);
}
*/
function testNewFunctionCall() public {
RequestDemo rc = new RequestDemo();
rc.receivedFunctionRequest(this.newDataFunction);
}
function newDataFunction(uint256 _value) public view returns(uint256){
console.log("CallerDemo:: receivedData: value = ",_value);
return _value*2;
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract RequestDemo{
/*
// signature work
function receivedRequest(address _contractAddress , bytes4 callBackFunctionSignature ) public {
////////
console.log("RequestDemo :: receivedRequest Started");
(bool success, bytes memory data) = _contractAddress.call(abi.encodeWithSelector(callBackFunctionSignature,45));
console.log("RequestDemo :: receivedRequest after success", success);
}
*/
// function (uint256) external view returns(uint256) myCall;

function receivedFunctionRequest(function (uint256) external view returns(uint256) demoCall) public view {
// myCall = demoCall;
uint256 valueAfter = demoCall(23);
console.log("receivedFunctionRequest", valueAfter);
}

// function hello() public{
// myCall(12);
// }
}
18 changes: 18 additions & 0 deletions step03_solidity_tutorial/contracts/Constant/constant.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";
contract DemoConstant {
uint public constant AMOUNT = 45; // constant variable must need to be initialize
uint public immutable value; // if we initialized here so can't initialize later
// string public immutable name ; //we can't create immutable variable for other than value type
constructor(uint _value){
value = _value;

}

function updateAmount() public {
// AMOUNT = 23; // we cann't assign value in the constant variable
// value = _value; can't change immutable variable
}
}
61 changes: 61 additions & 0 deletions step03_solidity_tutorial/contracts/Constructor/constructor.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";


/*
Constructor is a special function declared using constructor keyword. It is an optional funtion and is used to initialize state variables of a contract. Following are the key characteristics of a constructor.
A contract can have only one constructor.
A constructor code is executed once when a contract is created and it is used to initialize contract state.
After a constructor code executed, the final code is deployed to blockchain. This code include public functions and code reachable through public functions. Constructor code or any internal method used only by constructor are not included in final code.
A constructor can be either public or internal.
A internal constructor marks the contract as abstract.
In case, no constructor is defined, a default constructor is present in the contract.

*/
//default constructor
// constructor() {
// }

contract ConstA {
//if we define constructor of just Child Contract this will work
//if we define contructor of parent Contract with argument we should define constructor of child contract otherwise solidity will not allow us
uint public value;
constructor(uint a){
value = a;

}

}


//Derived Contract
contract ConstB is ConstA {
//if we don't define constructor so the default constructor is this
// constructor() ContractA() {

// }
//so we have to pass the argument
// constructor() ContractA(2){

// }

constructor(uint b) ConstA(b ) {

}
function abc () public view returns(uint) {
return value;
}


}
contract ConstC {
function checkfunctionA(uint _value) public returns(uint) {
// ConstB b = new ConstB();
ConstB b = new ConstB(_value);
return b.abc();
}

}

44 changes: 44 additions & 0 deletions step03_solidity_tutorial/contracts/Demo Contract/Demo1.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";

contract Demo1 {
uint256 public balance;
string public name;

constructor(string memory _name) {
name = _name;
}

function getName() public view returns (string memory ) {
return name;
}

}

contract Demo2 {
address public demo1Address;//we create this variable of storing address of new contract

function createExample(string memory _name) public returns(address ) {
/*
1. This function create a new instanse of Demo1 Contract with the of d1
2. Update the demo1Address to address of d1
3. Return the Address of d1
*/
Demo1 d1 = new Demo1(_name);
demo1Address = address(d1);
return address(d1);
}

function getNameOfContract(address _addr1)public view returns(string memory) {
/*
1. This function takes address
2. Create the refrence of Demo1 and Pass this address to the Demo1 contract
3. D1 return the Name of the given address
*/
Demo1 d1 = Demo1(_addr1);
return d1.getName();
}

}
34 changes: 0 additions & 34 deletions step03_solidity_tutorial/contracts/Demo1.sol

This file was deleted.

52 changes: 52 additions & 0 deletions step03_solidity_tutorial/contracts/Enums/enum.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";
/*
Enums restrict a variable to have one of only a few predefined values. The values in this enumerated list are called enums.

With the use of enums it is possible to reduce the number of bugs in your code.
*/
contract Enum {
enum OrderStatus {
gettingReady,
onYourWay,
delivered
}

OrderStatus status = OrderStatus.gettingReady;

function getStatus()public view returns(OrderStatus) {
//This function is return the status of our order now it is 0
return status;
}

function updateOrderStatus(OrderStatus _status ) public {
// here we take argument _status with type of OrderStatus so whenever we pass argument which is not in the status so this will return error
//There is another way of doing this we will look inthe next senario which is define bellow
//This function is return the updated status of our order .
status = _status;
}
function verifyOrderStatus() public view returns(bool ) {
//This function will verify first than return the status of our order now it is 0
return status == OrderStatus.delivered;
}

enum FundingR {
SEED,
PRIVATE,
PUBLIC
}
FundingR Cround = FundingR.SEED;
function gerCFR()public view returns (FundingR) {
return Cround;
}
function changeR(FundingR _round)public {
//This is the another way
require(_round == FundingR.SEED || _round == FundingR.PUBLIC || _round == FundingR.PRIVATE, "Invalid Round Information");
Cround = FundingR(_round);

}

}

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//SPDX-License-Identifier: Unlicense
pragma solidity ^0.8.0;

import "hardhat/console.sol";
/*
Solidity provides various functions for error handling. Generally when an error occurs, the state is reverted back to its original state. Other checks are to prevent unauthorized code access.
Following are some of the important methods used in error handling −
assert(bool condition) − In case condition is not met, this method call causes an invalid opcode and any changes done to state got reverted. This method is to be used for internal errors.
require(bool condition) − In case condition is not met, this method call reverts to original state. - This method is to be used for errors in inputs or external components.
require(bool condition, string memory message) − In case condition is not met, this method call reverts to original state. - This method is to be used for errors in inputs or external components. It provides an option to provide a custom message.
revert() − This method aborts the execution and revert any changes done to the state.
revert(string memory reason) − This method aborts the execution and revert any changes done to the state. It provides an option to provide a custom message
*/

contract ErrorHandling{
uint totalSupply =29999;
function mint(uint256 numberOfTokens) view public {
require(numberOfTokens < 10 ,"Number of tokens can not be more than 10");
require(numberOfTokens > 5);
if (numberOfTokens > 5 && numberOfTokens >10){
if(numberOfTokens > 7 && numberOfTokens < 9){
revert("Number of tokens can not be more than 10");
}
}

assert(totalSupply < 10000);
}
}

Loading