- React
- React Native
- Flutter
Copy
Ask AI
import { FC, FormEventHandler, useState } from "react";
import { parseEther } from "viem";
import { useDynamicContext } from "@dynamic-labs/sdk-react-core";
import { isEthereumWallet } from "@dynamic-labs/ethereum";
export const SendTransactionSection: FC = () => {
const { primaryWallet } = useDynamicContext();
const [txnHash, setTxnHash] = useState("");
if (!primaryWallet || !isEthereumWallet(primaryWallet)) return null;
const onSubmit: FormEventHandler<HTMLFormElement> = async (event) => {
event.preventDefault();
const formData = new FormData(event.currentTarget);
const address = formData.get("address") as string;
const amount = formData.get("amount") as string;
const publicClient = await primaryWallet.getPublicClient();
const walletClient = await primaryWallet.getWalletClient();
const transaction = {
to: address,
value: amount ? parseEther(amount) : undefined,
};
const hash = await walletClient.sendTransaction(transaction);
setTxnHash(hash);
const receipt = await publicClient.getTransactionReceipt({
hash,
});
console.log(receipt);
};
return (
<form onSubmit={onSubmit}>
<p>Send to ETH address</p>
<input name="address" type="text" required placeholder="Address" />
<input name="amount" type="text" required placeholder="0.05" />
<button type="submit">Send</button>
<span data-testid="transaction-section-result-hash">{txnHash}</span>
</form>
);
};
React Native
Copy
Ask AI
// Requires setting up the RN Viem extension
// See: /react-native/wallets/viem
import { dynamicClient } from '<path-to-your-dynamicClient>'; // extended with ViemExtension
import { parseEther } from 'viem'
export const sendTransaction = async (to: `0x${string}`, amountEth: string) => {
const wallet = dynamicClient.wallets.primary;
if (!wallet) return;
const publicClient = dynamicClient.viem.createPublicClient({ chain: { id: await wallet.getNetwork() } as any })
const walletClient = dynamicClient.viem.createWalletClient({ wallet });
const hash = await walletClient.sendTransaction({
to,
value: parseEther(amountEth),
});
const receipt = await publicClient.getTransactionReceipt({ hash });
console.log(receipt);
return hash;
}
Copy
Ask AI
final network = await DynamicSDK.instance.wallets.getNetwork(
wallet: widget.wallet,
);
final client = DynamicSDK.instance.web3dart.createPublicClient(
chainId: network.intValue()!,
);
final gasPrice = await client.getGasPrice();
final maxFeePerGas =
gasPrice.getValueInUnitBI(EtherUnit.wei) * BigInt.from(2);
final maxPriorityFeePerGas = gasPrice.getValueInUnitBI(EtherUnit.wei);
final amountInWei =
(double.parse(amount) * BigInt.from(10).pow(18).toDouble()).toInt();
final transaction = Transaction(
to: EthereumAddress.fromHex(
recipientAddress,
),
maxGas: 21000,
// gasPrice: gasPrice,
from: EthereumAddress.fromHex(widget.wallet.address),
maxFeePerGas: EtherAmount.inWei(
maxFeePerGas,
),
maxPriorityFeePerGas: EtherAmount.inWei(
maxPriorityFeePerGas,
),
value: EtherAmount.inWei(
BigInt.from(amountInWei),
),
);
final response = await DynamicSDK.instance.web3dart.sendTransaction(
transaction: transaction,
wallet: widget.wallet,
);
Simulate a Transaction
Before sending a transaction, you can simulate it to preview the effects and validate that it will succeed. Transaction simulation shows all asset transfers involved in the transaction.Transaction simulation uses the currently selected primary wallet. Make sure the user has a wallet connected before attempting to simulate a transaction.
- React Native
React Native
Copy
Ask AI
import { dynamicClient } from '<path-to-your-dynamicClient>';
import { parseEther } from 'viem';
const simulateEvmTransaction = async (to: `0x${string}`, amountEth: string) => {
const simulationResult = await dynamicClient.wallets.simulateEVMTransaction({
transaction: {
from: dynamicClient.wallets.primary.address,
to,
value: parseEther(amountEth),
data: '0x',
},
type: 'SignTransaction',
});
console.log('Simulation result:', simulationResult);
return simulationResult;
};