Add listener to fetch account balance details
This commit is contained in:
parent
616ead7ae5
commit
b200563139
49
src/App.tsx
49
src/App.tsx
@ -4,6 +4,8 @@ import { TxBody, AuthInfo } from "cosmjs-types/cosmos/tx/v1beta1/tx";
|
|||||||
|
|
||||||
import { SignClientTypes } from "@walletconnect/types";
|
import { SignClientTypes } from "@walletconnect/types";
|
||||||
import { useNavigation } from "@react-navigation/native";
|
import { useNavigation } from "@react-navigation/native";
|
||||||
|
import { DirectSecp256k1Wallet } from "@cosmjs/proto-signing";
|
||||||
|
import { SigningStargateClient } from "@cosmjs/stargate";
|
||||||
import {
|
import {
|
||||||
createStackNavigator,
|
createStackNavigator,
|
||||||
StackNavigationProp,
|
StackNavigationProp,
|
||||||
@ -36,6 +38,8 @@ import styles from "./styles/stylesheet";
|
|||||||
import { Header } from "./components/Header";
|
import { Header } from "./components/Header";
|
||||||
import { WalletEmbed } from "./screens/WalletEmbed";
|
import { WalletEmbed } from "./screens/WalletEmbed";
|
||||||
import { AutoSignIn } from "./screens/AutoSignIn";
|
import { AutoSignIn } from "./screens/AutoSignIn";
|
||||||
|
import { checkSufficientFunds, getPathKey, sendMessage } from "./utils/misc";
|
||||||
|
import { retrieveSingleAccount } from "./utils/accounts";
|
||||||
|
|
||||||
const Stack = createStackNavigator<StackParamsList>();
|
const Stack = createStackNavigator<StackParamsList>();
|
||||||
|
|
||||||
@ -219,6 +223,51 @@ const App = (): React.JSX.Element => {
|
|||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const handleCheckBalance = async (event: MessageEvent) => {
|
||||||
|
if (event.data.type !== 'CHECK_BALANCE') return;
|
||||||
|
|
||||||
|
const { chainId, address, amount } = event.data;
|
||||||
|
const network = networksData.find(net => net.chainId === chainId);
|
||||||
|
|
||||||
|
if (!network) {
|
||||||
|
console.error('Network not found');
|
||||||
|
throw new Error('Requested network not supported.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const account = await retrieveSingleAccount(network.namespace, network.chainId, address);
|
||||||
|
if (!account) {
|
||||||
|
throw new Error('Account not found for the requested address.');
|
||||||
|
}
|
||||||
|
|
||||||
|
const cosmosPrivKey = (
|
||||||
|
await getPathKey(`${network.namespace}:${chainId}`, account.index)
|
||||||
|
).privKey;
|
||||||
|
|
||||||
|
const sender = await DirectSecp256k1Wallet.fromKey(
|
||||||
|
Buffer.from(cosmosPrivKey.split('0x')[1], 'hex'),
|
||||||
|
network.addressPrefix
|
||||||
|
);
|
||||||
|
|
||||||
|
const client = await SigningStargateClient.connectWithSigner(network.rpcUrl!, sender);
|
||||||
|
|
||||||
|
const balance = await client.getBalance(
|
||||||
|
account.address,
|
||||||
|
network.nativeDenom!.toLowerCase()
|
||||||
|
);
|
||||||
|
|
||||||
|
const areFundsSufficient = !checkSufficientFunds(amount, balance.amount);
|
||||||
|
|
||||||
|
sendMessage(event.source as Window, 'IS_SUFFICIENT', areFundsSufficient, event.origin);
|
||||||
|
};
|
||||||
|
|
||||||
|
window.addEventListener('message', handleCheckBalance);
|
||||||
|
|
||||||
|
return () => {
|
||||||
|
window.removeEventListener('message', handleCheckBalance);
|
||||||
|
};
|
||||||
|
}, [networksData]);
|
||||||
|
|
||||||
const showWalletConnect = useMemo(() => accounts.length > 0, [accounts]);
|
const showWalletConnect = useMemo(() => accounts.length > 0, [accounts]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
@ -19,7 +19,7 @@ import { createWallet, retrieveAccounts, retrieveSingleAccount } from '../utils/
|
|||||||
import AccountDetails from '../components/AccountDetails';
|
import AccountDetails from '../components/AccountDetails';
|
||||||
import styles from '../styles/stylesheet';
|
import styles from '../styles/stylesheet';
|
||||||
import DataBox from '../components/DataBox';
|
import DataBox from '../components/DataBox';
|
||||||
import { getPathKey } from '../utils/misc';
|
import { checkSufficientFunds, getPathKey, sendMessage } from '../utils/misc';
|
||||||
import { useNetworks } from '../context/NetworksContext';
|
import { useNetworks } from '../context/NetworksContext';
|
||||||
import TxErrorDialog from '../components/TxErrorDialog';
|
import TxErrorDialog from '../components/TxErrorDialog';
|
||||||
import { MEMO } from '../screens/ApproveTransfer';
|
import { MEMO } from '../screens/ApproveTransfer';
|
||||||
@ -62,22 +62,6 @@ export const WalletEmbed = () => {
|
|||||||
return accounts.map(account => account.address);
|
return accounts.map(account => account.address);
|
||||||
}, [networksData]);
|
}, [networksData]);
|
||||||
|
|
||||||
const sendMessage = (
|
|
||||||
source: Window | null,
|
|
||||||
type: string,
|
|
||||||
data: any,
|
|
||||||
origin: string
|
|
||||||
): void => {
|
|
||||||
source?.postMessage({ type, data }, origin);
|
|
||||||
};
|
|
||||||
|
|
||||||
const checkSufficientFunds = (amount: string, balance: string) => {
|
|
||||||
const amountBigNum = BigNumber.from(String(amount));
|
|
||||||
const balanceBigNum = BigNumber.from(balance);
|
|
||||||
|
|
||||||
return balanceBigNum.gt(amountBigNum);
|
|
||||||
};
|
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const handleGetAccounts = async (event: MessageEvent) => {
|
const handleGetAccounts = async (event: MessageEvent) => {
|
||||||
if (event.data.type !== 'REQUEST_WALLET_ACCOUNTS') return;
|
if (event.data.type !== 'REQUEST_WALLET_ACCOUNTS') return;
|
||||||
@ -184,7 +168,8 @@ export const WalletEmbed = () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
if (!checkSufficientFunds(amount, balance.amount)) {
|
if (!checkSufficientFunds(amount, balance.amount)) {
|
||||||
console.log("Insufficient funds detected. Throwing error.");
|
console.log("Insufficient funds detected");
|
||||||
|
sendMessage(event.source as Window, 'INSUFFICIENT_FUNDS', null, event.origin);
|
||||||
throw new Error('Insufficient funds');
|
throw new Error('Insufficient funds');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
/* Importing this library provides react native with a secure random source.
|
/* Importing this library provides react native with a secure random source.
|
||||||
For more information, "visit https://docs.ethers.org/v5/cookbook/react-native/#cookbook-reactnative-security" */
|
For more information, "visit https://docs.ethers.org/v5/cookbook/react-native/#cookbook-reactnative-security" */
|
||||||
import 'react-native-get-random-values';
|
import 'react-native-get-random-values';
|
||||||
|
import { BigNumber } from 'ethers';
|
||||||
|
|
||||||
|
import { AccountData } from '@cosmjs/amino';
|
||||||
|
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
|
||||||
|
import { stringToPath } from '@cosmjs/crypto';
|
||||||
import '@ethersproject/shims';
|
import '@ethersproject/shims';
|
||||||
|
|
||||||
import {
|
import {
|
||||||
@ -9,10 +13,6 @@ import {
|
|||||||
resetInternetCredentials,
|
resetInternetCredentials,
|
||||||
setInternetCredentials,
|
setInternetCredentials,
|
||||||
} from './key-store';
|
} from './key-store';
|
||||||
|
|
||||||
import { AccountData } from '@cosmjs/amino';
|
|
||||||
import { DirectSecp256k1HdWallet } from '@cosmjs/proto-signing';
|
|
||||||
import { stringToPath } from '@cosmjs/crypto';
|
|
||||||
import { EIP155 } from './constants';
|
import { EIP155 } from './constants';
|
||||||
import { NetworksDataState } from '../types';
|
import { NetworksDataState } from '../types';
|
||||||
|
|
||||||
@ -149,10 +149,28 @@ const resetKeyServers = async (namespace: string) => {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
const sendMessage = (
|
||||||
|
source: Window | null,
|
||||||
|
type: string,
|
||||||
|
data: any,
|
||||||
|
origin: string
|
||||||
|
): void => {
|
||||||
|
source?.postMessage({ type, data }, origin);
|
||||||
|
};
|
||||||
|
|
||||||
|
const checkSufficientFunds = (amount: string, balance: string) => {
|
||||||
|
const amountBigNum = BigNumber.from(String(amount));
|
||||||
|
const balanceBigNum = BigNumber.from(balance);
|
||||||
|
|
||||||
|
return balanceBigNum.gt(amountBigNum);
|
||||||
|
};
|
||||||
|
|
||||||
export {
|
export {
|
||||||
getMnemonic,
|
getMnemonic,
|
||||||
getPathKey,
|
getPathKey,
|
||||||
updateAccountIndices,
|
updateAccountIndices,
|
||||||
getHDPath,
|
getHDPath,
|
||||||
resetKeyServers,
|
resetKeyServers,
|
||||||
|
sendMessage,
|
||||||
|
checkSufficientFunds,
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user