-
Notifications
You must be signed in to change notification settings - Fork 1
Structs
anggapurnajiwa edited this page Nov 26, 2021
·
3 revisions
// To define type of products on INFI
enum CoverType { SMART_PROTOCOL_FAILURE, STABLECOIN_DEVALUATION, CUSTODIAN_FAILURE, RUGPULL_LIQUIDITY_SCAM }
// Curreny of Stablecoins that accepts on INFI
enum CurrencyType { USDT, DAI, USDC }
// Offer or Request Funding Type
enum InsuredSumRule { PARTIAL, FULL } // Mapping from Stablecoins to Decimals that used by the coins
mapping(CurrencyType => uint8) currencyDecimals;
// Mapping from coin-id to Decimals that used by the coins
mapping(string => uint8) coinIdToDecimals; // For passing parameter and store state variables
CoverRequest[] requests; // CoverRequest.id
mapping(address => uint[]) buyerToRequests;
mapping(string => uint[]) coinIdToRequests;
struct CoverRequest {
uint coverQty; // coverQty decimals depends on coinIdToDecimals mapping
uint8 coverMonths; // represent month value 1-12
uint insuredSum;
uint insuredSumTarget; // if full funding : insuredSum - 2$
CurrencyType insuredSumCurrency;
uint premiumSum;
CurrencyType premiumCurrency;
uint expiredAt; // now + 14 days
string coinId; // Coin-id coming from CoinGecko
CoverLimit coverLimit;
InsuredSumRule insuredSumRule;
address buyer; // address which created Request
} // For passing parameter and store state variables
CoverOffer[] offers; // CoverOffer.id
mapping(address => uint[]) funderToOffers;
mapping(string => uint[]) coinIdToOffers;
struct CoverOffer {
uint8 minCoverMonths; // represent month value 1-12 (expiredAt + 1 month - now >= minCoverMonths)
uint insuredSum;
CurrencyType insuredSumCurrency;
uint premiumCostPerMonth; // $0.02 per $1 insured per Month (2000)
CurrencyType premiumCurrency;
// IMPORTANT: max date for buying cover = expiredAt + 1 month
uint expiredAt; // despositEndDate - 14 days beforeDepositEndDate
string coinId; // Coin-id coming from CoinGecko
CoverLimit coverLimit;
InsuredSumRule insuredSumRule;
address funder; // address which created the listing
} struct CoverLimit {
CoverType coverType;
uint[] teritoryIds; // Platform Id, Price Feed Id, Custodian Id , (Dex Pool Id not Yet implemented)
} // Storage struct
// Relationship: CoverCoverOffer ||--< Cover
// Relationship: CoverRequest ||--< Cover
// Relationship: One cover can have only one offer
// Relationship: One cover can have only one request
InsuranceCover[] covers; // InsuranceCover.id
mapping(address => uint[]) holderToCovers;
mapping(uint => uint[]) offerIdToCovers;
mapping(uint => uint[]) requestIdToCovers;
struct InsuranceCover {
// type computed from (offerId != 0) or (requestId != 0)
// If BuyCover (take offer)
uint offerId; // from BuyCover.offerId
// If CoverFunding (take request)
uint requestId; // from CoverFunding.requestId
// uint[] provideIds;
// will validate claimSender
address holder; // from BuyCover.buyer or CoverRequest.buyer
// will validate maximum claimSum
uint insuredSum; // from BuyCover.insuredSum or sum(CoverFunding.fundingSum)
// will validate maximum claimQuantity
uint coverQty; // from BuyCover.coverQty or CoverRequest.coverQty
// used inside endAt view function
uint startAt; // fullyFundedAt, or takeOfferAt,
// if (startAt == 0), use expiredAt (and check insuredSumTaken > CoverRequest.insuredSumTarget)
// will validate claimDeadline
// uint endAt; // moved to view function
// if take offer, cover starts immediately
// endAt = now + BuyCover.coverMonths
// if take request, and fully funded before expired, cover starts immediately...
// endAt = now + CoverRequest.coverMonths
// if take request, and expired before fully funded, cover starts on CoverRequest.expiredAt
// if (endAt == 0 && now > CoverRequest.expiredAt) endAt = expiredAt + CoverRequest.coverMonths
} // Storage: "Booking" object when take request
// Relationship: CoverRequest ||--< CoverFunding
mapping(uint => CoverFunding[]) requestIdToCoverFundings;
mapping(address => CoverFunding[]) funderToCoverFundings;
struct CoverFunding {
uint requestId;
address funder;
// insurance data:
uint fundingSum; // part or portion of total insuredSum
}
// Payload: object when take request
// Virtual struct/type for payload (type of payloadBuyCover)
struct ProvideCover {
uint requestId;
address provider;
// insurance data:
uint fundingSum;
CoinPricingInfo assetPriceInfo;
CollectPermit assetPermit;
} // Payload: object when take offer
// Virtual struct/type for payload (type of payloadBuyCover)
struct BuyCover {
uint offerId;
address buyer;
// insurance data:
uint8 coverMonths; // represent month value 1-12
uint coverQty; // coverQty decimals depends on coinIdToDecimals mapping
uint insuredSum; // need validation : coverQty * assetPriceInfo.coinCurrentPrice
CoinPricingInfo assetPriceInfo;
CollectPermit premiumPermit;
// TODO: validate coverQty based on insuredSum
} struct CoinPricingInfo {
string coinId;
string coinSymbol;
uint lastUpdatedAt;
uint coinCurrentPrice; // decimals 6
uint8 sigV; // EIP712 Signature
bytes32 sigR;
bytes32 sigS;
} Platform[] platforms;
struct Platform {
string name;
string website;
} Oracle[] oracles;
struct Oracle {
string name;
string website;
} PriceFeed[] usdPriceFeeds;
mapping(string => uint[]) symbolToUsdPriceFeeds;
struct PriceFeed {
uint oracleId;
uint chainId;
uint8 decimals;
address proxyAddress;
} Custodian[] custodians;
struct Custodian {
string name;
string website;
} struct EIP2612Permit {
address owner;
uint value;
address spender;
uint deadline;
uint8 sigV; // EIP712 Signature
bytes32 sigR;
bytes32 sigS;
} struct EIP1363Data {
bytes32 payType;
bytes payData;
}
bytes32 public constant CREATE_COVER_REQUEST = keccak256("CREATE_COVER_REQUEST");
bytes32 public constant CREATE_COVER_OFFER = keccak256("CREATE_COVER_OFFER");
function onTransferReceived(address operator, address from, uint256 value, bytes memory data) external returns (bytes4) {
// decode incoming data
(bytes32 payType, bytes memory payData) = abi.decode(data, (bytes32, bytes));
if (payType == CREATE_COVER_REQUEST) {
_createCoverRequest(operator, from, value, payData);
} else if (payType == CREATE_COVER_OFFER) {
_createCoverOffer(operator, from, value, payData);
} else {
revert("ERC1363Receiver: INVALID_PAY_TYPE");
}
}
function _createCoverRequest(address operator, address from, uint256 value, bytes memory payData) internal {
(CreateCoverRequestData memory data) = abi.decode(payData, (CreateCoverRequestData));
}
function _createCoverOffer(address operator, address from, uint256 value, bytes memory payData) internal {
(CreateCoverOfferData memory data) = abi.decode(payData, (CreateCoverOfferData));
} struct CreateCoverRequestData {
CoverRequest request; //
CoinPricingInfo assetPricing; //
CoinPricingInfo feePricing; //
EIP2612Permit collectPremiumPermit; // for transfer DAI, USDT, USDC
} struct CreateCoverOfferData {
CoverOffer offer; //
CoinPricingInfo assetPricing; //
CoinPricingInfo feePricing; //
EIP2612Permit collectFundingPermit; // for transfer DAI, USDT, USDC
}