Skip to content

Commit

Permalink
Merge pull request #17 from curvefi/deleverage
Browse files Browse the repository at this point in the history
Deleverage
  • Loading branch information
Macket authored Oct 5, 2023
2 parents d4c6ac6 + 8d34ce9 commit ae1d428
Show file tree
Hide file tree
Showing 9 changed files with 627 additions and 15 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
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@curvefi/stablecoin-api",
"version": "1.4.0",
"version": "1.5.0",
"description": "JavaScript library for Curve Stablecoin",
"main": "lib/index.js",
"author": "Macket",
Expand Down
248 changes: 248 additions & 0 deletions src/constants/abis/DeleverageZap.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
[
{
"stateMutability": "nonpayable",
"type": "constructor",
"inputs": [
{
"name": "_controller",
"type": "address"
},
{
"name": "_collateral",
"type": "address"
},
{
"name": "_router",
"type": "address"
},
{
"name": "_routes",
"type": "address[11][]"
},
{
"name": "_route_params",
"type": "uint256[5][5][]"
},
{
"name": "_route_pools",
"type": "address[5][]"
},
{
"name": "_route_names",
"type": "string[]"
}
],
"outputs": []
},
{
"stateMutability": "view",
"type": "function",
"name": "get_stablecoins",
"inputs": [
{
"name": "collateral",
"type": "uint256"
},
{
"name": "route_idx",
"type": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "uint256"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "calculate_debt_n1",
"inputs": [
{
"name": "collateral",
"type": "uint256"
},
{
"name": "route_idx",
"type": "uint256"
},
{
"name": "user",
"type": "address"
}
],
"outputs": [
{
"name": "",
"type": "int256"
}
]
},
{
"stateMutability": "nonpayable",
"type": "function",
"name": "callback_repay",
"inputs": [
{
"name": "user",
"type": "address"
},
{
"name": "stablecoins",
"type": "uint256"
},
{
"name": "collateral",
"type": "uint256"
},
{
"name": "debt",
"type": "uint256"
},
{
"name": "callback_args",
"type": "uint256[]"
}
],
"outputs": [
{
"name": "",
"type": "uint256[2]"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "CONTROLLER",
"inputs": [],
"outputs": [
{
"name": "",
"type": "address"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "COLLATERAL",
"inputs": [],
"outputs": [
{
"name": "",
"type": "address"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "ROUTER",
"inputs": [],
"outputs": [
{
"name": "",
"type": "address"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "routes",
"inputs": [
{
"name": "arg0",
"type": "uint256"
},
{
"name": "arg1",
"type": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "address"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "route_params",
"inputs": [
{
"name": "arg0",
"type": "uint256"
},
{
"name": "arg1",
"type": "uint256"
},
{
"name": "arg2",
"type": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "uint256"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "route_pools",
"inputs": [
{
"name": "arg0",
"type": "uint256"
},
{
"name": "arg1",
"type": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "address"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "route_names",
"inputs": [
{
"name": "arg0",
"type": "uint256"
}
],
"outputs": [
{
"name": "",
"type": "string"
}
]
},
{
"stateMutability": "view",
"type": "function",
"name": "routes_count",
"inputs": [],
"outputs": [
{
"name": "",
"type": "uint256"
}
]
}
]
Loading

0 comments on commit ae1d428

Please sign in to comment.