Skip to content

Instantly share code, notes, and snippets.

View KolevDarko's full-sized avatar
🏠
Working from home

Darko Kolev KolevDarko

🏠
Working from home
View GitHub Profile
@KolevDarko
KolevDarko / learn.md
Created July 14, 2023 09:20 — forked from rylev/learn.md
How to Learn Rust

Learning Rust

The following is a list of resources for learning Rust as well as tips and tricks for learning the language faster.

Warning

Rust is not C or C++ so the way your accustomed to do things in those languages might not work in Rust. The best way to learn Rust is to embrace its best practices and see where that takes you.

The generally recommended path is to start by reading the books, and doing small coding exercises until the rules around borrow checking become intuitive. Once this happens, then you can expand to more real world projects. If you find yourself struggling hard with the borrow checker, seek help. It very well could be that you're trying to solve your problem in a way that goes against how Rust wants you to work.

@KolevDarko
KolevDarko / mapping-yul.sol
Created April 3, 2023 05:22
Solidity access mapping
function getMapping(uint256 key) external view returns (uint256 ret) {
uint256 slot;
assembly{
slot := myMapping.slot
}
bytes32 location = keccak256(abi.encode(key, uint256(slot)));
assembly{
ret := sload(location)
}
}
@KolevDarko
KolevDarko / dynamic-array-yul.sol
Created April 3, 2023 05:11
Solidity dynamic array assembly
function readBigArrayLocation(uint256 index) external view returns (uint256 ret) {
uint256 slot;
assembly{
slot := bigArray.slot
}
bytes32 location = keccak256(abi.encode(slot));
assembly{
ret := sload(add(location, index))
}
@KolevDarko
KolevDarko / fixed-array-storage.sol
Created April 3, 2023 05:08
Fixed array storage
function viewFixedArray(uint256 index) external view returns (uint256 ret) {
assembly {
ret := sload(add(fixedArray.slot, index))
}
}
@KolevDarko
KolevDarko / storage-read-write.sol
Last active April 1, 2023 07:50
Storage read/write
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract StoragePart1 {
uint128 public C = 4;
uint8 public D = 6;
uint24 public E = 8;
function readBySlot() external view returns(bytes32 value) {
assembly {
@KolevDarko
KolevDarko / storage-basics.sol
Created March 29, 2023 10:13
Storage basics
//SPDX-License-Identifier: MIT
pragma solidity 0.8.19;
contract StorageBasics {
uint256 x = 1;
uint256 y = 23;
uint256 z = 48;
uint128 a = 1;
uint128 b = 2;
@KolevDarko
KolevDarko / check-max.sol
Created March 29, 2023 09:38
Yul check max
function getMax(uint256 x, uint256 y) public pure returns (uint256 ret) {
assembly {
if lt(x, y) {
ret := y
}
if iszero(lt(x, y)) {
ret := x
}
}
@KolevDarko
KolevDarko / yul-primer.sol
Created March 29, 2023 09:16
Yul prime number
function isPrime(uint256 x) external pure returns (bool ret) {
ret = true;
assembly {
let halfX := add(div(x, 2), 1)
for {let i := 1} lt(i, halfX) {i := add(i, 1)} {
if iszero(mod(x, i)) {
ret := 0
break
}
}
function _beforeExecute(
uint256, /* proposalId */
address[] memory targets,
uint256[] memory, /* values */
bytes[] memory calldatas,
bytes32 /*descriptionHash*/
) internal virtual {
if (_executor() != address(this)) {
for (uint256 i = 0; i < targets.length; ++i) {
if (targets[i] == address(this)) {
@KolevDarko
KolevDarko / governor-execute-1
Created December 15, 2022 05:42
Governor execute
function execute(
address[] memory targets,
uint256[] memory values,
bytes[] memory calldatas,
bytes32 descriptionHash
) public payable virtual override returns (uint256) {
uint256 proposalId = hashProposal(targets, values, calldatas, descriptionHash);
ProposalState status = state(proposalId);
require(