Leverage

5. Preview the Estimated Post-Leverage Position & Approval Permissions

Specify the collateralToken and collateralAmount that the user wants to leverage. Then It returns a quotation used for getting the estimated position. approvals need to be signed and submitted on-chain. logics is the detailed steps used for building transaction. Logics workflow is as follows:

  1. Initiates a flash loan of the base token.

  2. Swap the base token to the leverage token.

  3. Supply the leverage token for the user.

  4. Borrow the base token.

  5. Repay the flash loan.

import * as compoundkit from '@protocolink/compound-kit';
import * as common from '@protocolink/common';
import * as logics from '@protocolink/logics';

const chainId = common.ChainId.polygon;
const marketId = compoundkit.MarketId.USDC;
const params = {
  account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa',
  collateralToken: logics.compoundv3.polygonTokens.WETH,
  collateralAmount: '1',
  slippage: 100,
};
const leverageQuotation = await compoundkit.getLeverageQuotation(chainId, marketId, params);
// {
//   "quotation": {
//     "leverageTimes": "0.02",
//     "currentPosition": {
//       "utilization": "0.6526",
//       "healthRate": "1.64",
//       "liquidationThreshold": "0.7774",
//       "supplyUSD": "0",
//       "borrowUSD": "76852.19",
//       "collateralUSD": "161883.97",
//       "netAPR": "-0.0434"
//     },
//     "targetPosition": {
//       "utilization": "0.6601",
//       "healthRate": "1.62",
//       "liquidationThreshold": "0.7779",
//       "supplyUSD": "0",
//       "borrowUSD": "78644.65",
//       "collateralUSD": "163675.97",
//       "netAPR": "-0.0444"
//     }
//   },
//   "fees": [
//     {
//       "rid": "utility:flash-loan-aggregator",
//       "feeAmount": {
//         "token": {
//           "chainId": 137,
//           "address": "0x0000000000000000000000000000000000001010",
//           "decimals": 18,
//           "symbol": "MATIC",
//           "name": "Matic Token"
//         },
//         "amount": "1.451848970467797743"
//       }
//     },
//     {
//       "rid": "compound-v3:borrow",
//       "feeAmount": {
//         "token": {
//           "chainId": 137,
//           "address": "0x0000000000000000000000000000000000001010",
//           "decimals": 18,
//           "symbol": "MATIC",
//           "name": "Matic Token"
//         },
//         "amount": "5.80739750171045147"
//       }
//     }
//   ],
//   "approvals": [
//     {
//       "to": "0xF25212E676D1F7F89Cd72fFEe66158f541246445",
//       "data": "0x110496e50000000000000000000000000e7c086090b361c955950956105df200b20f66d70000000000000000000000000000000000000000000000000000000000000001"
//     }
//   ],
//   "logics": [
//     {
//       "rid": "utility:flash-loan-aggregator",
//       "fields": {
//         "id": "e8c17e8d-3efc-4c68-92bf-5ac9ba82f41e",
//         "protocolId": "balancer-v2",
//         "loans": [
//           {
//             "token": {
//               "chainId": 137,
//               "address": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
//               "decimals": 6,
//               "symbol": "USDC",
//               "name": "USD Coin (PoS)"
//             },
//             "amount": "1792.584826"
//           }
//         ],
//         "isLoan": true
//       }
//     },
//     {
//       "rid": "paraswap-v5:swap-token",
//       "fields": {
//         "input": {
//           "token": {
//             "chainId": 137,
//             "address": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
//             "decimals": 6,
//             "symbol": "USDC",
//             "name": "USD Coin (PoS)"
//           },
//           "amount": "1792.584826"
//         },
//         "output": {
//           "token": {
//             "chainId": 137,
//             "address": "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619",
//             "decimals": 18,
//             "symbol": "WETH",
//             "name": "Wrapped Ether"
//           },
//           "amount": "1"
//         },
//         "slippage": 100
//       }
//     },
//     {
//       "rid": "compound-v3:supply-collateral",
//       "fields": {
//         "marketId": "USDC",
//         "input": {
//           "token": {
//             "chainId": 137,
//             "address": "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619",
//             "decimals": 18,
//             "symbol": "WETH",
//             "name": "Wrapped Ether"
//           },
//           "amount": "1"
//         },
//         "balanceBps": 10000
//       }
//     },
//     {
//       "rid": "compound-v3:borrow",
//       "fields": {
//         "marketId": "USDC",
//         "output": {
//           "token": {
//             "chainId": 137,
//             "address": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
//             "decimals": 6,
//             "symbol": "USDC",
//             "name": "USD Coin (PoS)"
//           },
//           "amount": "1792.584826"
//         }
//       }
//     },
//     {
//       "rid": "utility:flash-loan-aggregator",
//       "fields": {
//         "id": "e8c17e8d-3efc-4c68-92bf-5ac9ba82f41e",
//         "protocolId": "balancer-v2",
//         "loans": [
//           {
//             "token": {
//               "chainId": 137,
//               "address": "0x2791Bca1f2de4661ED88A30C99A7a9449Aa84174",
//               "decimals": 6,
//               "symbol": "USDC",
//               "name": "USD Coin (PoS)"
//             },
//             "amount": "1792.584826"
//           }
//         ],
//         "isLoan": false
//       }
//     }
//   ]
// }

6. Obtain Transaction Data for Execution

Provides transaction data that is needed to execute this operation. Armed with the logics from the previous step, it generates the to, data and value including fees then signs and submits to finalize the operation.

If you wish to include a referral for fee sharing, you can append the referral property to the routerData object. For detailed information on using routerData, please refer to the Router Data Documentation.

import * as compoundkit from '@protocolink/compound-kit';
import * as common from '@protocolink/common';
import * as apisdk from '@protocolink/api';

const routerData: apisdk.RouterData = {
  chainId: common.ChainId.polygon,
  account: '0xaAaAaAaaAaAaAaaAaAAAAAAAAaaaAaAaAaaAaaAa',
  logics: leverageQuotation.logics,
  referral: '0xbBbBBBBbbBBBbbbBbbBbbbbBBbBbbbbBbBbbBBbB',
};
const transactionRequest = await compoundkit.buildLeverageTransactionRequest(routerData);
// {
//   "to": "0xf4dEf6B4389eAb49dF2a7D67890810e5249B5E70",
//   "data": "0x...",
//   "value": "7245995915658097461"
// }

Last updated