首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >send()和sendTransaction()之间的区别

send()和sendTransaction()之间的区别
EN

Ethereum用户
提问于 2022-07-10 11:06:40
回答 1查看 212关注 0票数 1

我想知道send()和sendTransaction()在web3中的区别。以下列测试代码为例:

合同:

代码语言:javascript
复制
// 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");
    }
}

测试文件

代码语言:javascript
复制
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

有什么关系呢?

EN

回答 1

Ethereum用户

回答已采纳

发布于 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(),它直接与地址一起工作。例如,当您想要在帐户之间进行以太转移时。

票数 2
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/131508

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档