Skip to content

0xlynett/miniamm

Repository files navigation

MiniAMM

Super simple AMM implementation in 17 lines of code.

contract MiniAMM {
    mapping(bool => address) public toks;
    constructor(address ta, address tb) {
        (toks[true], toks[false]) = (ta, tb);
    }
    function swap(bool toka, uint256 tin) public {
        require(
            IERC20(toks[toka]).transferFrom(msg.sender, address(this), tin)
        );
        IERC20(toks[!toka]).transfer(msg.sender, getOut(toka, tin));
    }
    function getOut(bool toka, uint256 tin) public view returns (uint256) {
        return
            (tin * IERC20(toks[!toka]).balanceOf(address(this))) /
            IERC20(toks[toka]).balanceOf(address(this));
    }
}

About

tiniest AMM you've ever seen

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published