bridge_ui: fix solana native target balance

Change-Id: I187c08cb2108f83090c43f43e1ebf43487d9a50c
This commit is contained in:
Evan Gray 2021-08-26 13:44:08 +00:00 committed by Reisen
parent 568210edd4
commit f97ff5e079
5 changed files with 36 additions and 27 deletions

View File

@ -1,11 +1,8 @@
import { CHAIN_ID_SOLANA } from "@certusone/wormhole-sdk";
import { Button, makeStyles, MenuItem, TextField } from "@material-ui/core";
import { PublicKey } from "@solana/web3.js";
import { useCallback, useMemo } from "react";
import { useDispatch, useSelector } from "react-redux";
import useSyncTargetAddress from "../../hooks/useSyncTargetAddress";
import {
selectTransferIsSourceAssetWormholeWrapped,
selectTransferIsTargetComplete,
selectTransferShouldLockFields,
selectTransferSourceChain,
@ -15,7 +12,7 @@ import {
selectTransferTargetChain,
} from "../../store/selectors";
import { incrementStep, setTargetChain } from "../../store/transferSlice";
import { hexToUint8Array } from "../../utils/array";
import { hexToNativeString } from "../../utils/array";
import { CHAINS } from "../../utils/consts";
import KeyAndBalance from "../KeyAndBalance";
@ -36,17 +33,8 @@ function Target() {
const targetChain = useSelector(selectTransferTargetChain);
const targetAddressHex = useSelector(selectTransferTargetAddressHex); // TODO: make readable
const targetAsset = useSelector(selectTransferTargetAsset);
const isSourceAssetWormholeWrapped = useSelector(
selectTransferIsSourceAssetWormholeWrapped
);
// TODO: wrapped stuff in hex, but native in not hex?
const readableTargetAsset =
isSourceAssetWormholeWrapped &&
targetChain === CHAIN_ID_SOLANA &&
targetAsset
? new PublicKey(hexToUint8Array(targetAsset)).toString()
: targetAsset || "";
// TODO: why doesn't this show up for solana wrapped?
const readableTargetAddress =
hexToNativeString(targetAddressHex, targetChain) || "";
const uiAmountString = useSelector(selectTransferTargetBalanceString);
const isTargetComplete = useSelector(selectTransferIsTargetComplete);
const shouldLockFields = useSelector(selectTransferShouldLockFields);
@ -83,14 +71,14 @@ function Target() {
label="Address"
fullWidth
className={classes.transferField}
value={targetAddressHex || ""}
value={readableTargetAddress}
disabled={true}
/>
<TextField
label="Asset"
fullWidth
className={classes.transferField}
value={readableTargetAsset}
value={targetAsset || ""}
disabled={true}
/>
<Button

View File

@ -1,4 +1,8 @@
import { CHAIN_ID_TERRA, CHAIN_ID_ETH, CHAIN_ID_SOLANA } from "@certusone/wormhole-sdk";
import {
CHAIN_ID_ETH,
CHAIN_ID_SOLANA,
CHAIN_ID_TERRA,
} from "@certusone/wormhole-sdk";
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useEthereumProvider } from "../contexts/EthereumProviderContext";
@ -11,6 +15,7 @@ import {
selectTransferTargetChain,
} from "../store/selectors";
import { setTargetAsset } from "../store/transferSlice";
import { hexToNativeString } from "../utils/array";
import {
getForeignAssetEth,
getForeignAssetSol,
@ -31,7 +36,7 @@ function useFetchTargetAsset() {
// TODO: this may not cover wrapped to wrapped, should always use origin?
useEffect(() => {
if (isSourceAssetWormholeWrapped && originChain === targetChain) {
dispatch(setTargetAsset(originAsset));
dispatch(setTargetAsset(hexToNativeString(originAsset, originChain)));
return;
}
// TODO: loading state, error state

View File

@ -5,13 +5,12 @@ import {
TokenImplementation__factory,
} from "@certusone/wormhole-sdk";
import { Connection, PublicKey } from "@solana/web3.js";
import { useConnectedWallet } from "@terra-money/wallet-provider";
import { formatUnits } from "ethers/lib/utils";
import { useEffect } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useEthereumProvider } from "../contexts/EthereumProviderContext";
import { useSolanaWallet } from "../contexts/SolanaWalletContext";
import { useConnectedWallet } from "@terra-money/wallet-provider";
import {
selectTransferSourceAsset,
selectTransferSourceChain,
@ -22,7 +21,6 @@ import {
setSourceParsedTokenAccount,
setTargetParsedTokenAccount,
} from "../store/transferSlice";
import { hexToUint8Array } from "../utils/array";
import { SOLANA_HOST } from "../utils/consts";
function createParsedTokenAccount(
@ -83,11 +81,7 @@ function useGetBalanceEffect(sourceOrTarget: "source" | "target") {
if (lookupChain === CHAIN_ID_SOLANA && solPK) {
let mint;
try {
mint = new PublicKey(
sourceOrTarget === "source"
? lookupAsset
: hexToUint8Array(lookupAsset)
);
mint = new PublicKey(lookupAsset);
} catch (e) {
return;
}

View File

@ -1,4 +1,20 @@
import {
ChainId,
CHAIN_ID_ETH,
CHAIN_ID_SOLANA,
} from "@certusone/wormhole-sdk";
import { PublicKey } from "@solana/web3.js";
import { hexValue } from "ethers/lib/utils";
export const uint8ArrayToHex = (a: Uint8Array) =>
Buffer.from(a).toString("hex");
export const hexToUint8Array = (h: string) =>
new Uint8Array(Buffer.from(h, "hex"));
export const hexToNativeString = (h: string | undefined, c: ChainId) =>
!h
? undefined
: c === CHAIN_ID_SOLANA
? new PublicKey(hexToUint8Array(h)).toString()
: c === CHAIN_ID_ETH
? hexValue(hexToUint8Array(h))
: h;

View File

@ -28,6 +28,12 @@ export async function redeemOnSolana(
isSolanaNative: boolean,
mintAddress?: string // TODO: read the signedVAA and create the account if it doesn't exist
) {
// TODO: this gets the target account off the vaa, but is there a way to do this via wasm?
// also, would this always be safe to do?
// should we rely on this function to create accounts at all?
// const { parse_vaa } = await import("../solana/core/bridge")
// const parsedVAA = parse_vaa(signedVAA);
// const targetAddress = new PublicKey(parsedVAA.payload.slice(67, 67 + 32)).toString()
const { complete_transfer_wrapped_ix, complete_transfer_native_ix } =
await import("../solana/token/token_bridge");
const ixs = [];