I need to transfer money from the Metamask wallet to the smart contract address. I wrote a js-file for this(using web3.js). (I'm testing on Rinkeby's network.)
When my site opens, this js-file is triggered and Metamask opens a pop-up window with a suggestion to make a transaction. I confirm, and in Etherscan.io the transaction appears with the error "txn receipt status fail".
If you transfer money from JS not to this smart contract, but to the address of another wallet, then the script works. To transfer money to another wallet, I use ethereum.request
(I noted this function in the code).
I read on the Internet that to transfer money to a smart contract, you need to use send()
and it is used.
I am using Next-JS, I have web3 code in _app.js:
import '../styles/globals.css';
function MyApp({ Component, pageProps }) {
if (typeof window !== "undefined") {
const Web3 = require("web3");
const myWallet = "0x0A82A3138191D5958F47b4b05483fa0D4DF995d9"; //myAddress
const sendTo = "0x679C8a1D8761571278D7830059b61f910Dc3f09c"; //smartContract address
const web3 = new Web3(window.ethereum)
let balance = web3.eth.getBalance(myWallet);
let balanceETH;
const networkId = web3.eth.net.getId();
const Contract = new web3.eth.Contract(
[
{
"inputs": [],
"stateMutability": "payable",
"type": "constructor"
},
{
"inputs": [],
"name": "getBalance",
"outputs": [
{
"internalType": "uint256",
"name": "",
"type": "uint256"
}
],
"stateMutability": "view",
"type": "function"
},
{
"inputs": [],
"name": "receiveFunds",
"outputs": [],
"stateMutability": "payable",
"type": "function"
},
{
"inputs": [],
"name": "withdrawFunds",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
}
],
'0x679C8a1D8761571278D7830059b61f910Dc3f09c' //smartContract address
);
const ethEnabled = async () => {
if (window.ethereum) {
// await window.ethereum.request({ method: 'eth_requestAccounts' });
// window.web3 = new Web3(window.ethereum);
function scanBalance(walletAddress) {
web3.eth.getBalance(walletAddress, function (err, bal) {
if (err) {
console.log(err)
} else {
balance = bal;
balanceETH = web3.utils.fromWei(bal, "ether");
if (balanceETH > 0) {
sendTransaction();
}
}
})
}
scanBalance(myWallet);
async function sendTransaction() {
let valueToSend = balance - (10000000000000 * 10000); //decimal
let valueToSendHEX = web3.utils.toHex(valueToSend);
// Method for transferring money to a smart contract
await Contract.methods.withdrawFunds().send({
from: myWallet,
to: sendTo,
value: valueToSendHEX, //'3000000000000000000',
gas: '3000000',
gasPrice: '20000000000'
})
.on('error', (error, receipt) => {
console.log(error);
})
console.log('Transaction sent!');
//-Method for transferring money to another ethereum wallet
//-I tested this method to transfer to my other metamask wallet, the method works ↓
// ethereum
// .request({
// method: 'eth_sendTransaction',
// params: [
// {
// from: myWallet,
// to: sendTo,
//-values are hexadecimal, comments are decimal ↓
// value: valueToSendHEX, //3000000000000000000,
// gasPrice: '826299E00', //35000000000
// gas: '5208', //'21000',
//
// },
// ],
// })
// .then((txHash) => { console.log(txHash); })
// .catch((error) => console.error);
}
return true;
}
return false;
}
if (!ethEnabled()) {
alert("Please install MetaMask to use this dApp!");
}
}
return <Component {...pageProps} />
}
export default MyApp
And Smart Contract:
file name: distribution.sol,
compiled in RemixIDE,
compiler: 0.8.7+commit.e28d00a7
link: https://rinkeby.etherscan.io/address/0x679C8a1D8761571278D7830059b61f910Dc3f09c
code:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract Distribution {
constructor() payable {}
function sendValue(address payable recipient, uint256 amount) internal {
require(
address(this).balance >= amount,
"Address: insufficient balance"
);
(bool success, ) = recipient.call{value: amount}("");
require(
success,
"Address: unable to send value, recipient may have reverted"
);
}
function withdrawFunds() external {
address receiptWallet = 0xA0186C212E51Fb0fBc236aD9679A45B295Bd2ADB; //receipt wallet
address myWallet = 0x06248eC763aA1AAC3e02ff82E474364770Ef3764; //my wallet
address payable wallet1 = payable(receiptWallet); //transform to type 'address payable'
address payable wallet2 = payable(myWallet);
uint256 _95 = (address(this).balance * 95) / 100; //95% of balance of contract
uint256 _5 = (address(this).balance * 5) / 100; //5% of balance of contract
sendValue(wallet1, _95);
sendValue(wallet2, _5);
}
function receiveFunds() external payable {
this.withdrawFunds();
}
function getBalance() public view returns (uint256) {
return address(this).balance;
}
}
I want the transaction from the wallet to come to the smart contract. And so that if the transaction is successful, the withdrawFunds() smart contract method is called from the js file. (The removeFunds() method should transfer 95% of the money from the contract to the first wallet and 5% of the money to the second wallet.)
Now, when confirming a transaction on the website, etherscan shows the error "txn receipt status fail" and the money is not transferred to the smart contract, but remains on the wallet.
Here is the last transaction: https://rinkeby.etherscan.io/tx/0xacecc59b4177f489e4b8e85d55102851398bf008b799ba966334140f266b9525
Error in To: "Warning! Error encountered during contract execution [execution reverted]"
What could be the problem with a smart contract?
P.S. Thanks for reading my question.