How to integrate Madhouse

Integrate Madhouse swap functionality into your application in 3 simple steps: request a quote, approve tokens (if needed), and execute the swap.

Step 1: Request a Quote

To get a swap quote, send a GET request to the Madhouse API with the required parameters.

Endpoint:

GET https://api.madhouse.ag/swap/v1/quote

Required Parameters:

Parameter
Type
Description
Example

chain

string

Chain ID

10143 (Monad Testnet)

tokenIn

string

Address of input token (token to sell)

0x760AfE86e5de5fa0Ee542fc7B7B713e1c5425701

tokenOut

string

Address of output token (token to buy)

0xf817257fed379853cDe0fa4F97AB987181B1E5Ea

amountIn

string

Amount of input token in smallest unit

1000000000000000000 (1 token with 18 decimals)

slippage

number

Maximum acceptable slippage as decimal

0.005 (0.5%)

Optional Parameters:

Parameter
Type
Description
Example

protocols

string

Comma-separated list of protocols for routing

uniswap-v2,uniswap-v3

includeTokenInfo

boolean

Include detailed token information

true

includePoolInfo

boolean

Include pool information

true

integratorAddress

string

Your wallet address to receive integration fees

0xYourAddress...

integratorFee

number

Your fee percentage (required if integratorAddress is set)

  • Format: Decimal number between 0 and 0.05

  • Range: 0% to 5%

  • 0.01 = 1%

  • 0.025 = 2.5%

  • 0.05 = 5%

Defined in src/api/types.ts:41-42: export const MIN_INTEGRATOR_FEE = 0; // 0% export const MAX_INTEGRATOR_FEE = 0.05; // 5%

Understanding Integrator Fees:

As an integrator, you can earn fees on every swap executed through your application:

  • integratorFee is specified in decimal format:

    • 0.01 = 1%

    • 0.025 = 2.5%

    • 0.05 = 5%

  • The fee is deducted from amountIn before the swap

  • Example: User wants to swap 100 USDC with integratorFee= 0.01 (1%):

    • You receive: 1 USDC (sent to your integratorAddress)

    • Swap amount: 99 USDC actually gets swapped for tokenOut

    • User receives: Whatever 99 USDC swaps to in tokenOut

  • integratorAddress is required when integratorFee > 0

Fee Calculation Examples:

Important Notes:

  • Token Amounts: Always use smallest unit (e.g., 1 USDC with 6 decimals = 1000000, 1 WMON with 18 decimals = 1000000000000000000)

  • Slippage: Specified as decimal (e.g., 0.005 = 0.5%, 0.01 = 1%)

  • Fee Scale: Use decimal number for fees (e.g., 0.01 = 1%, 0.005 = 0.5%)

Example Request:


Step 2: Understanding the Response

The API returns transaction data and swap details:

Key Fields:

  • tx.to - Contract address to send the transaction to

  • tx.data - Encoded swap instructions

  • tx.value - Native token amount to send (usually "0" for ERC-20 swaps)

  • amountOut - Estimated tokens you'll receive (in smallest unit)

  • routes - Array of DEX protocols and pools used for the swap


Step 3: Set Token Allowance (ERC-20 Only)

For ERC-20 token swaps, approve the Madhouse contract to spend your tokens. Skip this step for native token swaps.


Step 4: Execute the Swap

Once you have the quote and approval (if needed), send the transaction to execute the swap.

Using ethers.js:

Using web3.js:

Using viem:


Complete Integration Example


Error Handling

Common API errors and their meanings:

Status Code
Error
Solution

400

Invalid parameters

Check token addresses and amounts

404

No route found

Try different token pair or smaller amount

429

Rate limit exceeded

Wait and retry

500

Server error

Retry after a short delay

Last updated