Skip to content

Commit

Permalink
docs: deleverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Macket committed Oct 5, 2023
1 parent 8cdc841 commit 851d82a
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 4 deletions.
78 changes: 74 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -676,16 +676,16 @@ import crvusd from "@curvefi/stablecoin-api";
// |
// collateral | crvUSD crvUSD
// user --> controller --> leverage_zap --> curve_router
// ^ |
// |_____________________________________________|
// leverage_collateral
// ^ | ^ |
// |______________________| |___________________|
// leverage_collateral leverage_collateral

await crvusd.init('JsonRpc', {});

const llamma = crvusd.getLlamma('wsteth');


await llamma.leverage.createLoanMaxRecv(0.5, 5);
await llamma.leverage.createLoanMaxRecv(1, 5);
// {
// maxBorrowable: '16547.886068664425693035',
// maxCollateral: '8.789653769216069731',
Expand Down Expand Up @@ -842,6 +842,76 @@ import crvusd from "@curvefi/stablecoin-api";
// }
```
### Deleverage
```ts
(async () => {

// Deleveraged position (fully or partially)
// ^
// |
// | collateral collateral
// controller --> leverage_zap --> curve_router
// ^ | ^ |
// |______________________| |___________________|
// crvUSD crvUSD

await crvusd.init('JsonRpc', {});

const llamma = crvusd.getLlamma('wsteth');


await llamma.userState();
// {
// collateral: '1.532865973844812038',
// stablecoin: '0.0',
// debt: '1000.0'
// }
const { stablecoins, routeIdx } = await llamma.deleverage.repayStablecoins(0.5);
// { stablecoins: '936.993512434228957835', routeIdx: 2 }
await llamma.deleverage.getRouteName(routeIdx)
// wstETH wrapper -> steth -> factory-tricrypto-4 (TriCRV)
await llamma.deleverage.repayBands(0.5)
// [ 344, 340 ]
await llamma.deleverage.repayPrices(0.5)
// [ '65.389368517832066821', '68.759256234814550815' ]
await llamma.deleverage.repayHealth(0.5) // FULL
// 2962.6116372201716629
await llamma.deleverage.repayHealth(0.5, false) // NOT FULL
// 3.3355078309621505
await llamma.deleverage.priceImpact(0.5)
// 0.0080 %
await llamma.deleverage.isAvailable(0.5)
// true
await llamma.deleverage.isFullRepayment(0.5)
// false

await llamma.deleverage.repay(0.5, 0.3)

await llamma.userState()
// {
// collateral: '1.032865973844812038',
// stablecoin: '0.0',
// debt: '63.006629410173187253'
// }
await llamma.userBands()
// [ 344, 340 ]
await llamma.userPrices()
// [ '65.389377792947951092', '68.759265987930143609' ]
await llamma.userHealth() // FULL
// 2962.6210276926274746
await llamma.userHealth(false) // NOT FULL
// 3.3352898532375197
await llamma.userBandsBalances()
// {
// '340': { stablecoin: '0.0', collateral: '0.20657319476896241' },
// '341': { stablecoin: '0.0', collateral: '0.206573194768962407' },
// '342': { stablecoin: '0.0', collateral: '0.206573194768962407' },
// '343': { stablecoin: '0.0', collateral: '0.206573194768962407' },
// '344': { stablecoin: '0.0', collateral: '0.206573194768962407' }
// }
})()
```
## Gas estimation
Every non-constant method has corresponding gas estimation method. Rule: ```obj.method -> obj.estimateGas.method```
Expand Down
29 changes: 29 additions & 0 deletions test/readme.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,33 @@ const leverageAllRangesTest = async () => {
console.log(await llamma.leverage.createLoanPricesAllRanges(1, 14000));
}

const deleverageTest = async () => {
await crvusd.init('JsonRpc', {});

const llamma = crvusd.getLlamma('wsteth');

console.log(await llamma.userState());
const { stablecoins, routeIdx } = await llamma.deleverage.repayStablecoins(0.5);
console.log({ stablecoins, routeIdx });
console.log(await llamma.deleverage.getRouteName(routeIdx));
console.log(await llamma.deleverage.repayBands(0.5));
console.log(await llamma.deleverage.repayPrices(0.5));
console.log(await llamma.deleverage.repayHealth(0.5)); // FULL
console.log(await llamma.deleverage.repayHealth(0.5, false)); // NOT FULL
console.log(await llamma.deleverage.priceImpact(0.5));
console.log(await llamma.deleverage.isAvailable(0.5));
console.log(await llamma.deleverage.isFullRepayment(0.5));

console.log(await llamma.deleverage.repay(0.5, 0.3));

console.log(await llamma.userState());
console.log(await llamma.userBands());
console.log(await llamma.userPrices());
console.log(await llamma.userHealth()); // FULL
console.log(await llamma.userHealth(false)); // NOT FULL
console.log(await llamma.userBandsBalances());
}

(async () => {
console.log("\n--- generalMethodsTest ---\n")
await generalMethodsTest();
Expand All @@ -345,4 +372,6 @@ const leverageAllRangesTest = async () => {
await leverageTest();
console.log("\n--- leverageAllRangesTest ---\n")
await leverageAllRangesTest();
console.log("\n--- deleverageTest ---\n")
await deleverageTest();
})()

0 comments on commit 851d82a

Please sign in to comment.