# CX4153 Dev Project Q&A This is a collection of good questions asked by some of your peers regarding your development project, or Solidity in general. ⚠️ **Note:** Please for project requirement related questions, we strongly encourage you to directly contact the liaison persons from repsective projects: - Kyber: contact Desmond on Teams or via email at `desmond@kyber.network` - Pendle: contact Anton on Teams or via email at `anton@pendle.finance` - Bluejay: contact Raymond via email at `raymond@bluejay.finance` - Daimler: contact Srikanth Kaja at `srikanth.kaja@daimler.com` ⚠️ **Note:** Please make sure you finish the following before building your project: 1. [CryptoZombie](https://cryptozombies.io/en/course) course 1~4 2. ["hello-dapp" workshop](https://github.com/BlockchainCourseNTU/hello-dapp/) for using the modern `hardhat + waffle + ethers.js` stack. - alternatively, you can also use an older tech stack `gananche + truffle + web3.js` stack, for which you can find our [tutorial](https://github.com/BlockchainCourseNTU/resource/tree/master/development/hello-dapp) from the last year. --- ## Generally Applicable Question #### Q1: how to interact with existing (already deployed contracts) inside my project? Do I need to redeploy them? Here's how to interact with an deployed ERC20 contract: 1. search the open sourced contract's interface code (usually under `contracts/interfaces/*.sol`), copy related files and paste into locally at your own `contracts/interfaces/` folder. 2. query companies' website to find out about their deployed address. - e.g. For Aave, their deployed contract addresses are documented [here](https://docs.aave.com/developers/deployed-contracts/deployed-contracts). - e.g. For Uniswap v3, their deployed contract addresses are shown [here](https://docs.uniswap.org/protocol/reference/deployments). - e.g. For Kyber, their deployments are [here](https://developer.kyber.network/docs/Addresses-Intro/). - e.g. For Pendle, their deployments are [here](https://github.com/pendle-finance/pendle-core/tree/master/deployments) 👈 this is a perfect example of another project that use hardhat, you can usually go to their repo directly and find all deployments info under `deployments/` folder. 3. then in your contract, you will do something like the following: ```solidity import './path/to/interfaces/IERC20.sol'; // optional: // import "@openzeppelin/contracts/access/Ownable.sol"; contract MyAwesomeContract { // declare the contract you want to interact with IERC20 private _kyberToken; // copy deployment address you searched from last step // and put as a global constant here: address public kyberTokenAddress; // either instantiate the contract in constructor constructor (address _kyber) { kyberTokenAddress = address(_kyber); _kyberToken = IERC20(_kyber) } // or via a setter with proper access control constructor () { // don't instantiate here } function setKyberAddress(address _kyber) external onlyOwner { kyberTokenAddress = address(_kyber); _kyberToken = IERC20(_kyber) } // finally, call the contract in your function: function createDerivative(...) external { // ... // you can call any function the interfaces provide. _kyberToken.transferFrom(alice, bob, 10); } } ``` 4. When it comes to testing, use **mainnet forking** feature of `hardhat`, (see our [dev workshop](https://github.com/BlockchainCourseNTU/hello-dapp/blob/main/boilerplate-part2.md#special-testing-mainnet-forking)) where you replicate the related mainnet deployed contracts' states locally in your hardhat blockchain, then forking it by deploying your own contracts locally and test against them. --- #### Q2: how can we get the price information of an asset/token inside our contract? (by Boon Khang) For this we need a **price oracle**. Please read explainer on the concept of an oracle [here](https://ethereum.org/en/developers/docs/oracles/). Recommended price oracles to consider: - [Uniswap Oracle](https://docs.uniswap.org/protocol/reference/core/libraries/Oracle) - [Aave Oracle](https://docs.aave.com/developers/the-core-protocol/price-oracle) - [Compound Open Price Feed](https://compound.finance/docs/prices - [Chainlink Price Feed](https://docs.chain.link/docs/using-chainlink-reference-contracts/) --- ### On option 9: Bluejay short token #### Q: How does the short token work? > The method to short is simply to perform a flash loan of the collateral and drawing out the stablecoin which will be sold to repay the flash loan, entering into a leveraged position. When the stablecoin value goes down in relations to the collateral, the user can perform the reversed action by conducting flash loan on the stablecoin, repaying debt and withdrawing collaterals. Let's start with the most obvious and _unleveraged_ way to short as a Collateralized Debt Position (CDP) depositor. ![](https://i.imgur.com/vUgGwYd.jpg) Now let's step up our game for risk seeker who want to do **leveraged shorting**. For the following examples, say our CDP pool require 125% over-collateralization, and you already have 1k USD worth of ETH. **Strategy 1**: recursively enter into CDP to create a leveraged shorting position. Iteration 1: create debt of 800 DAI with your 1k USD worth of ETH; Iteration 2: sell your 800 DAI for ETH, deposit into CDP and draw out 1k * 0.8^2 = 640 DAI as debt; Iteration 3: sell your 640 DAI for ETH, deposit into CDP and draw out 1k * 0.8^3 = 512 DAI as debt; ... so on and so forth. This is a typical geometirc series summation. It's easy to calculate the maximum leverage: `1*(1-0)/(1-0.8)=5` -- meaning you can end up with "5k ETH and 0 DAI" position, in case DAI price drop w.r.t. ETH, you are making a profit! (but also note you have 4k USD worth of DAI in your debt, so it's very high liquidiation risk) :point_up: this is one way to bring up the leverage, but it's not the most captial efficient way because recursively entering into CDP costs lots of gas (your ETH). **Strategy 2: leverged shorting via flashloan** You borrow 4k USD worth of ETH from Aave or other flash loan protocol, together with your existing 1k USD worth of ETH depoisited, you can draw out 5k*0.8 = 4k USD worth of DAI, which is 4k DAI, and you can immediately pay back your flashloan using these DAI. (in reality you probably would borrow slightly less to count for the interest rate from flashloan protocol) You effortless enter into a leveraged "virtual shorting" position of "5k ETH, 0 DAI". clearing your holding on DAI and long on ETH -- equivalent of saying you are shorting DAI. (also, high liquidation risk since you have 4k USD worth of DAI in debt in your CDP, but then this is an extreme shorting position by someone with high risk appetite, they also always choose to borrow less and be less leveraged.) On the contrary, if you think DAI price is gonna rise w.r.t. ETH, you can flashloan in reverse direction: borrow DAI from flashloan, withdraw ETH collateral, sell ETH collateral to repay your flashloan, and leaving yourself with less ETH but lot more DAI at hands! #### Q: What stablecoin do we use, does it have to be Bluejay's? No, just use MakerDAO's well-documented DAI. ### On option 5: NFT as Liquidity Reward with Pendle <details><summary>Q: Should we deploy the Pendle-core repo first? Should we start by extending the current liquidity mining? (by Sebastian)</summary><p> Alex: First, I don't think you need to redeploy the entire Pendle-core, you can reply on deployed contracts if you don't need to change them, but just want to interact with them, and only extend (rewrite) its Liquity Mining related contracts. Please refer to general Q1 for details. </p></details>