forked from cerc-io/laconic-wallet
Simulate gas limit for cosmos transactions (#111)
* Simulate gas limit for cosmos transactions * Reject request if funds are not sufficient * Fix submit button disable condition * Dont estimate gas if funds are not sufficient for evm chains * Handle review changes
This commit is contained in:
parent
b1a0831e78
commit
db0b21ddd1
@ -1,3 +1,4 @@
|
|||||||
WALLET_CONNECT_PROJECT_ID=
|
WALLET_CONNECT_PROJECT_ID=
|
||||||
DEFAULT_GAS_LIMIT=
|
|
||||||
DEFAULT_GAS_PRICE=
|
DEFAULT_GAS_PRICE=
|
||||||
|
# Reference: https://github.com/cosmos/cosmos-sdk/issues/16020
|
||||||
|
DEFAULT_GAS_ADJUSTMENT=2
|
||||||
|
2
react-native-config.d.ts
vendored
2
react-native-config.d.ts
vendored
@ -2,8 +2,8 @@
|
|||||||
declare module 'react-native-config' {
|
declare module 'react-native-config' {
|
||||||
export interface NativeConfig {
|
export interface NativeConfig {
|
||||||
WALLET_CONNECT_PROJECT_ID: string;
|
WALLET_CONNECT_PROJECT_ID: string;
|
||||||
DEFAULT_GAS_LIMIT: string;
|
|
||||||
DEFAULT_GAS_PRICE: string;
|
DEFAULT_GAS_PRICE: string;
|
||||||
|
DEFAULT_GAS_ADJUSTMENT: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const Config: NativeConfig;
|
export const Config: NativeConfig;
|
||||||
|
@ -20,6 +20,7 @@ import { DirectSecp256k1Wallet } from '@cosmjs/proto-signing';
|
|||||||
import {
|
import {
|
||||||
calculateFee,
|
calculateFee,
|
||||||
GasPrice,
|
GasPrice,
|
||||||
|
MsgSendEncodeObject,
|
||||||
SigningStargateClient,
|
SigningStargateClient,
|
||||||
} from '@cosmjs/stargate';
|
} from '@cosmjs/stargate';
|
||||||
|
|
||||||
@ -38,6 +39,8 @@ import { useNetworks } from '../context/NetworksContext';
|
|||||||
import { COSMOS, EIP155 } from '../utils/constants';
|
import { COSMOS, EIP155 } from '../utils/constants';
|
||||||
import TxErrorDialog from '../components/TxErrorDialog';
|
import TxErrorDialog from '../components/TxErrorDialog';
|
||||||
|
|
||||||
|
const MEMO = 'Sending signed tx from Laconic Wallet';
|
||||||
|
|
||||||
type SignRequestProps = NativeStackScreenProps<
|
type SignRequestProps = NativeStackScreenProps<
|
||||||
StackParamsList,
|
StackParamsList,
|
||||||
'ApproveTransaction'
|
'ApproveTransaction'
|
||||||
@ -60,21 +63,54 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
const [isTxLoading, setIsTxLoading] = useState(false);
|
const [isTxLoading, setIsTxLoading] = useState(false);
|
||||||
const [cosmosStargateClient, setCosmosStargateClient] =
|
const [cosmosStargateClient, setCosmosStargateClient] =
|
||||||
useState<SigningStargateClient>();
|
useState<SigningStargateClient>();
|
||||||
const [fees, setFees] = useState('');
|
const [fees, setFees] = useState<string>();
|
||||||
const [cosmosGasLimit, setCosmosGasLimit] = useState(
|
const [cosmosGasLimit, setCosmosGasLimit] = useState<string>();
|
||||||
Config.DEFAULT_GAS_LIMIT,
|
const [txError, setTxError] = useState<string>();
|
||||||
);
|
|
||||||
const [txError, setTxError] = useState<string>('');
|
|
||||||
const [isTxErrorDialogOpen, setIsTxErrorDialogOpen] = useState(false);
|
const [isTxErrorDialogOpen, setIsTxErrorDialogOpen] = useState(false);
|
||||||
const [ethGasPrice, setEthGasPrice] = useState<string>();
|
const [ethGasPrice, setEthGasPrice] = useState<string>();
|
||||||
const [ethGasLimit, setEthGasLimit] = useState<string>();
|
const [ethGasLimit, setEthGasLimit] = useState<string>();
|
||||||
|
|
||||||
|
const isSufficientFunds = useMemo(() => {
|
||||||
|
if (!transaction.value) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!balance) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const amountBigNum = BigNumber.from(String(transaction.value));
|
||||||
|
const balanceBigNum = BigNumber.from(balance);
|
||||||
|
|
||||||
|
if (amountBigNum.gte(balanceBigNum)) {
|
||||||
|
return false;
|
||||||
|
} else {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}, [balance, transaction]);
|
||||||
|
|
||||||
const requestedNetwork = networksData.find(
|
const requestedNetwork = networksData.find(
|
||||||
networkData =>
|
networkData =>
|
||||||
`${networkData.namespace}:${networkData.chainId}` === chainId,
|
`${networkData.namespace}:${networkData.chainId}` === chainId,
|
||||||
);
|
);
|
||||||
const namespace = requestedNetwork!.namespace;
|
const namespace = requestedNetwork!.namespace;
|
||||||
|
|
||||||
|
const sendMsg: MsgSendEncodeObject = useMemo(() => {
|
||||||
|
return {
|
||||||
|
typeUrl: '/cosmos.bank.v1beta1.MsgSend',
|
||||||
|
value: {
|
||||||
|
fromAddress: transaction.from,
|
||||||
|
toAddress: transaction.to,
|
||||||
|
amount: [
|
||||||
|
{
|
||||||
|
amount: String(transaction.value),
|
||||||
|
denom: requestedNetwork!.nativeDenom!,
|
||||||
|
},
|
||||||
|
],
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}, [requestedNetwork, transaction]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (namespace !== COSMOS) {
|
if (namespace !== COSMOS) {
|
||||||
return;
|
return;
|
||||||
@ -149,6 +185,11 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
const gasPrice = GasPrice.fromString(
|
const gasPrice = GasPrice.fromString(
|
||||||
requestedNetwork?.gasPrice! + requestedNetwork?.nativeDenom,
|
requestedNetwork?.gasPrice! + requestedNetwork?.nativeDenom,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
if (!cosmosGasLimit) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const cosmosFees = calculateFee(Number(cosmosGasLimit), gasPrice);
|
const cosmosFees = calculateFee(Number(cosmosGasLimit), gasPrice);
|
||||||
|
|
||||||
setFees(cosmosFees.amount[0].amount);
|
setFees(cosmosFees.amount[0].amount);
|
||||||
@ -174,7 +215,6 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
|
|
||||||
try {
|
try {
|
||||||
const response = await approveWalletConnectRequest({
|
const response = await approveWalletConnectRequest({
|
||||||
networksData,
|
|
||||||
requestEvent,
|
requestEvent,
|
||||||
account,
|
account,
|
||||||
namespace,
|
namespace,
|
||||||
@ -185,11 +225,11 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
// This amount is total fees required for transaction
|
// This amount is total fees required for transaction
|
||||||
amount: [
|
amount: [
|
||||||
{
|
{
|
||||||
amount: fees,
|
amount: fees!,
|
||||||
denom: requestedNetwork!.nativeDenom!,
|
denom: requestedNetwork!.nativeDenom!,
|
||||||
},
|
},
|
||||||
],
|
],
|
||||||
gas: cosmosGasLimit,
|
gas: cosmosGasLimit!,
|
||||||
},
|
},
|
||||||
ethGasLimit: transaction.gasLimit
|
ethGasLimit: transaction.gasLimit
|
||||||
? String(transaction.gasLimit)
|
? String(transaction.gasLimit)
|
||||||
@ -197,6 +237,8 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
ethGasPrice: transaction.gasPrice
|
ethGasPrice: transaction.gasPrice
|
||||||
? String(transaction.gasPrice)
|
? String(transaction.gasPrice)
|
||||||
: ethGasPrice,
|
: ethGasPrice,
|
||||||
|
sendMsg,
|
||||||
|
memo: MEMO,
|
||||||
});
|
});
|
||||||
|
|
||||||
const { topic } = requestEvent;
|
const { topic } = requestEvent;
|
||||||
@ -273,6 +315,10 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!isSufficientFunds) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
if (!provider) {
|
if (!provider) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -296,7 +342,39 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
getEthGas();
|
getEthGas();
|
||||||
}, [provider, transaction]);
|
}, [provider, transaction, isSufficientFunds]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
const getCosmosGas = async () => {
|
||||||
|
if (!cosmosStargateClient) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (!isSufficientFunds) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const gasEstimation = await cosmosStargateClient.simulate(
|
||||||
|
transaction.from!,
|
||||||
|
[sendMsg],
|
||||||
|
MEMO,
|
||||||
|
);
|
||||||
|
|
||||||
|
setCosmosGasLimit(
|
||||||
|
String(
|
||||||
|
Math.round(gasEstimation * Number(Config.DEFAULT_GAS_ADJUSTMENT)),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
getCosmosGas();
|
||||||
|
}, [cosmosStargateClient, isSufficientFunds, sendMsg, transaction]);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (balance && !isSufficientFunds) {
|
||||||
|
setTxError('Insufficient funds');
|
||||||
|
setIsTxErrorDialogOpen(true);
|
||||||
|
}
|
||||||
|
}, [isSufficientFunds, balance]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
@ -384,7 +462,7 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
mode="contained"
|
mode="contained"
|
||||||
onPress={acceptRequestHandler}
|
onPress={acceptRequestHandler}
|
||||||
loading={isTxLoading}
|
loading={isTxLoading}
|
||||||
disabled={!balance && !cosmosStargateClient}>
|
disabled={!balance || !fees}>
|
||||||
{isTxLoading ? 'Processing' : 'Yes'}
|
{isTxLoading ? 'Processing' : 'Yes'}
|
||||||
</Button>
|
</Button>
|
||||||
<Button
|
<Button
|
||||||
@ -397,9 +475,15 @@ const ApproveTransaction = ({ route }: SignRequestProps) => {
|
|||||||
</>
|
</>
|
||||||
)}
|
)}
|
||||||
<TxErrorDialog
|
<TxErrorDialog
|
||||||
error={txError}
|
error={txError!}
|
||||||
visible={isTxErrorDialogOpen}
|
visible={isTxErrorDialogOpen}
|
||||||
hideDialog={() => setIsTxErrorDialogOpen(false)}
|
hideDialog={() => {
|
||||||
|
setIsTxErrorDialogOpen(false);
|
||||||
|
if (!isSufficientFunds) {
|
||||||
|
rejectRequestHandler();
|
||||||
|
navigation.navigate('Laconic');
|
||||||
|
}
|
||||||
|
}}
|
||||||
/>
|
/>
|
||||||
</>
|
</>
|
||||||
);
|
);
|
||||||
|
@ -4,16 +4,19 @@ import { Wallet, providers } from 'ethers';
|
|||||||
import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils';
|
import { formatJsonRpcError, formatJsonRpcResult } from '@json-rpc-tools/utils';
|
||||||
import { SignClientTypes } from '@walletconnect/types';
|
import { SignClientTypes } from '@walletconnect/types';
|
||||||
import { getSdkError } from '@walletconnect/utils';
|
import { getSdkError } from '@walletconnect/utils';
|
||||||
import { SigningStargateClient, coins, StdFee } from '@cosmjs/stargate';
|
import {
|
||||||
|
SigningStargateClient,
|
||||||
|
StdFee,
|
||||||
|
MsgSendEncodeObject,
|
||||||
|
} from '@cosmjs/stargate';
|
||||||
|
|
||||||
import { EIP155_SIGNING_METHODS } from './EIP155Data';
|
import { EIP155_SIGNING_METHODS } from './EIP155Data';
|
||||||
import { signDirectMessage, signEthMessage } from '../sign-message';
|
import { signDirectMessage, signEthMessage } from '../sign-message';
|
||||||
import { Account, NetworksDataState } from '../../types';
|
import { Account } from '../../types';
|
||||||
import { getMnemonic, getPathKey } from '../misc';
|
import { getMnemonic, getPathKey } from '../misc';
|
||||||
import { getCosmosAccounts } from '../accounts';
|
import { getCosmosAccounts } from '../accounts';
|
||||||
|
|
||||||
export async function approveWalletConnectRequest({
|
export async function approveWalletConnectRequest({
|
||||||
networksData,
|
|
||||||
requestEvent,
|
requestEvent,
|
||||||
account,
|
account,
|
||||||
namespace,
|
namespace,
|
||||||
@ -23,8 +26,9 @@ export async function approveWalletConnectRequest({
|
|||||||
cosmosFee,
|
cosmosFee,
|
||||||
ethGasLimit,
|
ethGasLimit,
|
||||||
ethGasPrice,
|
ethGasPrice,
|
||||||
|
sendMsg,
|
||||||
|
memo,
|
||||||
}: {
|
}: {
|
||||||
networksData: NetworksDataState[];
|
|
||||||
requestEvent: SignClientTypes.EventArguments['session_request'];
|
requestEvent: SignClientTypes.EventArguments['session_request'];
|
||||||
account: Account;
|
account: Account;
|
||||||
namespace: string;
|
namespace: string;
|
||||||
@ -34,15 +38,12 @@ export async function approveWalletConnectRequest({
|
|||||||
cosmosFee?: StdFee;
|
cosmosFee?: StdFee;
|
||||||
ethGasLimit?: string;
|
ethGasLimit?: string;
|
||||||
ethGasPrice?: string;
|
ethGasPrice?: string;
|
||||||
|
sendMsg?: MsgSendEncodeObject;
|
||||||
|
memo?: string;
|
||||||
}) {
|
}) {
|
||||||
const { params, id } = requestEvent;
|
const { params, id } = requestEvent;
|
||||||
const { request } = params;
|
const { request } = params;
|
||||||
|
|
||||||
const requestChainId = requestEvent.params.chainId;
|
|
||||||
const requestedChain = networksData.find(
|
|
||||||
networkData => networkData.chainId === requestChainId.split(':')[1],
|
|
||||||
);
|
|
||||||
|
|
||||||
const path = (await getPathKey(`${namespace}:${chainId}`, account.index))
|
const path = (await getPathKey(`${namespace}:${chainId}`, account.index))
|
||||||
.path;
|
.path;
|
||||||
const mnemonic = await getMnemonic();
|
const mnemonic = await getMnemonic();
|
||||||
@ -128,12 +129,6 @@ export async function approveWalletConnectRequest({
|
|||||||
});
|
});
|
||||||
|
|
||||||
case 'cosmos_sendTokens':
|
case 'cosmos_sendTokens':
|
||||||
const amount = coins(
|
|
||||||
request.params[0].value,
|
|
||||||
requestedChain!.nativeDenom!,
|
|
||||||
);
|
|
||||||
const receiverAddress = request.params[0].to;
|
|
||||||
|
|
||||||
if (!(provider instanceof SigningStargateClient)) {
|
if (!(provider instanceof SigningStargateClient)) {
|
||||||
throw new Error('Cosmos stargate client not found');
|
throw new Error('Cosmos stargate client not found');
|
||||||
}
|
}
|
||||||
@ -142,12 +137,15 @@ export async function approveWalletConnectRequest({
|
|||||||
throw new Error('Cosmos fee not found');
|
throw new Error('Cosmos fee not found');
|
||||||
}
|
}
|
||||||
|
|
||||||
const result = await provider.sendTokens(
|
if (!sendMsg) {
|
||||||
|
throw new Error('Message to be sent not found');
|
||||||
|
}
|
||||||
|
|
||||||
|
const result = await provider.signAndBroadcast(
|
||||||
address,
|
address,
|
||||||
receiverAddress,
|
[sendMsg],
|
||||||
amount,
|
|
||||||
cosmosFee,
|
cosmosFee,
|
||||||
'Sending signed tx from Laconic Wallet',
|
memo,
|
||||||
);
|
);
|
||||||
|
|
||||||
return formatJsonRpcResult(id, {
|
return formatJsonRpcResult(id, {
|
||||||
|
Loading…
Reference in New Issue
Block a user