我想知道send()和sendTransaction()在web3中的区别。以下列测试代码为例:
合同:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract TestError {
constructor() {}
function method1(uint a) external pure {
if (a <= 100) revert("value too small");
}
}测试文件
it('common-test', async () => {
let txHash = null
let accounts = await web3.eth.getAccounts()
let TestError = artifacts.require("TestError")
let TestErrorDeployed = await TestError.deployed()
let contract = new web3.eth.Contract(
TestError.abi,
TestErrorDeployed.address
)
await contract.methods.method1(10).send({
from: accounts[0]
}).on('transactionHash', (txHash) => {
console.log("txHash: "+ txHash)
}).on('error', (error) => {
console.log("error: " + error)
})
})我可以用TestErrorDeployed.methods['method1(uint)'].sendTransaction()或contract.methods.method1(ARG).send()打电话给D3。
有什么关系呢?
发布于 2022-07-13 02:57:55
从TestError.deployed()在代码中的使用来看,您似乎是在混合工作组和web3。
因为sendTransaction是一种低级的原语。通常不会被用户调用。相反,直接调用方法:instance.setValue(5),而不是instance.setValue(5).sendTransaction()。参见这里的预期用法:https://github.com/trufflesuite/truffle/tree/v5.5.21/packages/contract#making-a-transaction-via-a-contract-function。
Web3js是一个更冗长的instance.methods.setValue(5).send()。在创建事务并发送到块链的情况下,行为完全相同,返回一个PromiEvent,这样您就可以等待事务被挖掘。
web3js还有另一个函数sendTransaction(),它直接与地址一起工作。例如,当您想要在帐户之间进行以太转移时。
https://ethereum.stackexchange.com/questions/131508
复制相似问题