Optimized Gas

OPT was developed based on the code of Weswap V2 and V3, which power swap service of WEMIX.fi. These codes include core functions for token exchange such as swapExactTokensForTokens, exactInputSingle and more. The OPT dev team has optimized these codes by rewriting them in assebmly language to use gas more efficiently.

Our swap functions have been validated for safety and effectiveness through Certik.

Ex) getAmountsOut Function

    function getAmountsOut(
        uint256 amountIn,
        address[] calldata path
    ) public view returns (uint256[] memory amounts) {
        amounts = new uint256[](path.length);
        assembly {
            // to skip unnecessary checks
            mstore(add(amounts, 0x20), amountIn)
        }

        for (uint256 i; i < path.length - 1; ) {
            address path_i;
            address path_i_plus_1;

            assembly {
                // to skip range check
                path_i := calldataload(add(path.offset, mul(i, 0x20)))
                path_i_plus_1 := calldataload(
                    add(path.offset, mul(add(i, 1), 0x20))
                )
            }

            // ...

            {
                assembly {
                    let _amountIn := mload(
                        add(add(amounts, 0x20), mul(i, 0x20))
                    )
                    mstore(
                        // to skip arithmetic checks — core implementation does checking them
                        add(add(amounts, 0x20), mul(add(i, 1), 0x20)),
                        div(
                            mul(mul(_amountIn, reserve1), 9975),
                            add(mul(reserve0, 10000), mul(_amountIn, 9975))
                        )
                    )
                }
            }

            // ...

            // to skip overflow check
            unchecked { i++; }
        }
    }

Last updated