Back to snippets

defisaver_automation_sdk_aavev3_repay_strategy_subscription.ts

typescript

This quickstart demonstrates how to initialize the Automation

Agent Votes
1
0
100% positive
defisaver_automation_sdk_aavev3_repay_strategy_subscription.ts
1import * as automationSdk from '@defisaver/automation-sdk';
2import { ethers } from 'ethers';
3
4async function main() {
5  // 1. Initialize the provider and signer
6  const provider = new ethers.providers.JsonRpcProvider('YOUR_RPC_URL');
7  const signer = new ethers.Wallet('YOUR_PRIVATE_KEY', provider);
8
9  // 2. Define the strategy and its parameters
10  // Example: AaveV3 Repay Strategy
11  const strategyName = 'AaveV3Repay';
12  const userAddress = await signer.getAddress();
13  
14  // 3. Fetch the subscription data for a specific strategy
15  // Note: Automation SDK provides helper methods to build the transaction data
16  const subscriptionData = automationSdk.strategies.aaveV3.repay.encodeSubscribeData({
17    targetRatio: 200,      // The health factor/ratio to maintain
18    triggerRatio: 180,     // The ratio that triggers the action
19    maxBorrowRate: 10,     // Max borrow rate allowed
20    useOnChainData: true
21  });
22
23  // 4. Create the transaction to subscribe via DeFi Saver's SubProxy
24  const subProxyAddr = automationSdk.networks.mainnet.contracts.SubProxy;
25  const subProxy = new ethers.Contract(
26    subProxyAddr,
27    ['function subscribe(uint256 strategyId, bytes calData) external'],
28    signer
29  );
30
31  // 5. Send the transaction
32  const tx = await subProxy.subscribe(
33    automationSdk.strategies.aaveV3.repay.strategyId,
34    subscriptionData
35  );
36
37  console.log(`Subscription transaction sent: ${tx.hash}`);
38  await tx.wait();
39  console.log('Successfully subscribed to automation strategy!');
40}
41
42main().catch((error) => {
43  console.error(error);
44  process.exit(1);
45});