🇺🇲
Protocolink
  • 🔮Overview
  • 🌟Why Protocolink?
  • 💡Use Cases
    • Flash Loans
    • Permit2 Amplifier
    • Zap-in & Zap-out
    • Position Management
    • Multi-Action Bundling
  • 📋Networks & Protocols
  • 🛡️Security & Audits
  • 💎Fees
  • ❓FAQ
  • Protocolink API
    • 🔮Overview
      • Swap & Supply (Example)
    • 📗Swagger
      • Request Protocols
      • Request Tokens
      • Request Quote
      • Estimate Logics Result
      • Request Transaction Data
  • Protocolink SDK
    • 🔮Overview
    • 1️⃣Install SDK
    • 2️⃣Build Logics
    • 3️⃣Estimate Router Data
    • 4️⃣Send Router Transaction
    • ⚒️API SDK Interfaces
      • Global Types
      • FlashLoan Logic
      • Aave V2
      • Aave V3
      • Balancer V2
      • Compound V3
      • Iolend
      • Magicsea
      • Morphoblue
      • OpenOcean V2
      • ParaSwap V5
      • Permit2
      • Spark
      • Stargate
      • Stargate V2
      • SyncSwap
      • Uniswap V3
      • Utility
      • Wagmi
      • ZeroEx V4
    • ⚒️Common SDK Interfaces
      • Constants
      • Network
      • Token
      • Web3Toolkit
      • Utility Functions
  • Lending SDK
    • 🔮Overview
    • ✳️SDK
      • Open By Collateral
      • Open By Debt
      • Close
      • Leverage By Collateral
      • Leverage By Debt
      • Deleverage
      • Collateral swap
      • Debt swap
      • Zap supply
      • Zap withdraw
      • Zap repay
      • Zap borrow
  • Smart Contract
    • 🔮Overview
      • Router
      • Agent
      • Callback
      • Fees
      • Utility
      • Data Type
      • ERC721/ERC1155 Support
    • 📑Deployment Addresses
    • 🧑‍💻Security Review Details
  • COMPOUND KIT
    • 🔮Overview
    • ✳️SDK
      • Leverage
      • Deleverage
      • Collateral Swap
      • Zap Supply
      • Zap Withdraw
      • Zap Repay
      • Zap Borrow
    • 📗API
  • Video Tutorials
    • 1. Introducing Protocolink
    • 2. Lending SDK
    • 3. Protocolink Q&A
  • Social medias
    • Twitter
  • Support
Powered by GitBook
On this page
  • 5. Preview the estimated post-collateral-swap portfolio
  • 6. Obtain the required approval permission and send the router transaction
  1. Lending SDK
  2. SDK

Collateral swap

PreviousDeleverageNextDebt swap

Last updated 1 year ago

Continuing from .

Collateral swap enables users to replace one collateral asset with another in a single step by using a flash loan.

  • Source token: the collateral token to be swapped from.

  • Destination token: the collateral token to be swapped for.

5. Preview the estimated post-collateral-swap portfolio

By specifying the source token, the source token amount, and the destination token, the function will return the destination token amount, the updated user portfolio, and the logics to be executed.

// User obtains a quotation for swapping the collateral from src token to dest token
const srcToken = mainnetTokens.WBTC;
const srcAmount = '1000';
const destToken = mainnetTokens.WETH;
const collateralSwapInfo = await adapter.collateralSwap({
  account,
  portfolio,
  srcToken,
  srcAmount,
  destToken
});

The logic should include:

  1. Borrow a flash loan of the source token

  2. Swap the source token for the destination token

  3. Deposit the destination token

  4. Return the destination protocol token (aToken) to the user

  5. Get the source protocol token (aToken) from the user

  6. Withdraw the source token

  7. Repay the flash loan token with the source token

6. Obtain the required approval permission and send the router transaction

To perform the logics, certain approvals need to be processed. You may refer to Estimate Router Data and Send Router Transaction for more details.

// User needs to permit the Protocolink user agent to borrow for the user
const estimateResult = await apisdk.estimateRouterData(
  { chainId, account, logics: collateralSwapInfo.logics }
);

// User obtains a collateral swap transaction request
const routerData: apisdk.RouterData = {
  chainId,
  account,
  logics: collateralSwapInfo.logics
};
const transactionRequest = await apisdk.buildRouterTransactionRequest(routerData);
✳️
#id-4.-select-an-use-case