Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/philogy/huff-stuff


https://github.com/philogy/huff-stuff

Last synced: 11 days ago
JSON representation

Awesome Lists containing this project

README

        

# Huff Stuff

A collection of heavily optimized Huff contrats.

## ERC20
Source: `ERC20.huff`

Example use: `src/test/contracts/MockERC20.huff`

**Basic Macros:**

The `_ERC20_SELECTOR_SWITCH` macro inserts the ERC20 selector switch into your
code. It takes 1 stack argument, the selector and returns it, meaning the macro
can seamlessly be added to an existing selector switch. Example:

```huff
GET_SELECTOR()
dup1 __FUNC_SIG(mint) eq mint jumpi
dup1 _ERC20_SELECTOR_SWITCH(decimals, symbol, name)
__FUNC_SIG(burn) eq burn jumpi
```

The `_ERC20_SELECTOR_SWITCH` macro takes the token's decimals and 2 jump labels
as macro arguments. The first jump label being the logic returning the token's
symbol and the second the name.

**Minting / Burning:**

In order to mint / burn tokens in the constructor or elsewhere use the
`ERC20_SAFE_BURN` and `ERC20_SAFE_MINT` macros. They take 2 stack arguments:
\[amount, account\]. They're called "safe" because they do overflow / underflow
checking, meaning that if only these methods are used to adjust the supply,
balances and the total supply cannot not overflow / underflow.

The two macros also take 2 macro arguments `mem1` and `mem2`. These are meant
to be free memory pointers whereby `mem2 = 0x20 + mem1`, `mem2` is passed as a
macro argument to avoid having to calculate it via `mem1 0x20 add` at runtime.

**Other Macros:**

The `ERC20.huff` library currently has no other macros for retrieving
balance / allowance / total supply internally. They may however be added in the
future.

**Storage layout:**
Variable|Slot
------|------
totalSupply|`SLOT_TOTAL_SUPPLY`
balanceOf(account)|`keccak256(SLOT_BALANCE_OF . account)`
allowance(owner, spender)|`keccak256(SLOT_ALLOWANCE . spender . owner)`