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

WIP: Getting signer working #6

Merged
merged 8 commits into from
Jul 3, 2024
Merged
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: 4 additions & 1 deletion wormhole-connect/src/AppRouter.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useEffect, useMemo } from 'react';
import React, { useContext, useEffect, useMemo } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import { makeStyles } from 'tss-react/mui';

Expand All @@ -24,6 +24,7 @@ import { useExternalSearch } from 'hooks/useExternalSearch';
import internalConfig from 'config';

import BridgeV2 from 'views/v2/Bridge';
import { RouteContext } from 'contexts/RouteContext';

const useStyles = makeStyles()((theme: any) => ({
appContent: {
Expand All @@ -49,6 +50,7 @@ interface Props {
function AppRouter(props: Props) {
const { classes } = useStyles();
const dispatch = useDispatch();
const routeContext = useContext(RouteContext);

// We update the global config once when WormholeConnect is first mounted, if a custom
// config was provided.
Expand Down Expand Up @@ -80,6 +82,7 @@ function AppRouter(props: Props) {
if (prevRoute === redeemRoute && route !== redeemRoute) {
dispatch(clearRedeem());
dispatch(clearWallets());
routeContext.clear();
internalConfig.wh.registerProviders(); // reset providers that may have been set during transfer
}
// reset transfer state on leave
Expand Down
5 changes: 4 additions & 1 deletion wormhole-connect/src/WormholeConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { getDesignTokens, dark } from './theme';
import ErrorBoundary from './components/ErrorBoundary';
import { WormholeConnectConfig } from './config/types';
import { WormholeConnectPartialTheme } from 'theme';
import { RouteProvider } from './contexts/RouteContext';

export interface WormholeConnectProps {
// theme can be updated at any time to change the colors of Connect
Expand All @@ -32,7 +33,9 @@ export default function WormholeConnect({
<ThemeProvider theme={muiTheme}>
<ScopedCssBaseline enableColorScheme>
<ErrorBoundary>
<AppRouter config={config} />
<RouteProvider>
<AppRouter config={config} />
</RouteProvider>
</ErrorBoundary>
</ScopedCssBaseline>
</ThemeProvider>
Expand Down
27 changes: 22 additions & 5 deletions wormhole-connect/src/config/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,29 @@ export class SDKConverter {
}
}

toTokenIdV2(token: v1.TokenId | TokenConfigV1): v2.TokenId {
toTokenIdV2(
token: v1.TokenId | TokenConfigV1,
chain?: v1.ChainName,
): v2.TokenId {
if (this.isTokenConfigV1(token)) {
return v2.Wormhole.tokenId(
this.toChainV2(token.nativeChain),
token.tokenId?.address ?? 'native',
);
if (chain && chain != token.nativeChain) {
// Getting foreign address
const foreignAsset = token.foreignAssets?.[chain];
if (foreignAsset) {
return v2.Wormhole.tokenId(
this.toChainV2(chain),
foreignAsset.address,
);
} else {
throw new Error('no foreign asset');
}
} else {
// Getting native address
return v2.Wormhole.tokenId(
this.toChainV2(token.nativeChain),
token.tokenId?.address ?? 'native',
);
}
} else {
return v2.Wormhole.tokenId(this.toChainV2(token.chain), token.address);
}
Expand Down
44 changes: 44 additions & 0 deletions wormhole-connect/src/contexts/RouteContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import React, { useCallback } from 'react';
import { Network, routes } from '@wormhole-foundation/sdk';

interface RouteContextType {
route: routes.Route<Network> | null;
receipt: routes.Receipt | null;
setRoute: (route: routes.Route<Network>) => void;
setReceipt: (receipt: routes.Receipt) => void;
clear: () => void;
}

export const RouteContext = React.createContext<RouteContextType>({
route: null,
receipt: null,
setRoute: () => {
// Keep the empty function for initial context value
},
setReceipt: () => {
// Keep the empty function for initial context value
},
clear: () => {
// Keep the empty function for initial context value
},
});

export const RouteProvider: React.FC<{ children: React.ReactNode }> = ({
children,
}) => {
const [route, setRoute] = React.useState<routes.Route<Network> | null>(null);
const [receipt, setReceipt] = React.useState<routes.Receipt | null>(null);

const clear = useCallback(() => {
setRoute(null);
setReceipt(null);
}, []);

return (
<RouteContext.Provider
value={{ route, receipt, setRoute, setReceipt, clear }}
>
{children}
</RouteContext.Provider>
);
};
12 changes: 7 additions & 5 deletions wormhole-connect/src/hooks/useComputeDestinationTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@ import {
setDestToken,
setSupportedDestTokens,
setAllSupportedDestTokens,
getNativeVersionOfToken,
//getNativeVersionOfToken,
} from 'store/transferInput';

import type { Route } from 'config/types';
import type { ChainName } from 'sdklegacy';

import { isPorticoRoute } from 'routes/porticoBridge/utils';
import { ETHBridge } from 'routes/porticoBridge/ethBridge';
import { wstETHBridge } from 'routes/porticoBridge/wstETHBridge';
//import { isPorticoRoute } from 'routes/porticoBridge/utils';
//import { ETHBridge } from 'routes/porticoBridge/ethBridge';
//import { wstETHBridge } from 'routes/porticoBridge/wstETHBridge';
import RouteOperator from 'routes/operator';

import { getWrappedToken } from 'utils';
//import { getWrappedToken } from 'utils';

type Props = {
sourceChain: ChainName | undefined;
Expand Down Expand Up @@ -86,6 +86,7 @@ export const useComputeDestinationTokens = (props: Props): void => {
}
}

/*
// If the source token is supported by a Portico bridge route,
// then select the native version on the dest chain
if (sourceToken && destChain && (!route || isPorticoRoute(route))) {
Expand All @@ -107,6 +108,7 @@ export const useComputeDestinationTokens = (props: Props): void => {
}
}
}
*/
};

computeDestTokens();
Expand Down
31 changes: 20 additions & 11 deletions wormhole-connect/src/hooks/useGetTokenBalances.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ const useGetTokenBalances = (
needsUpdate.push(token as TokenConfigWithId);
}
}

if (needsUpdate.length > 0) {
try {
const wh = await getWormholeContextV2();
Expand All @@ -85,19 +86,27 @@ const useGetTokenBalances = (
try {
let address: string | null = null;

const foreignAddress = await getForeignTokenAddress(
config.sdkConverter.toTokenIdV2(tokenConfig),
chainV2,
);

if (foreignAddress) {
address = foreignAddress.toString();
if (
tokenConfig.nativeChain === chain &&
tokenConfig.tokenId === undefined
) {
tokenAddresses.push('native');
tokenIdMapping['native'] = tokenConfig;
} else {
console.error('no fa', tokenConfig);
continue;
const foreignAddress = await getForeignTokenAddress(
config.sdkConverter.toTokenIdV2(tokenConfig),
chainV2,
);

if (foreignAddress) {
address = foreignAddress.toString();
} else {
console.error('no fa', tokenConfig);
continue;
}
tokenIdMapping[address] = tokenConfig;
tokenAddresses.push(address);
}
tokenIdMapping[address] = tokenConfig;
tokenAddresses.push(address);
} catch (e) {
// TODO SDKV2 SUI ISNT WORKING
console.error(e);
Expand Down
3 changes: 2 additions & 1 deletion wormhole-connect/src/hooks/usePorticoSwapInfo.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useEffect } from 'react';
/*import { useEffect } from 'react';
import { useDispatch, useSelector } from 'react-redux';
import RouteOperator from 'routes/operator';
import { PorticoBridge } from 'routes/porticoBridge';
Expand Down Expand Up @@ -88,3 +88,4 @@ export const usePorticoSwapInfo = (): void => {
dispatch,
]);
};
*/
57 changes: 57 additions & 0 deletions wormhole-connect/src/hooks/useTrackTransfer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { isCompleted } from '@wormhole-foundation/sdk';
import { RouteContext } from 'contexts/RouteContext';
import { useContext, useEffect } from 'react';
import { useDispatch } from 'react-redux';
import { setTransferComplete } from 'store/redeem';
import { sleep } from 'utils';

const TRACK_TIMEOUT = 120 * 1000;

// TODO: document this hook, especially since it sets and depends on the receipt state
const useTrackTransfer = () => {

Check failure on line 11 in wormhole-connect/src/hooks/useTrackTransfer.ts

View workflow job for this annotation

GitHub Actions / lint

Missing return type on function
const dispatch = useDispatch();

const routeContext = useContext(RouteContext);

useEffect(() => {
let isActive = true;

const track = async () => {
const { route, receipt } = routeContext;
if (!route || !receipt) {
return;
}
while (isActive && !isCompleted(receipt)) {
try {
// TODO: the timeout may be longer for chains with slower finality times
// but we will retry so maybe it doesn't matter
const result = await route.track(receipt, TRACK_TIMEOUT).next();
if (result.done || !isActive) {
break;
}
const currentReceipt = result.value;
if (currentReceipt.state !== receipt.state) {
routeContext.setReceipt(currentReceipt);
if (isCompleted(currentReceipt)) {
dispatch(setTransferComplete(true));
}
break;
}
} catch (e) {
console.error('Error tracking transfer:', e);
}
// retry
// TODO: exponential backoff depending on the current state?
await sleep(5000);
}
};

track();

return () => {
isActive = false;
};
}, [routeContext]);
};

export default useTrackTransfer;
2 changes: 1 addition & 1 deletion wormhole-connect/src/routes/abstracts/routeAbstract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ export abstract class RouteAbstract {
public abstract getMaxSendAmount(): number;

public abstract send(
token: TokenId | 'native',
token: TokenConfig,
amount: string,
sendingChain: ChainName | ChainId,
senderAddress: string,
Expand Down
10 changes: 6 additions & 4 deletions wormhole-connect/src/routes/mappings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import { routes } from '@wormhole-foundation/sdk';
import { SDKv2Route } from './sdkv2/route';

// Legacy routes
import { RouteAbstract } from './abstracts/routeAbstract';
import { ETHBridge } from './porticoBridge/ethBridge';
import { wstETHBridge } from './porticoBridge/wstETHBridge';
//import { RouteAbstract } from './abstracts/routeAbstract';
//import { ETHBridge } from './porticoBridge/ethBridge';
//import { wstETHBridge } from './porticoBridge/wstETHBridge';

export function getRoute(route: Route): RouteAbstract {
export function getRoute(route: Route): SDKv2Route {
switch (route) {
// Migrated routes:
case Route.Bridge:
Expand All @@ -18,10 +18,12 @@ export function getRoute(route: Route): RouteAbstract {
return new SDKv2Route(routes.AutomaticTokenBridgeRoute, Route.Bridge);

// Legacy routes:
/*
case Route.ETHBridge:
return new ETHBridge();
case Route.wstETHBridge:
return new wstETHBridge();
*/
// TODO SDKV2
default:
return new SDKv2Route(routes.TokenBridgeRoute, Route.Bridge);
Expand Down
6 changes: 3 additions & 3 deletions wormhole-connect/src/routes/operator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
} from './types';
import { TokenPrices } from 'store/tokenPrices';

import { routes } from '@wormhole-foundation/sdk';
import { Network, routes } from '@wormhole-foundation/sdk';

import { getRoute } from './mappings';

Expand Down Expand Up @@ -428,15 +428,15 @@ export class Operator {

async send(
route: Route,
token: TokenId | 'native',
token: TokenConfig,
amount: string,
sendingChain: ChainName | ChainId,
senderAddress: string,
recipientChain: ChainName | ChainId,
recipientAddress: string,
destToken: string,
routeOptions: any,
): Promise<string | routes.Receipt> {
): Promise<[routes.Route<Network>, routes.Receipt]> {
const r = this.getRoute(route);
return await r.send(
token,
Expand Down
12 changes: 0 additions & 12 deletions wormhole-connect/src/routes/porticoBridge/abis.ts

This file was deleted.

9 changes: 0 additions & 9 deletions wormhole-connect/src/routes/porticoBridge/consts.ts

This file was deleted.

12 changes: 0 additions & 12 deletions wormhole-connect/src/routes/porticoBridge/ethBridge.ts

This file was deleted.

1 change: 0 additions & 1 deletion wormhole-connect/src/routes/porticoBridge/index.ts

This file was deleted.

Loading
Loading