When I run truffle test, I get the following statement. Is it an error?
Compilation warnings encountered:
project:/contracts/Fundraiser.sol:18:5: Warning: This declaration shadows an existing declaration.
address payable _beneficiary,
^--------------------------^
project:/contracts/Fundraiser.sol:8:5: The shadowed declaration is here:
address payable _beneficiary;
^--------------------------^
,project:/contracts/Fundraiser.sol:19:5: Warning: This declaration shadows an existing declaration.
address _custodian
^----------------^
project:/contracts/Fundraiser.sol:9:5: The shadowed declaration is here:
address _custodian;
^----------------^
CompileError: project:/contracts/Fundraiser.sol:27:9: DeclarationError: Undeclared identifier. Did you mean "_beneficiary" or "_beneficiary"?
beneficiary = _beneficiary;
^---------^
,project:/contracts/Fundraiser.sol:28:9: DeclarationError: Undeclared identifier. Did you mean "_custodian" or "_custodian"?
custodian = _custodian;
^-------^
The code for the smart contract is as follows.
#Fundraiser.sol
pragma solidity >0.4.23 <0.7.0;
contract Fundraiser{
string public name;
string public url;
string public imageURL;
string public description;
address payable _beneficiary;
address _custodian;
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;
custodian = _custodian;
}
}
The code for the test is as follows.
#test/fundraiser_test.js
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 cunstodian = accounts[0];
beforeEach(async () => {
fundraiser = await FundraiserContract.new(
name,
url,
imageURL,
description,
beneficiary,
cunstodian
)
});
describe("initialization", () => {
it("gets the beneficiary name", async () => {
const actual = await [fundraiser.name](http://fundraiser.name/)();
assert.equal(actual, name, "names should match");
});
it("gets the beneficiary url", async () => {
const actual = await fundraiser.imageURL();
assert.equal(actual, imageURL, "imageURL should match");
});
it("gets the beneficiary image url", async () => {
const actual = await [fundraiser.name](http://fundraiser.name/)();
assert.equal(actual, name, "names 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 should match");
});
it("gets the cunstodian", async () => {
const actual = await fundraiser.cunstodian();
assert.equal(actual, cunstodian, "cunstodian should match");
});
});
});;
You can't name state variables and input argument Variables the same, it will confuse the compiler on which one to use.
You first defined _beneficiary
and _custodian
as a state variables on
project:/contracts/Fundraiser.sol:8:5
and
project:/contracts/Fundraiser.sol:9:5
respectively. and then later on in the constructor input you named the variable same, at line number
project:/contracts/Fundraiser.sol:18:5
project:/contracts/Fundraiser.sol:19:5
You have to change the naming and differentiate it so that compiler can understand, what you are trying to do. For instance, normally when we add an underscore before the variable name in the temp variable (input arguments), not with state variables unless they are private.
in short your code shold look like this.
pragma solidity >0.4.23 <0.7.0;
contract Fundraiser{
string public name;
string public url;
string public imageURL;
string public description;
address payable beneficiary; //removed underscore
address custodian; //removed underscore
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;
custodian = _custodian;
}
}
Don't forget to accept the answer.