Compile, deploy and interact

Introduction

This section explains how to compile, deploy and interact with the fire insurance example product.

The fire insurance product is a simple example product that can be used to demonstrate the basic functionality of the GIF platform. It is a simple parametric insurance product that pays out an amount of USDC if a fire event is triggered by the oracle depending on the fire category (S - no payout, M - 20% of the sum insured, L - 100% of the sum insured).

Compiling and running the unit tests

Start with compiling all contracts.

brownie compile --all

Before running the tests for the first time you will likely need to add an empty .env file before.

touch /home/vscode/.brownie/packages/etherisc/gif-contracts@b58fd27/.env

Now run the unit tests for the sandbox.

brownie test -n auto

This shoud result in output similar to the one provided below.

⬢ [Docker] ❯ brownie test -n auto
Brownie v1.19.3 - Python development framework for Ethereum

================================================================= test session starts =================================================================
platform linux -- Python 3.9.16, pytest-6.2.5, py-1.11.0, pluggy-1.0.0
rootdir: /workspace
plugins: eth-brownie-1.19.3, forked-1.4.0, hypothesis-6.27.3, web3-5.31.3, xdist-1.34.0, anyio-3.6.2
gw0 [8] / gw1 [8] / gw2 [8] / gw3 [8] / gw4 [8] / gw5 [8] / gw6 [8] / gw7 [8]
........                                                                                                                                        [100%]
-- Docs: https://docs.pytest.org/en/stable/warnings.html
========================================================== 8 passed, 101 warnings in 44.52s ===========================================================

The important part is that all tests pass and no errors or failures are shown.

Deploy and Verify with Ganache

brownie console

In the console use the following steps.

from scripts.deploy_fire import help
help()

The help command then shows an example session.

from scripts.deploy_fire import all_in_1, verify_deploy, create_bundle, create_policy, help

(customer, customer2, product, oracle, riskpool, riskpoolWallet, investor, usdc, instance, instanceService, instanceOperator, bundleId, processId, d) = all_in_1(deploy_all=True)

verify_deploy(d, usdc, product)

This will deploy a new GIF instance as well as the fire insurance product and verify the installation.

Deploy to a different network containing a pre-installed GIF instance

As an example use the Ganache chain that runs in a separate container (ganache) of this devcontainer setup.

brownie console --network=ganache

With an existing instance set parameter deploy_all=False. In this case the file gif_instance_address.txt needs to exist and contain the addresses of the instance registry The file should be automatically created during the devconainer setup procedure of this repository.

from scripts.deploy_fire import all_in_1, verify_deploy, create_bundle, create_policy, help

(customer, customer2, product, oracle, riskpool, riskpoolWallet, investor, usdc, instance, instanceService, instanceOperator, bundleId, processId, d) = all_in_1(deploy_all=False)

verify_deploy(d, usdc, product)

Interacting with the fire insurance product via console

Creating a new policy

from scripts.util import s2h

# fund customer wallet and approve treasry with a large amount for simplicity
usdc.transfer(customer, 100000 * 10 ** 6, {'from': instanceOperator})
usdc.approve(instanceService.getTreasuryAddress(), 100000 * 10 ** 6, {'from': customer})

# Create a new policy for a house with a value of 10'000 USDC.
policy = product.applyForPolicy('My house', 10000 * 10 ** 6, {'from': customer})

# Retrieve the `processId` of the new policy
processId = policy.events['LogApplicationCreated'][0]['processId']

# Fetch the state of the applicaton (if [state == 2](https://github.com/etherisc/gif-interface/blob/develop/contracts/modules/IPolicy.sol#L58) -> policy is underwritten)
instanceService.getApplication(processId).dict()

# Fetch the state of the policy (if [state == 0](https://github.com/etherisc/gif-interface/blob/develop/contracts/modules/IPolicy.sol#L59) -> policy is active, also make sure the premiumPaidAmount is > 0 ... if not probably the allowance was not set correctly)
instanceService.getPolicy(processId).dict()

Sending an oracle response to trigger a claim creation and payout

# Retrieve the requestId (created during the underwriting process) of the policy and send oracle response with fire category `M` (20% payout) or use `L` for large fire with 100% payout
requestId = oracle.requestId('My house')
oracle_tx = oracle.respond(requestId, s2h('M'))

# the log list should contain a entry `LogFirePayoutExecuted` with the amount of USDC 2000
oracle_tx.events

Expiring a policy

# this will expire the policy - any claims following after expiration will be rejected due to the policy being expired
product.expirePolicy(processId)

Interacting with the fire insurance product via api

After the deployment of the contracts, its possible to interact with the product via the openapi interface instead of the brownie console. Therefor start the openapi server

uvicorn server.api:app --host 0.0.0.0

and point your browser to http://localhost:8000/docs. This should show the openapi documentation of the server, the documentation interface also allows interactive usage.

To start, you first need to initialize the openapi instance with the address of the deployed contracts. Then you can create a new policy, send an oracle response and expire the policy as described in the previous section.

Also don’t forget to fund the customer wallet and approve the treasury with a large amount before applying for a policy.

Initialize the openapi instance

Use the POST /config request to set the config. The method expects a json body with the following structure (real addresses can be found in the gif_instance_address.txt file or via the product/oracle objects in the brownie console after deployment and the mnemonic (candy maple cake sugar pudding cream honey rich smooth crumble sweet treat) is the preconfigured default mnemonic used in the ganache chain of the devcontainer).

{
    "registry_address": "0xF12b5dd4EAD5F743C6BaA640B0216200e89B60Da",
    "product_address": "0xC791F12F1Cea9B63D3F8C53e5B15ab90bcCe6796",
    "oracle_address": "0x61271F03b0C18F6E15da03c21185d419d3f76b97",
    "mnemonic": "candy maple cake sugar pudding cream honey rich smooth crumble sweet treat"
}

Build and test with foundry

Foundry is a new toolkit to build and test smart contracts. More documentation about foundry can be found in the foundry Foundry book.

The project is configured to use foundry. All contracts in the contracts folder can be compiled using foundry as well as brownie (results are stored in build_foundry). Foundry tests are writte in solidy and can be found in the tests_foundry folder (they need to be separate from brownie based tests). Dependencies are stored in the lib folder and are mapped in the foundry.yaml config file.

To compile the contracts using foundry, run the following command:

forge build

To run the foundry based tests, run the following command:

forge test