Skip to content

Commit

Permalink
chore: simplify SKy transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
0xp3th1um committed Jan 9, 2025
1 parent 1f64a16 commit 8e83f5e
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
22 changes: 7 additions & 15 deletions src/DssSpell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -236,24 +236,16 @@ contract DssSpellAction is DssAction {
/// @param usr The SKY receiver.
/// @param wad The SKY amount in wad precision (10 ** 18).
function _transferSky(address usr, uint256 wad) internal {
// Note: Calculate the remainder
uint256 wadRem = wad % MKR_SKY_RATE;
// Note: The remainder has to be strictly less than the rate (24_000)
require(wadRem < MKR_SKY_RATE, "transferUsds/wrong-remainder");
// Note: this extra amount is used for enforce exact conversion and avoiding rounding errors
uint256 extraWad;
if (wadRem != 0) {
extraWad = MKR_SKY_RATE - wadRem;
}
// Note: Calculate the amount of MKR required
require((wad + extraWad) % MKR_SKY_RATE == 0, "transferSky/non-exact-conversion");
uint256 mkrWad = (wad + extraWad) / MKR_SKY_RATE;
// Note: Calculate the equivalent amount of MKR required
uint256 mkrWad = wad / MKR_SKY_RATE;
// Note: if rounding error is expected, add an extra wei of MKR
if (wad % MKR_SKY_RATE != 0) { mkrWad++; }
// Note: Approve MKR_SKY for the amount sent to be able to convert it
MKR.approve(MKR_SKY, mkrWad);
// Note: Convert the calculated amount to SKY
// Note: Convert the calculated amount to SKY for `PAUSE_PROXY`
MkrSkyLike(MKR_SKY).mkrToSky(address(this), mkrWad);
// Note: Transer the required SKY to 'usr'
SkyLike(SKY).transfer(usr, wad);
// Note: Transfer originally requested amount, leaving extra on the `PAUSE_PROXY`
GemAbstract(SKY).transfer(usr, wad);
}
}

Expand Down
29 changes: 3 additions & 26 deletions src/DssSpell.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -674,11 +674,6 @@ contract DssSpellTest is DssSpellTestBase {
int256 sky;
}

struct RoundingErrorHandling {
int256 mkrSkyRate;
int256 extraSky;
}

function testPayments() public { // add the `skipped` modifier to skip
bool ignoreTotalSupplyDaiUsds = true; // Set to false unless there is SubDAO spell interference

Expand Down Expand Up @@ -706,14 +701,6 @@ contract DssSpellTest is DssSpellTestBase {
Payee(address(sky), wallets.addr("CLOAKY"), 438_000 ether) // Note: ether is only a keyword helper
];

// Created a struct to avoid "Stack too deep" errors
RoundingErrorHandling memory roundingErrorHandling = RoundingErrorHandling({
mkrSkyRate: 24000,
// Set the variable with the amount of the extra SKY that was sent to PAUSE_PROXY
// for each SKY payment do: extraSky += rate - (paymentAmount % rate)
extraSky: 24000 - 16000 // only one payment (BLUE - 550_000 SKY) is not divisible by rate
});

// Fill the total values from exec sheet
PaymentAmounts memory expectedTotalPayments = PaymentAmounts({
dai: 0 ether, // Note: ether is only a keyword helper
Expand All @@ -724,8 +711,8 @@ contract DssSpellTest is DssSpellTestBase {

// Fill the total values based on the source for the transfers above
TreasuryAmounts memory expectedTreasuryBalancesDiff = TreasuryAmounts({
mkr: -int(988_000 ether + roundingErrorHandling.extraSky) / roundingErrorHandling.mkrSkyRate, // Note: ether is only a keyword helper
sky: roundingErrorHandling.extraSky // Note: ether is only a keyword helper
mkr: -(22916666666666666667 + 18250000000000000000), // Note: ether is only a keyword helper
sky: 8000
});

// Vote, schedule and warp, but not yet cast (to get correct surplus balance)
Expand All @@ -748,8 +735,7 @@ contract DssSpellTest is DssSpellTestBase {
});
PaymentAmounts memory calculatedTotalPayments;
PaymentAmounts[] memory previousPayeeBalances = new PaymentAmounts[](payees.length);
// This is to account for the extra SKy we add for dealing with precision issues
int256 totalExtraSky;

for (uint256 i = 0; i < payees.length; i++) {
if (payees[i].token == address(dai)) {
calculatedTotalPayments.dai += payees[i].amount;
Expand All @@ -758,10 +744,6 @@ contract DssSpellTest is DssSpellTestBase {
} else if (payees[i].token == address(usds)) {
calculatedTotalPayments.usds += payees[i].amount;
} else if (payees[i].token == address(sky)) {
int256 rem = payees[i].amount % roundingErrorHandling.mkrSkyRate;
if (rem != 0) {
totalExtraSky += roundingErrorHandling.mkrSkyRate - rem;
}
calculatedTotalPayments.sky += payees[i].amount;
} else {
revert('TestPayments/unexpected-payee-token');
Expand All @@ -774,11 +756,6 @@ contract DssSpellTest is DssSpellTestBase {
});
}

// Check calculated vs expected totals
assertEq(roundingErrorHandling.extraSky,
totalExtraSky,
"TestPayments/calculated-vs-expected-extra-sky-mismatch"
);
assertEq(
calculatedTotalPayments.dai,
expectedTotalPayments.dai,
Expand Down

0 comments on commit 8e83f5e

Please sign in to comment.