/uniswap-stylus-curve-template

A template for writing Uniswap Custom Swap Curves in Stylus.

Primary LanguageRustMIT LicenseMIT

Uniswap Custom Swap Curve

Warning

This project is still in a very early and experimental phase. It has never been audited nor thoroughly reviewed for security vulnerabilities. Do not use in production.

A template for writing Uniswap Custom Swap Curves in Stylus

Use this Template

  1. The example curve lib.rs demonstrates a constant-sum swap curve, in which tokens are traded exactly 1:1.

Tip

You can modify ICurve trait based on your needs, or provide your custom algorithm for a swap curve calculation.

Caution

If anything changes in the contract's interface, make sure to update your Solidity contract with Uniswap hooks to use the new Solidity ABI Interface of this contract.

Getting started

Follow the instructions in the Stylus quickstart to configure your development environment.

You'll also need Foundry to interact with the contract.

Check and deploy

You can use cargo stylus to check that your contract is compatible with Stylus by running

cargo stylus check

With the following command you can deploy it to an Arbitrum chain

cargo stylus deploy --private-key $PRIVATE_KEY -e $RPC_URL --no-verify

For example

cargo stylus deploy --private-key=ac0974bec39a17e36ba4a6b4d238ff944bacb478cbed5efcae784d7bf4f2ff80 -e=http://localhost:8547 --no-verify

Tests

For unit testing, this example integrates the motsu library from OpenZeppelin. To run unit tests, you can simply use

cargo test --locked --features std --lib

Alternatively, you can use the bash script available test-unit.sh.

Exporting Solidity ABI Interface

To export the Solidity ABI interface run the following command

cargo stylus export-abi

How to run a local dev node

Instructions to setup a local dev node can be found here.

Alternatively, you can use the bash script available nitro-testnode.sh

./scripts/nitro-testnode.sh -d -i

If you need to have some testnet tokens, you can use this script

./nitro-testnode/test-node.bash script send-l2 --to address_<address> --ethamount <amount>

For example

./nitro-testnode/test-node.bash script send-l2 --to address_0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266 --ethamount 5

Solidity Interface

This is the current Solidity ABI Interface for the contract

interface IUniswapCurve {
    function getAmountInForExactOutput(uint256 amount_out, address input, address output, bool zero_for_one) external returns (uint256);

    function getAmountOutFromExactInput(uint256 amount_in, address input, address output, bool zero_for_one) external returns (uint256);

    error CurveCustomError();
}