Fire insurance example
Overview
The fire components is a minimal fully functional and permissioned example for a insurance product built on the GIF. It consists of the following components:
- 
FirePoolAuthorization: A contract that authorizes the functions on the pool
 - 
FirePool: A pool that holds the funds for the insurance product
 - 
FireProductAuthorization: A contract that authorizes the functions on the product
 - 
FireProduct: The insurance product that is used to insure against fires
 - 
(optional) FireUSD: A ERC20 token that is used as the currency for the insurance product
 
The product is build in such a way that a customer can buy a policy which insures against fires in a specific city. The product owner can report fires in the city (the report also contains the damage level and the time of the fire). Once a fire has been reported in a city, the customer can submit a claim for the policy and receive an immediate payout (if the policy is eligable for a payout). The payout amount is calculated from the damage level and the sum insured.
The payout is 25% for small fires, 50% for medium fires and 100% for large fires. If the payout exceeds the sum insured, only the remaining sum insured is paid out. If the payout amount is the same as the sum insured after the payout, the policy is automatically expired.
Deployment
With Remix
- 
Checkout repository
gif-next`(https://github.com/etherisc/gif-next.git) in Remix IDE. - 
Call 'Update submodules' (Link at the bottom left of the page).
 - 
Open the
InstanceServicecontract in directorycontracts/instance/InstanceService.soland compile it. - 
Switch to the
Deploy & Run Transactionstab and connect to the network of choice (must have a GIF deployment). - 
Connect to the existing
InstanceServicecontract. - 
Decide if you want to use a registered token or use an unregistered one (the latter allows for more flexibility during testing but will create an unsupported instance).
 - 
Call the
createInstancefunction (set theallowAnyTokenparameter according to above decision) and find the logLogInstanceServiceInstanceCreatedthat shows the address of the new instance in fieldinstanceand the instance nft id in fieldinstanceNftId. - 
Now compile the contracts
FireUSD.sol,FirePoolAuthorization,FirePool,FireProductAuthorizationandFireProductin the directorycontracts/examples/fire. - 
Deploy the FireUSD contract and save the address. You can also use any pre-existing ERC20 Token. If you deploy a new token and have not enabled the
allowAnyTokenflag on the instance, then please ensure that the new token is registered with the token registry as well. - 
Deploy the FirePoolAuthorization contract with an arbitrary unique name and save the address.
 - 
Deploy the FireProductAuthorization contract with an arbitrary unique name and save the address.
 - 
Deploy the FireProduct contract and save the address. The product requires the registry address, the instance nft id, the name of the component (same as used in step 11) and the address of the product authorization contract as arguments.
 - 
Call
registerProducton theInstancecontract and provide the address of the product contract as well as the token address as argument. - 
Get the nft id of the product by calling
getNftIdfunction on theFireProductcontract. - 
Deploy the FirePool contract and save the address. The pool requires the registry address, the product nft id, the name of the component (same as used in step 10) , the address of the token as well as the address of the pool authorization contract as arguments.
 - 
Call
registerComponenton theFireProductcontract and provide the address of the pool as argument. - 
Get the nft if of the pool by calling
getNftIdfunction on theFirePoolcontract. - 
Congratulations, the fire product is now deployed and ready to use.
 
Using the hardhat script
Run the script scripts/deploy_fire_components.ts on an instance that has a gif deployment. The script will deploy the FireUSD, FirePoolAuthorization, FirePool, FireProductAuthorization and FireProduct contracts and register the pool and product in the instance.
It requires the following environment variables to be set:
AMOUNTLIB_ADDRESS
CONTRACTLIB_ADDRESS
FEELIB_ADDRESS
NFTIDLIB_ADDRESS
OBJECTTYPELIB_ADDRESS
REFERRALLIB_ADDRESS
RISKIDLIB_ADDRESS
ROLEIDLIB_ADDRESS
SECONDSLIB_ADDRESS
SELECTORLIB_ADDRESS
STRLIB_ADDRESS
TIMESTAMPLIB_ADDRESS
UFIXEDLIB_ADDRESS
VERSIONPARTLIB_ADDRESS
INSTANCE_SERVICE_ADDRESS
Usage
Bundle creation
- 
The investor must call
createBundleon theFirePoolcontract with thefee, theinitialAmountof the bundle and thelifetimeof the bundle as arguments. - 
The response contains the
bundleNftIdwhich is required when purchasing policies. 
Registration of cities
- 
Registration of new cities is done via call to the method
initializeCityon theFireProductcontract. Anybody can call this function. 
Reporting of fires
- 
To report a fire make sure the city is registered first.
 - 
Then the
ProductOwnermust callreportFirewith a uniquefireIdas well as thecityName,the damage level (Small - 25% payout, Medium - 50% payout, Large - 100% payout) and the time the fire occured. 
Policy purchase
- 
Make sure the city is registered beforehand
 - 
As customer, call
calculatePremiumonFireProductwith argumentscityName,sumInsured,lifetimeandbundleNftIdto get the premium amount for this parameter combination. - 
As customer, call
createApplicationwith thecityName,sumInsured,lifetimeandbundleNftIdto create a new application. The response contains thepolicyNftIdthat is needed for the next step. - 
Once the application is created, the
ProductOwnermust confirm the application by callingcreatePolicywith thepolicyNftIdand time the policy is active (activateAt) as arguments. 
Claim & Payout
- 
After a fire was reported, the customer can now submit a claim and received a payout for this fire by calling
submitClaimwith thepolicyNftIdand thefireIdas arguments. The payout is calculated based on the damage level reported and the sum insured. - 
The payout amount is immediately transferred to the customer.
 - 
If the payout amount exceeds the sum insured, only the remaining sum insured is paid out.
 - 
If the payout amount is the same as the sum insured after the payout, the policy is automatically expired.