Conditional Redemption
Conditional On-Chain Package Redemption
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.15;
import "./PostOffice.sol";
import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";
contract ConditionalDeliveryService {
PostOffice public postOffice;
AggregatorV3Interface public chainlinkAggregator;
uint8 public constant USDC_DECIMALS = 6;
uint256 public constant CHAINLINK_DECIMALS = 8;
uint256 public constant TO_SCALE = 10 ** (CHAINLINK_DECIMALS - USDC_DECIMALS);
// The target price ($3,000)
uint256 public constant TARGET_PRICE = 3000 * 10 ** USDC_DECIMALS;
constructor(address postOfficeAddress_, address chainlinkAggregator_) {
postOffice = PostOffice(postOfficeAddress_);
chainlinkAggregator = AggregatorV3Interface(chainlinkAggregator_);
}
function shipPackage(
CapsuleData.CapsuleContent calldata packageContent_,
PostOffice.SecurityInfo calldata securityInfo_,
address receiver_
) external returns (uint256) {
return postOffice.shipPackage(packageContent_, securityInfo_, receiver_);
}
function pickupPackageIfEthAbove3k(
uint256 packageId_,
string calldata rawPassword_,
string calldata salt_,
bool shouldRedeem_
) external {
(, int256 _price, , , ) = chainlinkAggregator.latestRoundData();
uint256 priceInUSDC = uint256(_price) / TO_SCALE;
require(priceInUSDC >= TARGET_PRICE, "ETH price is not above 3000 USDC");
postOffice.pickup(packageId_, rawPassword_, salt_, shouldRedeem_);
}
}Last updated