The most common approach is to deposit and leverage in a single transaction. This is ideal for users who want to create a leveraged position from scratch.
Preparing for Leverage
Before leveraging, you need to:
Understand your target position: Determine the collateral asset, borrow asset, and desired leverage ratio
Set an appropriate slippage tolerance: Typically 0.5-2% depending on asset volatility
Approve the necessary contracts: Your collateral token must approve the Position Management contract
Here's how to prepare the deposit and leverage transaction:
// Approve the position management contract to spend your tokens if depositing and leveraging in one transaction
const underlyingContract = new ethers.Contract(
underlyingAsset,
UNDERLYING_ABI,
signer
);
const depositAmount = ethers.utils.parseUnits('1000', 18); // Adjust amount and decimals
await underlyingContract.approve(POSITION_MANAGEMENT, depositAmount);
Constructing the LeverageStruct
The key to a successful leverage operation is properly constructing the LeverageStruct:
// Create the swap data structure
// This example uses 1inch swap data, but any DEX can be used
const swapData = {
target: '0x1111111254EEB25477B68fb85Ed929f73A960582', // 1inch router
inputToken: COLLATERAL_ASSET_UNDERLYING,
outputToken: BORRWED_ASSET_ADDRESS,
inputAmount: ethers.utils.parseUnits('500', 18), // Amount to borrow and swap
call: '0x...', // Encoded swap call data from 1inch API
};
// Construct the leverage struct
const leverageData = {
borrowToken: E_TOKEN,
borrowAmount: ethers.utils.parseUnits('500', 18),
positionToken: P_TOKEN,
swapData: swapData,
auxData: '0x' // Optional auxiliary data for specialized protocols
};
Executing the Leverage Operation
With the LeverageStruct prepared, execute the leverage operation:
// Set slippage tolerance (1%)
const slippage = ethers.utils.parseUnits('0.01', 18);
// Execute deposit and leverage in one transaction
const tx = await positionManagement.depositAndLeverage(
depositAmount,
leverageData,
slippage,
2000000
);
const receipt = await tx.wait();
console.log(`Leveraged position created! Tx hash: ${receipt.transactionHash}`);
2. Leveraging an Existing Position
If you already have collateral deposited, you can increase your leverage without an additional deposit:
const tx = await positionManagement.leverage(
leverageData, // Same structure as above
slippage,
2000000
);
const receipt = await tx.wait();
console.log(`Position further leveraged! Tx hash: ${receipt.transactionHash}`);