Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: manual rebalancer support #1252

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions api/suggested-fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,9 @@ const handler = async (
.mul(parseUnits(tokenPriceUsd.toString(), 18))
.div(parseUnits("1", inputToken.decimals));

if (amount.gt(maxDeposit)) {
const skipAmountLimitEnabled = skipAmountLimit === "true";

if (!skipAmountLimitEnabled && amount.gt(maxDeposit)) {
throw new AmountTooHighError({
message: `Amount exceeds max. deposit limit: ${ethers.utils.formatUnits(
maxDeposit,
Expand All @@ -237,7 +239,6 @@ const handler = async (

const isAmountTooLow = BigNumber.from(amountInput).lt(minDeposit);

const skipAmountLimitEnabled = skipAmountLimit === "true";
if (!skipAmountLimitEnabled && isAmountTooLow) {
throw new AmountTooLowError({
message: `Sent amount is too low relative to fees`,
Expand Down
8 changes: 8 additions & 0 deletions src/utils/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -529,3 +529,11 @@ export const vercelApiBaseUrl =
export const defaultSwapSlippage = Number(
process.env.REACT_APP_DEFAULT_SWAP_SLIPPAGE || 0.5
);

// List of addresses that have special manual rebalancing rights in the FE
export const manualRebalancerAddresses = String(
process.env.REACT_APP_MANUAL_REBALANCER_ADDRESSES ||
"0x1d933Fd71FF07E69f066d50B39a7C34EB3b69F05"
)
.split(",")
.map(ethers.utils.getAddress);
8 changes: 5 additions & 3 deletions src/views/Bridge/hooks/useBridge.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useEffect, useState } from "react";
import { BigNumber } from "ethers";
import { BigNumber, utils, constants } from "ethers";

import { useConnection, useIsWrongNetwork, useAmplitude } from "hooks";
import { ampli } from "ampli";
import { defaultSwapSlippage, bnZero } from "utils";
import { defaultSwapSlippage, bnZero, manualRebalancerAddresses } from "utils";

import { useBridgeAction } from "./useBridgeAction";
import { useToAccount } from "./useToAccount";
Expand Down Expand Up @@ -58,12 +58,14 @@ export function useBridge() {
shouldUpdateQuote &&
(transferQuoteQuery.isInitialLoading || feesQuery.isInitialLoading) &&
!transferQuote;
const isManualRebalancer =
account && manualRebalancerAddresses.includes(utils.getAddress(account));

const { error: amountValidationError } = validateBridgeAmount(
parsedAmount,
quotedFees?.isAmountTooLow,
maxBalance,
limitsQuery.limits?.maxDeposit,
isManualRebalancer ? constants.MaxUint256 : limitsQuery.limits?.maxDeposit,
selectedRoute.type === "swap" && quotedSwap?.minExpectedInputTokenAmount
? BigNumber.from(quotedSwap?.minExpectedInputTokenAmount)
: parsedAmount
Expand Down
31 changes: 29 additions & 2 deletions src/views/Bridge/hooks/useBridgeAction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
TransferQuoteReceivedProperties,
ampli,
} from "ampli";
import { BigNumber, constants, providers } from "ethers";
import { BigNumber, constants, providers, utils } from "ethers";
import {
useConnection,
useApprove,
Expand All @@ -23,6 +23,8 @@ import {
sendSpokePoolVerifierDepositTx,
sendDepositV3Tx,
sendSwapAndBridgeTx,
manualRebalancerAddresses,
ChainId,
} from "utils";
import { TransferQuote } from "./useTransferQuote";
import { SelectedRoute } from "../utils";
Expand Down Expand Up @@ -153,7 +155,32 @@ export function useBridgeAction(

let tx: providers.TransactionResponse;

if (isSwapRoute) {
// If the connected wallet is configured as a manual rebalancer and the origin
// chain is mainnet, we force the deposit to be unprofitable with a long
// exclusivity. The idea is to have these deposits paid out as a slow fill, in
// which case the SpokePool's own balance will be used to complete the fill, and
// the HubPool will pull funds back from the mainnet SpokePool instead. This
// allows us to skip the 7 day bridges and ensure a safe level of HubPool liquidity
if (
manualRebalancerAddresses.includes(utils.getAddress(frozenAccount)) &&
frozenRoute.fromChain === ChainId.MAINNET
) {
const { spokePool } = await getSpokePoolAndVerifier(frozenRoute);
tx = await sendDepositV3Tx(
signer,
{
...frozenDepositArgs,
inputTokenAddress: frozenRoute.fromTokenAddress,
outputTokenAddress: frozenRoute.toTokenAddress,
// Force overrides to make deposit unattractive for relayers
relayerFeePct: BigNumber.from(0),
exclusiveRelayer: frozenAccount,
exclusivityDeadline: 10 * 60, // 10 minutes
},
spokePool,
networkMismatchHandler
);
} else if (isSwapRoute) {
tx = await sendSwapAndBridgeTx(
signer,
{
Expand Down
Loading