简体   繁体   English

(ethereum/solidity/truffle) 从测试/客户问题调用智能合约方法

[英](ethereum/solidity/truffle) calling smart contract method from test/client question

I am taking a udemy course and I encounter a code like this我正在上 udemy 课程,遇到这样的代码

https://github.com/acloudfan/Blockchain-Course-Basic-Solidity/blob/93ca256bcf8c436c144425291257dcff5c3b269f/test/constants_payable.js#L45 https://github.com/acloudfan/Blockchain-Course-Basic-Solidity/blob/93ca256bcf8c436c144425291257dcff5c3b269f/test/constants_payable.js#L45

I am confuse why the call to a method is called directly instead of using.call or something, wherein if I do google, the way to call a method of a contract is either using.call or.send but at this point the author just calls it directly, is this allowed, why?我很困惑为什么直接调用方法而不是 using.call 或其他方法,其中如果我使用 google,调用合同方法的方法是 using.call 或 .send 但此时作者只是直接调用,这样可以吗,为什么?

here is the contract code https://github.com/acloudfan/Blockchain-Course-Basic-Solidity/blob/master/contracts/ConstantsPayable.sol这里是合约代码https://github.com/acloudfan/Blockchain-Course-Basic-Solidity/blob/master/contracts/ConstantsPayable.sol

More or less, what is the context of calling smart contract method from a truffle test here?或多或少,在这里从松露测试调用智能合约方法的上下文是什么? is it like the real environment where it waits for the transaction to be mined before returning or do tests just directly calls it like an ordinary function?是不是像真实环境一样等待交易被挖掘后再返回,还是像普通的 function 一样直接调用它进行测试?

I am posting it here since the author of the udemy course is non responsive and its almost a week and more than a dozen Q&A question are not answered, so the author probably is busy or forgets about the course already (as it is kinda old course but reviewed well).我把它贴在这里是因为 udemy 课程的作者没有回应,而且它将近一周和十几个问答问题没有得到回答,所以作者可能很忙或者已经忘记了这门课程(因为它有点老了)但审查得很好)。

Before Truffle returns the contract instance ( line 41 ), it uses the ABI interface (provided by the Solidity compiler) to build a map of JS functions for interacting with the contract, including receiveEthers() .在 Truffle 返回合约instance第 41 行)之前,它使用 ABI 接口(由 Solidity 编译器提供)构建一个 map 的 JS 函数用于与合约交互,包括receiveEthers()

what is the context of calling smart contract method from a truffle test here从这里的松露测试调用智能合约方法的上下文是什么

Even though Truffle JS tests can be connected to a public testnet or mainnet, it's usually used together with another Truffle tool - local EVM and blockchain emulator called Ganache (see the config file where the author defines connection to a local blockchain).尽管 Truffle JS 测试可以连接到公共测试网或主网,但它通常与另一个 Truffle 工具 - 本地 EVM 和名为Ganache的区块链模拟器一起使用(参见配置文件,其中作者定义了与本地区块链的连接)。 By default, Ganache mines a block after each transaction so that you (as a developer or a tester) don't need to worry about mining and other processes in setting up the network, and the response from the local blockchain it returned almost instantly.默认情况下,Ganache 在每笔交易后挖掘一个区块,因此您(作为开发人员或测试人员)无需担心设置网络时的挖掘和其他过程,并且本地区块链的响应几乎立即返回。

if I do google, the way to call a method of a contract is either using.call or.send如果我用谷歌搜索,调用合约方法的方法是 using.call 或 .send

Answering only about Truffle.只回答松露。 Other packages such as Web3js or Ethers.js might have slightly different rules. Web3js 或 Ethers.js 等其他包的规则可能略有不同。 And there are .call() and .send() methods in Solidity (for interacting with other contracts or addresses), that also behave differently than explained here: Solidity 中有 .call( .call().send()方法(用于与其他合约或地址交互),它们的行为也与此处解释的不同:

You can interact with a contract in two different ways:您可以通过两种不同的方式与合约交互:

  • transactions (can make state changes - change contract storage, emit events)交易(可以进行 state 更改 - 更改合约存储,发出事件)
  • calls (only read the contract data - no state changes)调用(仅读取合约数据 - 没有 state 更改)

By default, if you don't specify whether you want to make a transaction or a call, Truffle makes a transaction .默认情况下,如果您不指定是要进行事务还是调用,Truffle 会进行事务 You can override this decision and make a call instead by using the .call() method.您可以使用.call()方法覆盖此决定并进行调用

The .send() method is only used for low-level built transactions. .send()方法仅用于低级构建事务。 A common use case is sending ETH - you need to build the transaction data field, fill the (ETH) value , and call the .send() method (assuming you have configured Truffle to use your private key to sign the transaction).一个常见的用例是发送 ETH - 您需要构建交易data字段,填写 (ETH) value ,并调用.send()方法(假设您已将 Truffle 配置为使用您的私钥来签署交易)。

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

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