Ambient Docs
  • Introduction to Ambient
  • Concepts
    • AMMs
    • Concentrated Liquidity
    • Knockout Liquidity
    • Governance & Policy
    • Surplus Collateral
    • Permissioned Pools
    • Account Abstraction
  • Users
    • Swaps
    • LP Positions
    • Surplus Collateral
    • Dynamic Fees
    • Knockout Positions
    • Initializing Pools
    • Gasless Transactions
    • External Routers
  • Governance & Policy
    • Ops & Treasury Multisigs
    • Policy
    • Upgrading Code
    • Emergency
  • Developers
    • Deployed Contracts
    • Token Transfers
    • Type Conventions
    • DEX Contract Interface
      • userCmd Callpaths
      • Swaps
        • Hot Path Swap Migration
          • Directly Calling CrocSwapDex
          • External Swap Router
      • Flat LP Calls
      • Long Form Orders
      • Knockout LP Calls
      • Pool Initialization
      • Surplus Collateral
      • Router Calls
      • Relayer Calls
    • Query Contracts
      • CrocQuery Contract
      • CrocImpact Contract
    • Logs and Queries
    • Libraries and APIs
      • SDK
      • Indexer API
      • Subgraph
Powered by GitBook
On this page
  • Solidity
  • Javascript
  1. Developers
  2. DEX Contract Interface
  3. Swaps
  4. Hot Path Swap Migration

Directly Calling CrocSwapDex

The most gas efficient approach is for users to directly call the CrocSwapDex contract. However with the swap path upgrade, swappers must use the userCmd()function.

This method takes the same arguments as swap()but requires pre-formatting the arguments to the call into an ABI byte array. userCmd() should be called with a proxy index of 1 (the swap callapth) and the standard swap arguments ABI encoded.

Solidity

// Ethereum
address dex = 0xAaAaAAAaA24eEeb8d57D431224f73832bC34f688

uint16 SWAP_PROXY = 1

CrocSwapDex(dex).userCmd(SWAP_PROXY, abi.encode(
	base, 
  quote,
  poolIdx, 
  isBuy, 
  inBaseQty, 
  qty, 
  tip,
  limitPrice, 
  minOut,
  settleFlags));

------------------------------------------------------------
// Scroll
address dex = 0xAaAaAAAaA24eEeb8d57D431224f73832bC34f688

uint16 SWAP_PROXY = 1

CrocSwapDex(dex).userCmd(SWAP_PROXY, abi.encode(
	base, 
  quote,
  poolIdx, 
  isBuy, 
  inBaseQty, 
  qty, 
  tip,
  limitPrice, 
  minOut,
  settleFlags));

Javascript

const { ethers } = require('ethers');
import { AbiCoder } from "ethers/lib/utils";

const provider = new ethers.providers.JsonRpcProvider(...);

const routerAbi = ... // See below

const etherDexAddr = "0xAaAaAAAaA24eEeb8d57D431224f73832bC34f688"
const scrollDexAddr = "0xAaAaAAAaA24eEeb8d57D431224f73832bC34f688"

const contract = new ethers.Contract(etherRouterAddr, routerAbi, provider);

const abi = new ethers.utils.AbiCoder()
const cmd = abi.encode([
	"address", 
	"address", 
	"uint256", 
	"bool", 
	"bool", 
	"uint128", 
	"uint16", 
	"uint128", 
	"uint128", 
	"uint8"], 
[
	base, 
  quote,
  420, 
  isBuy, 
  inBaseQty, 
  qty, 
  0,
  limitPrice, 
  minOut,
  settleFlags])

contract.userCmd(1, cmd)

ABI

[{
      "inputs": [
        {
          "internalType": "uint16",
          "name": "callpath",
          "type": "uint16"
        },
        {
          "internalType": "bytes",
          "name": "cmd",
          "type": "bytes"
        }
      ],
      "name": "userCmd",
      "outputs": [
        {
          "internalType": "bytes",
          "name": "",
          "type": "bytes"
        }
      ],
      "stateMutability": "payable",
      "type": "function"
    }]
PreviousHot Path Swap MigrationNextExternal Swap Router

Last updated 1 year ago