-
Notifications
You must be signed in to change notification settings - Fork 306
/
hintExamples.js
125 lines (92 loc) · 5.02 KB
/
hintExamples.js
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
const { TestHelper: th } = require("../utils/testHelpers.js")
const dh = require("./deploymentHelpers.js")
// const [borrower, A, B, C] = (() => Array.from(Array(4), x => web3.eth.accounts.create().address))()
async function main() {
const accounts = await web3.eth.getAccounts()
const [borrower, A, B] = accounts
const coreContracts = await dh.deployLiquityCoreHardhat()
const ARBITRARY_ADDRESS = "0x5f4eC3Df9cbd43714FE2740f5E3616155c5b8419"
const LQTYContracts = await dh.deployLQTYContractsHardhat(
ARBITRARY_ADDRESS,
ARBITRARY_ADDRESS,
ARBITRARY_ADDRESS
)
const { troveManager, borrowerOperations, hintHelpers, sortedTroves, priceFeedTestnet } = coreContracts
await dh.connectCoreContracts(coreContracts, LQTYContracts)
await dh.connectLQTYContracts(LQTYContracts)
await dh.connectLQTYContractsToCore(LQTYContracts, coreContracts)
// Examples of off-chain hint calculation for Open Trove
const toWei = web3.utils.toWei
const toBN = web3.utils.toBN
const price = toBN(toWei('2500'))
await priceFeedTestnet.setPrice(toBN(toWei('2500')))
const LUSDAmount = toBN(toWei('2500')) // borrower wants to withdraw 2500 LUSD
const ETHColl = toBN(toWei('5')) // borrower wants to lock 5 ETH collateral
// Call deployed TroveManager contract to read the liquidation reserve and latest borrowing fee
const liquidationReserve = await troveManager.LUSD_GAS_COMPENSATION()
const expectedFee = await troveManager.getBorrowingFeeWithDecay(LUSDAmount)
// Total debt of the new trove = LUSD amount drawn, plus fee, plus the liquidation reserve
const expectedDebt = LUSDAmount.add(expectedFee).add(liquidationReserve)
// Get the nominal NICR of the new trove
const _1e20 = toBN(toWei('100'))
let NICR = ETHColl.mul(_1e20).div(expectedDebt)
// Get an approximate address hint from the deployed HintHelper contract. Use (15 * number of troves) trials
// to get an approx. hint that is close to the right position.
let numTroves = await sortedTroves.getSize()
let numTrials = numTroves.mul(toBN('15'))
let { 0: approxHint } = await hintHelpers.getApproxHint(NICR, numTrials, 42) // random seed of 42
// Use the approximate hint to get the exact upper and lower hints from the deployed SortedTroves contract
let { 0: upperHint, 1: lowerHint } = await sortedTroves.findInsertPosition(NICR, approxHint, approxHint)
// Finally, call openTrove with the exact upperHint and lowerHint
const maxFee = '5'.concat('0'.repeat(16)) // Slippage protection: 5%
await borrowerOperations.openTrove(maxFee, LUSDAmount, upperHint, lowerHint, { value: ETHColl })
// --- adjust trove ---
const collIncrease = toBN(toWei('1')) // borrower wants to add 1 ETH
const LUSDRepayment = toBN(toWei('230')) // borrower wants to repay 230 LUSD
// Get trove's current debt and coll
const {0: debt, 1: coll} = await troveManager.getEntireDebtAndColl(borrower)
const newDebt = debt.sub(LUSDRepayment)
const newColl = coll.add(collIncrease)
NICR = newColl.mul(_1e20).div(newDebt)
// Get an approximate address hint from the deployed HintHelper contract. Use (15 * number of troves) trials
// to get an approx. hint that is close to the right position.
numTroves = await sortedTroves.getSize()
numTrials = numTroves.mul(toBN('15'))
({0: approxHint} = await hintHelpers.getApproxHint(NICR, numTrials, 42))
// Use the approximate hint to get the exact upper and lower hints from the deployed SortedTroves contract
({ 0: upperHint, 1: lowerHint } = await sortedTroves.findInsertPosition(NICR, approxHint, approxHint))
// Call adjustTrove with the exact upperHint and lowerHint
await borrowerOperations.adjustTrove(maxFee, 0, LUSDRepayment, false, upperHint, lowerHint, {value: collIncrease})
// --- RedeemCollateral ---
// Get the redemptions hints from the deployed HintHelpers contract
const redemptionhint = await hintHelpers.getRedemptionHints(LUSDAmount, price, 50)
const {0: firstRedemptionHint, 1: partialRedemptionNewICR, 2: truncatedLUSDAmount} = redemptionhint
// Get the approximate partial redemption hint
const {
hintAddress: approxPartialRedemptionHint,
latestRandomSeed
} = await contracts.hintHelpers.getApproxHint(partialRedemptionNewICR, numTrials, 42)
/* Use the approximate partial redemption hint to get the exact partial redemption hint from the
* deployed SortedTroves contract
*/
const exactPartialRedemptionHint = (await sortedTroves.findInsertPosition(partialRedemptionNewICR,
approxPartialRedemptionHint,
approxPartialRedemptionHint))
/* Finally, perform the on-chain redemption, passing the truncated LUSD amount, the correct hints, and the expected
* ICR of the final partially redeemed trove in the sequence.
*/
await troveManager.redeemCollateral(truncatedLUSDAmount,
firstRedemptionHint,
exactPartialRedemptionHint[0],
exactPartialRedemptionHint[1],
partialRedemptionNewICR,
0, maxFee,
{ from: redeemer },
)
}
main()
.then(() => process.exit(0))
.catch(error => {
console.error(error);
process.exit(1);
});