简体   繁体   English

来自前端问题的智能合约交互

[英]Smart contract interaction from front-end issue

Any help will be much appreciated:任何帮助都感激不尽:

Am building the front end for my smart contract.正在为我的智能合约构建前端。 Was able not only to receive data but also to send data to the smart contract from front-end.不仅能够接收数据,还能够从前端向智能合约发送数据。

The way I called data from my smart contract:我从智能合约中调用数据的方式:

Solidity function: function getBalance() public view returns(uint256) { return address(this).balance; } Solidity 函数: function getBalance() public view returns(uint256) { return address(this).balance; } function getBalance() public view returns(uint256) { return address(this).balance; }

My front-end code to call the above function:我调用上述函数的前端代码:

const getBalance = async () => {
    setBalance((prevState) => ({
      ...prevState,
      loading: true,
    }))
    info.contract.methods
      .getBalance()
      .call()
      .then((res) => {
        console.log(res)
        setBalance({
          loading: false,
          value: res,
        })
      })
      .catch((err) => {
        console.log(err)
        setBalance(initBalanceState)
      })
  }

The way I sent data to my smart contract我将数据发送到智能合约的方式

Solidity code:坚固性代码:

function plantSeeds(address ref) public payable {
        require(initialized);
        uint256 seedsBought = calculateSeedBuy(msg.value,SafeMath.sub(address(this).balance,msg.value));
        seedsBought = SafeMath.sub(seedsBought,devFee(seedsBought));
        uint256 fee = devFee(msg.value);
        recAddr.transfer(fee);
        claimedSeeds[msg.sender] = SafeMath.add(claimedSeeds[msg.sender],seedsBought);
        replantSeeds(ref);
    }

My front-end code to send eth to the above function:我将 eth 发送到上述函数的前端代码:

const plantSeeds = () => {
    let web3 = new Web3(window.ethereum)
    info.contract.methods
      .plantSeeds(ref)
      .send({
        from: info.account,
        value: web3.utils.toWei(num, 'ether'),
      })
      .then((res) => {
        console.log(res)
        
      })
      .catch((err) => {
        console.log(err)
        
      })
  }

So, I was able to read data and send data to the smart contract without problem.因此,我能够毫无问题地读取数据并将数据发送到智能合约。 BUT, How do you call a withdrawable function?但是,您如何调用可撤回功能? I need to send funds from Smart contract to the wallet.我需要将资金从智能合约发送到钱包。 from etherscan/write section can do it, it's working, but how to do it from website front end?从 etherscan/write 部分可以做到,它正在工作,但是如何从网站前端做到这一点? Any help?有什么帮助吗?

here is the solidity code am trying to initiate from the front-end:这是我试图从前端启动的可靠代码:

function harvestSeeds() public {
        require(initialized);
        uint256 hasSeeds = getMySeeds(msg.sender);
        uint256 seedValue = calculateSeedSell(hasSeeds);
        uint256 fee = devFee(seedValue);
        claimedSeeds[msg.sender] = 0;
        lastPlanted[msg.sender] = block.timestamp;
        marketSeeds = SafeMath.add(marketSeeds,hasSeeds);
        recAddr.transfer(fee);
        payable (msg.sender).transfer(SafeMath.sub(seedValue,fee));
    }

Thanks a lot!!!非常感谢!!!

就像你从智能合约中读取数据一样调用它,它就会起作用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM