Most Ethereum libraries and tools are written in JavaScript, and so is Hardhat. If you're not familiar with Node.js, it's a JavaScript runtime built on Chrome's V8 JavaScript engine. It's the most popular solution to run JavaScript outside of a web browser and Hardhat is built on top of it.
You can skip this section if you already have a working Node.js >=12.0
installation. If not, here's how to install it on Ubuntu, MacOS and Windows.
Copy and paste these commands in a terminal:
sudo apt update
sudo apt install curl git
curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
sudo apt install nodejs
Make sure you have git
installed. Otherwise, follow these instructions.
There are multiple ways of installing Node.js on MacOS. We will be using Node Version Manager (nvm).
Copy and paste these commands in a terminal:
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.35.2/install.sh | bash
nvm install 12
nvm use 12
nvm alias default 12
npm install npm --global # Upgrade npm to the latest version
Installing Node.js on Windows requires a few manual steps. We'll install git, Node.js 12.x and npm.
Download and run these:
- Git's installer for Windows
node-v12.XX.XX-x64.msi
from here
If your version of Node.js is older than 12.0
follow the instructions below to upgrade.
- Run
sudo apt remove nodejs
in a terminal to remove Node.js. - Find the version of Node.js that you want to install here and follow the instructions.
- Run
sudo apt update && sudo apt install nodejs
in a terminal to install Node.js again.
You can change your Node.js version using nvm. To upgrade to Node.js 12.x
run these in a terminal:
nvm install 12
nvm use 12
nvm alias default 12
npm install npm --global # Upgrade npm to the latest version
You need to follow the same installation instructions as before but choose a different version. You can check the list of all available versions here.
To install it do the following:
npm install -g yarn
To install required packages and dependencies, do the following:
yarn
followed by
npm i
To install hardhat, run:
npm install hardhat
npm install --save-dev "hardhat@^2.9.1" "@nomiclabs/hardhat-waffle@^2.0.0" "ethereum-waffle@^3.0.0" "chai@^4.2.0" "@nomiclabs/hardhat-ethers@^2.0.0" "ethers@^5.0.0"
To install the hardhat deploy package, do:
npm i hardhat-deploy
To compile the contracts, run:
npx hardhat compile
To deploy the contracts, run the following:
npx hardhat deploy --tags DEFIAI,DEFIAIFarm_deploy,DEFIAIFarm_init
This will deploy the contracts to your local environment.
NOTE: Do make sure the "--tags" is greyed-out, if it is not, try retyping the "--tags" in the terminal
To run the test scripts, run this command in a terminal:
npx hardhat test
If you encounter this error:
Rerun "npx hardhat test", but stop the execution with Ctrl+C, once this appears:
Then, go to contracts/solc_0.6/pcs/PancakeRouter.sol, find this function:
Replace the hex value with the Cake Factory INIT value
Go to contracts/solc_0.6/bsw/BiswapRouter02.sol, find this function:
Replace the hex value with the BSW Factory INIT value
Run "npx hardhat test" again:
npx hardhat test
-
Normal Flow
- User deposits into farm
- Time passes
- User withdraws from farm and get their rewards
-
User deposits twice Flow
- User deposits into farm
- Time passes
- User deposits again into farm, receives rewards from previous deposit
- Dev/Owner change strategy
- User deposits into farm
- Time passes
- Dev/Owner changes strategy
- User withdraws from farm (inactive) and get their rewards
- User deposits into farm again, but this time it has a different strategy, earning different type of reward token
- Users are able to deposit into farm
- Users are able to withdraw from the farm anytime, as long as it is not in the same block/next block with their deposit
- Users will get their rewards (CAKE/MDX/BSW) when they deposit again/withdraw
- Developers are able to get 30% of the rewards earned by the user every time user deposits/withdraws
- Developers are able to change the active strategy
- Users have to withdraw their balance from the previous strategy and restake them into the new active strategy on their own
- Users are able to withdraw their balance from previous pools, even after the strategy has changed
- Developers are able to emergency withdraw to take any residue left in the contract
- For every action a shareholder takes (deposit/withdraw), their reward (if any) will be harvested from the pool, and distributed to the shareholders, before updating their share (due to the deposit/withdraw). As such, there is no unfair reward distribution. There is hence no need to record the passage of time, as we are able to distribute their reward (CAKE/MDX/BSW), based on thier shares.
- Block numbers are not used for calculation purposes. They are only used to record the block for users' last deposit, and prevent them from making a withdrawal in the same/next block, to prevent a flashloan attack.
- The main contracts of the system are DeFiAIFarmv2.sol, DeFiAIStrat.sol, IDefiMultiStrat.sol and IDeFiAIStrat.sol. Besides these contracts, the other third-part contracts are used for the testnet and unit testing environments for the purpose of mocking the mainnet environment. The mainnet deployment will be using the respective contracts already deployed by the third-party farms.