Skip to content

Instantly share code, notes, and snippets.

@2tanayk
Created March 15, 2022 21:41
Show Gist options
  • Save 2tanayk/1da13a7ac271324c2be37005118fcd6c to your computer and use it in GitHub Desktop.
Save 2tanayk/1da13a7ac271324c2be37005118fcd6c to your computer and use it in GitHub Desktop.
Solidity basics by freeCodeCamp.org
pragma solidity >=0.7.0 <0.9.0;
//inheritance in solidity
contract Parent{
string public name;
mapping(address=>uint256) public balances;
constructor(string memory _name) public {
name=_name;
}
function mint() virtual public{
balances[tx.origin]++;
}
}
contract Child is Parent{
string public symbol;
address[] public owners;
uint256 ownerCount;
constructor(string memory _name, string memory _symbol) Parent(_name) public {
symbol=_symbol;
}
function mint() public override{
super.mint();
ownerCount++;
owners.push(msg.sender);
}
}
pragma solidity >=0.7.0 <0.9.0;
import "./Math.sol";
contract MyLibContract{
uint256 public value;
function calculate(uint _value1, uint _value2) public {
value=Math.divide(_value1,_value2);
}
}
pragma solidity >=0.7.0 <0.9.0;
//solidity library
/pure - ensure that they not read or modify the state.
library Math {
function divide(uint256 a, uint256 b) internal pure returns(uint256){
require(b>0);
uint256 c=a/b;
return c;
}
}
pragma solidity >=0.7.0 <0.9.0;
// a separate contract to mint tokens
contract ERC20Token{
string public name;
uint256 public ct=0;
mapping(address=>uint256) public balances;
function mint() public{
//our tokens are added here
ct+=1;
balances[tx.origin]++;
}
}
contract MyContract{
string str;
string public str2 = "John"; //we get an implict getter here
string public constant str3="Doe";//constant
bool public myBool=true;
int public myInt=1;
uint public myUint=1; //unsigned
uint256 public pCt=0;
//enumeration
enum State {Waiting, Ready, Active}
struct Person{
string _firstName;
string _lastName;
}
//array of struct
Person[] public people;
//mapping(like hash map)
mapping(uint=>Person) public peopleMap;
State public state;
address owner;
// variable which represents current time
uint256 openingTime=1647285267;
mapping(address=>uint256) public balances;
address payable wallet;
address public token;
//events are a way for external consumers to susbscribe/listen to things that happen on a smart contract
//indexed - to filter events for certain buyers
event Purchase(address indexed _buyer,uint256 _amount);
//this modifier lets only the person who is the owner, do the operation
modifier onlyOwner(){
//msg - global variable for metadata
//"_" is a special character that is used in functional modifiers.
// It returns the flow of execution to the original function that is annotated.
//read more here, https://ethereum.stackexchange.com/questions/19171/what-does-underscore-do
require(msg.sender==owner);
_;
}
// modifier to allow an action only after a certain time
modifier onlyWhileOpen(){
//block - global variable
require(block.timestamp>=openingTime);
_;
}
// executed as soon as the smart-contract is created
constructor(address payable _wallet, address _token) public{
str="Tanay";
state=State.Waiting;
//the person who depoys the smart contract(msg.sender) is the owner
owner=msg.sender;
wallet=_wallet;
token=_token;
}
function activate() public{
state = State.Active;
}
//we want only the owner to add people
function addPerson(string memory _firstName, string memory _lastName) public onlyOwner onlyWhileOpen{
people.push(Person(_firstName,_lastName));
}
function addPersonToMap(string memory _firstName, string memory _lastName) public{
pCt+=1;
peopleMap[pCt]=Person(_firstName,_lastName);
}
function isActive() public view returns(bool){
return state==State.Active;
}
// functions
// public - visible throughout the blockchain
// view - read-only function
// memory - does not persist between function calls
// returns - return type
function get() public view returns (string memory) {
return str;
}
function set(string memory _str ) public{
str=_str;
}
// this is like private modifier, won't be exposed outside
function justAFunction() internal {
}
//fallback function to send ether
//external - can be called only from outside
//The solidity fallback function is executed if none of the other functions match
//the function identifier or no data was provided with the function call.
//Only one unnamed function can be assigned to a contract and it is executed whenever
//the contract receives plain Ether without any data.
fallback() external payable{
buyToken();
}
//buy a token, send ether to the wallet(the wallet we passed in our constructor)
//payable- to send ether
function buyToken() public payable{
// our tokens are added here
// balances[msg.sender]+=1;
ERC20Token _token = ERC20Token(token);
_token.mint();
//transfer ether (to the address in the constructor)
// wallet.transfer(msg.value);
//emitting a buy event
// emit Purchase(msg.sender,1);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment