Coder Social home page Coder Social logo

tolmachevroman / farmers-micro-insurance-on-blockchain Goto Github PK

View Code? Open in Web Editor NEW
2.0 3.0 0.0 898 KB

Example of a weather-based micro insurance application using blockchain backend and React frontend app

JavaScript 65.47% Solidity 19.75% HTML 6.01% CSS 8.78%
ethereum blockchain solidity smart-contracts hardhat react

farmers-micro-insurance-on-blockchain's Introduction

Weather-Based Micro Insurance application using Blockchain and React

Project overview

Consider a situation: you're a farmer πŸ§‘β€πŸŒΎ and need to protect your crops from draught. In developed countries, you have sophisticated insurance industry, and after you send a claim, experts from your insurance company come and actually check the damage to decide on the money you'll receive.

However, farmers living in developing countries often times don't have access to that level of insurance or traditional banking. You'd like to provide them with an accessible insurance product and decide on settlements automatically to save costs.

Imagine such a product: farmers can buy insurance starting from 0.1 ETH (Ether, native cryptocurrency of Ethereum and second market cap after Bitcoin), about 300$ in early February 2022. Smart contract, a publicly available code, has no small letters or human meddling. It receives temperature updates from some external provider, say maximum daily temperature every day. If the last five temperatures were hotter than some defined threshold value, contract will pay the settlement automatically.

Granted it's a contrived example and real insurance companies don't work this way, but it's a great way to discover smart contracts and how to interact with them!

Demo of what we're gonna build:

Click to watch

Project structure

This is a full stack project. Backend provides smart contract logic running on a local Ethereum blockchain using Hardhat, frontend provides a React web app to connect to this local blockchain and interact with the smart contract.

Technology stack includes Hardhat and React frameworks, Solidity for backend and Javascript for both of them. Both use Ethers.js extensively, too.

Project can be divided in the following sections:

Backend

Getting started with Hardhat

Hardhat is an excellent choice when developing EVM compatible software. It provides clean and simple CLI to create, test and deploy smart contracts. It comes with local blockchain and predefined accounts to deploy your contracts locally to avoid typical delays when working with public chains.

There's no need to deploy our contract on public test chains, and local chain fits our needs perfectly. Still as an exercise, I highly recommend going through the Alchemy tutorials. They are highly educational, and in fact this project was inspired by Alchemy samples.

We'll follow the basic guide and create an empty Hardhat project and add an empty WeatherInsurance.sol contract.

Contract general overview

So what do we need in our contract? We should have a way to buy an insurance and to keep it on-chain, a way to send a temperature update, to decide on conditions and finally to pay settlement to a given insurance.

Contract implementation

Let's go through the contract step by step.

Line 8 Ownable means that only address that created this contract (i.e. owner) can execute certain methods marked with onlyOwner identifier. Ownable is an open-source contract provided by an organization called Zeppelin.

Lines 12-19 We define three constants: settlement (payment the insuree receives) multiplier, minimum premium price and temperature threshold. To give an example, if I bought an insurance with 1 ETH premium and temperature has been hotter than the threshold for five days in a row, then I should receive premium * multiplier, or 3 ETH.

Line 21 We need to keep track of active insurances, so that one person/address cannot have more than one active policy.

Line 22 We keep insurances on-chain in an array.

Lines 24-36 Each Insurance object has an insuree (address of its owner), a premium value and last five temperatures encoded in a Temperature object. There multiple ways to implement this, actually I started with an array, but there were issues with logging this array later while testing, so I decided to keep it as a struct instead.

Line 38 Events are a way for Solidity to broadcast changes to the exterior. This event in particular will be useful later when interacting with the web app.

Lines 40-46 Our contract receives money on creation, so it is payable. Settlements are greater than premiums, so it's a good idea to deploy contract with some reasonable amount of money to start with.

Lines 49-71 createInsurance() is mostly for testing purposes, to get an Insurance object without adding it on-chain.

Lines 73-98 buyInsurance() is called by our clients. It receives ETH sent by them and uses their address to create a new Insurance and add it on-chain. Notice that it will revert if the ETH sent is below minimum or if there's an active policy owned by this address.

Lines 100-121 updateTemperature() is called by a contract owner only, to avoid setting wrong temperature by someone else. This method updates temperature list in all active insurances and checks whether we should pay them.

Lines 124-134 shiftTemperatures() is a helper method to shift temperatures from right to left when new one is added.

Lines 137-150 shouldPaySettlement() is a helper method to decide if there should be a settlement payment.

Lines 152-166 paySettlement() is called by a contract owner only, and transfers settlement to a given insuree.

Lines 169-176 remove() is a helper method to remove an Insurance from the _insurances array.

Notice that many methods should be private when deploying to a public chain, it's just that in order to test them from Javascript code they should be visible. So in a real-world situation, you would mark them private after testing and before deployment.

Testing the contract

Testing is an essential part of smart contracts development, more so that it's difficult to update a contract once it's deployed. We'll use Ethers and Chai libraries to interact with the contract and test it, respectively.

Let's go through the weather-insurance-test.js file.

Line 4 We refactor contract creation to a separate function, we'll use it everywhere later. Notices that it deploys the contract with 0.1 ETH by default.

Lines 11-18 Make sure that contract gets deployed with the initial value we specify.

Lines 20-48 Make sure that temperatures get shifted.

Lines 50-87 Make sure that payment conditions are triggered.

Lines 89-136 Make sure that settlement is paid.

Lines 138-162 Make sure temperatures get updated for multiple insurances.

Lines 164-191 Make sure clients can have one and only one insurance, bought at a price greater than the minimum specified.

Hardhat config file

While working with Hardhat you'll eventually need to specify this or that parameter, add some dependency. Hardhat uses a special config file for that.

Let's take a look at our hardhat.config.js file. We added hardhat-abi-exporter to have an ABI .json file every time contract is compiled. ABI stands for Application Binary Interface, it is a window to our smart contract and is required on the frontend side.

Another point to mention is Hardhat local chain. We set chainId to 1337 to make it visible to Metamask, a popular cryptowallet we'll use later. Also, we set initial balance of fake accounts to 100 ETH.

Deploying to local Hardhat chain

As was mentioned earlier, you can deploy your contract to a local blockchain, or to public blockchains. Popular public test blockchains are Rinkeby and Ropsten, and of course you can deploy to the Ethereum mainnet.

We don't want to deploy it to public chains, rather, we'll follow the guide and deploy it to a local standalone Hardhat chain.

We set 10 ETH as the initial balance when deploying our contract locally.

Let's try and deploy it πŸš€

enter image description here enter image description here enter image description here

Check this: the contract owner is in fact the first account of the 20 accounts provided by Hardhat.

You may have noticed that in the tests, we skipped the first signer, and started with Alice and Bob as our clients. And that's because the first signer is just us, the ones deploying the contract πŸ™‹. We can have an insurance too, no doubt! But I like to think of contract owner as a company, and of others as clients.

Frontend

Getting started with React

We'll create a new React app and add a couple of files, namely a new widget and a helper Javascript file. This is inspired by Alchemy projects, so I again encourage you to check it.

Interacting with the backend

What do we need in a web app to interact with an Ethereum blockchain? Well, several things: a cryptowallet to manage your money (aka tokens) and connect to a blockchain (not necessarily Ethereum), and a way to call contract methods and get responses.

Metamask 🦊is a popular wallet, it's installed as a browser extension. You can add multiple accounts there, for multiple blockchains, including a local one.

To get information of what methods are available from your smart contract, you use contract-abi.json file. You get this file automatically after installing the hardhat-abi-exporter in the Hardhat project.

Finally, to tie this all together, you add a couple of callbacks to connect a Metamask wallet, to call smart contract methods and to listen to SettlementPaid event.

We won't go line by line this time, but take a look at the WeatherInsurance.js file.

One important point to mention is WeatherInsurance contract address on the blockchain you're connected to. You get this address after deploying the contract (see image above, Contract address line). So each time contract gets deployed it will get a new address, make sure to update your web app code with your current contract address.

Notice also that here we have only one signer, the actual address connected via Metamask.

FYI, I've run into a problem with "Nonce too high" after restarting the local Hardhat chain. Here's the solution πŸ”§if you run into the same issue.

Testing functionality with local Hardhat chain

Make sure you have the local Hardhat running and contract deployed, an you use correct contract address.

Go to the frontend folder and start the web app. You should see something like this:

enter image description here

Click on the Metamask icon in your browser, and you'll see your accounts. Connect your account.

I have mine connected already. Notice it has less than 100 ETH, that's because I deployed contract using this account and spent 10 ETH as the initial balance and some gas per each transaction:

enter image description here

When testing with Hardhat local chain, it's useful to import several Hardhat accounts to your Metamask and switch between them to test for different users:

enter image description here

Specify a premium, say 0.3 ETH, and try to buy an insurance. Metamask will ask you to confirm the transaction. Check that section on top of the Metamask window: your currently connected account is interacting with the contract on a localhost:8545.

enter image description here

If transaction has been successful, you'll see a message in the web app and also in the terminal running the blockchain:

enter image description here

Now let's try to update temperature:

enter image description here

Again, you'll see messages in web app and terminal:

enter image description here

Let's do it several times in a row with values above 41 degrees. After you do it 5 times, you'll get paid your premium times multiplier (0.3 ETH times 3 = 0.9 ETH), and your insurance will be closed. You can get a new one now!

enter image description here enter image description here

farmers-micro-insurance-on-blockchain's People

Contributors

tolmachevroman avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar  avatar

farmers-micro-insurance-on-blockchain's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.