Solidity

我收到“ReferenceError:未定義帳戶”的錯誤

  • October 11, 2022

我目前正在閱讀《Hands-On Smart Contract Development with Solidity and Ethereum》(O’REILLY)並抄錄本書中的程式碼。

我遇到了錯誤,我的雙手被束縛了。

錯誤如下。

ReferenceError:帳戶未定義(/Users/tsuru/fundraiser/test/fundraiser_test.js:59:28)

根據錯誤,似乎有問題      

const newBeneficiary = accounts[2];

程式碼在這裡。

/fundraiser/test/fundraiser_test

const FundraiserContract = artifacts.require("Fundraiser");

contract("Fundraiser", accounts => {
let fundraiser;
const name =  "Beneficiary Name";
const url = "[beneficiaryname.org](http://beneficiaryname.org/)";
const imageURL = "[https://placekitten.com/600/350] 
(https://placekitten.com/600/350)";
const description = "Beneficiary description";
const beneficiary = accounts[1];
const owner = accounts[0];

beforeEach(async () => {
fundraiser = await FundraiserContract.new(
name,
url,
imageURL,
description,
beneficiary,
owner
)
});
});

describe("initialization", () => {
it("gets the beneficiary name", async () => {
const actual = await fundraiser.name();
assert.equal(actual, name, "names should match");
});

it("gets the beneficiary url", async () => {
const actual = await fundraiser.url();
assert.equal(actual, url, "url should match");
});

it("gets the beneficiary image url", async () => {
const actual = await fundraiser.imageURL();
assert.equal(actual, imageURL, "imageURL should match");
});

it("gets the beneficiary description", async () => {
const actual = await fundraiser.description();
assert.equal(actual, description, "description should match");
});

it("gets the beneficiary", async () => {
const actual = await fundraiser.beneficiary();
assert.equal(actual, beneficiary, "beneficiary addresses should match");
});

it("gets the owner", async () => {
const actual = await fundraiser.owner();
assert.equal(actual, owner, "bios should match");
});
});

describe("setBeneficiary", () => {

//The line where the error is pointed out
const newBeneficiary = accounts[2];

it("updated beneficiary when called by owner account", async () => {
await fundraiser.setBeneficiary(newBeneficiary, {from: owner});
const actualBeneficiary = await fundraiser.beneficiary();
assert.equal(actualBeneficiary, newBeneficiary, "beneficiaries should match");
});


it("throws and error when called from a non-owner account", async () => {
try {
await fundraiser.setBeneficiary(newBeneficiary, {from: accounts[3]});
assert.fail("withdraw was not restricted to owners")
} catch(err) {
const expectedError = "Ownable: caller is not the owner"
const actualError = err.reason;
assert.equal(actualError, expectedError, "should not be permitted")
}
})
})

結構/Fundraiser.sol

// SPDX-License-Identifier: MIT

pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";

contract Fundraiser is Ownable {

string public name;
string public url;
string public imageURL;
string public description;
address payable public beneficiary;

constructor(
string memory _name,
string memory _url,
string memory _imageURL,
string memory _description,
address payable _beneficiary,
address _custodian
)
public
{
name = _name;
url = _url;
imageURL = _imageURL;
description = _description;
beneficiary = _beneficiary;
transferOwnership(_custodian);
}

function setBeneficiary(address payable _beneficiary) public onlyOwner {
beneficiary = _beneficiary;
}
}

所有程式碼都可以在這裡找到。 https://github.com/okahijiki/fundraiser

您在beforeEach鉤子後儘早關閉合約括號。因此,最後的右括號更少。這應該工作:

const FundraiserContract = artifacts.require("Fundraiser");

contract("Fundraiser", (accounts) => {
 let fundraiser;
 const name = "Beneficiary Name";
 const url = "[beneficiaryname.org](http://beneficiaryname.org/)";
 // I do not know what kind of syntax is this
 const imageURL =
   "[https://placekitten.com/600/350](https://placekitten.com/600/350)";
 const description = "Beneficiary description";
 const beneficiary = accounts[1];
 const owner = accounts[0];

 beforeEach(async () => {
   fundraiser = await FundraiserContract.new(
     name,
     url,
     imageURL,
     description,
     beneficiary,
     owner
   );
 });

 describe("initialization", () => {
   it("gets the beneficiary name", async () => {
     const actual = await fundraiser.name();
     assert.equal(actual, name, "names should match");
   });

   it("gets the beneficiary url", async () => {
     const actual = await fundraiser.url();
     assert.equal(actual, url, "url should match");
   });

   it("gets the beneficiary image url", async () => {
     const actual = await fundraiser.imageURL();
     assert.equal(actual, imageURL, "imageURL should match");
   });

   it("gets the beneficiary description", async () => {
     const actual = await fundraiser.description();
     assert.equal(actual, description, "description should match");
   });

   it("gets the beneficiary", async () => {
     const actual = await fundraiser.beneficiary();
     assert.equal(actual, beneficiary, "beneficiary addresses should match");
   });

   it("gets the owner", async () => {
     const actual = await fundraiser.owner();
     assert.equal(actual, owner, "bios should match");
   });
 });

 describe("setBeneficiary", () => {
   //The line where the error is pointed out
   const newBeneficiary = accounts[2];

   it("updated beneficiary when called by owner account", async () => {
     await fundraiser.setBeneficiary(newBeneficiary, { from: owner });
     const actualBeneficiary = await fundraiser.beneficiary();
     assert.equal(
       actualBeneficiary,
       newBeneficiary,
       "beneficiaries should match"
     );
   });

   it("throws and error when called from a non-owner account", async () => {
     try {
       await fundraiser.setBeneficiary(newBeneficiary, { from: accounts[3] });
       assert.fail("withdraw was not restricted to owners");
     } catch (err) {
       const expectedError = "Ownable: caller is not the owner";
       const actualError = err.reason;
       assert.equal(actualError, expectedError, "should not be permitted");
     }
   });
 });
});

引用自:https://ethereum.stackexchange.com/questions/137255