Skip to content

Commit

Permalink
minor changes to CropProduct
Browse files Browse the repository at this point in the history
  • Loading branch information
matthiaszimmermann committed Dec 23, 2024
1 parent 7de86e2 commit 4f4b737
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 11 deletions.
24 changes: 21 additions & 3 deletions contracts/examples/crop/CropProduct.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import {ReferralLib} from "../../type/Referral.sol";
import {RiskId} from "../../type/RiskId.sol";
import {RequestId} from "../../type/RequestId.sol";
import {Seconds, SecondsLib} from "../../type/Seconds.sol";
import {Str} from "../../type/String.sol";
import {Str, StrLib} from "../../type/String.sol";
import {Timestamp, TimestampLib} from "../../type/Timestamp.sol";
import {UFixed, UFixedLib} from "../../type/UFixed.sol";

Expand All @@ -29,6 +29,9 @@ contract CropProduct is
Product
{

// Events
event LogCropPolicyCreated(NftId policyNftId);

// Custom errors
error ErrorInvalidId(string id);
error ErrorRecordAlreadyExists(string id);
Expand Down Expand Up @@ -92,7 +95,6 @@ contract CropProduct is
Str [] internal _crops;

mapping(Str id => RiskId riskId) internal _riskId;
mapping(RiskId riskId => RequestId requestId) internal _requests;

// GIF V3 specifics
NftId internal _defaultBundleNftId;
Expand Down Expand Up @@ -274,6 +276,8 @@ contract CropProduct is
_collectPremium(
policyNftId,
TimestampLib.zero()); // keep activation timestamp

emit LogCropPolicyCreated(policyNftId);
}


Expand Down Expand Up @@ -358,6 +362,14 @@ contract CropProduct is
function seasons() public view returns (Str [] memory) { return _seasons; }
function crops() public view returns (Str [] memory) { return _crops; }

function getRiskId(Str id)
external
view
returns (RiskId riskId)
{
return _riskId[id];
}

function getRisk(RiskId riskId)
public
view
Expand Down Expand Up @@ -411,7 +423,13 @@ contract CropProduct is
return sumInsuredAmount.multiplyWith(payoutFactor);
}

function getRequestForRisk(RiskId riskId) public view returns (RequestId requestId) { return _requests[riskId]; }
function toStr(string memory str) public pure returns (Str) {
return StrLib.toStr(str);
}

function toString(Str str) public pure returns (string memory) {
return StrLib.toString(str);
}

//--- internal functions ------------------------------------------------//

Expand Down
37 changes: 29 additions & 8 deletions test/examples/crop/CropProduct.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,15 @@ contract CropProductTest is CropBaseTest {
}


function approveProductTokenHandler() public {
// approve flight product to buy policies
vm.startPrank(customer);
accountingToken.approve(
address(cropProduct.getTokenHandler()),
accountingToken.balanceOf(customer));
vm.stopPrank();
function test_setUp() public {
assertEq(address(cropProduct), registry.getObjectAddress(cropProductNftId), "unexpected product address");
assertEq(address(cropProduct.getToken()), address(accountingToken), "unexpected token address");
}


function test_cropProductSetup() public {
// GIVEN - setp from flight base test
approveProductTokenHandler();
_approveProductTokenHandler();

_printAuthz(instance.getInstanceAdmin(), "instance");

Expand Down Expand Up @@ -155,6 +151,8 @@ contract CropProductTest is CropBaseTest {
vm.stopPrank();

// THEN
assertTrue(riskId.toInt() > 0, "unexpected risk id (zero)");
assertEq(riskId.toInt(), cropProduct.getRiskId(riskIdStr).toInt(), "unexpected risk id");
assertEq(instanceReader.risks(cropProductNftId), 1, "unexpected risk count");
assertTrue(instanceReader.getRiskId(cropProductNftId, 0) == riskId, "unexpected risk id");

Expand Down Expand Up @@ -329,6 +327,20 @@ contract CropProductTest is CropBaseTest {
}


function test_cropProductStringHelper() public {
// GIVEN
// string memory helloString = unicode"hello wörld!";
string memory helloString = "hello world!";

// WHEN
Str helloStr = cropProduct.toStr(helloString);

// THEN
assertEq(helloStr.toString(), helloString, "unexpected string (a)");
assertEq(cropProduct.toString(helloStr), helloString, "unexpected string (b)");
}


function _createSeason(string memory nanoId) internal returns (Str seasonId) {
seasonId = StrLib.toStr(nanoId);
uint16 year = 2025;
Expand Down Expand Up @@ -398,4 +410,13 @@ contract CropProductTest is CropBaseTest {
premiumAmount);
vm.stopPrank();
}


function _approveProductTokenHandler() internal {
vm.startPrank(customer);
accountingToken.approve(
address(cropProduct.getTokenHandler()),
accountingToken.balanceOf(customer));
vm.stopPrank();
}
}

0 comments on commit 4f4b737

Please sign in to comment.