Skip to content

Commit

Permalink
📝 4-4-8 to 4-4-12.
Browse files Browse the repository at this point in the history
  • Loading branch information
0xfps committed May 21, 2024
1 parent 6f7308f commit 7cbfbc5
Show file tree
Hide file tree
Showing 5 changed files with 168 additions and 1 deletion.
40 changes: 40 additions & 0 deletions book/src/4-yul-implementations/4-4-yul-actions/4-4-10-hash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Hash

To hash a part of memory in Yul, we use the `keccak256(a, b)` function. This function takes in two arguments, the location in memory to start and the size of the memory to hash.

## Normal Hash

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
function hash(uint256 a) public pure returns (bytes32) {
assembly {
mstore(0x80, a)
let hash := keccak256(0x80, 0x20)
mstore(0x80, hash)
return(0x80, 0x20)
}
}
}
```

## To Has A Variable In Memory

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
function hashString(string memory s) public pure returns (bytes32) {
assembly {
mstore(0x80, 0x20)
mstore(0xa0, mload(s))
mstore(0xc0, mload(add(s, 0x20)))
mstore(0x80, keccak256(0x80, 0x60))
return(0x80, 0x20)
}
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Is Contract

This is used to check if an address is the address of a smart contract or an EOA.

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
function isContract(address _address) public view returns (bool) {
assembly {
let size := extcodesize(_address)
switch size
case 0 { mstore(0x80, 0x00) }
default { mstore(0x80, 0x01) }
return(0x80, 0x20)
}
}
}
```

The `extcodesize(a)` function takes in an address and returns the size of the contract code at that address, for EOAs, it's 0, for contracts, it's greater than 0.

In this case, we returned `bool` and Solidity automatically converted it for us. Had we returned `bytes32` like we did below, we would have seen `0x01` for `true` and `0x00` for `false`.

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
function isContract(address _address) public view returns (bytes32) {
assembly {
let size := extcodesize(_address)
switch size
case 0 { mstore(0x80, 0x00) }
default { mstore(0x80, 0x01) }
return(0x80, 0x20)
}
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Ether Balance

This is how you check the Ether balance of an address on any EVM chain.

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
function getBalance(address _address) public view returns (bytes32) {
assembly {
mstore(0x80, balance(_address))
return(0x80, 0x20)
}
}
}
```

The `balance(a)` function will take in an address and return the Ether balance of that address.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ contract Yul {

## `while` Loops

To make the `for` loop act like a `while` loop, this is how we do it.
To make a `for` loop act like a `while` loop, this is how we do it.

```solidity
// SPDX-License-Identifier: GPL-3.0
Expand Down
62 changes: 62 additions & 0 deletions book/src/4-yul-implementations/4-4-yul-actions/4-4-9-errors.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
# Errors

Errors are encoded using the ABI Specification. We will take a look at basic reverts, reverts of errors without
parameters and errors with parameters.

## Basic Revert

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
function basicRevert(uint256 num) public pure {
assembly {
if lt(num, 0x06) {
revert(0x00, 0x00)
}
}
}
}
```

## Revert Without Arguments

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
// bytes4(keccak256(NumberLess())) => 0x994823ad.
function revertWithArgs(uint256 num) public pure {
assembly {
if lt(num, 0x06) {
mstore(0x80, 0x994823ad) // 4 bytes.
revert(0x9c, 0x04) // Reads 4 bytes.
}
}
}
}
```

## Revert With Arguments

```solidity
// SPDX-License-Identifier: GPL-3.0
pragma solidity ^0.8.0;
contract Yul {
// bytes4(keccak256(NumberLessThan6(uint256))) => 0x8205edea
function revertWithErrorMessage(uint256 num) public pure {
assembly {
if lt(num, 0x06) {
mstore(0x80, 0x8205edea)
mstore(0xa0, num)
revert(0x9c, 0x24)
}
}
}
}
```

0 comments on commit 7cbfbc5

Please sign in to comment.