Skip to content

Commit

Permalink
improve: more effectively estimate suggested fees quote (#887)
Browse files Browse the repository at this point in the history
* improve: use valid utils

* chore: api endpoint

* improve: pipe recipient address into suggested fees

* fix: allow changed recipient
  • Loading branch information
james-a-morris authored Oct 27, 2023
1 parent 95e89d5 commit 7a244e4
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 10 deletions.
8 changes: 6 additions & 2 deletions api/suggested-fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ import { BlockFinder } from "@uma/sdk";
import { VercelResponse } from "@vercel/node";
import { ethers } from "ethers";
import { type, assert, Infer, optional, string } from "superstruct";
import { disabledL1Tokens, DEFAULT_QUOTE_TIMESTAMP_BUFFER } from "./_constants";
import {
disabledL1Tokens,
DEFAULT_QUOTE_TIMESTAMP_BUFFER,
DEFAULT_SIMULATED_RECIPIENT_ADDRESS,
} from "./_constants";
import { TypedVercelRequest } from "./_types";
import {
getLogger,
Expand Down Expand Up @@ -79,7 +83,7 @@ const handler = async (
const destinationChainId = Number(_destinationChainId);

relayerAddress ??= sdk.constants.DEFAULT_SIMULATED_RELAYER_ADDRESS;
recipientAddress ??= sdk.constants.DEFAULT_SIMULATED_RELAYER_ADDRESS;
recipientAddress ??= DEFAULT_SIMULATED_RECIPIENT_ADDRESS;
token = ethers.utils.getAddress(token);

const [latestBlock, tokenDetails] = await Promise.all([
Expand Down
11 changes: 9 additions & 2 deletions src/hooks/useBridgeFees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,17 @@ export function useBridgeFees(
amount: ethers.BigNumber,
fromChainId?: ChainId,
toChainId?: ChainId,
tokenSymbol?: string
tokenSymbol?: string,
recipientAddress?: string
) {
const { block } = useBlock(toChainId);
const enabledQuery =
!!toChainId && !!fromChainId && !!block && !!tokenSymbol && amount.gt(0);
!!toChainId &&
!!fromChainId &&
!!block &&
!!tokenSymbol &&
amount.gt(0) &&
!!recipientAddress;
const queryKey = enabledQuery
? bridgeFeesQueryKey(
tokenSymbol,
Expand All @@ -38,6 +44,7 @@ export function useBridgeFees(
blockTimestamp: block!.timestamp,
toChainId: toChainId!,
fromChainId: fromChainId!,
recipientAddress: recipientAddress!,
});
},
{
Expand Down
5 changes: 4 additions & 1 deletion src/utils/bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ type GetBridgeFeesArgs = {
blockTimestamp: number;
fromChainId: ChainId;
toChainId: ChainId;
recipientAddress: string;
};

export type GetBridgeFeesResult = BridgeFees & {
Expand All @@ -52,6 +53,7 @@ export async function getBridgeFees({
tokenSymbol,
fromChainId,
toChainId,
recipientAddress,
}: GetBridgeFeesArgs): Promise<GetBridgeFeesResult> {
const timeBeforeRequests = Date.now();
const {
Expand All @@ -66,7 +68,8 @@ export async function getBridgeFees({
amount,
getConfig().getTokenInfoBySymbol(fromChainId, tokenSymbol).address,
toChainId,
fromChainId
fromChainId,
recipientAddress
);
const timeAfterRequests = Date.now();

Expand Down
3 changes: 2 additions & 1 deletion src/utils/serverless-api/mocked/suggested-fees.mocked.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ export async function suggestedFeesMockedApiCall(
_amount: ethers.BigNumber,
_originToken: string,
_toChainid: ChainId,
_fromChainid: ChainId
_fromChainid: ChainId,
_recipientAddress: string
): Promise<SuggestedApiFeeReturnType> {
return {
relayerFee: {
Expand Down
4 changes: 3 additions & 1 deletion src/utils/serverless-api/prod/suggested-fees.prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@ export async function suggestedFeesApiCall(
amount: ethers.BigNumber,
originToken: string,
toChainid: ChainId,
fromChainid: ChainId
fromChainid: ChainId,
recipientAddress: string
): Promise<SuggestedApiFeeReturnType> {
const response = await axios.get(`/api/suggested-fees`, {
params: {
token: originToken,
destinationChainId: toChainid,
originChainId: fromChainid,
recipientAddress,
amount: amount.toString(),
skipAmountLimit: true,
},
Expand Down
3 changes: 2 additions & 1 deletion src/utils/serverless-api/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,8 @@ export type SuggestedApiFeeType = (
amount: ethers.BigNumber,
originToken: string,
toChainid: ChainId,
fromChainid: ChainId
fromChainid: ChainId,
recipientAddress: string
) => Promise<SuggestedApiFeeReturnType>;

export type RetrieveLinkedWalletType = (
Expand Down
3 changes: 2 additions & 1 deletion src/views/Bridge/hooks/useTransferQuote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@ export function useTransferQuote(
amountToBridge,
selectedRoute.fromChain,
selectedRoute.toChain,
selectedRoute.fromTokenSymbol
selectedRoute.fromTokenSymbol,
toAddress
);
const limitsQuery = useBridgeLimits(
selectedRoute.fromTokenSymbol,
Expand Down
3 changes: 2 additions & 1 deletion src/views/Transactions/hooks/useSpeedUp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export function useSpeedUp(transfer: Deposit, token: Token) {
BigNumber.from(transfer.amount),
transfer.sourceChainId,
transfer.destinationChainId,
token.symbol
token.symbol,
transfer.recipientAddr
);

const [speedUpTxLink, setSpeedUpTxLink] = useState<string>("");
Expand Down

2 comments on commit 7a244e4

@vercel
Copy link

@vercel vercel bot commented on 7a244e4 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

goerli-frontend-v2 – ./

goerli-frontend-v2-git-master-uma.vercel.app
goerli-frontend-v2.vercel.app
goerli-frontend-v2-uma.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 7a244e4 Oct 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.