Swaps and Limit Orders | zkSync Documentation (2024)

  • Swaps and Limit Orders
    • Atomic swaps
      • Signing an order
      • Submitting a swap
    • Limit orders
      • Trading accounts
      • Signing limit orders
      • Filling a limit order
      • Note on limit order matching
      • Suggestions
      • Example
    • Utils

# Atomic swaps

Atomic swaps let you safely and cheaply swap funds with an existing zkSync account.

There are 3 steps required to successfully make a swap:

  • Sign an order that confirms that you want perform a certain swap
  • Acquire a signed order of the same format from the account that you want to swap with
  • Submit both orders with a fee to the zkSync server

# Signing an order

To sign an order, you need the following info:

  • token you want to swap
  • token you want to swap for
  • amount of the token that you want to swap
  • ratio of the swapped tokens, relevant to one another

Ratios are 15-byte integers that represent the proportion in which tokens are swapped.

To sign an order, use the getOrder method of Wallet:

const order = await wallet.getOrder({ tokenSell: 'ETH', tokenBuy: 'USDT', amount: tokenSet.parseToken('ETH', '2.5'), ratio: utils.tokenRatio({ ETH: 1, USDT: '4234.5' })});

An order can also include:

  • recipient - an address of an existing account, to which the result of a swap should be transferred, in case you wantto perform a swap-and-transfer. Defaults to self
  • validFrom and validUntil Unix timestamps which limit a timespan where a block with a swap can be processed
  • nonce that is going to be used for the swap

# Submitting a swap

Anyone can submit 2 orders for a swap if they meet the following limitations:

  • orders have matching tokens: if orderA specifies tokenA -> tokenB, then orderB should specify tokenB -> tokenA
  • ratios in orders are compatible: 1/orderB.ratio <= orderA.amount/orderB.amount <= orderA.ratio
  • if orders have recipients, their accounts already exist in zkSync

Fee is paid by the submitter, and the token it is paid in should be specified. After a swap is executed, nonce isincremented on both swapping accounts and the submitter. If swap was submitted from one of the swapping accounts, nonceis incremented only once.

If the user wishes to cancel the swap that has not yet been submitted, they simply have to increment their nonce (e.g.send a zero-transfer).

To submit a swap, use the syncSwap method of Wallet:

const swap = await wallet.syncSwap({ orders: [orderA, orderB], feeToken: 'wBTC'});

# Limit orders

Limit orders provide a way to exchange a certain token for another at a certain price. They are designed to be usedprimarily by other platforms that want to provide trustless and scalable exchange services.

The differences between an atomic swap and a limit order are:

  • limit orders infer the amount that can be exchanged directly from the balance
  • limit orders can be partially filled
  • limit orders do not increment account's nonce when partially filled

This means that once a limit order is signed for a certain token, the whole balance can potentially be

exchanged for another token (at a specified ratio). There is no way to limit the amount to be exchanged other than use aspecial trading account. :::

# Trading accounts

A trading account is an ordinary account that can be used to sign a limit order. It's function is to limit the amount ofa certain token that a user wants to exchange.

To do this, user has to:

  1. Transfer the desired amount of a desired token to a new account.
  2. Set a signing key for the account.
  3. Sign a limit order.

This way the limit order will exchange at most the amount you transferred to the trading account. Remaining balance onthe main account will be left untouched.

# Signing limit orders

To sign a limit order, use the getLimitOrder method of Wallet:

const order = await wallet.getLimitOrder({ tokenSell: 'ETH', tokenBuy: 'USDT', ratio: utils.tokenRatio({ ETH: 1, USDT: 3900 })});

# Filling a limit order

Limit order itself represents only a half of the swap operation. In order to be filled, the following criteria must bemet:

  • There exists a counterpart order (a normal order or a limit order) that fits the original order's tokens andbuy/sell ratio.
  • There exists someone willing to combine both orders into a swap operation and submit it.

Amounts that are being filled should be specified in the swap operation. Limit orders can be partially filled, soamounts can be different from actual balances, although must be compatible with the ratios specified in the orders. Fordetails, see example.

const swap = await wallet.syncSwap({ orders: [orderA, orderB], amounts: [tokenSet.parseToken('ETH', '2'), tokenSet.parseToken('UDST', '7800')], feeToken: 'wBTC'});

# Note on limit order matching

Performing atomic swaps is as simple as sharing a signed order message with a party that you want to swap with.

On the other hand, collecting and matching limit orders should probably be performed by platform(s) with an alreadyexisting userbase by some kind of matching engine. zkSync tries to be as generic as possible, so creating matchingengines is not considered - we will only provide an L2 framework that other platforms can integrate with.

# Suggestions

Trading accounts can be created as CREATE2 accounts. This approach has the following benefits:

  • Setting a signing key on a CREATE2 account is cheaper
  • Salt argument in CREATE2 can be used to deterministically generate trading account addresses for a certain mainaccount
  • Same L2 private key can be used for all trading accounts and the main account if desired. Although this bears somerisks (compromising a single account would mean compromising all of them), key management can be inconvenient in somesituations.

Should a platform decide to use CREATE2 for trading accounts, it will have to choose a contract bytecode to be used foraddress calculation. The contract should be open-source and have full exit and withdrawal functionality since in therare case of censorship users will have to deploy it to rescue their funds.

It is also suggested to reuse trading accounts on which orders were filled or cancelled since this way a signing keywould not have to be set again.

# Example

This section provides an example of how ratios specified in orders affect account balances.

Let the swap consist of 2 limit orders:

// walletA wants to swap wBTC for ETH at a ratio 2:5const orderA = await walletA.getLimitOrder({ tokenSell: 'wBTC', tokenBuy: 'ETH', ratio: utils.tokenRatio({ wBTC: 2, ETH: 5 })});// walletB wants to swap ETH for wBTC at a ratio 4:1const orderB = await walletB.getLimitOrder({ tokenSell: 'ETH', tokenBuy: 'wBTC', ratio: utils.tokenRatio({ wBTC: 1, ETH: 4 })});

Specified ratios mean that:

  • walletA expects to get 2.5 ETH for each wBTC (or more)
  • walletB expects to get 0.25 wBTC for each ETH (or more)

Ratios are compatible, because at either ratio (or in between), both parties will be happy:

  • at orderB's ratio walletA will get 4 ETH per wBTC, which is more than expected
  • at orderA's ratio walletB will get 0.4 wBTC per ETH, which is more than expected

Now let's actually submit the swap, and pick a ratio in between - 3 ETH per wBTC:

const swap = await walletC.syncSwap({ orders: [orderA, orderB], amounts: [tokenSet.parseToken('wBTC', '100'), tokenSet.parseToken('ETH', '300')], feeToken: 'USDT'});

This will exchange 100 wBTC from walletA for 300 ETH from walletB. For detailed information, see table below.

walletA walletB
wBTC before swap 100 0
ETH before swap 0 300
Expected wBTC after swap 0 75
Expected ETH after swap 250 0
Actual wBTC after swap 0 100
Actual ETH after swap 300 0

# Utils

To construct a ratio, use either of the two utility functions:

  • tokenRatio constructs a ratio relevant to the tokens themselves, so { ETH: 4, wBTC: 1 } would mean you want 4 ETHfor each wBTC.
  • weiRatio constructs a ratio relevant to the lowest denomination of the token, so { ETH: 4, wBTC: 1 } would meanyou want 4 wei (10-18 ETH) for each satoshi (10-8 wBTC).

If tokens symbols or IDs are contained in variables, use the following syntax to pass them into ratio objects:

utils.tokenRatio({ [tokenA]: valueA, [tokenB]: valueB});

For more information, visit the API reference.

Edit this page (opens new window)

Last Updated: 6/3/2022, 11:20:50 AM

Swaps and Limit Orders | zkSync Documentation (2024)

FAQs

Can I swap tokens on zkSync? ›

Select tokens you want to exchange in the 'Transfer from' field. In the 'Transfer to' field, select the token that you would like to get on zkSync, and enter the recipient address. Please note: by default, the recipient's address is the same as the sender's address.

What is a limit order on zkSync? ›

Limit order system

On zkSync Lite you can only have one order open at a time. If you have a limit order open, you can't place other orders on that account. If you make a normal transaction (sending funds from your account) or a bridge transaction, your limit order will be cancelled.

What is an atomic swap? ›

Atomic swaps are a way for two people to trade tokenized assets across different blockchain networks without relying on a centralized intermediary to facilitate the transaction. This provides DeFi users with a way to maintain high levels of decentralization as they move across the multi-chain Web3 ecosystem.

Does zkSync have a native token? ›

zkSync token

ZkSync currently has no native token to its name, but investors should anticipate something from the zero-knowledge proof systems. For instance, one can note from their official page that zkSync would create tokens for investors to stake with and to become zkSync network validators.

Can you swap tokens on ledger? ›

Cripto InterCambio (CIC) is a LATAM exchange that offers swapping service for major coins and tokens, including ETH, BNB, ERC-20 and BEP-20 tokens. You can now swap crypto with CIC through Ledger Live both on desktop and mobile. The exchange will appear as one of the service providers.

Is a limit order worth it? ›

A limit order works better when:

If you're looking to get a specific price for your stock, a limit order will ensure that the trade does not happen unless you get that price or better. You are able to wait for your price. If your limit price is not the market price, you'll probably have to wait to have it filled.

Is limit order risky? ›

The risk inherent to limit orders is that should the actual market price never fall within the limit order guidelines, the investor's order may fail to execute. Another possibility is that a target price may finally be reached, but there is not enough liquidity in the stock to fill the order when its turn comes.

What is a good through limit order? ›

Good through is a type of limit order used to buy or sell a security or asset at a certain price for a specified period of time. Good through orders are an example of time in force orders.

Does Uniswap support zkSync? ›

A new Era has dawned with the launch of the first Ethereum Virtual Machine compatible ZK rollup, enabling projects like Uniswap and Sushi to easily port over for scaling.

Where can I swap my tokens? ›

Some popular exchanges are:
  • Uniswap.
  • Sushiswap.
  • 1Inch.
  • Curve.

Can I swap any token? ›

What tokens can users trade? The Token Swap has specifically been designed to support ERC-20 and BEP-20 tokens. These are fungible tokens on Ethereum, Polygon and Binance Smart Chain. You can technically swap any token following the ERC-20 standard on each of these blockchains.

Can you swap ERC20 tokens on ledger? ›

Swap ERC20 tokens

Through Ledger Live, you can swap one asset for another without using fiat currencies. Head over to Swap. On the Exchange page, select the accounts that you want to swap. Enter the desired amount and click Exchange.

Top Articles
Latest Posts
Article information

Author: Kareem Mueller DO

Last Updated:

Views: 6046

Rating: 4.6 / 5 (66 voted)

Reviews: 81% of readers found this page helpful

Author information

Name: Kareem Mueller DO

Birthday: 1997-01-04

Address: Apt. 156 12935 Runolfsdottir Mission, Greenfort, MN 74384-6749

Phone: +16704982844747

Job: Corporate Administration Planner

Hobby: Mountain biking, Jewelry making, Stone skipping, Lacemaking, Knife making, Scrapbooking, Letterboxing

Introduction: My name is Kareem Mueller DO, I am a vivacious, super, thoughtful, excited, handsome, beautiful, combative person who loves writing and wants to share my knowledge and understanding with you.