Swap Crypto
This guide demonstrates how to perform token swaps and cross-chain swaps using the Notus API. You will learn how to retrieve swap quotes, execute swaps, and cross-swaps.
1. Initialize Wallet AccountCopied!
Use your private key to initialize a wallet account. This will allow you to sign messages and interact with the blockchain.
import { privateKeyToAccount } from 'viem/accounts'
const BRZ_POLYGON = '0x4eD141110F6EeeAbA9A1df36d8c26f684d2475Dc'
const USDC_POLYGON = '0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359'
const USDC_ARB = "0xaf88d065e77c8cc2239327c5edb3a432268e5831";
const privKey = '0x<private-key>'
const account = privateKeyToAccount(privKey)
2. Get Smart Wallet AddressCopied!
Before transferring tokens, you need to register and retrieve the smart wallet address for Account Abstraction.
import { useState } from "react";
export default function App() {
const [accountAbstraction, setAccountAbstraction] = useState("");
const getSmartWalletAddress = async () => {
const FACTORY_ADDRESS = "0x0000000000400CdFef5E2714E63d8040b700BC24";
let res = await fetch(`https://<baseUrl>/api/v1/wallets/register`, {
method: "POST",
headers: {
"Content-Type": "application/json",
"x-api-key": "<api-key>",
},
body: JSON.stringify({
externallyOwnedAccount: externallyOwnedAccount,
factory: FACTORY_ADDRESS,
salt: "0",
}),
});
if (!res?.ok) {
return
}
const data = await res.json();
setAccountAbstraction(data.wallet.accountAbstraction);
};
};
3. Generate a Swap QuoteCopied!
Query a swap quote for token swaps on the same chain (e.g., USDC to BRZ on Polygon).
const swapParams = {
payGasFeeToken: USDC_POLYGON,
tokenIn: USDC_POLYGON,
tokenOut: BRZ_POLYGON,
amountIn: "5",
walletAddress: accountAbstraction,
toAddress: accountAbstraction,
signerAddress: externallyOwnedAccount,
chainIdIn: 137,
chainIdOut: 137,
gasFeePaymentMethod: "DEDUCT_FROM_AMOUNT",
};
const { data } = await fetch('http://<baseUrl>/api/v1/crypto/swap', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': '<api-key>',
},
body: JSON.stringify(swapParams),
}).then((res) => res.json())
If you are swapping from a native token (e.g., ETH, BNB, AVAX), use the following address as
tokenIn
:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
. This ensures that the Notus API recognizes the asset as a native token.
4.Generate a Cross-Swap QuoteCopied!
Query a quote for cross-chain swaps (e.g., USDC on Arbitrum to USDC on Polygon).
const swapParams = {
payGasFeeToken: USDC_ARB,
tokenIn: USDC_ARB,
tokenOut: USDC_POLYGON,
amountIn: "5",
walletAddress: accountAbstraction,
toAddress: accountAbstraction,
signerAddress: externallyOwnedAccount,
chainIdIn: 42161,
chainIdOut: 137,
gasFeePaymentMethod: "DEDUCT_FROM_AMOUNT",
};
const { data } = await fetch('http://<baseUrl>/api/v1/crypto/cross-swap', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': '<api-key>',
},
body: JSON.stringify(crossSwap),
}).then((res) => res.json())
For cross-chain swaps involving a native token, use the following address as
tokenIn
:0xeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
. This ensures that the Notus API recognizes the asset as a native token.
5.Execute Swap or Cross-SwapCopied!
Once you have a quote, sign and execute the swap or cross-swap operation.
const signature = await account.signMessage({
message: {
raw: data.quoteId,
},
})
const res = await fetch('http://<baseUrl>/api/v1/crypto/execute-user-op', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'x-api-key': '<api-key>',
},
body: JSON.stringify({ signature, quoteId: data.quoteId }),
}).then((res) => res.json())