> ## Documentation Index
> Fetch the complete documentation index at: https://docs.saucerswap.finance/llms.txt
> Use this file to discover all available pages before exploring further.

# Swap Quote

> Get a token swap quote based on an input or output value

Below are the two methods to get a quote for a swap from a given route using JSON-RPC or REST-API:

* [Get output quote from exact input amount](/v/developer/saucerswap-v1/swap-operations/swap-quote#get-output-quote-from-input-amount)
* [Get input quote from exact output amount](/v/developer/saucerswap-v1/swap-operations/swap-quote#get-input-quote-from-output-amount)

<Info>
  **Contract ID:** [SaucerSwapV1RouterV3](https://hashscan.io/mainnet/contract/0.0.3045981)
</Info>

<Tip>
  When providing HBAR in the path array, use the wrapped HBAR token ID ([WHBAR](https://hashscan.io/mainnet/token/0.0.1456986)).
</Tip>

## Get Output Quote from Input Amount

⛽ *No gas cost*

Get the output amounts from a given input amount and a swap route.

Function name: **getAmountsOut**

| Parameter Name    | Description                                 |
| ----------------- | ------------------------------------------- |
| *uint amountIn*   | The input token amount in its smallest unit |
| *address\[] path* | An ordered list of token EVM addresses      |

<CodeGroup>
  ```solidity IUniswapV2Router01.sol theme={null}
  function getAmountsOut(
    uint amountIn,
    address[] calldata path
  ) external view returns (uint[] memory amounts);
  ```

  ```solidity UniswapV2Router02.sol theme={null}
  function getAmountsOut(uint amountIn, address[] memory path)
    public
    view
    virtual
    override
    returns (uint[] memory amounts)
  {
    return UniswapV2Library.getAmountsOut(factory, amountIn, path);
  }
  ```
</CodeGroup>

### Code Overview

<Tabs>
  <Tab title="JSON RPC">
    **Resources:**

    * [SaucerSwap deployed contract IDs](/developerx/contract-deployments)
    * [Ethers.js docs (v6)](https://docs.ethers.org/v6/)
    * [Hedera JSON RPC Relay](https://docs.hedera.com/hedera/core-concepts/smart-contracts/deploying-smart-contracts/json-rpc-relay)

    ```typescript Typescript theme={null}
    import * as ethers from 'ethers'; //V6

    //Set one of Hedera's JSON RPC Relay as the provider
    const provider = new ethers.JsonRpcProvider(hederaJsonRelayUrl, '', {
      batchMaxCount: 1, //workaround for V6
    });

    //ABI data for the getAmountsOut
    const abi = ['function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'];

    //Load the ABI
    const abiInterfaces = new ethers.Interface(abi);

    //Example route
    const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
    const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
    const route = [tokenIn, tokenOut];

    const routerContract = new ethers.Contract(routerEvmAddress, abiInterfaces.fragments, provider);
    const result = await routerContract.getAmountsOut(inputAmountInSmallestUnit, route);
    const amounts = result; //uint[] amounts
    const finalOutputAmount = amounts[amounts.length - 1]; //in token's smallest unit
    ```
  </Tab>

  <Tab title="REST API">
    **Resources:**

    * [SaucerSwap deployed contract IDs](/developerx/contract-deployments)
    * [Ethers.js docs (v6)](https://docs.ethers.org/v6/)
    * [Hedera REST API](https://docs.hedera.com/hedera/sdks-and-apis/rest-api)

    ```typescript Typescript theme={null}
    import * as ethers from 'ethers'; //V6

    //ABI data for the getAmountsOut
    const abi = ['function getAmountsOut(uint amountIn, address[] calldata path) external view returns (uint[] memory amounts)'];

    //Load the ABI
    const abiInterfaces = new ethers.Interface(abi);

    //Example route (WHBAR > SAUCE)
    const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
    const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
    const route = [tokenIn, tokenOut];

    const routerContract = ContractId.fromString(routerContractId);
    const params = [inputAmountInSmallestUnit, route];
    const encodedData = abiInterfaces.encodeFunctionData(abiInterfaces.getFunction('getAmountsOut')!, params);    

    const url = `${mirrorNodeBaseUrl}/api/v1/contracts/call`;
    const data = {
      'block': 'latest',
      'data': encodedData,
      'to': routerContract.toSolidityAddress(),
    };
     
    const response = await axios.post(url, data, { headers: {'content-type': 'application/json'} });
    const amounts = abiInterfaces.decodeFunctionResult('getAmountsOut', response.data.result)[0]; //uint[] amounts
    const finalOutputAmount = amounts[amounts.length - 1]; //in token's smallest unit

    ```
  </Tab>
</Tabs>

***

## Get Input Quote from Output Amount

⛽ *No gas cost*

Get the input amounts from a given output amount and a swap route.

Function name: **getAmountsIn**

| Parameter Name    | Description                                  |
| ----------------- | -------------------------------------------- |
| *uint amountOut*  | The output token amount in its smallest unit |
| *address\[] path* | An ordered list of token EVM addresses       |

<CodeGroup>
  ```solidity IUniswapV2Router01.sol theme={null}
  function getAmountsIn(
    uint amountOut,
    address[] calldata path
  ) external view returns (uint[] memory amounts);
  ```

  ```solidity UniswapV2Router02.sol theme={null}
  function getAmountsIn(uint amountOut, address[] memory path)
    public
    view
    virtual
    override
    returns (uint[] memory amounts)
  {
    return UniswapV2Library.getAmountsIn(factory, amountOut, path);
  }
  ```
</CodeGroup>

### Code Overview

<Tabs>
  <Tab title="JSON RPC">
    **Resources:**

    * [SaucerSwap deployed contract IDs](/developerx/contract-deployments)
    * [Ethers.js docs (v6)](https://docs.ethers.org/v6/)
    * [Hedera JSON RPC Relay](https://docs.hedera.com/hedera/core-concepts/smart-contracts/deploying-smart-contracts/json-rpc-relay)

    ```typescript Typescript theme={null}
    import * as ethers from 'ethers'; //V6

    const networkId = 'testnet';
    const provider = new ethers.JsonRpcProvider(`https://${networkId}.hashio.io/api`, '', {
      batchMaxCount: 1,
    });

    //ABI data for the getAmountsIn
    const abi = ['function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts)'];

    //Load the ABI
    const abiInterfaces = new ethers.Interface(abi);

    //Example route (WHBAR > SAUCE)
    const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
    const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
    const route = [tokenIn, tokenOut];

    const routerContract = new ethers.Contract(routerEvmAddress, abiInterfaces.fragments, provider);
    const result = await routerContract.getAmountsIn(outputAmountInSmallestUnit, route);
    const amounts = results; //uint[] amounts
    const finalInputAmount = amounts[0]; //in token's smallest unit
    ```
  </Tab>

  <Tab title="REST API">
    **Resources:**

    * [SaucerSwap deployed contract IDs](/developerx/contract-deployments)
    * [Ethers.js docs (v6)](https://docs.ethers.org/v6/)
    * [Hedera REST API](https://docs.hedera.com/hedera/sdks-and-apis/rest-api)

    ```typescript Typescript theme={null}
    import * as ethers from 'ethers'; //V6

    //ABI data for the getAmountsIn
    const abi = ['function getAmountsIn(uint amountOut, address[] calldata path) external view returns (uint[] memory amounts)'];

    //Load the ABI
    const abiInterfaces = new ethers.Interface(abi);

    //Example route (WHBAR > SAUCE)
    const tokenIn = '0x' + TokenId.fromString(whbarTokenId).toSolidityAddress();
    const tokenOut = '0x' + TokenId.fromString(sauceTokenId).toSolidityAddress();
    const route = [tokenIn, tokenOut];

    const routerContract = ContractId.fromString(routerContractId);
    const params = [outputAmountInSmallestUnit, route];
    const encodedData = abiInterfaces.encodeFunctionData(abiInterfaces.getFunction('getAmountsIn')!, params);    

    const url = `${mirrorNodeBaseUrl}/api/v1/contracts/call`;
    const data = {
      'block': 'latest',
      'data': encodedData,
      'to': routerContract.toSolidityAddress(),
    };
     
    const response = await axios.post(url, data, { headers: {'content-type': 'application/json'} });
    const amounts = abiInterfaces.decodeFunctionResult('getAmountsIn', response.data.result)[0]; //uint[] amounts
    const finalInputAmount = amounts[0]; //in token's smallest unit
    ```
  </Tab>
</Tabs>

###
