Skip to content

Commit

Permalink
Removing unnecessary SDK calls during initial load (#2137)
Browse files Browse the repository at this point in the history
Signed-off-by: Emre Bogazliyanlioglu <emre@wormholelabs.xyz>
  • Loading branch information
emreboga authored May 28, 2024
1 parent e422da6 commit 50413f9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
2 changes: 1 addition & 1 deletion wormhole-connect/src/routes/sdkv2/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,10 @@ export class SDKv2Route<N extends Network> extends RouteAbstract {

return routes.RouteTransferRequest.create(
wh,
/* @ts-ignore */
{
source: srcTokenV2,
destination: dstTokenV2,
/* @ts-ignore */
},
srcChain,
dstChain,
Expand Down
26 changes: 24 additions & 2 deletions wormhole-connect/src/views/Bridge/Bridge.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,12 @@ function Bridge() {

// check destination native balance
useEffect(() => {
if (!fromChain || !toChain || !receiving.address) return;
if (!fromChain || !toChain || !receiving.address) {
return;
}

const chainConfig = config.chains[toChain]!;

config.wh
.getNativeBalance(receiving.address, toChain)
.then((res: BigNumber) => {
Expand All @@ -132,7 +136,12 @@ function Bridge() {
}, [fromChain, toChain, receiving.address, dispatch]);

useEffect(() => {
if (!fromChain) {
return;
}

let active = true;

const computeSrcTokens = async () => {
const supported = await RouteOperator.allSupportedSourceTokens(
config.tokens[destToken],
Expand All @@ -150,14 +159,20 @@ function Bridge() {
}
}
};

computeSrcTokens();

return () => {
active = false;
};
// IMPORTANT: do not include token in dependency array
}, [route, fromChain, destToken, dispatch]);

useEffect(() => {
if (!toChain) {
return;
}

let canceled = false;

const computeDestTokens = async () => {
Expand Down Expand Up @@ -231,21 +246,28 @@ function Bridge() {
}
}
};

computeDestTokens();

return () => {
canceled = true;
};
// IMPORTANT: do not include destToken in dependency array
}, [route, token, fromChain, toChain, dispatch]);

useEffect(() => {
if (!route || !amount || !token || !destToken || !fromChain || !toChain) {
return;
}

const recomputeReceive = async () => {
if (!route) return;
try {
const routeOptions = isPorticoRoute(route)
? portico
: { toNativeToken, relayerFee };

dispatch(setFetchingReceiveAmount());

const newReceiveAmount = await RouteOperator.computeReceiveAmount(
route,
Number.parseFloat(amount),
Expand Down

0 comments on commit 50413f9

Please sign in to comment.