Solidity contract integration

Get list of whitelisted policies

First import interfaces

  • IContractRegistry.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

interface IContractsRegistry {
  function getPolicyBookRegistryContract() external view returns (address);
}
  • IPolicyBookFabric.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;

interface IPolicyBookFabric {
    enum ContractType {
        CONTRACT,
        STABLECOIN,
        SERVICE,
        EXCHANGE,
        VARIOUS
    }
}
  • IPolicyBookRegistry.sol

pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

import "./IPolicyBookFabric.sol";

interface IPolicyBookRegistry {
    struct PolicyBookStats {
        string symbol;
        address insuredContract;
        IPolicyBookFabric.ContractType contractType;
        uint256 maxCapacity;
        uint256 totalSTBLLiquidity;
        uint256 stakedSTBL;
        uint256 APY;
        uint256 annualInsuranceCost;
        uint256 bmiXRatio;
        bool whitelisted;
    }

    function countWhitelisted() external view returns (uint256);

    function listWithStatsWhitelisted(uint256 offset, uint256 limit)
        external
        view
        returns (address[] memory _policyBooksArr, PolicyBookStats[] memory _stats);
}

Then insert this code

    function getWhiteListedPolicies()
        public
        view
        returns (address[] memory _policyBooksArr, IPolicyBookRegistry.PolicyBookStats[] memory _stats)
    {
        // SET UP
        address contractRegistryAddress = "0x8050c5a46FC224E3BCfa5D7B7cBacB1e4010118d";
        IContractRegistry contractRegistry = IContractsRegistry(contractRegistryAddress);
        IPolicyBookRegistry policyBookRegistry = IPolicyBookRegistry(contractRegistry.getPolicyBookRegistryContract());
​
        // FUNCTION CALL
        uint256 countWhiteListed = policyBookRegistry.countWhitelisted();
        return policyBookRegistry.listWithStatsWhitelisted(0, countWhiteListed);
    }

Returns

Name

Type

Description

_policyBooksArr

address[]

List of whitelisted policies

_stats

IPolicyBookRegistry.PolicyBookStats[]

The array of policies stats (struct PolicyBookStats)

Arguments

None

Purchase policy

Before calling this function the msg.sender should approve the address(this) to spend the totalPrice get by the following method in PolicyBook.sol:

(uint256 totalSeconds, uint256 totalPrice) = getPolicyPrice(_epochsNumber, _coverTokens, msg.sender)

converted in stblPrice:

uint256 stblPrice = totalPrice / 10**12;

First import interfaces

  • IPolicyBookFacade.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

interface IPolicyBookFacade {
    function buyPolicyFromDistributorFor(
        address _buyer,
        uint256 _epochsNumber,
        uint256 _coverTokens
    ) external;
}
  • IPolicyBook.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

import "./IPolicyBookFacade.sol";

interface IPolicyBook {
    function policyBookFacade() external view returns (IPolicyBookFacade);
    
     function getPolicyPrice(
        uint256 _epochsNumber,
        uint256 _coverTokens,
        address _buyer
    ) external view returns (uint256 totalSeconds, uint256 totalPrice);
}

Then insert this code

    function purchasePolicy(
        address policyBookAddress,
        uint256 _epochsNumber,
        uint256 _coverTokens,
        address _distributor
    ) public {
        // SET UP
        IPolicyBook policyBook = IPolicyBook(policyBookAddress);
        IPolicyBookFacade policyBookFacade = policyBook.policyBookFacade();
        stablecoin = ERC20(contractRegistry.getUSDTContract());

        // FUNCTION CALL
        (, uint256 totalPrice) = policyBook.getPolicyPrice(_epochsNumber, _coverTokens, msg.sender);
        uint256 stblPrice = totalPrice / 10**12;
        stablecoin.safeTransferFrom(msg.sender, address(this), stblPrice);

        stablecoin.approve(address(policyBook), stblPrice)

        policyBookFacade.buyPolicyFromDistributorFor(
            msg.sender,
            _epochsNumber,
            _coverTokens,
            _distributor
        );
    }

Returns

None

Arguments

Name

Type

Description

policyBookAddress

address

The address of the chosen policy

_epochsNumber

uint256

The period policy will cover

_coverTokens

uint256

The amount paid for the coverage

_distributor

address

The address of the distributor if it was sold by a distributor, he will get a fee

Get list of purchased policies

First import interfaces

  • IContractRegistry.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

interface IContractsRegistry {
  function getPolicyRegistryContract() external view returns (address);
}
  • IClaimingRegistry.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

interface IClaimingRegistry {
  enum ClaimStatus {
    CAN_CLAIM,
    UNCLAIMABLE,
    PENDING,
    AWAITING_CALCULATION,
    REJECTED_CAN_APPEAL,
    REJECTED,
    ACCEPTED
  }
}
  • IPolicyRegistry.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

import "./IClaimingRegistry.sol";

interface IPolicyRegistry {
  struct PolicyInfo {
    uint256 coverAmount;
    uint256 premium;
    uint256 startTime;
    uint256 endTime;
  }
  function getPoliciesLength(address _userAddr) external view returns (uint256);
  function getPoliciesInfo(address _userAddr, bool _isActive, uint256 _offset, uint256 _limit)
    external
    view
    returns (
      uint256 _policiesCount,
      address[] memory _policyBooksArr,
      PolicyInfo[] memory _policies,
      IClaimingRegistry.ClaimStatus[] memory _policyStatuses
    );
}

Then insert this code

    function getPurchasedPolicies(bool _isActive)
        public
        view
        returns (
            uint256 _policiesCount,
            address[] memory _policyBooksArr,
            IPolicyRegistry.PolicyInfo[] memory _policies,
            IClaimingRegistry.ClaimStatus[] memory _policyStatuses
        )
    {
        // SET UP
        address contractRegistryAddress = "0x8050c5a46FC224E3BCfa5D7B7cBacB1e4010118d";
        IContractRegistry contractRegistry = IContractsRegistry(contractRegistryAddress);
        IPolicyRegistry policyRegistry = IPolicyRegistry(
            contractRegistry.getPolicyRegistryContract()
        );

        // FUNCTION CALL
        uint256 count = policyRegistry.getPoliciesLength(msg.sender);
        return policyRegistry.getPoliciesInfo(msg.sender, _isActive, 0, count);
    }

Returns

Name

Type

Description

_policiesCount

uint256

The number of police in the array

_policyBooksArr

address[]

The array of policy books addresses

_policies

IPolicyRegistry.PolicyInfo[]

The array of policies info (struct PolicyInfo)

_policyStatuses

IClaimingRegistry.ClaimStatus[]

The array of status of claim (enum ClaimStatus)

Arguments

Name

Type

Description

_isActive

bool

If true, returns an array with information about active policies, if false, about inactive

Earn interest

Before calling this function the msg.sender should approve the address(this) to spend the _liquidityAmount converted in stblAmount

uint256 stblAmount = _liquidityAmount / 10**12;    

First import interfaces

  • IPolicyBookFacade.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

interface IPolicyBookFacade {
    function addLiquidityFromDistributorFor(
        address _user,
        uint256 _liquidityAmount
    ) external;
}
  • IPolicyBook.sol

// SPDX-License-Identifier: MIT
pragma solidity ^0.7.4;
pragma experimental ABIEncoderV2;

import "./IPolicyBookFacade.sol";

interface IPolicyBook {
    function policyBookFacade() external view returns (IPolicyBookFacade);
}

Then insert this code

    function earnInterest(address policyBookAddress, uint256 _liquidityAmount) public {
        // SET UP
        IPolicyBook policyBook = IPolicyBook(policyBookAddress);
        IPolicyBookFacade policyBookFacade = policyBook.policyBookFacade();
        stablecoin = ERC20(contractRegistry.getUSDTContract());

        // FUNCTION CALL
        uint256 stblAmount = _liquidityAmount / 10**12;
        stablecoin.safeTransferFrom(msg.sender, address(this), stblAmount);
        stablecoin.approve(address(policyBook), stblAmount)

        policyBookFacade.addLiquidityFromDistributorFor(msg.sender, _liquidityAmount);
    }

Returns

None

Arguments

Name

Type

Description

policyBookAddress

address

The address of the chosen policy

_liquidityAmount

uint256

The amount of coverage provision

Last updated