Fill notification hook

It is possible to use PUBLIC_ORDER_WITH_HOOK or PRIVATE_ORDER_WITH_HOOK in order to open an order that calls an hook function when the filler fills the order. The contract that holds the hook function must implement IHook7683Recipientarrow-up-right interface and its address must be passed through the recipient address in the orderData object.

The hook is called at the end of the BasicSwap7683 _fillOrder functionarrow-up-right:

if (
    orderData.orderType == OrderEncoder.PUBLIC_ORDER_WITH_HOOK
        || orderData.orderType == OrderEncoder.PRIVATE_ORDER_WITH_HOOK
) {
    IHook7683Recipient(recipient).onFilledOrder(orderData);
}

Implementation

An hook function implementation is available to be tested in the example section. In this example implementation the hook is called at the end of the filling function and it is used to record the order commitment.

Interface

interface IHook7683Recipient {
    function onFilledOrder(OrderData memory orderData) external;
}

Hook implementation

function onFilledOrder(OrderData calldata orderData) external {
    require(msg.sender == GATEWAY, "not gateway");
    address token = _bytes32ToAddress(orderData.outputToken);
    bytes32 commitment = sha256(abi.encodePacked(token, orderData.data));
    require(finalizableDeposits[commitment] == 0, "commitment already used");
    // NOTE: Only the user who owns the full zkPassport proof can call `claim` by providing the proof.
    // The proof commitment must also include `msg.sender` to prevent others from stealing the proof
    // and using it to fraudulently claim the amount.
    finalizableDeposits[commitment] = orderData.amountOut;
    emit NewDepositToFinalize(commitment, token, orderData.amountOut);
}

Full codearrow-up-right

Last updated