Skip to content

Commit

Permalink
refactor: rename event and function names for off-ramp order settlement
Browse files Browse the repository at this point in the history
  • Loading branch information
OnahProsperity committed Oct 1, 2024
1 parent 78bbc75 commit eac75e9
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 86 deletions.
82 changes: 41 additions & 41 deletions contracts/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
uint256 liquidityProviderAmount;
}

mapping(bytes32 => OffRampOrder) private offRampOrder;
mapping(bytes32 => OnRampOrder) private onRampOrder;
mapping(bytes32 => OrderOut) private orderSettledOut;
mapping(bytes32 => OrderIn) private orderSettledIn;
mapping(address => uint256) private _nonce;
mapping(address => mapping(address => uint256)) private balance;
mapping(bytes32 => bool) private processedOrders;
Expand Down Expand Up @@ -112,7 +112,7 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {

// update transaction
uint256 _protocolFee = (_amount * protocolFeePercent) / MAX_BPS;
offRampOrder[orderId] = OffRampOrder({
orderSettledOut[orderId] = OrderOut({
sender: msg.sender,
token: _token,
senderFeeRecipient: _senderFeeRecipient,
Expand All @@ -127,9 +127,9 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {

// emit order created event
emit OrderCreated(
offRampOrder[orderId].sender,
orderSettledOut[orderId].sender,
_token,
offRampOrder[orderId].amount,
orderSettledOut[orderId].amount,
_protocolFee,
orderId,
_rate,
Expand Down Expand Up @@ -162,46 +162,46 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
/* ##################################################################
AGGREGATOR FUNCTIONS
################################################################## */
/** @dev See {settleOrder-IGateway}. */
function settleOrder(
/** @dev See {settleOrderOut-IGateway}. */
function settleOrderOut(
bytes32 _splitOrderId,
bytes32 _orderId,
address _provider,
uint64 _settlePercent
) external onlyAggregator returns (bool) {
// ensure the transaction has not been fulfilled
require(!offRampOrder[_orderId].isFulfilled, 'OrderFulfilled');
require(!offRampOrder[_orderId].isRefunded, 'OrderRefunded');
require(!orderSettledOut[_orderId].isFulfilled, 'OrderFulfilled');
require(!orderSettledOut[_orderId].isRefunded, 'OrderRefunded');

// load the token into memory
address token = offRampOrder[_orderId].token;
address token = orderSettledOut[_orderId].token;

// subtract sum of amount based on the input _settlePercent
offRampOrder[_orderId].currentBPS -= _settlePercent;
orderSettledOut[_orderId].currentBPS -= _settlePercent;

if (offRampOrder[_orderId].currentBPS == 0) {
if (orderSettledOut[_orderId].currentBPS == 0) {
// update the transaction to be fulfilled
offRampOrder[_orderId].isFulfilled = true;
orderSettledOut[_orderId].isFulfilled = true;

if (offRampOrder[_orderId].senderFee > 0) {
if (orderSettledOut[_orderId].senderFee > 0) {
// transfer sender fee
IERC20(offRampOrder[_orderId].token).transfer(
offRampOrder[_orderId].senderFeeRecipient,
offRampOrder[_orderId].senderFee
IERC20(orderSettledOut[_orderId].token).transfer(
orderSettledOut[_orderId].senderFeeRecipient,
orderSettledOut[_orderId].senderFee
);

// emit event
emit SenderFeeTransferred(
offRampOrder[_orderId].senderFeeRecipient,
offRampOrder[_orderId].senderFee
orderSettledOut[_orderId].senderFeeRecipient,
orderSettledOut[_orderId].senderFee
);
}

}

// transfer to liquidity provider
uint256 liquidityProviderAmount = (offRampOrder[_orderId].amount * _settlePercent) / MAX_BPS;
offRampOrder[_orderId].amount -= liquidityProviderAmount;
uint256 liquidityProviderAmount = (orderSettledOut[_orderId].amount * _settlePercent) / MAX_BPS;
orderSettledOut[_orderId].amount -= liquidityProviderAmount;

uint256 protocolFee = (liquidityProviderAmount * protocolFeePercent) / MAX_BPS;
liquidityProviderAmount -= protocolFee;
Expand All @@ -212,34 +212,34 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
IERC20(token).transfer(_provider, liquidityProviderAmount);

// emit settled event
emit OfframpOrderSettled(_splitOrderId, _orderId, _provider, _settlePercent);
emit OrderSettledOut(_splitOrderId, _orderId, _provider, _settlePercent);

return true;
}

/** @dev See {refundOrder-IGateway}. */
function refundOrder(uint256 _fee, bytes32 _orderId) external onlyAggregator returns (bool) {
// ensure the transaction has not been fulfilled
require(!offRampOrder[_orderId].isFulfilled, 'OrderFulfilled');
require(!offRampOrder[_orderId].isRefunded, 'OrderRefunded');
require(offRampOrder[_orderId].protocolFee >= _fee, 'FeeExceedsProtocolFee');
require(!orderSettledOut[_orderId].isFulfilled, 'OrderFulfilled');
require(!orderSettledOut[_orderId].isRefunded, 'OrderRefunded');
require(orderSettledOut[_orderId].protocolFee >= _fee, 'FeeExceedsProtocolFee');

if (_fee > 0) {
// transfer refund fee to the treasury
IERC20(offRampOrder[_orderId].token).transfer(treasuryAddress, _fee);
IERC20(orderSettledOut[_orderId].token).transfer(treasuryAddress, _fee);
}

// reset state values
offRampOrder[_orderId].isRefunded = true;
offRampOrder[_orderId].currentBPS = 0;
orderSettledOut[_orderId].isRefunded = true;
orderSettledOut[_orderId].currentBPS = 0;

// deduct fee from order amount
uint256 refundAmount = offRampOrder[_orderId].amount - _fee;
uint256 refundAmount = orderSettledOut[_orderId].amount - _fee;

// transfer refund amount and sender fee to the refund address
IERC20(offRampOrder[_orderId].token).transfer(
offRampOrder[_orderId].refundAddress,
refundAmount + offRampOrder[_orderId].senderFee
IERC20(orderSettledOut[_orderId].token).transfer(
orderSettledOut[_orderId].refundAddress,
refundAmount + orderSettledOut[_orderId].senderFee
);

// emit refunded event
Expand All @@ -258,7 +258,7 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
}

/** @dev See {settleOrder-IGateway}. */
function settleOrder(
function settleOrderIn(
bytes32 _orderId,
bytes memory _signature,
address _provider,
Expand All @@ -285,7 +285,7 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
// Update balances
balance[_token][_provider] -= (_amount + _protocolFee);

onRampOrder[_orderId] = OnRampOrder({
orderSettledIn[_orderId] = OrderIn({
amount: _amount,
provider: _provider,
sender: _sender,
Expand All @@ -300,20 +300,20 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
IERC20(_token).transfer(treasuryAddress, _protocolFee);
}

emit OnrampOrderSettled(_provider, _sender, _amount, _token, _orderId);
emit OrderSettledIn(_provider, _sender, _amount, _token, _orderId);
}

/* ##################################################################
VIEW CALLS
################################################################## */
/** @dev See {getOffRampOrderInfo-IGateway}. */
function getOffRampOrderInfo(bytes32 _orderId) external view returns (OffRampOrder memory) {
return offRampOrder[_orderId];
/** @dev See {getOrderInfoOut-IGateway}. */
function getOrderInfoOut(bytes32 _orderId) external view returns (OrderOut memory) {
return orderSettledOut[_orderId];
}

/** @dev See {getOnRampOrderInfo-IGateway}. */
function getOnRampOrderInfo(bytes32 _orderId) external view returns (OnRampOrder memory) {
return onRampOrder[_orderId];
/** @dev See {getOrderInfoIn-IGateway}. */
function getOrderInfoIn(bytes32 _orderId) external view returns (OrderIn memory) {
return orderSettledIn[_orderId];
}

/** @dev See {isTokenSupported-IGateway}. */
Expand Down
28 changes: 14 additions & 14 deletions contracts/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ interface IGateway {
);

/**
* @notice Emitted when an aggregator settles an off-ramp transaction.
* @notice Emitted when an order is settled out.
* @param splitOrderId The ID of the split order.
* @param orderId The ID of the order.
* @param liquidityProvider The address of the liquidity provider.
* @param settlePercent The percentage at which the transaction is settled.
*/
event OfframpOrderSettled(
event OrderSettledOut(
bytes32 splitOrderId,
bytes32 indexed orderId,
address indexed liquidityProvider,
Expand Down Expand Up @@ -67,19 +67,19 @@ interface IGateway {
event Deposit(address indexed sender, address indexed token, uint256 indexed amount);

/**
* @dev Emitted when an off-ramp settlement is made.
* @dev Emitted when an order is settled in.
* @param provider The address of the provider.
* @param senderAddress The address of the sender.
* @param amount The address of the deposited token.
* @param token The amount of the deposit.
* @param orderId The ID of the order.
*/
event OnrampOrderSettled(address indexed provider, address indexed senderAddress, uint256 indexed amount, address token, bytes32 orderId);
event OrderSettledIn(address indexed provider, address indexed senderAddress, uint256 indexed amount, address token, bytes32 orderId);
/* ##################################################################
STRUCTS
################################################################## */
/**
* @notice Struct representing an off-ramp order.
* @notice Struct representing an order out.
* @param sender The address of the sender.
* @param token The address of the token.
* @param senderFeeRecipient The address of the sender fee recipient.
Expand All @@ -91,7 +91,7 @@ interface IGateway {
* @param currentBPS The current basis points.
* @param amount The amount of the order.
*/
struct OffRampOrder {
struct OrderOut {
address sender;
address token;
address senderFeeRecipient;
Expand All @@ -105,14 +105,14 @@ interface IGateway {
}

/**
* @notice Struct representing onramp order.
* @notice Struct representing an order settled in.
* @param amount The amount of the order.
* @param provider The address of the provider.
* @param sender The address of the sender.
* @param token The address of the token.
* @param orderid The ID of the order.
*/
struct OnRampOrder {
struct OrderIn {
uint256 amount;
address provider;
address sender;
Expand Down Expand Up @@ -150,14 +150,14 @@ interface IGateway {
) external returns (bytes32 _orderId);

/**
* @notice Settles an offramp transaction and distributes fees accordingly.
* @notice Settles an order out transaction and distributes fees accordingly.
* @param _splitOrderId The ID of the split order.
* @param _orderId The ID of the transaction.
* @param _provider The address of the liquidity provider.
* @param _settlePercent The rate at which the transaction is settled.
* @return bool The settlement is successful.
*/
function settleOrder(
function settleOrderOut(
bytes32 _splitOrderId,
bytes32 _orderId,
address _provider,
Expand Down Expand Up @@ -188,15 +188,15 @@ interface IGateway {


/**
* @notice Allow aggregator to settle an onramp order.
* @notice Allow aggregator to settle an order coming in.
* @param _orderId The ID of the transaction.
* @param _signature The signature of the provider.
* @param _provider The address of the provider.
* @param _senderAddress The address of the sender.
* @param _token The address of the asset.
* @param _amount The amount to be transferred.
*/
function settleOrder(
function settleOrderIn(
bytes32 _orderId,
bytes memory _signature,
address _provider,
Expand All @@ -217,14 +217,14 @@ interface IGateway {
* @param _orderId The ID of the order.
* @return Order The order details.
*/
function getOffRampOrderInfo(bytes32 _orderId) external view returns (OffRampOrder memory);
function getOrderInfoOut(bytes32 _orderId) external view returns (OrderOut memory);

/**
* @notice Gets the details of an on ramp order.
* @param _orderId The ID of the order.
* @return Order The order details.
*/
function getOnRampOrderInfo(bytes32 _orderId) external view returns (OnRampOrder memory);
function getOrderInfoIn(bytes32 _orderId) external view returns (OrderIn memory);

/**
* @notice Gets the fee details of Gateway.
Expand Down
10 changes: 5 additions & 5 deletions test/gateway/gateway.createorder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe("Gateway create order", function () {
this.refundAddress,
this.currentBPS,
this.amount,
] = await this.gateway.getOffRampOrderInfo(orderId);
] = await this.gateway.getOrderInfoOut(orderId);
// expect sender balance to increase by sender fee
expect(await this.mockUSDT.balanceOf(this.sender.address)).to.eq(
ZERO_AMOUNT
Expand Down Expand Up @@ -237,7 +237,7 @@ describe("Gateway create order", function () {
this.refundAddress,
this.currentBPS,
this.amount,
] = await this.gateway.getOffRampOrderInfo(orderId);
] = await this.gateway.getOrderInfoOut(orderId);

expect(this.seller).to.eq(ZERO_ADDRESS);
expect(this.token).to.eq(ZERO_ADDRESS);
Expand Down Expand Up @@ -312,7 +312,7 @@ describe("Gateway create order", function () {
this.refundAddress,
this.currentBPS,
this.amount,
] = await this.gateway.getOffRampOrderInfo(orderId);
] = await this.gateway.getOrderInfoOut(orderId);

expect(this.seller).to.eq(ZERO_ADDRESS);
expect(this.token).to.eq(ZERO_ADDRESS);
Expand Down Expand Up @@ -387,7 +387,7 @@ describe("Gateway create order", function () {
this.refundAddress,
this.currentBPS,
this.amount,
] = await this.gateway.getOffRampOrderInfo(orderId);
] = await this.gateway.getOrderInfoOut(orderId);

expect(this.seller).to.eq(ZERO_ADDRESS);
expect(this.token).to.eq(ZERO_ADDRESS);
Expand Down Expand Up @@ -461,7 +461,7 @@ describe("Gateway create order", function () {
this.refundAddress,
this.currentBPS,
this.amount,
] = await this.gateway.getOffRampOrderInfo(orderId);
] = await this.gateway.getOrderInfoOut(orderId);

expect(this.seller).to.eq(ZERO_ADDRESS);
expect(this.token).to.eq(ZERO_ADDRESS);
Expand Down
Loading

0 comments on commit eac75e9

Please sign in to comment.