가스비 최적화

OPT는 Weswap V2 및 V3 의 코드를 기반으로 개발되었습니다. 이 코드들은 토큰 교환을 위한 핵심 기능인 swapExactTokensForTokens, exactInputSingle 등을 포함합니다. OPT 개발팀은 이 코드를 솔리디티 어셈블리 언어로 다시 작성하여 더욱 효율적으로 가스비를 사용할 수 있도록 최적화하였습니다.

재작성된 swap 함수들은 오딧을 통해 안전성과 유효성을 입증받았습니다.

예시) getAmountsOut 함수

    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