Skip to content

Commit

Permalink
Migrated xdc-prod branch to Hardhat (Asterizm Bridge included)
Browse files Browse the repository at this point in the history
  • Loading branch information
dmtrbch committed Oct 24, 2024
1 parent b40416a commit dd17c55
Show file tree
Hide file tree
Showing 100 changed files with 479 additions and 6,931 deletions.
119 changes: 40 additions & 79 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,13 @@ Various mechanisms, including price oracles, Stable Swap, and a risk management

## Package version requirements for your machine:

- node v18.12.1
- npm v8.19.2
- CoralX v0.2.0
- Solidity =0.8.17 (solc)
- Ganache CLI v6.12.2 (ganache-core: 2.13.2)
- node v20.17.0
- npm v10.8.2
- Hardhat v^2.22.12

## Basic setup

The smart contracts are written in [Solidity](https://github.com/ethereum/solidity) and tested/deployed using [CoralX](https://github.com/Securrency-OSS/CoralX).
The smart contracts are written in [Solidity](https://github.com/ethereum/solidity) and tested/deployed using [Hardhat](https://hardhat.org/).

### Install nodejs:

Expand All @@ -32,27 +30,6 @@ $ sudo apt install nodejs
$ sudo apt install npm
```

### Intall CoralX from the Securrency private registry.

### Install CoralX package globally:

```bash
$ npm install -g coral-x
```

### Install ganache-cli:

```bash
$ npm install -g ganache-cli
```

### Install Solc (https://docs.soliditylang.org/en/v0.8.13/installing-solidity.html)

```bash
$ curl -o /usr/bin/solc -fL https://github.com/ethereum/solidity/releases/download/v0.8.13/solc-static-linux \
&& chmod u+x /usr/bin/solc
```

## Running tests

### 0) After cloning the repo, install dependencies
Expand All @@ -61,23 +38,15 @@ $ curl -o /usr/bin/solc -fL https://github.com/ethereum/solidity/releases/downlo
$ npm i
```

### 1) Run ganache with predefined accounts:

```bash
$ ganache-cli -m MNEMONIC --gasLimit 12500000 -v -e 100000000
```

### 2) Create file called "privateKey" in the root directory (to run tests, copy the privateKey of the first account of ganache):
### 1) Create SEED_PHRASE environment variable:

```bash
$ echo -n PRIVATE_KEY_WITHOUT_0x_PREFIX > privateKey
$ echo -n SEED_PHRASE=12_WORD_MNEMONIC > .env
```

Please make sure that the privateKey file's content doesn't have any unneccesary text. The file should only contain privateKey without the 0x prefix. Otherwise, it will fail.

### 3) Create externalAddresses.json file in root
### 2) Create externalAddresses.json file in root

The chainId 31337 addresses are required to execute the test scripts on Ganache. Checksummed addresses must be included in the externalAddresses.json file as placeholder addresses for test execution. Use the content provided below for the externalAddresses.json file to run the test scripts.
The chainId 31337 addresses are required to execute the test scripts on built-in Hardhat Network. Checksummed addresses must be included in the externalAddresses.json file as placeholder addresses for test execution. Use the content provided below for the externalAddresses.json file to run the test scripts.

```JSON
{
Expand All @@ -93,41 +62,33 @@ The chainId 31337 addresses are required to execute the test scripts on Ganache.

```

### 4) Run commands to run tests in root
### 3) Run commands to run tests in root

```bash
$ coralX test
$ npm run test
```

The contracts will compile, be deployed, and then the test scripts will run.

To run a specific test script instead of running the the whole tests, run as below. For example, to run PositionPermissions.test.js
To run a specific test script instead of running the the whole tests, run as below. For example, to run AdminControls.test.js

```bash
$ coralX test --path integration/PositionPermissions.test.js
$ npx hardhat test test/integration/AdminControls.test.js
```

You might face some tests failing with 'out of gas' error.

## Deployment

The current codebase is structured so that the same set of smart contracts will be deployed, regardless of the target blockchain. By default, the protocol's price feed after deployment is set to `SimplePriceFeed`, a mock price feed that allows the protocol's deployer (owner) to freely modify prices. This default setting is based on the consideration that other price feeds for commercial use, whether sourcing from a DEX or a Centralized one, require external addresses or even price-related bots for successful deployment. Consequently, the adjusted deployment/migration script will deploy `DexPriceOracle`, `DelayFathomOraclePriceFeed`, `CentralizedOraclePriceFeed`, and `SlidingWindowDexOracle` using proxy patterns, but will not initialize them. Documentation regarding price feed changes will be included in this markdown document following the deployment section.

### On Ganache

#### 0) Run ganache with predefined accounts:

```bash
$ ganache-cli -m MNEMONIC --gasLimit 12500000 -v -e 100000000
```
### On built-in Hardhat network, Chain id: 31337

### 1) Create file called "privateKey" in the root directory (copy the privateKey of the first account of ganache):
### 0) Create SEED_PHRASE environment variable:

```bash
$ echo -n PRIVATE_KEY_WITHOUT_0x_PREFIX > privateKey
$ echo -n SEED_PHRASE=12_WORD_MNEMONIC > .env
```

#### 2) Create externalAddresses.json in root directory.
#### 1) Create externalAddresses.json in root directory.

The format of the content can be same as in the Running test sections. Like below

Expand All @@ -145,35 +106,35 @@ The format of the content can be same as in the Running test sections. Like belo

```

If you want to see the protocol in action by depositing test ETH and borrowing FXD, I recommend that you first deploy WXDC||WETH on Ganache. After that, update the WXDC address in the externalAddresses.json file with the address of the WXDC you deployed. Additionally, if you aim to thoroughly test the StableSwapModule post-deployment, you must deploy an ERC20 token that can subsequently be deposited into the StableSwapModule. Therefore, please deploy an ERC20 token to substitute for the USD token, and update the USD value in the aforementioned JSON file.
If you want to see the protocol in action by depositing test ETH and borrowing FXD, I recommend that you first deploy WXDC||WETH on Hardhat network. After that, update the WXDC address in the externalAddresses.json file with the address of the WXDC you deployed. Additionally, if you aim to thoroughly test the StableSwapModule post-deployment, you must deploy an ERC20 token that can subsequently be deposited into the StableSwapModule. Therefore, please deploy an ERC20 token to substitute for the USD token, and update the USD value in the aforementioned JSON file.

#### 3) Compile contracts
#### 2) Compile contracts

```bash
$ coralX compile
$ npm run compile
```

make sure that the contracts compile before deployment

#### 4) Deploy with below command
#### 3) Deploy with below command

```bash
$ coralX scenario --run deployLocal
$ npm run deploy-local
```

#### 5) Check contract addresses in addresses.json file in root
#### 4) Check contract addresses in addresses.json file in root

After deployment, addresses.json gets updated with addresses of proxies.

### On apothem (XDC Testnet)

#### 1) Create file called "privateKey" in the root directory (PRIVATE_KEY_WITHOUT_0x_PREFIX of the EOA that you would like to deploy contracts from):
#### 0) Create SEED_PHRASE environment variable:

```bash
$ echo -n PRIVATE_KEY_WITHOUT_0x_PREFIX > privateKey
$ echo -n SEED_PHRASE=12_WORD_MNEMONIC > .env
```

#### 2) Create externalAddresses.json in root directory.:
#### 1) Create externalAddresses.json in root directory.:

ChainID of apothem is 51. Therefore, the externalAddresses.json need to have the sets of addresses having 51 as key. For example, like below.

Expand All @@ -196,33 +157,33 @@ The USDSTABLE will be the USD address used for the StableSwapModule. You may kee

DEXFactory refers to the factory address of UniswapV2 fork. In this deployment, you can use the FathomSwap factory address provided above, or you can use your own factory address if you prefer. DEXFactory address was used for the priceOracle and priceFeed that use a DEX as the source of truth, but the default deployment setting does not use the address

#### 4) Compile contracts
#### 2) Compile contracts

```bash
$ coralX compile
$ npm run compile
```

make sure that the contracts compile before deployment

#### 5) Deploy with below command
#### 3) Deploy with below command

```bash
$ coralX scenario --run deployApothem
$ npm run deploy-apothem
```

#### 6) Check contract addresses in addresses.json file in root
#### 4) Check contract addresses in addresses.json file in root

After deployment, addresses.json gets updated with addresses of proxies.

### On XDC mainnet (chainID 50)

#### 1) Create file called "privateKey" in the root directory (PRIVATE_KEY_WITHOUT_0x_PREFIX of the EOA that you would like to deploy contracts from):
#### 0) Create SEED_PHRASE environment variable:

```bash
$ echo -n PRIVATE_KEY_WITHOUT_0x_PREFIX > privateKey
$ echo -n SEED_PHRASE=12_WORD_MNEMONIC > .env
```

#### 2) Create externalAddresses.json in root directory.:
#### 1) Create externalAddresses.json in root directory.:

ChainID of XDC mainnet is 50. Therefore, the externalAddresses.json need to have the sets of addresses having 51 as key. For example, like below.

Expand All @@ -245,21 +206,21 @@ The USDSTABLE will be the USD address used for the StableSwapModule. You may kee

DEXFactory refers to the factory address of UniswapV2. In this deployment, you can find and use the FathomSwap factory address or you can use your own factory address if you prefer. DEXFactory address was used for the priceOracle and priceFeed that use a DEX as the source of truth, but the default deployment setting does not use this address.

#### 4) Compile contracts
#### 2) Compile contracts

```bash
$ coralX compile
$ npm run compile
```

make sure that the contracts compile before deployment

#### 5) Deploy with below command
#### 3) Deploy with below command

```bash
$ coralX scenario --run deployMainnet
$ npm run deploy-mainnet
```

#### 6) Check contract addresses in addresses.json file in root
#### 4) Check contract addresses in addresses.json file in root

After deployment, addresses.json gets updated with addresses of proxies.

Expand Down Expand Up @@ -321,7 +282,7 @@ Line 5~6 verifies the solvency of the new priceFeed. New priceFeed must have poo
can be initialized with script below

```
scripts/migrations/priceFeed/1_initialize.js
tasks/price-feed.js
```

#### To run the script with DEX as the price source, ensure that:
Expand All @@ -334,7 +295,7 @@ The default PriceOracle for DelayFathomOraclePriceFeed is set as DexPriceOracle
#### Run the init script with below command

```bash
$ coralX execute --path scripts/migrations/priceFeed/1_initialize.js
$ npx hardhat price-feed --proxyFactoryAddress "Address of FathomProxyFactory contract"
```

### DEX price feed info
Expand Down
28 changes: 0 additions & 28 deletions coralX-scenarios.js

This file was deleted.

2 changes: 1 addition & 1 deletion deploy/deploy-add-collateral/01_deploy_add_collateral.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,4 @@ module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
await configPool(getChainId);
};

module.exports.tags = ["DeployAddCollateral"];
module.exports.tags = ["DeployAddCollateral"];
2 changes: 1 addition & 1 deletion deploy/deploy-main/01_deploy.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,4 +28,4 @@ module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
await addCollateralPools(deployments, getChainId);
};

module.exports.tags = ["DeployMain"];
module.exports.tags = ["DeployMain"];
2 changes: 1 addition & 1 deletion deploy/deploy-test-fixture/01_deploy_test_fixture.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@ module.exports = async ({ getNamedAccounts, deployments, getChainId }) => {
await addCollateralConfigPool(deployments, getChainId);
};

module.exports.tags = ["DeployTestFixture"];
module.exports.tags = ["DeployTestFixture"];
2 changes: 1 addition & 1 deletion deploy/deploy-tokens/01_deploy_tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,4 @@ module.exports = async ({ getNamedAccounts, deployments }) => {
await fthm.mint(deployer, BigNumber.from("10000000000000000000000000000"));
};

module.exports.tags = ["DeployTokens"];
module.exports.tags = ["DeployTokens"];
25 changes: 11 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,18 @@
"main": "",
"directories": {},
"scripts": {
"format": "prettier --write .",
"test": "coralX test",
"test-with-timeout": "coralX test --params TIMEOUT=100000",
"test-skip-compile": "coralX test skip_compile true",
"test-use-snapshot": "coralX test skip_compile true use_snapshot true",
"compile": "npm run prettier && npm run lint && coralX compile",
"prettier": "prettier --write 'contracts/**/*.sol'",
"lint": "solhint 'contracts/**/*.sol'",
"pre-release": "npm run prettier && npm run lint && npm run test-with-timeout",
"migrate-reset": "rm -rf build/contracts && rm -rf build/job-logs && coralX compile && coralX scenario --run migrateAndConfigureForTests",
"migrate-reset-local": "rm -rf build/contracts && rm -rf build/job-logs && coralX compile && coralX scenario --run deployLocal",
"migrate-reset-apothem": "rm -rf build/contracts && rm -rf build/job-logs && coralX compile && coralX scenario --run deployApothem",
"migrate-reset-mainnet": "rm -rf build/contracts && rm -rf build/job-logs && coralX compile && coralX scenario --run deployMainNet",
"deploy-tokens-local": "coralX scenario --run deployTokensLocal",
"deploy-tokens-apothem": "coralX scenario --run deployTokensApothem"
"lint:fix": "solhint 'contracts/**/*.sol' --fix",
"format": "prettier --write .",
"compile": "hardhat compile",
"test": "hardhat test",
"node": "hardhat node --tags DeployMain",
"pre-release": "npm run format && npm run lint && npm run test",
"deploy-local": "hardhat deploy --deploy-scripts deploy/deploy-main --network hardhat",
"deploy-apothem": "hardhat deploy --deploy-scripts deploy/deploy-main --network apothem",
"deploy-mainnet": "hardhat deploy --deploy-scripts deploy/deploy-main --network xdc",
"deploy-tokens-local": "hardhat deploy --deploy-scripts deploy/deploy-tokens --network hardhat",
"deploy-tokens-apothem": "hardhat deploy --deploy-scripts deploy/deploy-tokens --network apothem"
},
"author": "",
"license": "",
Expand Down
4 changes: 2 additions & 2 deletions scripts/configuration/add-collateral/configPool.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ async function configPool(getChainId) {
CLOSE_FACTOR_BPS.mul(2),
LIQUIDATOR_INCENTIVE_BPS,
TREASURY_FEE_BPS,
fixedSpreadLiquidationStrategy.address,
fixedSpreadLiquidationStrategy.address
);

await priceOracle.setPrice(poolId);
}
module.exports = { configPool };
module.exports = { configPool };
Loading

0 comments on commit dd17c55

Please sign in to comment.