Coder Social home page Coder Social logo

lambdaclass / yet-another-swap Goto Github PK

View Code? Open in Web Editor NEW
53.0 3.0 25.0 399 KB

YAS is Yet Another Swap on Starknet but bringing the best prices and yield to Degens.

Home Page: https://www.yetanotherswap.com

License: Apache License 2.0

Cairo 92.55% Makefile 0.13% Rust 5.17% Shell 1.06% Python 1.09%
defi starknet amm ethereum

yet-another-swap's Introduction

YAS - Yet Another Swap

YAS is Yet Another Swap on Starknet 😝. It's an AMM based on Uniswap v3 that will add some new features to the Starknet ecosystem.

  • Provide a more capital efficient liquidity layer
  • Based on a robust and battle-tested protocol. LPs from Uniswap v3 will feel right at home.
  • Provide the best prices for aggregators and traders.

At YAS we believe product quality should always be a priority so we are commited to provide the best experience for users that want to put Starknet to the limit.

This project will be built in the open, it's still in development. We love the Starknet developer ecosystem and we know that a lot of smart and hungry developers would like to collaborate in the future of Starknet. If that's your case you are more than welcome to join us!

Follow us on Twitter

Important Disclaimer

Currently, the project is in a development stage, it has not been audited yet and is not ready for production. We are also using our fork of Orion signed integer implementation until all features are implemented and supported in the latest version of Cairo.

Note: In our Orion fork, we've added the capability for signed integers to be stored in a smart contract.

Requirements

Rust and Cargo

Visit the rustup website and follow the instructions on the page or run the following command in the terminal:

curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh

Check the version;

rustc --version
cargo --version

Starkli

Starkli is CLI tool for Starknet. For install;

curl https://get.starkli.sh | sh

Update starkli simply by running;

starkliup

Install dependencies

Run the following command:

make deps

This will end up installing:

  • Scarb (Cairo/Starknet packet manager) - Includes a specific version of the Cairo compiler.
  • Starkli (Starknet CLI)

Build Project

Run the following command:

make build

This command executes the Scarb build process, resulting in the creation of a Sierra program.

Test Project

Run the following command to run all tests:

make test

You can also filter the tests to run, by module or even by individual test name, for example to run tests within the Mint module, execute:

make test Mint

Or to run only the "test_invalid_max_tick" function:

make test test_invalid_max_tick

Be careful though, this parameters is case-sensitive

Setting up a Testnet Smart Wallet

This guide will help you declare and deploy contracts on a testnet. Please note that you won't be able to use the commands in the Makefile unless you follow these instructions.

A smart wallet consists of two parts: a Signer and an Account Descriptor. The Signer is a smart contract capable of signing transactions (for which we need its private key). The Account Descriptor is a JSON file containing information about the smart wallet, such as its address and public key.

Follow the steps below to set up a testnet smart wallet using starkli:

  1. Connect to a Provider: to interact with the network you need an RPC Provider. For our project we will be using Alchemy's free tier in Goerli Testnet.

    1. Go to Alchemy website and create an account.

    2. It will ask which network you want to develop on and choose Starknet.

    3. Select the Free version of the service (we will only need access to send some transactions to deploy the contracts)

    4. Once the account creation process is done, go to My apps and create a new Application. Choose Starknet as a Chain and Goerli Starknet as a Network.

    5. Click on View key on the new Starknet Application and copy the HTTPS url.

    6. On your terminal run:

      export STARKNET_RPC="<ALCHEMY_API_HTTPS_URL>"
  2. Create a Keystore: A Keystore is a encrypted json file that stores the private keys.

    1. Create a hidden folder: Use the following command:

      mkdir -p ~/.starkli-wallets
    2. Generate a new Keystore file: Run the following command to create a new private key stored in the file. It will ask for a password to encrypt the file:

      starkli signer keystore new ~/.starkli-wallets/keystore.json

      The command will return the Public Key of your account, copy it to your clipboard to fund the account.

    3. Set STARKNET_ACCOUNT: To set the environment variable just run:

      export STARKNET_KEYSTORE="~/.starkli-wallets/keystore.json"
  3. Account Creation: In Starknet every account is a smart contract, so to create one it will need to be deployed.

    1. Initiate the account with the Open Zeppelin Account contract:

      starkli account oz init --keystore ~/.starkli-wallets/keystore.json ~/.starkli-wallets/account.json
    2. Deploy the account by running:

      starkli account deploy --keystore ~/.starkli-wallets/keystore.json ~/.starkli-wallets/account.json

      For the deployment starkli will ask you to fund an account. To do so you will need to fund the address given by starkli with the Goerli Starknet Faucet

  4. Setting Up Environment Variables: There are two primary environment variables vital for effective usage of Starkli’s CLI. These are the location of the keystore file for the Signer, and the location of the Account Descriptor file:

    export STARKNET_ACCOUNT=~/.starkli-wallets/account.json
    export STARKNET_KEYSTORE=~/.starkli-wallets/keystore.json

Declare and Deploy Contracts in Testnet

By following the previous two steps, you should now have a account funded on the Goerli testnet.

Now we are going to declare the contracts YASRouter, YASFactory, and YASPool. We are also going to deploy the contracts YASRouter and YASFactory on Testnet.

On Starknet, the deployment process is in two steps:

  • Declaring the class of your contract, or sending your contract’s code to the network
  • Deploying a contract or creating an instance of the previously declared code with the necessary parameters
  1. Updated .env file: Please modify the variables with your Testnet account and your RPC provider.

    ACCOUNT_ADDRESS=<ACCOUNT_ADDRESS>
    ACCOUNT_PRIVATE_KEY=<ACCOUNT_PRIVATE_KEY>
    ACCOUNT_SRC=~/.starkli-wallets/account.json
    RPC_URL=<STARKNET_RPC_HTTPS_URL>
  2. Build the project

    make build
  3. Declare and Deploy: We sequentially declare and deploy the contracts.

    make deploy

Note: To obtain your private key, you need to execute the following command:

starkli signer keystore inspect-private ~/.starkli-wallets/account.json

The contract address we see in the above command belongs to one of Katana's pre-funded accounts. Now we are ready to declare and deploy our contracts in Katana.

Declare and Deploy Contracts in Katana

Katana provides us with pre-funded accounts. We will use one of them for deployment. Katana generates the same accounts, and since we know the port it's running on, we can now complete the .env file.

  1. Updated .env file:

    # Katana Prefunded Account
    ACCOUNT_ADDRESS=0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973
    ACCOUNT_PRIVATE_KEY=0x1800000000300000180000000000030000000000003006001800006600
    ACCOUNT_SRC=~/.starkli-wallets/account_katana.json
    RPC_URL=http://0.0.0.0:5050
  2. Build the project

    make build
  3. Start Local Testnet

    make start-katana
  4. Setup Katana Account: This step, which involves setting up the Katana account, only needs to be done the first time.

    make setup-katana-account
  5. Declare and Deploy: We sequentially declare and deploy the contracts. Local deployment needs katana running. The account used for deployment is a pre-funded one.

    make deploy

Run local demo in Katana

This demo will perform the following steps:

  • Declaration of the following contracts: ERC20 Token, YASFactory, YASPool, and YASRouter.
  • Deployment of 2 ERC20 Tokens, YASFactory, YASPool, and YASRouter.
  • Initialization of YASPool with a 1:1 token price.
  • Execute approve() for the router to use tokens from the user.
  • Execute mint() within the range [-887220, 887220] with 2000000000000000000 tokens.
  • Execute swap() exchanging 500000000000000000 of token 0 for token 1.
  • Display current balances of both the pool and the user.
  1. Updated .env file:

    # Katana Prefunded Account
    ACCOUNT_ADDRESS=0x517ececd29116499f4a1b64b094da79ba08dfd54a3edaa316134c41f8160973
    ACCOUNT_PRIVATE_KEY=0x1800000000300000180000000000030000000000003006001800006600
    ACCOUNT_SRC=~/.starkli-wallets/account_katana.json
    RPC_URL=http://0.0.0.0:5050
  2. Build the project

    make build
  3. Start Local Testnet

    make start-katana
  4. Run Local Demo

    make demo-local

Contracts Starknet Testnet

  • Router: 0x06c3b1076e09b1d16642808b0bfef750a683ad06724f9e4f1aaaca17bc44fad5
  • Factory: 0x0490c81e19516eba9eb531b48e58f9876259ac9396444dc759a84e4a8aefa628
  • Pool (USDC-ETH): 0x016648670892a29b68b66c98892f408ab2e174ffb827c023ad9f4ca0eff816eb

Version Specifications

  • Cairo 2.2.0
  • Scarb v0.7.0
  • Starkli 0.1.9
  • Orion main branch (library from Giza)

Tooling

Useful resources

License

This project is licensed under the Apache 2.0 license. See LICENSE for more information.

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Roberto Catalan
Roberto Catalan

💻
Damián Piñones
Damián Piñones

💻
Santiago Galván (Dub)
Santiago Galván (Dub)

💻
Santiago Pittella
Santiago Pittella

💻
Francisco Strambini
Francisco Strambini

💻
Musa AbdulKareem
Musa AbdulKareem

💻
dblanco
dblanco

💻
Nadai
Nadai

💻
Daniel Bejarano
Daniel Bejarano

💻
ftupas
ftupas

💻

This project follows the all-contributors specification. Contributions of any kind welcome!

yet-another-swap's People

Contributors

0xd3bs avatar 0xjunger avatar dbejarano820 avatar dpinones avatar dubzn avatar franfiuba avatar ftupas avatar gianfranco99 avatar nadai2010 avatar rcatalan98 avatar santiagopittella avatar uri-99 avatar wisemrmusa 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

Watchers

 avatar  avatar  avatar

yet-another-swap's Issues

Refactor by adding a tick type to replace i32 for ticks

Currently there are no verifications that the ticks received are between the tick range defined in int24 in solidity. We should consider adding a tick type using i32 and adding a check or verifying always tick is used correctly.

FIX: i256 shifts.

Right now we are using the i256 with selected numbers that doesn't fail at shift.
But when using numbers near the i256::MIN and i256::MAX, the behavior isn't the expected.

The tests for this are existing but are failing, so ATM those are commented.

Add simple Factory Contract

The objective is to create a first approach to the Factory contract. This means that it will not interact with the Pool contract.
In more practical terms the PR should include:

  • YASFactory will include all the functionality from the UniswapV3Factory except the full logic from addPool that will be an empty function.
  • All the tests from UniswapV3Factory

Useful links:

Disclaimer: In our research for the design of this project we decided to avoid UniswapV3Deployer because we considered unnecessary. If you believe this is not the case include the UniswapV3Deployer Contract.

Add Error Handling for Libraries

We should avoid panics in the libraries and use a proper error handling mechanism. There needs to be some research done to see what's the current standard for Cairo libraries.
Check out this examples from the Cairo Book.

This are the files we should refactor:

The refactor consists on:

  • Making the functions of those libraries that use assert return Result instead.
  • Refactoring the other libraries or contracts that use the lower libraries. This is where the errors with pattern matching should be handled.
  • Refactoring the tests to try the errors.

This is an example of how errors from lower libraries should be handled.

Add #[key] tag to YASFactory contract

The #[key] tags were not added to the events of the YASFactory contract because when comparing the values in the tests, we encountered values that did not match our expectations. The issue would involve adding the #[key] tags (as needed) to the OwnerChanged, PoolCreated, FeeAmountEnabled events, understanding the process, and adapting the tests.

Some questions:

  • How are the values of the event attributes displayed when they have the #[key] tag?
  • Would the values lose their original meaning if we use them as keys?

Sources:
Contract Events

Add YASPool simple contract

The PR should include:

  • A Pool contract
  • Constructor that can be called from the factory
  • Storage variables initialized through the constructor

Add mint to YASPool

Implement mint to YASPool. You can check out the code from UniswapV3 Core.
Take into account we don't need all the functionality from the Oracle.
Blocked by: #88

Add Katana for local testing development

Katana is a tool designed by Dojo that allows developers to test Starknet locally. For this project we will use to test deployment and then interaction with the contracts by sending transactions.

The result of this work should include:

  • Makefile target to install Katana locally
  • Makefile target to deploy contracts on Katana (explain what you should modify in Starkli)
  • Makefile target to initiate the state of the contracts with liquidity for a specific Pool (it should include deploying ERC-20 contracts)
  • Documentation on the README on what is Katana and how can I use it in the project

Check out:

Add event indexer

In order to provide a good and responsive front end we need to index events from the smart contract and stream the data. There are a couple of options to choose from, right now Apibara seems to be the best option.

The project should include a backend that allows for the stream of relevant data to a frontend. In particular, there needs to be an indexer for the events emitted by our two core smart contracts (Pool and Factory).

Revisit errors in TickMath Library.

At the moment, we are using the errors as they are in Solidity('T' and 'R'), which aren't meaningful at all.

We need to revisit those errors and maybe replace it with more significant ones.

Update to Cairo 2.2.0

Bump Cairo to 2.2.0 and Scarb to 0.7.0. This should include the project and the CI.

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.