【问题标题】:Sign a transaction to smart contract from migrations从迁移签署交易到智能合约
【发布时间】:2020-05-26 08:20:39
【问题描述】:

我想通过 sendTransaction 从其中一个迁移中调用智能合约方法。我正在使用松露。在此迁移期间,我创建了一个带有助记符的新钱包。

const seed = bip39.mnemonicToSeed(mnemonic)
const hdk = hdkey.fromMasterSeed(seed)
const addrNode = hdk.derivePath("m/44'/60'/0'/0/0")
const walletAddr = wallet.getAddressString()
await someFactory.createProfile.sendTransaction(detailsHash, { from: walletAddr })

在交易过程中我收到一个异常

Returned error: sender account not recognized

如何使用从助记配置文件新创建的交易发送交易?

【问题讨论】:

  • 您创建的钱包超出了松露提供商的范围
  • 我也是这么想的!另外,我试图使它像这样: const provider = new HDWalletProvider(mnemonic, "127.0.0.1:7545", 0, 1, true, "m/44'/60'/0'/0/0") web3.setProvider (provider) 和使用accounts[0],但是结果是一样的。如何将使用 ethereumjs-wallet 创建的钱包注入提供者?或者有没有更好的方法来做到这一点?
  • 你想用这段代码做什么?如果您只想从一个地址部署您的合约并与另一个地址调用,那么您可以指定私钥而不是助记符如果地址不是从相同的助记符派生的。您可以使用相同的 HDWalletProvider 在 truffle-config 中指定私钥数组。
  • 我想在迁移过程中生成一个随机钱包,然后用这个钱包创建一个合约(通过工厂使用 { from: wallet })。稍后使用 Web3J 在 Android 设备上恢复它。我尝试使用具有相同助记符的 HDWalletProvider,但没有运气,同样的错误。

标签: blockchain ethereum web3js truffle go-ethereum


【解决方案1】:

然后你可以将你的提供者设置为你的合约实例

const HDWalletProvider = require("@truffle/hdwallet-provider");
const mnemonic = "Your mnemonic"; //

module.exports = function(deployer) {

  deployer.deploy(SomeFactory).then(someFactory => {
  provider = new HDWalletProvider(mnemonic, "YourURL", 0);

  someFactory.contract.setProvider(provider);

  someFactory.createProfile.sendTransaction(detailsHash, { from:provider.addresses[0] })
  });
}; 

【讨论】:

    猜你喜欢
    • 2021-04-03
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-03-12
    相关资源
    最近更新 更多