Skip to content

Commit

Permalink
fix latestExpiry
Browse files Browse the repository at this point in the history
  • Loading branch information
sofa-org committed May 18, 2024
1 parent f749863 commit a83bfbf
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
21 changes: 10 additions & 11 deletions contracts/vaults/AAVEDNTVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,10 @@ contract AAVEDNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeable,
}

function _burn(uint256 term, uint256 expiry, uint256[2] memory anchorPrices, uint256 collateralAtRiskPercentage, uint256 isMaker) internal nonReentrant returns (uint256 payoff) {
(uint256 latestTerm, bool _isBurnable) = isBurnable(term, expiry, anchorPrices);
(uint256 latestTerm, uint256 latestExpiry, bool _isBurnable) = isBurnable(term, expiry, anchorPrices);
require(_isBurnable, "Vault: not burnable");

// check if settled
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

uint256 productId = getProductId(term, expiry, anchorPrices, collateralAtRiskPercentage, isMaker);
Expand Down Expand Up @@ -310,14 +309,14 @@ contract AAVEDNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeable,
uint256 aTokenBalance = ATOKEN.balanceOf(address(this));
uint256 settlementFee;
for (uint256 i = 0; i < products.length; i++) {
// check if settled
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

Product memory product = products[i];
(uint256 latestTerm, bool _isBurnable) = isBurnable(product.term, product.expiry, product.anchorPrices);

(uint256 latestTerm, uint256 latestExpiry, bool _isBurnable) = isBurnable(product.term, product.expiry, product.anchorPrices);
require(_isBurnable, "Vault: not burnable");

// check if settled
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

uint256 productId = getProductId(product.term, product.expiry, product.anchorPrices, product.collateralAtRiskPercentage, product.isMaker);
uint256 amount = balanceOf(_msgSender(), productId);
require(amount > 0, "Vault: zero amount");
Expand Down Expand Up @@ -393,19 +392,19 @@ contract AAVEDNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeable,
function isBurnable(uint256 term, uint256 expiry, uint256[2] memory anchorPrices)
public
view
returns (uint256, bool)
returns (uint256, uint256, bool)
{
if (expiry <= block.timestamp) {
return (term, true);
return (term, expiry, true);
} else {
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
uint256 termGap = (expiry - latestExpiry) / 86400;
if (termGap > term) {
return (term, false);
return (term, latestExpiry, false);
} else {
uint256 latestTerm = term - termGap;
uint256[2] memory prices = ORACLE.getHlPrices(latestTerm, latestExpiry);
return(latestTerm, prices[0] <= anchorPrices[0] || prices[1] >= anchorPrices[1]);
return(latestTerm, latestExpiry, prices[0] <= anchorPrices[0] || prices[1] >= anchorPrices[1]);
}
}
}
Expand Down
26 changes: 11 additions & 15 deletions contracts/vaults/DNTVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,6 @@ contract DNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeable, Reen
_;
}

// Deposit ETH if balance is not enough for withdrawal
receive() external payable {}

function initialize(
Expand Down Expand Up @@ -319,15 +318,13 @@ contract DNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeable, Reen
}

function _burn(uint256 term, uint256 expiry, uint256[2] memory anchorPrices, uint256 isMaker) internal nonReentrant returns (uint256 payoff) {
uint256 productId = getProductId(term, expiry, anchorPrices, isMaker);

(uint256 latestTerm, bool _isBurnable) = isBurnable(term, expiry, anchorPrices);
(uint256 latestTerm, uint256 latestExpiry, bool _isBurnable) = isBurnable(term, expiry, anchorPrices);
require(_isBurnable, "Vault: not burnable");

// check if settled
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

uint256 productId = getProductId(term, expiry, anchorPrices, isMaker);
uint256 amount = balanceOf(_msgSender(), productId);
require(amount > 0, "Vault: zero amount");

Expand Down Expand Up @@ -373,16 +370,15 @@ contract DNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeable, Reen
uint256[] memory payoffs = new uint256[](products.length);
uint256 settlementFee;
for (uint256 i = 0; i < products.length; i++) {
// check if settled
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

Product memory product = products[i];
uint256 productId = getProductId(product.term, product.expiry, product.anchorPrices, product.isMaker);

(uint256 latestTerm, bool _isBurnable) = isBurnable(product.term, product.expiry, product.anchorPrices);
(uint256 latestTerm, uint256 latestExpiry, bool _isBurnable) = isBurnable(product.term, product.expiry, product.anchorPrices);
require(_isBurnable, "Vault: not burnable");

// check if settled
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

uint256 productId = getProductId(product.term, product.expiry, product.anchorPrices, product.isMaker);
uint256 amount = balanceOf(_msgSender(), productId);
require(amount > 0, "Vault: zero amount");

Expand Down Expand Up @@ -445,19 +441,19 @@ contract DNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeable, Reen
function isBurnable(uint256 term, uint256 expiry, uint256[2] memory anchorPrices)
public
view
returns (uint256, bool)
returns (uint256, uint256, bool)
{
if (expiry <= block.timestamp) {
return (term, true);
return (term, expiry, true);
} else {
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
uint256 termGap = (expiry - latestExpiry) / 86400;
if (termGap > term) {
return (term, false);
return (term, latestExpiry, false);
} else {
uint256 latestTerm = term - termGap;
uint256[2] memory prices = ORACLE.getHlPrices(latestTerm, latestExpiry);
return(latestTerm, prices[0] <= anchorPrices[0] || prices[1] >= anchorPrices[1]);
return(latestTerm, latestExpiry, prices[0] <= anchorPrices[0] || prices[1] >= anchorPrices[1]);
}
}
}
Expand Down
21 changes: 10 additions & 11 deletions contracts/vaults/LeverageDNTVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -236,11 +236,10 @@ contract LeverageDNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeab
}

function _burn(uint256 term, uint256 expiry, uint256[2] memory anchorPrices, uint256 collateralAtRiskPercentage, uint256 isMaker) internal nonReentrant returns (uint256 payoff) {
(uint256 latestTerm, bool _isBurnable) = isBurnable(term, expiry, anchorPrices);
(uint256 latestTerm, uint256 latestExpiry, bool _isBurnable) = isBurnable(term, expiry, anchorPrices);
require(_isBurnable, "Vault: not burnable");

// check if settled
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

uint256 productId = getProductId(term, expiry, anchorPrices, collateralAtRiskPercentage, isMaker);
Expand Down Expand Up @@ -287,14 +286,14 @@ contract LeverageDNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeab
uint256[] memory payoffs = new uint256[](products.length);
uint256 settlementFee;
for (uint256 i = 0; i < products.length; i++) {
// check if settled
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

Product memory product = products[i];
(uint256 latestTerm, bool _isBurnable) = isBurnable(product.term, product.expiry, product.anchorPrices);

(uint256 latestTerm, uint256 latestExpiry, bool _isBurnable) = isBurnable(product.term, product.expiry, product.anchorPrices);
require(_isBurnable, "Vault: not burnable");

// check if settled
require(ORACLE.settlePrices(latestExpiry, 1) > 0, "Vault: not settled");

uint256 productId = getProductId(product.term, product.expiry, product.anchorPrices, product.collateralAtRiskPercentage, product.isMaker);
uint256 amount = balanceOf(_msgSender(), productId);
require(amount > 0, "Vault: zero amount");
Expand Down Expand Up @@ -370,19 +369,19 @@ contract LeverageDNTVault is Initializable, ContextUpgradeable, ERC1155Upgradeab
function isBurnable(uint256 term, uint256 expiry, uint256[2] memory anchorPrices)
public
view
returns (uint256, bool)
returns (uint256, uint256, bool)
{
if (expiry <= block.timestamp) {
return (term, true);
return (term, expiry, true);
} else {
uint256 latestExpiry = (block.timestamp - 28800) / 86400 * 86400 + 28800;
uint256 termGap = (expiry - latestExpiry) / 86400;
if (termGap > term) {
return (term, false);
return (term, latestExpiry, false);
} else {
uint256 latestTerm = term - termGap;
uint256[2] memory prices = ORACLE.getHlPrices(latestTerm, latestExpiry);
return(latestTerm, prices[0] <= anchorPrices[0] || prices[1] >= anchorPrices[1]);
return(latestTerm, latestExpiry, prices[0] <= anchorPrices[0] || prices[1] >= anchorPrices[1]);
}
}
}
Expand Down

0 comments on commit a83bfbf

Please sign in to comment.