Skip to content

Commit

Permalink
Refactoring of Exchanger.sol to reduce size on OVM (#1291)
Browse files Browse the repository at this point in the history
  • Loading branch information
justin j. moses authored May 26, 2021
1 parent f3a6076 commit 00df930
Show file tree
Hide file tree
Showing 12 changed files with 184 additions and 293 deletions.
77 changes: 46 additions & 31 deletions contracts/BaseSynthetix.sol
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,17 @@ contract BaseSynthetix is IERC20, ExternStateToken, MixinResolver, ISynthetix {
uint sourceAmount,
bytes32 destinationCurrencyKey
) external exchangeActive(sourceCurrencyKey, destinationCurrencyKey) optionalProxy returns (uint amountReceived) {
return exchanger().exchange(messageSender, sourceCurrencyKey, sourceAmount, destinationCurrencyKey, messageSender);
(amountReceived, ) = exchanger().exchange(
messageSender,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
messageSender,
false,
messageSender,
bytes32(0)
);
}

function exchangeOnBehalf(
Expand All @@ -176,14 +186,17 @@ contract BaseSynthetix is IERC20, ExternStateToken, MixinResolver, ISynthetix {
uint sourceAmount,
bytes32 destinationCurrencyKey
) external exchangeActive(sourceCurrencyKey, destinationCurrencyKey) optionalProxy returns (uint amountReceived) {
return
exchanger().exchangeOnBehalf(
exchangeForAddress,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey
);
(amountReceived, ) = exchanger().exchange(
exchangeForAddress,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
exchangeForAddress,
false,
exchangeForAddress,
bytes32(0)
);
}

function settle(bytes32 currencyKey)
Expand All @@ -202,39 +215,41 @@ contract BaseSynthetix is IERC20, ExternStateToken, MixinResolver, ISynthetix {
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address originator,
address rewardAddress,
bytes32 trackingCode
) external exchangeActive(sourceCurrencyKey, destinationCurrencyKey) optionalProxy returns (uint amountReceived) {
return
exchanger().exchangeWithTracking(
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
messageSender,
originator,
trackingCode
);
(amountReceived, ) = exchanger().exchange(
messageSender,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
messageSender,
false,
rewardAddress,
trackingCode
);
}

function exchangeOnBehalfWithTracking(
address exchangeForAddress,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address originator,
address rewardAddress,
bytes32 trackingCode
) external exchangeActive(sourceCurrencyKey, destinationCurrencyKey) optionalProxy returns (uint amountReceived) {
return
exchanger().exchangeOnBehalfWithTracking(
exchangeForAddress,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
originator,
trackingCode
);
(amountReceived, ) = exchanger().exchange(
exchangeForAddress,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
exchangeForAddress,
false,
rewardAddress,
trackingCode
);
}

function transfer(address to, uint value) external optionalProxy systemActive returns (bool) {
Expand Down
119 changes: 18 additions & 101 deletions contracts/Exchanger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -316,116 +316,33 @@ contract Exchanger is Owned, MixinSystemSettings, IExchanger {

/* ========== MUTATIVE FUNCTIONS ========== */
function exchange(
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address destinationAddress
) external onlySynthetixorSynth returns (uint amountReceived) {
uint fee;
(amountReceived, fee, ) = _exchange(
from,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
destinationAddress,
false
);

_processTradingRewards(fee, destinationAddress);
}

function exchangeOnBehalf(
address exchangeForAddress,
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey
) external onlySynthetixorSynth returns (uint amountReceived) {
require(delegateApprovals().canExchangeFor(exchangeForAddress, from), "Not approved to act on behalf");

uint fee;
(amountReceived, fee, ) = _exchange(
exchangeForAddress,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
exchangeForAddress,
false
);

_processTradingRewards(fee, exchangeForAddress);
}

function exchangeWithTracking(
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address destinationAddress,
address originator,
bytes32 trackingCode
) external onlySynthetixorSynth returns (uint amountReceived) {
uint fee;
(amountReceived, fee, ) = _exchange(
from,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
destinationAddress,
false
);

_processTradingRewards(fee, originator);

_emitTrackingEvent(trackingCode, destinationCurrencyKey, amountReceived, fee);
}

function exchangeOnBehalfWithTracking(
address exchangeForAddress,
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address originator,
bytes32 trackingCode
) external onlySynthetixorSynth returns (uint amountReceived) {
require(delegateApprovals().canExchangeFor(exchangeForAddress, from), "Not approved to act on behalf");

uint fee;
(amountReceived, fee, ) = _exchange(
exchangeForAddress,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
exchangeForAddress,
false
);

_processTradingRewards(fee, originator);

_emitTrackingEvent(trackingCode, destinationCurrencyKey, amountReceived, fee);
}

function exchangeWithVirtual(
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address destinationAddress,
bool virtualSynth,
address rewardAddress,
bytes32 trackingCode
) external onlySynthetixorSynth returns (uint amountReceived, IVirtualSynth vSynth) {
uint fee;
if (from != exchangeForAddress) {
require(delegateApprovals().canExchangeFor(exchangeForAddress, from), "Not approved to act on behalf");
}

(amountReceived, fee, vSynth) = _exchange(
from,
exchangeForAddress,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
destinationAddress,
true
virtualSynth
);

_processTradingRewards(fee, destinationAddress);
if (rewardAddress != address(0)) {
_processTradingRewards(fee, rewardAddress);
}

if (trackingCode != bytes32(0)) {
_emitTrackingEvent(trackingCode, destinationCurrencyKey, amountReceived, fee);
Expand All @@ -441,9 +358,9 @@ contract Exchanger is Owned, MixinSystemSettings, IExchanger {
ISynthetixInternal(address(synthetix())).emitExchangeTracking(trackingCode, toCurrencyKey, toAmount, fee);
}

function _processTradingRewards(uint fee, address originator) internal {
if (fee > 0 && originator != address(0) && getTradingRewardsEnabled()) {
tradingRewards().recordExchangeFeeForAccount(fee, originator);
function _processTradingRewards(uint fee, address rewardAddress) internal {
if (fee > 0 && rewardAddress != address(0) && getTradingRewardsEnabled()) {
tradingRewards().recordExchangeFeeForAccount(fee, rewardAddress);
}
}

Expand Down Expand Up @@ -661,12 +578,12 @@ contract Exchanger is Owned, MixinSystemSettings, IExchanger {

// SIP-139
function resetLastExchangeRate(bytes32[] calldata currencyKeys) external onlyOwner {
IExchangeRates exRates = exchangeRates();
require(!exRates.anyRateIsInvalid(currencyKeys), "Rates for given synths not valid");
(uint[] memory rates, bool anyRateInvalid) = exchangeRates().ratesAndInvalidForCurrencies(currencyKeys);

require(!anyRateInvalid, "Rates for given synths not valid");

for (uint i = 0; i < currencyKeys.length; i++) {
bytes32 currencyKey = currencyKeys[i];
lastExchangeRate[currencyKey] = exRates.rateForCurrency((currencyKey));
lastExchangeRate[currencyKeys[i]] = rates[i];
}
}

Expand Down
2 changes: 1 addition & 1 deletion contracts/PurgeableSynth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ contract PurgeableSynth is Synth {
uint amountHeld = tokenState.balanceOf(holder);

if (amountHeld > 0) {
exchanger().exchange(holder, currencyKey, amountHeld, "sUSD", holder);
exchanger().exchange(holder, holder, currencyKey, amountHeld, "sUSD", holder, false, address(0), bytes32(0));
emitPurged(holder, amountHeld);
}
}
Expand Down
12 changes: 11 additions & 1 deletion contracts/Synth.sol
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,17 @@ contract Synth is Owned, IERC20, ExternStateToken, MixinResolver, ISynth {
super._internalTransfer(messageSender, to, value);
} else {
// else exchange synth into sUSD and send to FEE_ADDRESS
amountInUSD = exchanger().exchange(messageSender, currencyKey, value, "sUSD", FEE_ADDRESS);
(amountInUSD, ) = exchanger().exchange(
messageSender,
messageSender,
currencyKey,
value,
"sUSD",
FEE_ADDRESS,
false,
address(0),
bytes32(0)
);
}

// Notify feePool to record sUSD to distribute as fees
Expand Down
30 changes: 17 additions & 13 deletions contracts/Synthetix.sol
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ contract Synthetix is BaseSynthetix {
returns (uint amountReceived, IVirtualSynth vSynth)
{
return
exchanger().exchangeWithVirtual(
exchanger().exchange(
messageSender,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
messageSender,
true,
messageSender,
trackingCode
);
}
Expand All @@ -79,20 +82,21 @@ contract Synthetix is BaseSynthetix {
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address originator,
address rewardAddress,
bytes32 trackingCode
) external exchangeActive(sourceCurrencyKey, destinationCurrencyKey) optionalProxy returns (uint amountReceived) {
return
exchanger().exchangeWithTracking(
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
// solhint-disable avoid-tx-origin
tx.origin,
originator,
trackingCode
);
(amountReceived, ) = exchanger().exchange(
messageSender,
messageSender,
sourceCurrencyKey,
sourceAmount,
destinationCurrencyKey,
// solhint-disable avoid-tx-origin
tx.origin,
false,
rewardAddress,
trackingCode
);
}

function settle(bytes32 currencyKey)
Expand Down
37 changes: 2 additions & 35 deletions contracts/interfaces/IExchanger.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,47 +51,14 @@ interface IExchanger {

// Mutative functions
function exchange(
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address destinationAddress
) external returns (uint amountReceived);

function exchangeOnBehalf(
address exchangeForAddress,
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey
) external returns (uint amountReceived);

function exchangeWithTracking(
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address destinationAddress,
address originator,
bytes32 trackingCode
) external returns (uint amountReceived);

function exchangeOnBehalfWithTracking(
address exchangeForAddress,
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address originator,
bytes32 trackingCode
) external returns (uint amountReceived);

function exchangeWithVirtual(
address from,
bytes32 sourceCurrencyKey,
uint sourceAmount,
bytes32 destinationCurrencyKey,
address destinationAddress,
bool virtualSynth,
address rewardAddress,
bytes32 trackingCode
) external returns (uint amountReceived, IVirtualSynth vSynth);

Expand Down
Loading

0 comments on commit 00df930

Please sign in to comment.