简体   繁体   English

Web3js 智能合约交互 Gas 和签名

[英]Web3js smart contract interaction gas and signing

So I can interact with a function within a smart contract.所以我可以在智能合约中与 function 进行交互。 I also know that to actually do the transaction I need something like this:我也知道要真正进行交易,我需要这样的东西:

const contractObj = new web3.eth.Contract(abi, contractAddress)

const transaction= await contractObj.methods.NameofTheMethod(x).send({from:myAddress,gasLimit:value})

My question is: what is the next step?我的问题是:下一步是什么? Because I put myAddress but I should sign this before send the transaction right?因为我放了我的地址,但我应该在发送交易之前签名,对吗?

Otherwise how will it be able to take the fee from the gas?否则怎么能从gas中收取费用呢?

You can pass the private key of the myAddress to the web3 instance using the wallet.add() method ( docs ).您可以使用wallet.add()方法 ( docs ) 将myAddress的私钥传递给 web3 实例。 Web3 then signs the transaction before it's sent to a public node.然后,Web3 在将交易发送到公共节点之前对其进行签名。

During local development, most node emulators (such as Ganache and Hardhat) contain private keys to the generated addresses.在本地开发过程中,大多数节点模拟器(例如 Ganache 和 Hardhat)都包含生成地址的私钥。 So in this case, the transaction is signed on the development node, and you don't need to sign it with your local web3 instance.所以在这种情况下,交易是在开发节点上签名的,你不需要用你本地的 web3 实例来签名。

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

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