Skip to content

Commit

Permalink
change stethNeeded to return the amount of extra allowance needed
Browse files Browse the repository at this point in the history
  • Loading branch information
davidbrai committed Dec 30, 2024
1 parent 6c59c74 commit 10b3660
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 15 deletions.
15 changes: 6 additions & 9 deletions src/STETHTokenBuyer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -250,21 +250,18 @@ contract STETHTokenBuyer is Ownable, Pausable, ReentrancyGuard {
░░░░░░░░░░░░░░░░░░░░░░░░░░░░░
*/

/// @notice Get how much STETH this contract needs in order to fund its current obligations plus `additionalTokens`, with
/// a safety buffer `bufferBPs` basis points.
/// @notice Get how much additional STETH allowance this contract needs in order to fund its current obligations plus `additionalTokens`.
/// @param additionalTokens an additional amount of `paymentToken` liability to use in this STETH requirement calculation, in payment token decimals.
/// @param bufferBPs the number of basis points to add on top of the token liability price in STETH as a safety buffer, e.g.
/// if `bufferBPs` is 10K, the function will return twice the amount it needs according to price alone.
/// @return the amount of STETH needed
function stethNeeded(uint256 additionalTokens, uint256 bufferBPs) public view returns (uint256) {
/// @return the amount of additional STETH allowance needed
function stethNeeded(uint256 additionalTokens) public view returns (uint256) {
uint256 tokenAmount = tokenAmountNeeded() + additionalTokens;
uint256 ethCostOfTokens = stethAmountPerTokenAmount(tokenAmount);
uint256 ethCostWithBuffer = (ethCostOfTokens * (bufferBPs + 10_000)) / 10_000;
uint256 stETHAllowance = stETH.allowance(treasury, address(this));

if (address(this).balance > ethCostWithBuffer) {
if (stETHAllowance > ethCostOfTokens) {
return 0;
} else {
return ethCostWithBuffer - address(this).balance;
return ethCostOfTokens - stETHAllowance;
}
}

Expand Down
13 changes: 7 additions & 6 deletions test/STETHTokenBuyer.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -999,23 +999,24 @@ contract STETHTokenBuyerTest is Test {
buyer.setMaxAdminBaselinePaymentTokenAmount(42);
}

function test_ethNeeded() public {
function test_stethNeeded() public {
priceFeed.setPrice(1400e18);

vm.prank(owner);
buyer.setBaselinePaymentTokenAmount(1_000e18);

uint256 ethNeeded = buyer.stethNeeded(100e18, 5000);
assertApproxEqAbs(ethNeeded, 1.178571429e18, 0.00001e18);
uint256 ethNeeded = buyer.stethNeeded(100e18);
assertApproxEqAbs(ethNeeded, 0.785714286e18, 0.00001e18);
}

function test_ethNeededIsZeroIfNothingNeeded() public {
function test_stethNeededIsZeroIfNothingNeeded() public {
priceFeed.setPrice(1400e18);

vm.prank(owner);
buyer.setBaselinePaymentTokenAmount(1_000e18);

vm.deal(address(buyer), 1.18 ether);
assertEq(buyer.stethNeeded(100e18, 5000), 0);
vm.prank(treasury);
stETH.approve(address(buyer), 0.8 ether);
assertEq(buyer.stethNeeded(100e18), 0);
}
}

0 comments on commit 10b3660

Please sign in to comment.