PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, September 25, 2022

[FIXED] How to send variable amount of ether to smart contract from React front end?

 September 25, 2022     blockchain, ethereum, reactjs, smartcontracts, solidity     No comments   

Issue

I am trying to send a variable amount of ether from my React front end to my smart contract. In remix, I can do this no problem by just selecting the amount and sending it with the function

In my front end, this is the function where values.amount is 100wei

const sendEth = async(e) => {
  e.preventDefault()
  try {
    const { ethereum } = window;

    if (ethereum) {
      const provider = new ethers.providers.Web3Provider(ethereum);
      const signer = provider.getSigner();
      const connectedContract = new ethers.Contract(CONTRACT_ADDRESS, escrowAbi.abi, signer);
      let nftTxn = await connectedContract.depositEth(values.amount);
        
      console.log("Mining...please wait.", nftTxn)
      await nftTxn.wait();
      
      console.log(`Mined, see transaction: https://rinkeby.etherscan.io/tx/${nftTxn.hash}`);
      // console.log(connectedContract)

    } else {
      console.log("Ethereum object doesn't exist!");
    }
  } catch (error) {
    console.log(error)
  }

}

In my smart contract this is my depositEth function - however msg.value is argument i want to pass but I can't pass this as an argument to this function?

FYI in my app, once you they pay eth to the contract it will release an NFT.

 function depositEth() public payable hasToken(address(this), nftAddress)  {
        require(msg.value == amountOwed, 'You ow more money');
        buyerAddress = payable(msg.sender);

        if(walletHoldsToken(address(this),nftAddress)) {
         ERC721(nftAddress).safeTransferFrom(address(this), buyerAddress, tokenID);
        }
    }

So what I am asking is how do I send x amount of eth to a contract with that value defined in the front end?


Solution

In your smart contract define a function to return the amount that owed:

function getOwedAmount() public view returns (uint256) {
    // i  am assuming " amountOwed" is state variable, uint256
    return amountOwed;
  }

After creating the contract.

const connectedContract = new ethers.Contract(CONTRACT_ADDRESS, escrowAbi.abi, signer)

get the owned amount

let owedAmount = await contract.getOwedAmount();
owedAmount=owedAmount.toString()
let transaction=await connectedContract.depositEth({value:owedAmount})
await transaction.wait();

since depositEth is payable we can pass last argument as an object specifying how much we send and solidity will automatically assign that amount to msg.value



Answered By - Yilmaz
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing