Last active
November 4, 2021 15:31
-
-
Save spandan114/3b6fe717bb51283b355a77e01847a04c to your computer and use it in GitHub Desktop.
Solidity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
//abstract contract is like a email template you can use it as per your need | |
pragma solidity >=0.5.0 <0.9.0; | |
abstract contract Abstract{ | |
function result() public virtual; | |
} | |
contract Derived is Abstract{ | |
function result() public override(Abstract){ | |
} | |
} | |
//We cant use struct & enum in abstaract contract thats why we use interfaces | |
interface Calculator { | |
function getResult() external view returns(uint); | |
} | |
contract Test is Calculator { | |
constructor() public {} | |
function getResult() public override view returns(uint){ | |
uint a = 1; | |
uint b = 2; | |
uint result = a + b; | |
return result; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.5.0 <0.9.0; | |
contract Array{ | |
uint[6] public fixedArr = [1,2,3]; //Fixed size array | |
uint[] public arr = [1,2,3]; //Dynamic array | |
function updateArrVal(uint index,uint val) public{ | |
arr[index] = val; | |
} | |
function pushArrVal(uint val) public{ | |
arr.push(val); | |
} | |
function poptArrVal() public{ | |
arr.pop(); | |
} | |
function arrLen() view public returns(uint){ | |
return arr.length; | |
} | |
} | |
// Bytes array (fixed size array) | |
// We can store hexa desimal num in byte array | |
// Byte array can not be modified (immutable) | |
contract byteArray{ | |
bytes3 public b3; | |
bytes2 public b2; | |
constructor(){ | |
b3 = "abc"; //0x616263 = [61,62,63] | |
b2= "ab"; //0x6162 = [61,62] | |
} | |
} | |
// Bytes array (dynamic size array) | |
contract byteArray{ | |
bytes public b1; | |
constructor(){ | |
b1 = "abc"; | |
} | |
function getArray() public { | |
b1.push("d"); | |
} | |
function getArray(uint i) public view returns(bytes1){ | |
return b1[i]; | |
} | |
function arrLen() view public returns(uint){ | |
return b1.length; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.5.0 <0.9.0; | |
contract State{ | |
enum user{ALLOWED,NOT_ALLOWED,PENDING} | |
user public u1 = user.ALLOWED; | |
uint public amount = 100; | |
function validate() public{ | |
if(u1 == user.ALLOWED){ | |
amount = 0; | |
u1 = user.PENDING; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.5.0 <0.9.0; | |
contract Loop{ | |
uint[8] public myArray; | |
uint count; | |
function loopWhile() public { | |
while(count < myArray.length){ | |
myArray[count] = count; | |
count++; | |
} | |
} | |
} | |
contract condition{ | |
function mycondition(uint a) public pure returns(string memory) { | |
string memory myval; | |
if(a<1){ | |
myval = "less than 1"; | |
}else if(a > 1 && a < 5){ | |
myval = "greater than 1 but less than 5"; | |
}else{ | |
myval = "greater than 5"; | |
} | |
return myval; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.5.0 <0.9.0; | |
contract Map{ | |
mapping(uint=>string) public roll_number; | |
function setRollNo(uint key,string memory value) public{ | |
roll_number[key]=value; | |
} | |
//Using struct with map | |
struct Student{ | |
string name; | |
uint class; | |
} | |
mapping(uint=>Student) public data; | |
function setData(uint roll_no,string memory _name,uint _class) public{ | |
data[roll_no]=Student(_name,_class); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.5.0 <0.9.0; | |
contract EtherTxn{ | |
address payable user = payable(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2); | |
function payEther() public payable{} | |
function getBalance() public view returns(uint){ | |
return address(this).balance; | |
} | |
function sendEther() public { | |
user.transfer(10 ether); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >=0.5.0 <0.9.0; | |
struct Student{ | |
uint roll; | |
string name; | |
} | |
contract School{ | |
Student public s1; | |
constructor(uint _roll,string memory _name){ | |
s1.roll=_roll; | |
s1.name=_name; | |
} | |
function update(uint _roll,string memory _name) public{ | |
Student memory new_student = Student({ | |
roll:_roll, | |
name:_name | |
}); | |
s1=new_student; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment