简体   繁体   English

如何更改松露(ganache)的帐户?

[英]How to change account in truffle(ganache)?

I use ganache-cli to build local blockchain and truffle to deploy contract.我使用 ganache-cli 构建本地区块链并使用 truffle 部署合约。 To interact with deployed contracts i use truffle console.为了与已部署的合约交互,我使用 truffle 控制台。 For example i transfer tokens from my current account(that is web3.eth.personal.getAccounts()[0] ) to web3.eth.personal.getAccounts()[1] after this i want to change my current account to web3.eth.personal.getAccounts()[1] address.例如,我将令牌从我的当前帐户(即web3.eth.personal.getAccounts()[0] )转移到web3.eth.personal.getAccounts()[1]之后,我想将我的当前帐户更改为web3.eth.personal.getAccounts()[1]地址。

How to do that?怎么做?

You can set the default account as您可以将默认帐户设置为

web3.eth.defaultAccount = web3.eth.personal.getAccounts()[1];

or simply as an address或简单地作为地址

web3.eth.defaultAccount = '0x11f4d0A3c12e86B4b5F39B213F7E19D048276DAe';

Mind that Ganache by default only has 10 predefined accounts that it knows the private keys to.请注意,Ganache 默认情况下只有 10 个预定义帐户,它知道其私钥。 So you need to pass an address that Ganache knows private key to.所以你需要传递一个 Ganache 知道私钥的地址。 If you pass an unknown account address, web3 (and Ganache) will not be able to submit transactions using this (unknown) address.如果您传递一个未知的帐户地址,web3(和 Ganache)将无法使用此(未知)地址提交交易。

Changing the value of web3.eth.defaultAccount doesn't work for me.更改web3.eth.defaultAccount的值对我不起作用。 But configuring from in the truffle.js works.但是在 truffle.js 中配置from有效的。

module.exports = {
  networks: {
    development: {
      host: "127.0.0.1",
      port: 7545,
      network_id: "*", 
      from: "0x12345678"
    }
  }
};

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

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