root💀senseicat:~#

Hack. Eat. Sleep. Repeat!!!


Project maintained by SENSEiXENUS Hosted on GitHub Pages — Theme by mattgraham

Solidity Basics


sudo npm install --global solc
//version
pragma solidity 0.8.30; //use only 0.8.30
//use versions between 0.8.19 and 0.9.0 (excluded)
pragma solidity ^0.8.19;
pragma solidity >=0.8.19 <0.9.0;
solcjs --bin main.sol

image

//SPDX-License-Identifier: MIT
//solidity version
pragma solidity ^0.8.30;

contract simpleStorage {
    //code goes thus
}
bool hasFavoriteNumber = true; //true or false
   bool hasFavoriteNumber = true;
    uint256 favoriteNumber = 88;
    string favoriteNumberInText = "eighty-eight";
    int256 favoriteInt = -88;
    address myAddress = 0xAB1b7206aa6840C795aB7A6AE8b15417B7E63A8D;
    bytes32 favoriteBytes32 = "cat";
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;

contract Storage{
    uint256 digit = 100;
    function store(uint256 _digit) public {
        digit = _digit;
    }
}
uint256 public digit = 1000;
public
private
internal
external
//SPDX-License-Identifier: MIT
pragma solidity ^0.8.1;

contract Storage{
    uint256 digit = 100;
    function store(uint256 _digit) public {
        digit = _digit;
    }
    //view
    function retrieve() public view returns {
        return digit;
    }
    //pure
    function retrieve() public pure returns {
        return 7;
    }
}
function createZombie(string memory _name,uint _dna) public {

}
string greeting = "What's up dog";

function sayHello() public returns (string memory) {
  return greeting;
}
function sayHello() public view returns (string memory){
}
function _multiplyNumbers(uint a, uint b) private pure returns (uint memory) {
  return a * b;
}

Arrays and Structs


uint256[] digits = [0,10,90];
struct Person {
   string name;
   uint256 age;
}
Person public ade = Person("Ade",100);
Person[] public people;
Person[3] public three_people;
//function push to three_people array
    function push_value(uint256 _negativeNumber, string memory _name) public{
        people.push(Person(_negativeNumber,_name));
  }

Memory Storage


Calldata
Memory
Storage
Stack
Code
Logs
string memory name = "Ade";

Keccak256 and Typecasting


keccak256(abi.encodePacked("deadbeef"))

uint a = 6;
uint8 b = 9;

uint8 c = b * uint8(a);

Events


// declare the event
event IntegersAdded(uint x, uint y, uint result);

function add(uint _x, uint _y) public returns (uint) {
  uint result = _x + _y;
  // fire an event to let the app know the function was called:
  emit IntegersAdded(_x, _y, result);
  return result;
}
YourContract.IntegersAdded(function(error, result) {
  // do something with the result
})
pragma solidity >=0.5.0 <0.6.0;

contract ZombieFactory {

    event NewZombie(uint zombieId,string name,uint dna);
    
    uint dnaDigits = 16;
    uint dnaModulus = 10 ** dnaDigits;

    struct Zombie {
        string name;
        uint dna;
    }

    Zombie[] public zombies;

    function _createZombie(string memory _name, uint _dna) private {
        uint id = zombies.push(Zombie(_name, _dna)) - 1; // 2. Store the result of `zombies.push(...) - 1` in a `uint` called `id`
        emit NewZombie(id, _name, _dna);
    }

    function _generateRandomDna(string memory _str) private view returns (uint) {
        uint rand = uint(keccak256(abi.encodePacked(_str)));
        return rand % dnaModulus;
    }

    function createRandomZombie(string memory _name) public {
        uint randDna = _generateRandomDna(_name);
        _createZombie(_name, randDna);
    }

}

Intermediate Solidity


mapping (address => uint) public accountBalance;
mapping (uint => string ) userIdToName;

Msg.sender


Note: In Solidity, function execution always needs to start with an external caller. A contract will just sit on the blockchain doing nothing until someone calls one of its functions. So there will always be a msg.sender.

mapping (address => uint ) public contractAddresses;

function setAddressValue(uint memory _number) public {
    contractAddresses[msg.sender] = _number;
}
uint number = 0;
number++;
require(abi.encodePacked('a') == abi.encodePacked('b'));

Inheritance


contract Doge {
}
//is extends a subclass to base class
contract baseDoge is Doge {

}

import "./someContract.sol"

contract Doge is someContract {
}
function feedAndMultiply(uint _zombieId, uint _targetDna) public {
    require(msg.sender == zombieToOwner[_zombieId]);
    Zombie storage myZombie = zombies[_zombieId];
    // start here
  }
myZombie.dna
contract KittyInterface {
  function getKitty(uint256 _id) external view returns (
    bool isGestating,
    bool isReady,
    uint256 cooldownIndex,
    uint256 nextActionAt,
    uint256 siringWithId,
    uint256 birthTime,
    uint256 matronId,
    uint256 sireId,
    uint256 generation,
    uint256 genes
  );
}
address ckAddress = 0x06012c8cf97BEaD5deAe237070F9587f8E7A266d;
  // Initialize kittyContract here using `ckAddress` from above
  KittyInterface kittyContract = KittyInterface(ckAddress);

function processMultipleReturns() external { uint a; uint b; uint c; // This is how you do multiple assignment: (a, b, c) = multipleReturns(); }

// Or if we only cared about one of the values: function getLastReturnValue() external { uint c; // We can just leave the other fields blank: (,,c) = multipleReturns(); }

- `if` statements-:

```solidity
if () {
}

Advanced Concepts


modifier onlyOwner() {
    require(isOwner());
    _;
  }
function tryModifier(uint256 _number) external onlyOwner {
}

Passing struct as arguments


function _doStuff(Zombie storage _zombie) internal {
  // do stuff with _zombie
}
pragma solidity >=0.5.0 <0.6.0;

import "./zombiefactory.sol";

contract KittyInterface {
  function getKitty(uint256 _id) external view returns (
    bool isGestating,
    bool isReady,
    uint256 cooldownIndex,
    uint256 nextActionAt,
    uint256 siringWithId,
    uint256 birthTime,
    uint256 matronId,
    uint256 sireId,
    uint256 generation,
    uint256 genes
  );
}

contract ZombieFeeding is ZombieFactory {

  KittyInterface kittyContract;

  function setKittyContractAddress(address _address) external onlyOwner {
    kittyContract = KittyInterface(_address);
  }

  // 1. Define `_triggerCooldown` function here
  function _triggerCooldown(Zombie storage _zombie) internal {
    _zombie.readyTime = uint32(now + cooldownTime);
  }

  // 2. Define `_isReady` function here
  function _isReady(Zombie storage _zombie) internal view returns (bool){
    return (_zombie.readyTime <= now );
  }
  function feedAndMultiply(uint _zombieId, uint _targetDna, string memory _species) public {
    require(msg.sender == zombieToOwner[_zombieId]);
    Zombie storage myZombie = zombies[_zombieId];
    _targetDna = _targetDna % dnaModulus;
    uint newDna = (myZombie.dna + _targetDna) / 2;
    if (keccak256(abi.encodePacked(_species)) == keccak256(abi.encodePacked("kitty"))) {
      newDna = newDna - newDna % 100 + 99;
    }
    _createZombie("NoName", newDna);
  }

  function feedOnKitty(uint _zombieId, uint _kittyId) public {
    uint kittyDna;
    (,,,,,,,,,kittyDna) = kittyContract.getKitty(_kittyId);
    feedAndMultiply(_zombieId, kittyDna, "kitty");
  }

}

Public functions & security


modifier aboveLevel(uint _level, uint _zombieId) {
    require(zombies[_zombieId].level >= _level);
    _;
  }

view functions don’t cost any gas when they’re called externally by a user.This is because view functions don’t actually change anything on the blockchain – they only read the data. So marking a function with view tells web3.js that it only needs to query your local Ethereum node to run the function, and it doesn’t actually have to create a transaction on the blockchain (which would need to be run on every single node, and cost gas).

 uint[] memory result = new uint[](ownerZombieCount[_owner]);
    // Start here
    return result;
 for (uint i = 0; i < zombies.length; i++) {
      if (zombieToOwner[i] == _owner) {
        result[counter] = i;
        counter++;
      }
    }

function withdraw() external onlyOwner {
    address payable _owner = address(uint160(owner()));
    _owner.transfer(address(this).balance);
  }
 uint randNonce = 0;
  function randMod(uint _modulus) internal returns (uint) {
    randNonce++;
    return uint(keccak256(abi.encodedPacked(now,msg.sender,randNonce))) % _modulus;
  }

Refactoring Code Logic


function changeDna(uint _zombieId, uint _newDna) external aboveLevel(20, _zombieId) ownerOf(_zombieId) {
    zombies[_zombieId].dna = _newDna;
  }

ERC721 & Crypto-Collectibles


transferFrom(address _from, address _to, uint256 _amount) and balanceOf(address _owner)

ERC721 Standard


contract ERC721 {
  event Transfer(address indexed _from, address indexed _to, uint256 indexed _tokenId);
  event Approval(address indexed _owner, address indexed _approved, uint256 indexed _tokenId);

  function balanceOf(address _owner) external view returns (uint256);
  function ownerOf(uint256 _tokenId) external view returns (address);
  function transferFrom(address _from, address _to, uint256 _tokenId) external payable;
  function approve(address _approved, uint256 _tokenId) external payable;
}
contract Sathoshi is  Sensei,Blackie {

}

ERC721 implementation


function transferFrom(address _from, address _to, uint256 _tokenId) external payable; and function approve(address _approved, uint256 _tokenId) external payable; function transferFrom(address _from, address _to, uint256 _tokenId) external payable;

import "./safemath.sol"

contract ZombieFactory is Ownable {

  // 2. Declare using safemath here
  using SafeMath for uint256;