0

I am trying to work with the smart contract I found on: https://github.com/pappas999/Parametric-Crop-Insurance/blob/main/contracts/Crop-Insurance.sol

I can deploy the contract but when I try to interact with the function newContract I always receive the metamask error "Warning! Error encountered during contract execution [Reverted]". Perhaps someone now how to solve it? It would be really great. I don`t really know what to do and where it come from..

I am using the code:

pragma solidity 0.4.24;
pragma experimental ABIEncoderV2;


//Imports 

import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.4/Aggregator.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.4/vendor/Ownable.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.4/interfaces/LinkTokenInterface.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.4/interfaces/AggregatorInterface.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.4/vendor/SafeMathChainlink.sol";
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.4/interfaces/AggregatorV3Interface.sol";



///////////////////////////////////////////////////////////////////


contract InsuranceProvider {

    using SafeMathChainlink for uint; //using SafeMath for uint means that the functions contained in the SafeMath.sol library can be used directly on objects of type uint
    address public insurer = msg.sender; //msg.sender will be the person who's currently connecting with the contract
    AggregatorV3Interface internal priceFeed; 

    uint public constant DAY_IN_SECONDS = 60; //How many seconds in a day. 60 for testing, 86400 for Production

    uint256 constant private ORACLE_PAYMENT = 0.1 * 10**18; // 0.1 LINK
    address public constant LINK_KOVAN = 0xa36085F69e2889c224210F603D836748e7dC0088 ; //address of LINK token on Kovan https://docs.chain.link/docs/link-token-contracts/

 
    //here is where all the insurance contracts are stored.
    mapping (address => InsuranceContract) contracts;


    constructor()   public payable {
        priceFeed = AggregatorV3Interface(0x9326BFA02ADD2366b30bacB125260Af641031331); //The constructor() {} initializes an interface object named priceFeed that uses AggregatorV3Interface and connects specifically to a proxy aggregator contract that is already deployed at 0x9326BFA02ADD2366b30bacB125260Af641031331
    }

    /**
     * @dev Prevents a function being run unless it's called by the Insurance Provider https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol#L28
    */

    modifier onlyOwner() {
        require(insurer == msg.sender, 'Only Insurance provider can do this');
        _;
    }
     

   /**
    * @dev Event to log when a contract is created
    */
    event contractCreated(address _insuranceContract, uint _premium, uint _totalCover);


    /**
     * @dev Create a new contract for client, automatically approved and deployed to the blockchain
     */
    function newContract(address _client, uint _duration, uint _premium, uint _payoutValue, string _cropLocation) public payable onlyOwner() returns(address) {
       
        //return(addresses){}in den Klappemern spezifiziert man welche adresse genau herausgegeben werden muss
        //create contract, send payout amount so contract is fully funded plus a small buffer
        InsuranceContract i = (new InsuranceContract).value((_payoutValue * 1 ether).div(uint(getLatestPrice())))(_client, _duration, _premium, _payoutValue, _cropLocation, LINK_KOVAN, ORACLE_PAYMENT);
        contracts[address(i)] = i;  //store insurance contract in contracts Map

        //emit an event to say the contract has been created and funded
        emit contractCreated(address(i), msg.value, _payoutValue);

        //now that contract has been created, we need to fund it with enough LINK tokens to fulfil 1 Oracle request per day, with a small buffer added
        LinkTokenInterface link = LinkTokenInterface(i.getChainlinkToken());
        link.transfer(address(i), ((_duration.div(DAY_IN_SECONDS)) + 2) * ORACLE_PAYMENT.mul(2));


        return address(i);

    }


    /**
     * @dev returns the contract for a given address
     */
    function getContract(address _contract) external view returns (InsuranceContract) {
        return contracts[_contract];
    }

    /**
     * @dev updates the contract for a given address
     */
    function updateContract(address _contract) external {
        InsuranceContract i = InsuranceContract(_contract);
        i.updateContract();
    }

    /**
     * @dev gets the current rainfall for a given contract address
     */
    function getContractRainfall(address _contract) external view returns(uint) {
        InsuranceContract i = InsuranceContract(_contract);
        return i.getCurrentRainfall();
    }

    /**
     * @dev gets the current rainfall for a given contract address
     */
    function getContractRequestCount(address _contract) external view returns(uint) {
        InsuranceContract i = InsuranceContract(_contract);
        return i.getRequestCount();
    }



    /**
     * @dev Get the insurer address for this insurance provider
     */
    function getInsurer() external view returns (address) {
        return insurer;
    }



    /**
     * @dev Get the status of a given Contract
     */
    function getContractStatus(address _address) external view returns (bool) {
        InsuranceContract i = InsuranceContract(_address);
        return i.getContractStatus();
    }

    /**
     * @dev Return how much ether is in this master contract
     */
    function getContractBalance() external view returns (uint) {
        return address(this).balance;
    }

    /**
     * @dev Function to end provider contract, in case of bugs or needing to update logic etc, funds are returned to insurance provider, including any remaining LINK tokens
     */
    function endContractProvider() external payable onlyOwner() {
        LinkTokenInterface link = LinkTokenInterface(LINK_KOVAN);
        require(link.transfer(msg.sender, link.balanceOf(address(this))), "Unable to transfer");
        selfdestruct(insurer);
    }



    /**
     * Returns the latest price
     */
    function getLatestPrice() public view returns (int) {
        (   uint80 roundID,
            int price,
            uint startedAt,
            uint timeStamp,
            uint80 answeredInRound
        ) = priceFeed.latestRoundData();
        // If the round is not complete yet, timestamp is 0
        require(timeStamp > 0, "Round not complete");
        return price;
    }

    /**
     * @dev fallback function, to receive ether
     */
    function() external payable {  }

}

1 Answer 1

0

Cannot post a comment so I will delete this answer soon.

Please post a more adequate code sample, ideally just the function you are interacting with alongside everything that function uses.

1
  • Thank you for the hint:) I changed it a little bit. I hope it is better now:)
    – varnold95
    Jun 7, 2022 at 17:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.