The Hydro Smart Contracts contain several functions that facilitate the creation of a secure and high performance trading environment. These function calls form a large portion of the underlying structure of Hydro Relayers.
This section details some of the noteworthy functions within the Hydro Smart Contracts.
Hybrid Exchange Contract
function matchOrders( OrderParam memory takerOrderParam, OrderParam memory makerOrderParams, uint256 memory baseTokenFilledAmounts, OrderAddressSet memory orderAddressSet )
The matchOrders function is designed to validate and settle a taker order against at least one maker order. The amount of token each maker order will be filling is explicitly defined in the baseTokenFilledAmounts array. Since all matching orders will contain a common set of values for certain fields (the address of tokens being exchanged, and the address of the relayer), we split those values into a separate OrderAddressSet parameter. This can save a significant amount of gas by reducing the amount of redundant data being passed into the function.
matchOrders performs many validations against each passed in order, including:
- Signatures are recomputed and validated on each order
- The caller of the function must be the relayer specified in each signed order
- Maker Orders must be limit orders (not market orders)
- Maker Orders are all orders of the same type (buy/sell), and opposite type from the Taker Order
- The baseToken amount must be greater than zero
If the validations are all successful, matchOrders computes the results of settling the entire set of orders, then performs the minimum number of token transfers from each participating address. All transfers are delegated to the proxy contract, which must be approved to transfer the appropriate tokens for all parties involved.
function cancelOrder(Order memory order)
The cancelOrder function will not be generally used, but is intended as a safety measure of last resort for traders. Since orders are signed and sent to a relayer to match off-chain, in the normal scenario a trader would simply request their order cancelled by the relayer, and the data would be deleted and never matched. This is normally a huge benefit, as cancellation costs no gas, and does not need to be validated on the blockchain. However, if a trader wants to be absolutely sure their trade will never be matched, they may block it at a contract level by calling this method with the signed order data that they submitted to the relayer.
cancelOrder may only be called by the owner of a particular order.
Note: The Hydro Protocol by design does not include a batch cancel method, as multiple cancels are quite expensive gas-wise. In the case where trust is lost between the trader and relayer, disapproving the proxy or moving all assets to another wallet is both faster and cheaper.
The Hydro Protocol has an optional incentive system based around the HOT token, giving traders holding HOT token a fee discount when trading. Hydro Relayers by default are part of the incentive system, but at any time may choose to opt-out. If the relayer contract calls:
They will be opted-out, and effectively become a tokenless protocol. They may rejoin at anytime with:
Additionally, a public function exists where you may check if a relayer is part of the incentive system.
function isParticipant(address relayer)
The Proxy contract utilized by the Hydro Exchange contract is generally used by the exchange to settle orders, but it contains a few public methods that may be useful if you want to allow transactions between ETH and ERC20 tokens. In particular, if a trader creates an order offering ETH (not ERC20), the trader must have enough ETH deposited into the Proxy, and the taker of the order will be able to withdraw that ETH from the proxy once the match is settled.
function depositEther() payable
Sending ETH to the Proxy contract address via this method will deposit it into your "Proxy Account". This ETH may be withdrawn at any time.
function withdrawEther(uint256 amount)
Call this method with an amount to withdraw, and it will be subtracted from your "Proxy Account" balance and sent back to your wallet. For obvious reasons, attempting to withdraw more ETH than you have deposited will result in an error.