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
- Renamed the event "OrderSettled" to "OfframpOrderSettlement" in the Gateway contract.
- Renamed the function "settle" to "settleOrder" in the IGateway interface and Gateway contract.
  • Loading branch information
OnahProsperity committed Sep 25, 2024
1 parent 8861c3e commit dbe784a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 53 deletions.
27 changes: 13 additions & 14 deletions contracts/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -161,11 +161,11 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
/* ##################################################################
AGGREGATOR FUNCTIONS
################################################################## */
/** @dev See {settle-IGateway}. */
function settle(
/** @dev See {settleOrder-IGateway}. */
function settleOrder(
bytes32 _splitOrderId,
bytes32 _orderId,
address _liquidityProvider,
address _provider,
uint64 _settlePercent
) external onlyAggregator returns (bool) {
// ensure the transaction has not been fulfilled
Expand Down Expand Up @@ -208,16 +208,16 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
// transfer protocol fee
IERC20(token).transfer(treasuryAddress, protocolFee);

IERC20(token).transfer(_liquidityProvider, liquidityProviderAmount);
IERC20(token).transfer(_provider, liquidityProviderAmount);

// emit settled event
emit OrderSettled(_splitOrderId, _orderId, _liquidityProvider, _settlePercent);
emit OfframpOrderSettlement(_splitOrderId, _orderId, _provider, _settlePercent);

return true;
}

/** @dev See {refund-IGateway}. */
function refund(uint256 _fee, bytes32 _orderId) external onlyAggregator returns (bool) {
function refundOrder(uint256 _fee, bytes32 _orderId) external onlyAggregator returns (bool) {
// ensure the transaction has not been fulfilled
require(!order[_orderId].isFulfilled, 'OrderFulfilled');
require(!order[_orderId].isRefunded, 'OrderRefunded');
Expand Down Expand Up @@ -256,27 +256,26 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
return true;
}

/** @dev See {deposit-IGateway}. */
function escrow(
/** @dev See {settleOrder-IGateway}. */
function settleOrder(
bytes32 _orderId,
bytes memory _signature,
address _provider,
address _senderAddress,
address _sender,
address _token,
uint256 _amount
) external onlyAggregator isValidAmount(_amount) {
require(!processedOrders[_orderId], "Order already processed");
require(_provider != address(0), "Invalid provider address");
require(_senderAddress != address(0), "Invalid sender address");
require(_sender != address(0), "Invalid sender address");

// Verify signature
bytes32 messageHash = keccak256(abi.encodePacked(_orderId, _provider, _senderAddress, _token, _amount));
bytes32 messageHash = keccak256(abi.encodePacked(_orderId, _provider, _sender, _token, _amount));
bytes32 ethSignedMessageHash = messageHash.toEthSignedMessageHash();
address recoveredAddress = ethSignedMessageHash.recover(_signature);
require(recoveredAddress == _provider, "Invalid signature");
// update transaction
uint256 _protocolFee = (_amount * protocolFeePercent) / MAX_BPS;
// uint256 sumAmount = _amount + _protocolFee;
// Check provider's balance,
// Note: There is no need for checks for token supported as the balance will be 0 if the token is not supported
require(balance[_token][_provider] >= _amount + _protocolFee, "Insufficient balance");
Expand All @@ -288,14 +287,14 @@ contract Gateway is IGateway, GatewaySettingManager, PausableUpgradeable {
balance[_token][_provider] -= (_amount + _protocolFee);

// transfer to sender
IERC20(_token).transfer(_senderAddress, _amount);
IERC20(_token).transfer(_sender, _amount);
if (_protocolFee > 0) {
// transfer protocol fee
IERC20(_token).transfer(treasuryAddress, _protocolFee);
}

// Emit event
emit Escrow(_provider, _senderAddress, _amount, _token, _orderId);
emit OnrampOrderSettlement(_provider, _sender, _amount, _token, _orderId);
}

/* ##################################################################
Expand Down
20 changes: 10 additions & 10 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 a transaction.
* @notice Emitted when an aggregator settles an off-ramp transaction.
* @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 OrderSettled(
event OfframpOrderSettlement(
bytes32 splitOrderId,
bytes32 indexed orderId,
address indexed liquidityProvider,
Expand Down Expand Up @@ -67,14 +67,14 @@ interface IGateway {
event Deposit(address indexed sender, address indexed token, uint256 indexed amount);

/**
* @dev Emitted when an escrow is made by a provider.
* @dev Emitted when an off-ramp settlement is made.
* @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 Escrow(address indexed provider, address indexed senderAddress, uint256 indexed amount, address token, bytes32 orderId);
event OnrampOrderSettlement(address indexed provider, address indexed senderAddress, uint256 indexed amount, address token, bytes32 orderId);
/* ##################################################################
STRUCTS
################################################################## */
Expand Down Expand Up @@ -137,14 +137,14 @@ interface IGateway {
* @notice Settles a transaction and distributes fees accordingly.
* @param _splitOrderId The ID of the split order.
* @param _orderId The ID of the transaction.
* @param _liquidityProvider The address of the liquidity provider.
* @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 settle(
function settleOrder(
bytes32 _splitOrderId,
bytes32 _orderId,
address _liquidityProvider,
address _provider,
uint64 _settlePercent
) external returns (bool);

Expand All @@ -156,7 +156,7 @@ interface IGateway {
* @param _orderId The ID of the transaction.
* @return bool the refund is successful.
*/
function refund(uint256 _fee, bytes32 _orderId) external returns (bool);
function refundOrder(uint256 _fee, bytes32 _orderId) external returns (bool);

/**
* @notice Allow a provider to deposit an asset into Gateway.
Expand All @@ -172,15 +172,15 @@ interface IGateway {


/**
* @notice Escrowed assets from provider to the sender.
* @notice Allow aggregator to settle an order.
* @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 escrow(
function settleOrder(
bytes32 _orderId,
bytes memory _signature,
address _provider,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const {
} = require("../utils/utils.manager.js");
const { expect } = require("chai");

describe("Gateway Escrow", function () {
describe("Gateway Onramp Order settlement", function () {
beforeEach(async function () {
[
this.deployer,
Expand Down Expand Up @@ -89,7 +89,7 @@ describe("Gateway Escrow", function () {
).to.emit(gateway, Events.Gateway.ProtocolAddressUpdated);
});

it("Should escrow assets from provider to sender", async function () {
it("Should settle onramp assets from provider to sender", async function () {
const orderId = ethers.utils.formatBytes32String("order1");
const amount = ethers.utils.parseEther("1000");

Expand Down Expand Up @@ -142,10 +142,9 @@ describe("Gateway Escrow", function () {
this.alice.address,
this.depositAmount
);

// Perform the escrow
// Perform the settling order
await expect(
this.gateway.connect(this.aggregator).escrow(
this.gateway.connect(this.aggregator).settleOrder(
orderId,
signature,
this.alice.address, // Provider
Expand All @@ -154,7 +153,7 @@ describe("Gateway Escrow", function () {
amount
)
)
.to.emit(this.gateway, Events.Gateway.Escrow)
.to.emit(this.gateway, Events.Gateway.OnrampOrderSettlement)
.withArgs(
this.alice.address,
this.bob.address,
Expand Down Expand Up @@ -207,10 +206,10 @@ describe("Gateway Escrow", function () {
ethers.utils.arrayify(messageHash)
);

// Perform the escrow
// Perform the settling
await this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
Expand All @@ -219,11 +218,11 @@ describe("Gateway Escrow", function () {
amount
);

// Try to perform the escrow again
// Try to perform the settling again
await expect(
this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
Expand Down Expand Up @@ -255,11 +254,11 @@ describe("Gateway Escrow", function () {
ethers.utils.arrayify(messageHash)
);

// Try to perform the escrow
// Try to perform the settling with the invalid signature
await expect(
this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
Expand Down Expand Up @@ -291,11 +290,11 @@ describe("Gateway Escrow", function () {
ethers.utils.arrayify(messageHash)
);

// Try to perform the escrow
// Try to perform the settling with insufficient balance
await expect(
this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
Expand Down Expand Up @@ -323,7 +322,7 @@ describe("Gateway Escrow", function () {
await expect(
this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
zeroAddress,
Expand Down Expand Up @@ -351,7 +350,7 @@ describe("Gateway Escrow", function () {
await expect(
this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
Expand Down Expand Up @@ -412,19 +411,19 @@ describe("Gateway Escrow", function () {
this.alice.address
);

// Perform the escrow
// Perform the settling order
await expect(
this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
this.bob.address,
this.mockUSDT.address,
amount
)
).to.emit(this.gateway, "Escrow");
).to.emit(this.gateway, "OnrampOrderSettlement");
const feeDetails = await this.gateway.getFeeDetails();
// Calculate expected fee
const expectedFee = amount.mul(feeDetails[0]).div(feeDetails[1]);
Expand Down Expand Up @@ -454,7 +453,7 @@ describe("Gateway Escrow", function () {
);
});

it("Should revert when a non-aggregator calls escrow", async function () {
it("Should revert when a non-aggregator calls settle onramp order", async function () {
const orderId = ethers.utils.formatBytes32String("nonAggregatorOrder");
const amount = ethers.utils.parseEther("1");

Expand All @@ -480,7 +479,7 @@ describe("Gateway Escrow", function () {
await expect(
this.gateway
.connect(this.alice)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
Expand Down Expand Up @@ -513,7 +512,7 @@ describe("Gateway Escrow", function () {
await expect(
this.gateway
.connect(this.aggregator)
.escrow(
.settleOrder(
orderId,
signature,
this.alice.address,
Expand Down
10 changes: 5 additions & 5 deletions test/gateway/gateway.settleOrder.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const {
} = require("../utils/utils.manager.js");
const { expect } = require("chai");

describe("Gateway settle order", function () {
describe("Gateway offramp order", function () {
beforeEach(async function () {
[
this.deployer,
Expand Down Expand Up @@ -197,9 +197,9 @@ describe("Gateway settle order", function () {
expect(
await gateway
.connect(this.aggregator)
.settle(orderId, orderId, this.liquidityProvider.address, MAX_BPS)
.settleOrder(orderId, orderId, this.liquidityProvider.address, MAX_BPS)
)
.to.emit(gateway, Events.Gateway.OrderSettled)
.to.emit(gateway, Events.Gateway.OfframpOrderSettlement)
.withArgs(orderId, orderId, this.liquidityProvider.address, MAX_BPS);

expect(await mockUSDT.balanceOf(this.liquidityProvider.address)).to.eq(
Expand Down Expand Up @@ -305,9 +305,9 @@ describe("Gateway settle order", function () {
expect(
await gateway
.connect(this.aggregator)
.settle(orderId, orderId, this.liquidityProvider.address, MAX_BPS)
.settleOrder(orderId, orderId, this.liquidityProvider.address, MAX_BPS)
)
.to.emit(gateway, Events.Gateway.OrderSettled)
.to.emit(gateway, Events.Gateway.OfframpOrderSettlement)
.withArgs(orderId, orderId, this.liquidityProvider.address, MAX_BPS);

expect(await mockUSDT.balanceOf(this.liquidityProvider.address)).to.eq(
Expand Down
4 changes: 2 additions & 2 deletions test/utils/utils.manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ const Errors = {
const Events = {
Gateway: {
OrderCreated: "OrderCreated",
OrderSettled: "OrderSettled",
OfframpOrderSettlement: "OfframpOrderSettlement",
OrderRefunded: "OrderRefunded",
SettingManagerBool: "SettingManagerBool",
ProtocolFeeUpdated: "ProtocolFeeUpdated",
ProtocolAddressUpdated: "ProtocolAddressUpdated",
Deposit: "Deposit",
Escrow: "Escrow",
OnrampOrderSettlement: "OnrampOrderSettlement",
},
};

Expand Down

0 comments on commit dbe784a

Please sign in to comment.