Insurance (solidity)
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Insurance {
// State variables for the insurance company
string public insuranceCompanyName;
string public insuranceCompanyAddress;
struct person{
uint age;
string name;
string personaddress;
string personcontact;
uint totalPremium;
uint installmentsPaid;
bool isFullyPaid;
PaymentMethod paymentmethod;
}
enum PaymentMethod {
DirectTransfer, // Payment directly in Ether
CreditCard, // Simulated credit card payment
BankTransfer,
None // Simulated bank transfer
}
mapping(address=> person) public persons;
// Constructor to initialize the insurance company details
constructor(string memory _companyName, string memory _companyAddress) {
insuranceCompanyName = _companyName;
insuranceCompanyAddress = _companyAddress;
}
// Getter function to retrieve insurance company details
function getInsuranceCompanyDetails() public view returns (string memory, string memory) {
return (insuranceCompanyName, insuranceCompanyAddress);
}
function setpersondetails( uint _age, string memory _name, string memory _personaddress, string memory _personcontact) public {
persons[msg.sender] = person({
age: _age,
name: _name,
personaddress: _personaddress,
personcontact: _personcontact,
totalPremium: 0,
installmentsPaid: 0,
isFullyPaid: false,
paymentmethod: PaymentMethod.None
});
}
function getpersondetails() public view returns( uint, string memory, string memory, string memory) {
person memory persondetails = persons[msg.sender];
return( persondetails.age, persondetails.name, persondetails.personaddress, persondetails.personcontact);
}
function setpremiumamount(address _insuredaddress, uint _amount) public{
require(persons[_insuredaddress].age != 0,"Person is not registered");
person storage insuredperson= persons[msg.sender];
insuredperson.totalPremium= _amount;
insuredperson.installmentsPaid= 0;
insuredperson.isFullyPaid= false;
}
function paypremium(uint _amount, PaymentMethod _method) public {
person storage insuredperson= persons[msg.sender];
require(insuredperson.age != 0,"Person is not registered");
require(!insuredperson.isFullyPaid, "Premium is already fully paid");
insuredperson.installmentsPaid += _amount;
if(insuredperson.installmentsPaid>= insuredperson.totalPremium){
insuredperson.isFullyPaid= true;
}
insuredperson.paymentmethod= _method;
}
function getPaymentMethod() public view returns (PaymentMethod) {
person memory insuredperson = persons[msg.sender];
require(insuredperson.age != 0, "Person is not registered");
return insuredperson.paymentmethod;
}
}
✅ 1. License and Solidity Version
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
SPDX-License-Identifier: Specifies the MIT open-source license.pragma solidity ^0.8.0;: Ensures the contract works with Solidity version 0.8.0 or newer.
✅ 2. Contract Declaration
contract Insurance {
Begins the definition of the smart contract named Insurance. This contract simulates a basic insurance system on the Ethereum blockchain.
✅ 3. Insurance Company Information
string public insuranceCompanyName;
string public insuranceCompanyAddress;
Stores public details of the insurance company, like:
insuranceCompanyName: Name (e.g., “ABC Insurance”)insuranceCompanyAddress: Address (e.g., “123 Main St”)
✅ 4. Data Structure: person
struct person {
uint age;
string name;
string personaddress;
string personcontact;
uint totalPremium;
uint installmentsPaid;
bool isFullyPaid;
PaymentMethod paymentmethod;
}
Each registered person has:
age,name: Personal details.personaddress,personcontact: Address and phone/email.totalPremium: Total amount they owe.installmentsPaid: How much they have paid so far.isFullyPaid: Whether full payment is completed.paymentmethod: How they paid (explained below).
✅ 5. Enum: PaymentMethod
enum PaymentMethod {
DirectTransfer,
CreditCard,
BankTransfer,
None
}
Defines how premiums can be paid:
DirectTransfer: Via ETH directly.CreditCard: Simulated credit card.BankTransfer: Simulated bank transfer.None: Default value when no payment made.
✅ 6. Mapping: Storage of People
mapping(address => person) public persons;
Stores each person’s data using their wallet address as the key.
✅ 7. Constructor: Set Company Info
constructor(string memory _companyName, string memory _companyAddress) {
insuranceCompanyName = _companyName;
insuranceCompanyAddress = _companyAddress;
}
When the contract is deployed, this sets the company’s name and address.
✅ 8. Getter for Insurance Company Info
function getInsuranceCompanyDetails() public view returns (string memory, string memory)
Returns both the name and address of the company for display.
✅ 9. Register a Person
function setpersondetails(uint _age, string memory _name, string memory _personaddress, string memory _personcontact) public
Allows someone to register themselves with:
- Age
- Name
- Address
- Contact info
Default values:
totalPremium: 0installmentsPaid: 0isFullyPaid: falsepaymentmethod: None
✅ 10. View Personal Info
function getpersondetails() public view returns (uint, string memory, string memory, string memory)
Returns the calling user’s own details:
- Age
- Name
- Address
- Contact info
✅ 11. Set Premium for a Person
function setpremiumamount(address _insuredaddress, uint _amount) public
Sets how much someone needs to pay in total for insurance:
- Can only be set if the person is registered.
- Resets their installments to 0 and payment status to
false.
🛑 Note: The logic uses msg.sender incorrectly. It should probably be:
solidity
CopyEdit
person storage insuredperson = persons[_insuredaddress];
Currently, it updates the sender’s record, not _insuredaddress.
✅ 12. Pay Insurance Premium
function paypremium(uint _amount, PaymentMethod _method) public
Allows a registered person to pay their premium:
- Adds the payment to their total paid.
- Marks as fully paid if enough has been paid.
- Updates their payment method.
✅ 13. Get Payment Method
function getPaymentMethod() public view returns (PaymentMethod)
Returns how the user paid (e.g., CreditCard, BankTransfer).
📄 Insurance Smart Contract Overview
This smart contract simulates a basic insurance system on the blockchain, where people can register, receive a premium quote, and pay using different methods.
🎯 Main Features:
- ✅ Register individuals with their details.
- ✅ Set and track total premium owed.
- ✅ Accept payments in installments.
- ✅ Check if premiums are fully paid.
- ✅ View payment method used.
💰 Supported Payment Methods:
- DirectTransfer: Pay in ETH (simulated).
- CreditCard: Simulated card payment.
- BankTransfer: Simulated bank transaction.
- None: Default if no payment made.