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; }
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:
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? 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!!!
就像你从智能合约中读取数据一样调用它,它就会起作用
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.