简体   繁体   中英

Sign a transaction to smart contract from migrations

I want to call smart contract method via sendTransaction from one of migrations. I'm using Truffle. During this migration I create a new wallet with a mnemonic.

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 })

During the transaction I receive an exception

Returned error: sender account not recognized

How to send transaction with a newly created from a mnemonic profile?

You can set your provider to your contract instance then

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] })
  });
}; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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