-
Notifications
You must be signed in to change notification settings - Fork 0
/
leverage.ts
83 lines (71 loc) · 2.76 KB
/
leverage.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import * as apisdk from "@protocolink/api"
import * as common from "@protocolink/common"
import {
destToken,
marketId,
slippage,
srcAmount,
srcToken,
} from "./leverage.config"
import * as lending from "@protocolink/lending"
// step 1: Register lending protocol and swapper
lending.Adapter.registerProtocol(lending.protocols.compoundv3.LendingProtocol)
lending.Adapter.registerSwapper(lending.swappers.paraswapv5.LendingSwapper)
// step 2: Initialze adapter with chainId
const chainId = common.ChainId.base
const adapter = new lending.Adapter(chainId)
const account = "0xa3C1C91403F0026b9dd086882aDbC8Cdbc3b3cfB"
const protocolId = "compound-v3"
;(async () => {
// step 3: Get account's protfolio, you will need to pass the `protfolio` to each operation
const portfolio = await adapter.getPortfolio(account, protocolId, marketId)
console.log("---portfolio---", portfolio)
// step 4: Leverage by collateral, which is enables users to achieve the desired collateral exposure in a single step by using a flash loan.
const { afterPortfolio, logics } = await adapter.leverageByCollateral({
account,
portfolio,
srcToken,
srcAmount,
destToken,
slippage,
})
console.log("---afterPortfolio---", afterPortfolio)
console.log("---logics---", logics)
const routerData: apisdk.RouterData = {
chainId,
account,
logics,
}
// step 5: Estimate Router Data
// estimate how much funds will be spent (funds) and how many balances will be obtained (balances) from this transaction.
// It will also identify any approvals that the user needs to execute (approvals) before the transaction.
const estimateResult = await apisdk.estimateRouterData(routerData)
const { funds, balances, approvals, permitData, fees } = estimateResult
console.log("---funds---", funds)
console.log("---balances---", balances)
console.log("---approvals---", approvals)
console.log("---permitData---", permitData)
console.log("---fees---", fees)
// step 5-1: Approvals (optional)
// const signer = provider.getSigner(account);
// for (const approval of estimateResult.approvals) {
// const tx = await signer.sendTransaction(approval);
// }
// step: PermitData (optional)
// const signer = provider.getSigner(account)
// const permitSig = await signer._signTypedData(
// permitData.domain,
// permitData.types,
// permitData.values
// )
// routerData.permitData = estimateResult.permitData;
// routerData.permitSig = permitSig;
// step 6: Build Router Transaction
const transactionRequest = await apisdk.buildRouterTransactionRequest(
routerData
)
console.log(transactionRequest)
// step 7: send transaction
// const signer = provider.getSigner(account)
// const tx = await signer.sendTransaction(transactionRequest)
})()