bridge_ui: fix allow migration from any token acct

Change-Id: Ia4a267bd940e2e3e7f2b5643e6b9322ec4f34024
This commit is contained in:
Evan Gray 2021-09-22 11:59:25 -04:00
parent 25d98e8500
commit 6e8668e56d
4 changed files with 31 additions and 33 deletions

View File

@ -168,7 +168,7 @@ function App() {
<Route exact path="/register">
<Attest />
</Route>
<Route exact path="/migrate/:legacyAsset">
<Route exact path="/migrate/:legacyAsset/:fromTokenAccount">
<Migration />
</Route>
<Route exact path="/">

View File

@ -26,7 +26,6 @@ import { COLORS } from "../../muiTheme";
import { MIGRATION_PROGRAM_ADDRESS, SOLANA_HOST } from "../../utils/consts";
import { getMultipleAccounts, signSendAndConfirm } from "../../utils/solana";
import ButtonWithLoader from "../ButtonWithLoader";
import LowBalanceWarning from "../LowBalanceWarning";
import ShowTx from "../ShowTx";
import SmartAddress from "../SmartAddress";
import SolanaCreateAssociatedAddress, {
@ -94,9 +93,11 @@ const getBalance = async (
export default function Workflow({
fromMint,
toMint,
fromTokenAccount,
}: {
fromMint: string;
toMint: string;
fromTokenAccount: string;
}) {
const classes = useStyles();
@ -112,9 +113,6 @@ export default function Workflow({
const [poolAddress, setPoolAddress] = useState("");
const [poolExists, setPoolExists] = useState<boolean | undefined>(undefined);
const [fromTokenAccount, setFromTokenAccount] = useState<string | undefined>(
undefined
);
const [fromTokenAccountBalance, setFromTokenAccountBalance] = useState<
string | undefined
>(undefined);
@ -254,23 +252,6 @@ export default function Workflow({
}
}, [poolAddress]);
//Set the associated token accounts when the designated mint changes
useEffect(() => {
if (wallet?.publicKey && fromMint) {
Token.getAssociatedTokenAddress(
ASSOCIATED_TOKEN_PROGRAM_ID,
TOKEN_PROGRAM_ID,
new PublicKey(fromMint),
wallet?.publicKey || new PublicKey([])
).then(
(result) => {
setFromTokenAccount(result.toString());
},
(error) => {}
);
}
}, [fromMint, wallet?.publicKey]);
useEffect(() => {
if (wallet?.publicKey && toMint) {
Token.getAssociatedTokenAddress(
@ -426,12 +407,11 @@ export default function Workflow({
Convert assets from legacy bridges to Wormhole V2 tokens
</Typography>
<Divider className={classes.divider} />
<SolanaWalletKey />
<LowBalanceWarning chainId={CHAIN_ID_SOLANA} />
{fromTokenAccount && toTokenAccount && fromTokenAccountBalance ? (
<div className={classes.spacer} />
{fromTokenAccount && toTokenAccount ? (
<>
<Typography variant="body2">
<Typography variant="body2" component="div">
<span>This will migrate</span>
{fromMintPretty}
<span>tokens in this account:</span>
@ -446,7 +426,7 @@ export default function Workflow({
})`}
</Typography>
<div className={classes.spacer} />
<Typography variant="body2">
<Typography variant="body2" component="div">
<span>into </span>
{toMintPretty}
<span> tokens in this account:</span>
@ -476,7 +456,7 @@ export default function Workflow({
{poolAddress && toCustodyAddress && toCustodyBalance ? (
<>
<div className={classes.spacer} />
<Typography variant="body2">
<Typography variant="body2" component="div">
<span>Using pool </span>
<SmartAddress
address={poolAddress}

View File

@ -7,29 +7,46 @@ import { withRouter } from "react-router";
interface RouteParams {
legacyAsset: string;
fromTokenAccount: string;
}
interface Migration extends RouteComponentProps<RouteParams> {}
const MigrationRoot: React.FC<Migration> = (props) => {
const legacyAsset: string = props.match.params.legacyAsset;
const fromTokenAccount: string = props.match.params.fromTokenAccount;
const targetAsset: string | undefined = MIGRATION_ASSET_MAP.get(legacyAsset);
let fromMint: string | undefined = "";
let toMint: string | undefined = "";
let fromTokenAcct: string | undefined = "";
try {
fromMint = legacyAsset && new PublicKey(legacyAsset).toString();
toMint = targetAsset && new PublicKey(targetAsset).toString();
fromTokenAcct =
fromTokenAccount && new PublicKey(fromTokenAccount).toString();
} catch (e) {}
if (fromMint && toMint) {
return <Workflow fromMint={fromMint} toMint={toMint} />;
} else {
if (!fromMint || !toMint) {
return (
<Typography style={{ textAlign: "center" }}>
This asset is not eligible for migration.
</Typography>
);
} else if (!fromTokenAcct) {
return (
<Typography style={{ textAlign: "center" }}>
Invalid token account.
</Typography>
);
} else {
return (
<Workflow
fromMint={fromMint}
toMint={toMint}
fromTokenAccount={fromTokenAcct}
/>
);
}
};

View File

@ -57,8 +57,9 @@ function Source({
const shouldLockFields = useSelector(selectTransferShouldLockFields);
const { isReady, statusMessage } = useIsWalletReady(sourceChain);
const handleMigrationClick = useCallback(() => {
parsedTokenAccount?.mintKey &&
history.push("/migrate/" + parsedTokenAccount.mintKey);
history.push(
`/migrate/${parsedTokenAccount?.mintKey}/${parsedTokenAccount?.publicKey}`
);
}, [history, parsedTokenAccount]);
const handleSourceChange = useCallback(
(event) => {