首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >accounts[0],accounts[1],accounts[2]等在DApp中的意义?

accounts[0],accounts[1],accounts[2]等在DApp中的意义?
EN

Ethereum用户
提问于 2021-03-19 11:36:24
回答 1查看 378关注 0票数 0

最近,我在GitHub上观看了一个GitHub。在.soltest.js中分别有以下两种情况。

代码语言:javascript
复制
    address contractOwner;
    address withdrawWallet;

    function contractName(address _contractOwner, address _withdrawWallet) {
        require(_contractOwner != address(0));
        require(_withdrawWallet != address(0));

        contractOwner = _contractOwner;
        withdrawWallet = _withdrawWallet;
    }
代码语言:javascript
复制
  contract('contractName', function(accounts) {
  const owner = accounts[0]; // this is the account we deploy as owner, see 2_deploy_contracts.js
  const withdrawWallet = accounts[1];
  const account1 = accounts[2];
  const account2 = accounts[3];

  web3.eth.sendTransaction({from:owner, to:account1, value: 10000000000000000000 }, function(err, r) { /* NOP */ });

  it("should have correct constants by default", function() {
    let CN;
    return contractName.new(owner, withdrawWallet)
      .then(function(instance) {
        CN = instance;

        return CN.weiProductPrice.call();
      })
      .then(function(wpp) {
        assert.equal(weiProductPrice, wpp.toNumber());
        return CN.productPerPage.call();
      })
      .then(function(ppp) {
        assert.equal(productPerPage, ppp.toNumber());
      });
  });

2_deploy_contracts.js包含

代码语言:javascript
复制
  // We deploy the contract with the owner being the first address from accounts
  const owner = accounts[0];
  if (network == "live") {
    const withdrawWallet = "0x00010dB ... legit wallet address";
    deployer.deploy(contractName, owner, withdrawWallet);
    return;
  }

accounts[0]accounts[1]accounts[2]accounts[3]是什么意思?它们是否应该表明用户必须赋值,或者它们是否有特定的含义?它们在项目中的任何地方都没有定义。

谢谢。

EN

回答 1

Ethereum用户

发布于 2021-03-19 20:29:15

accounts通过了测试,在这里:

代码语言:javascript
复制
contract('contractName', function(accounts)

这是一个可以方便地从钱包里捡到松露的阵列。

当我部署时,我必须输入实际值来代替2等帐户,对吗?

不是的。

当你部署时,这是一个预先确定的结论,你至少有一个埃瑟姆帐户,它有钱支付天然气。它在交易中签字。

松露是检查客户,以发现哪些帐户存在。这些是您在主板、testnet或ganache-cli上的地址(视情况而定)。

关于ganache的一个好处是,默认情况下,它为您设置了10个有资金支持的帐户。

这些测试是为了不可知帐户的实际地址,并依赖于那里的任何东西。您可以使用事务对象的from:成员来测试多方进程,即from: owner,from: alice,from: bob,这三个帐户都是您的帐户。

代码语言:javascript
复制
contract("Factory", accounts => {

  const owner = accounts[0];
  const alice = accounts[1];
  const bob = accounts[2];

  it("should do something", async () => {
    // transfer between MY accounts;
    await web3.eth.sendTransaction({ value: 10, from: alice, to: bob });
    ...

你可以这样做,这可能是说明性的:

代码语言:javascript
复制
it("should be ready to test", async () => {
   assert.isAtLeast(accounts.length, 3, "There are not at least three accounts to work with");
});

如果不清楚,您可以在主板或testnet上创建帐户,直到至少有三个帐户为止。测试与你如何完成它无关,但它需要完成,否则它将无法签署。https://geth.ethereum.org/docs/interface/managing-your-accounts

希望能帮上忙。

票数 1
EN
页面原文内容由Ethereum提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://ethereum.stackexchange.com/questions/94887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档