Skip to content

etherisc/gif-next-sandbox

Repository files navigation

GIF-next-sandbox

What's this?

This is a sandbox repository for the GIF-next project. It contains simple example components and tests for the GIF-next project. The contracts contracts/MyDistribution.sol, contracts/MyPool.sol and contracts/MyProduct.sol are examples for components that can be used in the GIF framework. None if them do more then expose the internal funtions right now, but they can be used as a basis to build own components. Additionally the contract contracts/Deployer.sol is a meta contract that helps to initialize components and prepare a new instance just by deploying the meta contract. This is useful for trying out the components quickly. See below (section Quickstart) on instructions on how to do this.

The project also contains an example foundry forge based unit test TestMyProduct.t.sol which demonstrates how to write a test for the MyProduct contract.

Notes

  • Renaming the contracts requires adapting the deployment script to be able to deploy the renamed contracts. The same goes for including additional libraries in the component contracts.
  • Updating the GIF framework requires updating the submodule in the gif-next directory. Since forge update gif-next will always update to the latest commit on the develop branch of the gif-next repo. If a specific version if required, its easier to first remove the module using forge remove gif-next and then re-add it again using forge install gif-next@version.
  • Updating the GIF framework might require changes to components as well as the deployment script.

Quickstart

These scripts/contracts require a chain where a gif-next instance is already deployed.

Quickstart setup using the Deployer contract

Note: This contract can easily be deployed from Remix or a similar IDE to get up and running quickly. It can be compiled and deployed without the need for a local development environment and external dependencies (almost everything is self-contained in the contract/respository).

Deploy the MyDistribution, MyPool, MyProduct and Deployer contracts to the chain where the GIF instance is deployed. The deployer contract requires the following parameters:

  • registryAddress the address of the deployed registry
  • distributionAddress the address of the distribution component
  • poolAddress the address of the pool component
  • productAddress the address of the product component
  • deploymentId a unique id for this deployment

During the deployment the contract will

  • create a new instance on the GIF framework
  • deploy a mock USDC token (and register it with the tokenregistry if not already done and instance has not disabled token registry)
  • deploy the components MyDistribution, MyPool and MyProduct
  • register MyProduct with the instance
  • register the components on the product
  • create a new bundle in the pool with a coverage amount of 10000 USDC
  • a new riskId for creating new policies
  • transfer the ownership of the instance, components and bundle to the caller of the deployment
  • transfer 10mio USDC to the caller of the deployment

Now the caller can use the functions applyForPolicy as well as underwritePolicy to create and underwrite policies on the deployed product. For futher details on function of the Deployer contract see the API documentation below.

Deploy the contract via hardhat

run the script scripts/run_deployer.ts with these environment variables set:

  • AMOUNTLIB_ADDRESS
  • FEELIB_ADDRESS
  • NFTIDLIB_ADDRESS
  • REFERRALLIB_ADDRESS
  • RISKIDLIB_ADDRESS
  • ROLEIDLIB_ADDRESS
  • SECONDSLIB_ADDRESS
  • SELECTORLIB_ADDRESS
  • STRLIB_ADDRESS
  • TIMESTAMPLIB_ADDRESS
  • UFIXEDLIB_ADDRESS
  • VERSIONPARTLIB_ADDRESS
  • REGISTRY_ADDRESS

This will deploy the Deployer contract and print the addresses and nfts of the deployed components. For futher details on function of the Deployer contract see the API documentation below.

Contract Deployer API documentation

  • getInstance returns the address of the created instance

  • getInstanceNftId returns the nftId of the created instance

  • getUsdc returns the address of the deployed USDC token

  • getDistributionNftId returns the nftId of the deployed BasicDistribution component

  • getDistribution returns the deployed BasicDistribution component

  • getPoolNftId returns the nftId of the deployed BasicPool component

  • getPool returns the deployed BasicPool component

  • getPoolTokenHandler returns the address of the token handler of the pool component

  • getProductNftId returns the nftId of the deployed InsuranceProduct component

  • getProduct returns the deployed InsuranceProduct component

  • getProductTokenHandler returns the address of the token handler of the product component

  • getInitialBundleNftId returns the nftId of the bundle created during deployment

  • getInitialRiskId returns the riskId created during deployment

  • createRisk create a new riskId and return the RiskId object

  • sendUsdcTokens send USDC tokens to an address

  • createBundle create a new bundle in the pool

  • applyForPolicy apply for a new policy and return the nftId. When run through an rpc node (e.g. remix), the id of the NftId of the created policy can be extracted from the transaction log LogRegistration field nftId.

  • underwritePolicy underwrite a policy application identified by policyNftId and transfer the premium amount to the bundle (requires a previous approval of the policy amount to the productTokenHandler)

  • getPolicyState get the current StateId of a policy

  • getBundleBalance get the balance of the bundleNftId

Hardhat/Forge Commands

Compiling

Compiling can be done through foundry forge as well as hardhat.

forge compile
npm run build

Run tests

Since the example tests are written using forge, they must be run using the forge test command.

forge test

Deployment

The deployment script scripts/deploy.ts is hardhat based and run using the command

npm run scripts/deploy.ts

As a prerequisite, the GIF framework is required to be deployed on the chain and a (new) instance created (see this script for deployment of the GIF). Make sure the version deployed and the version linked in this project (via Git submodules) are compatible. Also these environment variables must be set (e.g. in a .env file):

  • AMOUNTLIB_ADDRESS is the address of the AmountLib contract
  • FEELIB_ADDRESS is the address of the FeeLib contract
  • NFTIDLIB_ADDRESS is the address of the NftIdLib contract
  • REFERRALLIB_ADDRESS is the address of the ReferralLib contract
  • RISKIDLIB_ADDRESS is the address of the RiskIdLib contract
  • ROLEIDLIB_ADDRESS is the address of the RoleIdLib contract
  • SECONDSLIB_ADDRESS is the address of the SecondsLib contract
  • TIMESTAMPLIB_ADDRESS is the address of the TimestampLib contract
  • UFIXEDLIB_ADDRESS is the address of the UFixedLib contract
  • REGISTRY_ADDRESS is the address of the GIF registry

The values can be takes from the output of the deployment of the GIF framework.

To deploy to a different chain, configure it in hardhat.config.ts and run the deployment script with the --network flag.

Documentation

Find the documentation of the next GIF version at https://docs.etherisc.com/gif-next/3.x/

Setup of the development environment

Prerequisites

Installing Docker on Windows is sometimes a struggle. Recommended Approach: Follow the installation instructions for https://docs.docker.com/desktop/install/windows-install/[Docker Desktop]. Installing Docker on Linux or Mac should be straight forward.

Get the source code and editor ready

There are three ways to work with the sandbox (described below)

  • Use the devcontainer provided in the repository
  • Use Github Codespaces

Start the sandbox devcontainer

  • Start the devcontainer (either wait for the pop to build the devcontainer or open the command list (F1) and select the command Dev Containers: Rebuild and reopen in container)
  • Wait for the devcontainer to finish compiling and deploying the contracts

Use Github Codespaces

Github Codespaces is a new feature of Github that allows you to work with a repository in a container environment hosted by Github. To use Github Codespaces you need to have a Github account and you need to be logged in to Github. Open the https://github.com/etherisc/gif-next-sandbox repository in your browser and click on the button Code and select Open with Codespaces from the dropdown menu. This will open a new browser tab with the sandbox repository in a devcontainer hosted by Github. You can now work with the sandbox repository in the browser (or open the codespace in VS Code by clicking on the button Open with VS Code in the upper right corner of the browser tab).

To improve performance of the codespace you can change the machine type in the codespace settings.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •