feat: parse accounts

This commit is contained in:
bartosz-lipinski 2021-03-03 23:28:25 -06:00
parent e597b40d22
commit c7a9c0b79a
2 changed files with 26 additions and 13 deletions

View File

@ -4,6 +4,8 @@ import * as BufferLayout from 'buffer-layout'
import {WORMHOLE_PROGRAM_ID} from "../utils/ids"; import {WORMHOLE_PROGRAM_ID} from "../utils/ids";
import BN from "bn.js"; import BN from "bn.js";
import {ASSET_CHAIN, getAssetAmountInUSD, getAssetName, getAssetTokenSymbol} from "../utils/assets"; import {ASSET_CHAIN, getAssetAmountInUSD, getAssetName, getAssetTokenSymbol} from "../utils/assets";
import { useEthereum } from "../contexts";
import { PublicKey } from "@solana/web3.js";
const { useConnection } = contexts.Connection; const { useConnection } = contexts.Connection;
@ -13,8 +15,8 @@ interface ParsedData {
parsedAssetAddress: string, parsedAssetAddress: string,
parsedAccount: any, parsedAccount: any,
assetDecimals: number, assetDecimals: number,
tokenName: string, name: string,
tokenSymbol: string, symbol: string,
sourceAddress: string, sourceAddress: string,
targetAddress: string, targetAddress: string,
amountInUSD: number, amountInUSD: number,
@ -22,6 +24,7 @@ interface ParsedData {
export const useLockedFundsAccounts = () => { export const useLockedFundsAccounts = () => {
const connection = useConnection(); const connection = useConnection();
const { tokenMap: ethTokens } = useEthereum();
const [lockedSolanaAccounts, setLockedSolanaAccounts] = useState<ParsedData[]>([]); const [lockedSolanaAccounts, setLockedSolanaAccounts] = useState<ParsedData[]>([]);
const [loading, setLoading] = useState<boolean>(true); const [loading, setLoading] = useState<boolean>(true);
@ -59,17 +62,21 @@ export const useLockedFundsAccounts = () => {
const dec = new BN(10).pow(new BN(parsedAccount.assetDecimals)); const dec = new BN(10).pow(new BN(parsedAccount.assetDecimals));
const rawAmount = new BN(parsedAccount.amount, 2, "le") const rawAmount = new BN(parsedAccount.amount, 2, "le")
const amount = rawAmount.div(dec).toNumber(); const amount = rawAmount.div(dec).toNumber();
const parsedAssetAddress: string = new Buffer(parsedAccount.assetAddress.slice(12)).toString("hex") const parsedAssetAddress: string = parsedAccount.assetChain === ASSET_CHAIN.Solana ?
new PublicKey(parsedAccount.targetAddress).toString() :
new Buffer(parsedAccount.assetAddress.slice(12)).toString("hex")
const parsedData: ParsedData = { const parsedData: ParsedData = {
amount: amount, amount: amount,
rawAmount: rawAmount.toString(), rawAmount: rawAmount.toString(),
parsedAssetAddress: parsedAssetAddress, parsedAssetAddress: parsedAssetAddress,
parsedAccount: parsedAccount, parsedAccount: parsedAccount,
assetDecimals: parsedAccount.assetDecimals, assetDecimals: parsedAccount.assetDecimals,
sourceAddress: new Buffer(parsedAccount.sourceAddress.slice(12)).toString("hex"), sourceAddress: new PublicKey(parsedAccount.sourceAddress).toString(),
targetAddress: new Buffer(parsedAccount.targetAddress.slice(12)).toString("hex"), targetAddress: parsedAccount.toChain === ASSET_CHAIN.Solana ?
tokenName: getAssetName(parsedAssetAddress, parsedAccount.assetChain), new PublicKey(parsedAccount.targetAddress).toString()
tokenSymbol: getAssetTokenSymbol(parsedAssetAddress, parsedAccount.assetChain), : new Buffer(parsedAccount.targetAddress.slice(12)).toString("hex"),
name: getAssetName(parsedAssetAddress, parsedAccount.assetChain),
symbol: getAssetTokenSymbol(parsedAssetAddress, parsedAccount.assetChain),
amountInUSD: getAssetAmountInUSD(amount, parsedAssetAddress, parsedAccount.assetChain), amountInUSD: getAssetAmountInUSD(amount, parsedAssetAddress, parsedAccount.assetChain),
}; };
filteredParsedAccounts.push(parsedData) filteredParsedAccounts.push(parsedData)
@ -92,4 +99,4 @@ export const useLockedFundsAccounts = () => {
return acc + val.amountInUSD; return acc + val.amountInUSD;
}, 0) }, 0)
}; };
} }

View File

@ -86,12 +86,13 @@ export const HomeView = () => {
return lockedSolanaAccounts.map((acc, index) => { return lockedSolanaAccounts.map((acc, index) => {
return { return {
key: index.toString(), key: index.toString(),
symbol: acc.tokenSymbol, symbol: acc.symbol,
name: acc.tokenName, name: acc.name,
amount: acc.amountInUSD, amount: acc.amountInUSD,
sourceAddress: acc.parsedAccount.assetChain === ASSET_CHAIN.Solana ? assetAddress: acc.parsedAccount.assetChain === ASSET_CHAIN.Solana ?
<ExplorerLink address={acc.sourceAddress} type={"address"} /> : <ExplorerLink address={acc.parsedAssetAddress} type={"address"} /> :
<EtherscanLink address={acc.sourceAddress} type={"address"} />, <EtherscanLink address={acc.parsedAssetAddress} type={"address"} />,
sourceAddress: <ExplorerLink address={acc.sourceAddress} type={"address"} />,
targetAddress: acc.parsedAccount.toChain === ASSET_CHAIN.Solana ? targetAddress: acc.parsedAccount.toChain === ASSET_CHAIN.Solana ?
<ExplorerLink address={acc.targetAddress} type={"address"} /> : <ExplorerLink address={acc.targetAddress} type={"address"} /> :
<EtherscanLink address={acc.targetAddress} type={"address"} />, <EtherscanLink address={acc.targetAddress} type={"address"} />,
@ -115,6 +116,11 @@ export const HomeView = () => {
dataIndex: 'amount', dataIndex: 'amount',
key: 'amount', key: 'amount',
}, },
{
title: 'Asset Address',
dataIndex: 'assetAddress',
key: 'assetAddress',
},
{ {
title: 'Source Address', title: 'Source Address',
dataIndex: 'sourceAddress', dataIndex: 'sourceAddress',