如有任何帮助,我们将不胜感激:
我正在为我的智能合同建立前端。不仅能够接收数据,而且能够从前端向智能契约发送数据。
--我调用智能合同中的数据的方式:
固体函数:function getBalance() public view returns(uint256) { return address(this).balance; }
调用上述函数的前端代码:
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)
})
}--我将数据发送给智能契约的方式
稳固代码:
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);
}我的前端代码将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)
})
}因此,我能够没有问题地读取数据并将数据发送到智能契约。但是,你怎么叫可取款函数呢?我需要把资金从智能合同寄到钱包里。从以太扫描/写部分可以做到,它是可行的,但如何从网站前端做呢?有什么帮助吗?
下面是正在尝试从前端启动的稳固代码:
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));
}非常感谢!
发布于 2022-05-26 18:45:35
就像读取智能契约中的数据一样调用它,它就会工作。
https://stackoverflow.com/questions/72376901
复制相似问题