-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
168 additions
and
1 deletion.
There are no files selected for viewing
40 changes: 40 additions & 0 deletions
40
book/src/4-yul-implementations/4-4-yul-actions/4-4-10-hash.md
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
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) | ||
} | ||
} | ||
} | ||
``` |
45 changes: 45 additions & 0 deletions
45
book/src/4-yul-implementations/4-4-yul-actions/4-4-11-is-contract.md
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
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) | ||
} | ||
} | ||
} | ||
``` |
20 changes: 20 additions & 0 deletions
20
book/src/4-yul-implementations/4-4-yul-actions/4-4-12-ether-balance.md
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
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. |
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
62 changes: 62 additions & 0 deletions
62
book/src/4-yul-implementations/4-4-yul-actions/4-4-9-errors.md
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
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) | ||
} | ||
} | ||
} | ||
} | ||
``` |