Skip to content

Instantly share code, notes, and snippets.

@Josh4324
Last active May 10, 2022 13:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Josh4324/cff6ae4f379a4dd1ace7f1444f091463 to your computer and use it in GitHub Desktop.
Save Josh4324/cff6ae4f379a4dd1ace7f1444f091463 to your computer and use it in GitHub Desktop.
const sendEthToAnotherAddr = async () => {
setError("");
setMessage("");
if (walletRef.current.value === "") {
return setError("Please enter your wallet address");
}
if (walletRef.current.value.length !== 42) {
return setError("Please enter a valid wallet address");
}
if (ethers.utils.isAddress(walletRef.current.value) === false) {
return setError("Please enter a valid wallet address");
}
if (etherRef.current.value === "") {
return setError("Please enter ether amount");
}
if (etherRef.current.value <= 0) {
return setError("You cant send zero or less than zero ethers");
}
setLoading(true);
const provider = await getProviderOrSigner();
const signer = provider.getSigner();
const paymentContract = new ethers.Contract(
contractAddress,
contractABI,
signer
);
const walletAddress = walletRef.current.value;
const ethAmount = etherRef.current.value;
try {
const Txn = await paymentContract.sendPayment(walletAddress, {
gasLimit: 300000,
value: ethers.utils.parseEther(ethAmount),
});
await Txn.wait();
setLoading(false);
setMessage("Payment was successful");
const prov = await getProviderOrSigner();
const bal = await prov.getBalance(address);
setBalance(Number(BigNumber.from(bal)) / 10 ** 18);
walletRef.current.value = "";
etherRef.current.value = "";
} catch (error) {
if (error.code) {
console.log(error.toString);
setError(error.message);
} else {
setError(error.toString());
console.log(error.toString());
}
setLoading(false);
walletRef.current.value = "";
etherRef.current.value = "";
}
};
const sendEthToContract = async () => {
setError1("");
setMessage1("");
if (etherAmountRef.current.value === "") {
return setError1("Please enter ether amount");
}
if (etherAmountRef.current.value <= 0) {
return setError1("You cant send zero or less than zero ethers");
}
setLoading1(true);
const provider = await getProviderOrSigner();
const signer = provider.getSigner();
const paymentContract = new ethers.Contract(
contractAddress,
contractABI,
signer
);
const ethAmount = etherAmountRef.current.value;
try {
const Txn = await paymentContract.sendToContract({
gasLimit: 300000,
value: ethers.utils.parseEther(ethAmount),
});
await Txn.wait();
setLoading1(false);
setMessage1("Payment was successful");
const bal = await provider.getBalance(address);
setBalance(Number(BigNumber.from(bal)) / 10 ** 18);
walletRef.current.value = "";
etherRef.current.value = "";
} catch (error) {
if (error.code) {
console.log(error.toString);
setError1(error.message);
} else {
setError1(error.toString());
console.log(error.toString());
}
setLoading1(false);
walletRef.current.value = "";
etherRef.current.value = "";
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment