Giter Site home page Giter Site logo

jtriley-eth / token-types Goto Github PK

View Code? Open in Web Editor NEW
91.0 4.0 2.0 122 KB

Efficient Type Driven Smart Contract Interactions

License: GNU Affero General Public License v3.0

Solidity 100.00%
ethereum-virtual-machine solidity erc20 erc4626 erc721 erc6909

token-types's Introduction

Token Types

TL;DR

Token Types are drop-in replacements for interfaces.

All the expected functions, all of the available operators, none of the memory allocation, and none of the bloat.

// replace this:
import { IERC20 } from "some-other-project/IERC20.sol";

// with this:
import { ERC20 } from "token-types/ERC20.sol";

Motivation

Interfaces were a good abstraction for their time, but are outdated. Interface interactions allocate memory and abstract away important edge cases in smart contract interactions.

"Safe" transfer libraries are a patch, using function-call syntax to create a syntactically similar interface to external contracts with custom checks, such as the notorious ERC20 noncompliance edge cases where ERC20 tokens return no data. However, this is a clunky solution, especially in cases where the library "applies" functions to the same namespace as the core interface. This makes reasoning about a type's API difficult. We collectively spend far more time reading code than writing, so we should care about this.

Presented is one solution. We define custom type aliases over the address primitive. The custom types have the same names as the contract interfaces, contain the same functions that would otherwise exist in the interface's namespace, but instead of generating solidity's enshrined interface abstractions, we optimize the obvious cases and remove new memory allocations.

There are cases in which memory is expanded, such as dynamically sized values with unknown bounds at compile time; however, the free memory pointer is never increased, meaning in the rare case where evm memory is expanded, it can be reused by the solidity compiler.

Basic Usage

// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.24;

import { ERC20 } from "token-types/ERC20.sol";

contract Vault {
    mapping(ERC20 => mapping(address => uint256)) public deposits;

    function deposit(ERC20 token, uint256 amount) external {
        token.transferFrom(msg.sender, address(this), amount);
        deposits[token][msg.sender] += amount;
        // msize == 0x80
    }

    function withdraw(ERC20 token, uint256 amount) external {
        deposits[token][msg.sender] -= amount;
        token.transfer(msg.sender, amount);
        // msize == 0x80
    }
}

Behavior

  • Each external call asserts:
    • The call succeeded.
    • The call returned the correct returndatasize.
    • If a stateful call returns a boolean to indicate success:
      • The call returned true.
    • If the call is ERC20's transfer, transferFrom, or approve:
      • No data was returned OR (the returndata is 32 bytes AND is true).
  • The operators +, -, *, /, and % revert if the operation:
    • Overflows.
    • Underflows.
    • Divides by zero.
  • The operator ~ masks the address to 160 bits after performing bitwise not.

Existing Types

Documentation is prefixed to respective type and function definitions.

token-types's People

Contributors

jtriley-eth avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

0xneves zklim

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.