For technical reason, this repository is called gif-next, but it contains the code of the version 3 of the Generic Insurance Framework (GIF).
The project depends heavily on the use of submodules for dependencies, so it is important to checkout the submodules when cloning the repository.
For initial checkout call
git submodule update --init --recursiveTo update to the latest submodules, run the following command after updating to any revision (or checking out the repository)
git submodule update --recursiveThe project is setup with a devcontainer for Visual Studio Code. This is the recommended way for use with the project. It will provide a consistent development environment for all developers with all the required dependencies installed.
Hardhat is used for compiling the smart contracts and all deployment scripts are hardhat based. Unit tests are written using the forge testing framework. See below for most important commands to run tests and deployments.
If you are interested in creating your own products, have a look at our example sandbox repository that contains a simple example of how to build new products on top of the GIF. Or see our documentation on the setup of the fire example components.
Or dive into the gif framework code by running the tests and/or deploying the contracts.
forge testhh run scripts/deploy_gif.tsFor a non-local deployment add the --network switch.
The available network names are defined in hardhat.config.ts.
hh run scripts/deploy_gif.ts --network polygonAmoyTo include the fire example components instead run the following command
hh run scripts/deploy_all.tsThe command accepts regular hardhat parameters for network selection and other configuration. Also some environment variables are required for deployment.
Code verification is done separately using the following command (which read data from the serialized deployment state of the deploymnent script that ran beforehand)
hh run scripts/verify_deployment.tsThis uses the same environment variables as the deployment script.
NETWORK_URLthe rpc endpoint to useWALLET_MNEMONICthe HD wallet mnemonic to use for deployment. Wallet #0 will be the protocol owner.GAS_PRICEthe gas price to use for deploymentDIP_ADDRESSthe address of the already deployed DIP token. if not set, the script will deploy a new DIP tokenRESUMEABLE_DEPLOYMENTif this flag is set totrue, the deployment will write all transactions to a state file so the deployment can be resumed after a failure (or after a manual stop). data is stored in thedeployment/<chainid>/directory.ETHERSCAN_API_KEYthe api key for etherscanPOLYGONSCAN_API_KEYthe api key for polygonscanWRITE_ADDRESSES_TO_FILEdumps all deployed addresses to filedeployment.env
npm run build
npm run test
npm run ptest
npm run test-with-gas
npm run coverageProduce solidty-docgen documentation using the command below.
hh docgenThe resulting markdown files are written to docs
Install the dependencies before running the script below for the first time.
npm installThe deploy script will deploy all required contracts for gif and create a test instance.
# run deployment on a locally created ganache instance
hh run scripts/deploy_gif.ts# set appropriate values vor env variables (see below)
# run deployment on another network
hh run --network <networkname> scripts/deploy_gif.tsThe deployment will persist deployment information into the files deployments/<chainid>/deployment_state.json, deployments/<chainid>/libraries.json and deployments/<chainid>/verification_log.json.
This data can then be used for verification of the deployed contracts on etherscan/polygonscan.
For the verification of the contracts on etherscan/polygonscan the above files (created by previous deployment) are required and then the following command can be used:
hh run --network <networkname> scripts/verify_deployment.ts Environment variables:
WRITE_ADDRESSES_TO_FILEset totrueto write the addresses of the deployed contracts to a file (default:false)RESUMEABLE_DEPLOYMENTset totrueto have all (deployment) transactions written to a state file so the script can be resumed after a failure (or after a manual stop) (default:false)GAS_PRICEset to the gas price to use for deployment (default:undefined)WALLET_MNEMONICthe mnemonic of the wallet to use for deployment (required for mumbai and mainnet)DIP_ADDRESSthe address of the already deployed DIP token. if not set, the script will deploy a new DIP tokenWEB3_INFURA_PROJECT_IDset to infura project id (required for mumbai and mainnet)ETHERSCAN_API_KEYPOLYGONSCAN_API_KEYthe api key for etherscan/polygonscan (required for mumbai and mainnet)SKIP_INSTANCE_CREATION1 set totrueto skip the creation of a test instance at the end of the deployment (default:false)
hh run scripts/deploy_all.tsLike before, use the --network option to deploy on a different network.
hh run scripts/deploy_fire_components.tsEnsure that the deployment runs on a chain where a compatible version of the GIF is already deployed. Then ensure the correct environment variables are set. An up to date list of required environment variables can be found in the deploy_fire_components.ts script in the main method (just check the lines that contain a process.env statement). To be safe, set the environment variable WRITE_ADDRESSES_TO_FILE to true when deploying the gif and then copy all values from the generated deployment.env file to the .env file in the root directory of the repository before running the fire example deployment.
Like before, use the --network option to deploy on a different network.
hh console --network <networkname>
me = hre.ethers.Wallet.fromPhrase('...')
provider = hre.ethers.provider
await provider.getBalance(me)
hh run scripts/find_unrestricted_methods.tsChecks all public/external methods in services and components for methods that are missing the restricted modifier but should have one.
The script find_unrestricted_methods.ts is based on the ANTLR grammar Solidity.g4 (from https://github.com/solidity-parser/antlr) and uses the antlr4ng runtime (https://github.com/mike-lischke/antlr4ng).
To compile grammar to ts classes run antlr4ng -Dlanguage=TypeScript -o antlr/generated/ -visitor -listener antlr/Solidity.g4 (requires openjdk to be installed sudo apt install openjdk-17-jre-headless).
hh run scripts/find_missing_virtual_methods.ts.tsChecks all public/external methods in services and components for methods that are not marked as virtual but should be.
forge build
# contract sizes
forge build --sizes
forge test
# run single test case
forge test --mt test_deployAllSetup
# run all tests except long running
forge test --nmt longRunning
# run single test case with substantial logging
# to include logs as well use -vvvvv
# to add internal functions to trace add --
forge test --decode-internal --mt test_decimals -vvvv
# provide gas report for a single test
forge test --mt test_decimals --gas-report
# provide code coverage report
forge coverage
forge coverage --report lcov | Alias | Command |
|---|---|
| fb | forge build |
| fbw | forge build --watch |
| fbc | forge build contracts/ |
| fbcw | forge build --watch contracts/ |
| ft | forge test |
| ftf | forge test --nmt "_longRunning" |
| ftt | forge test -vvvv --mt |
| fttg | forge test -vvvv --gas-report --mt |
| fttw | forge test -vvvv --watch --mt |
| ftc | forge test -vvvv --mc |
| ftcw | forge test -vvvv --watch --mc |
| fcf | forge coverage --nmt "_longRunning" |
| fcfr | forge coverage --nmt "_longRunning" --report lcov |
Important: All profiles are run using the foundry ci profile which disables the optimizer (for speed).
### Library Linking
https://ethereum.stackexchange.com/questions/153411/does-foundry-support-dynamical-library-linking-in-solidity
```toml
# foundry.toml
[profile.default]
# expected format(example below): libraries = ["<path>:<lib name>:<address>"]
libraries = ["src/libraries/MyLibrary.sol:MyLibrary:0x..."]
https://book.getfoundry.sh/reference/
Please see https://docs.etherisc.com/gif-next/3.x/ for style guide and general coding rules.
We use prettier and the solidity plugin to format the code automatically.
The plugin is configured to use the style guide mentioned above.
To execute format checks run npm run styleCheck.
To execute formatting run npm run styleFix.
We use solhint to lint the code.
To execute linting run npm run lint.
Command line call
hh run scripts/analyze_storage_layout.tsAnalyses contract MockStorageLayout.sol and writes the storage layout to storage_layout.json and storage_layout.csv.
New structs must be added to MockStorageLayout to be included in analysis.
Storage layout details:
- Items fill up the current slot if possible
- If not enough space is left in the current slot, the item is placed in the next slot
- Stucts and arrays always start a new slot
- Items following a struct or array always start a new slot
More at https://docs.soliditylang.org/en/latest/internals/layout_in_storage.html
| Type | Size |
|---|---|
| Amount | 12 bytes |
| Blocknumber | 4 bytes |
| ClaimId | 2 bytes |
| DistributorType | 8 bytes |
| NftId | 12 bytes |
| ObjectType | 1 byte |
| ReferralId | 8 bytes |
| RoleId | 1 byte |
| RiskId | 8 bytes |
| Seconds | 5 bytes |
| Selector | 4 bytes |
| StateId | 1 byte |
| Str | 32 bytes |
| Timestamp | 5 bytes |
| Version | 3 byte |
| VersionPart | 1 byte |
| UFixed | 20 bytes |
| address | 20 bytes |
| uint256 | 32 bytes |
| uint128 | 16 bytes |
| uint160 | 20 bytes |
| uint64 | 8 bytes |
| uint32 | 4 bytes |
| uint16 | 2 bytes |
| string | 32 bytes |
| bytes | 32 bytes |
| bytes8 | 8 byte |
| bool | 1 byte |