how to override openzeppelin _countVote function in GovernorCountingSimple.sol file? - solidity

I am making a DAO using openzeppelin and I downloaded the governance contract that they provide from the openzeppelin contract wizard. once I had imported this file into remix IDE an error popped up saying
"Function has to override specified but does not override anything"
so I followed that error to the GovernorCountingSimple.sol file and saw that it was the _countVote function but I can't seem to override it. I think I am just missing something simple but I don't know how to override it when the last argument within the function is "bytes memory" with no declared name and I cant seem to just call the super statement on the function to pass the last argument.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.4;
import "#openzeppelin/contracts/governance/Governor.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorSettings.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorCountingSimple.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorVotes.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorVotesQuorumFraction.sol";
import "#openzeppelin/contracts/governance/extensions/GovernorTimelockControl.sol";
contract GovernernerContract is Governor, GovernorSettings, GovernorCountingSimple,
GovernorVotes, GovernorVotesQuorumFraction, GovernorTimelockControl {
constructor(
IVotes _token,
TimelockController _timelock,
uint256 _votingDelay,
uint256 _votingPeriod,
uint256 _quorumPercentage
)
Governor("governernerContract")
GovernorSettings( _votingDelay /* 1 block */, _votingPeriod /* 45818= ~ 1 week */, 0)
GovernorVotes(_token)
GovernorVotesQuorumFraction(_quorumPercentage)
GovernorTimelockControl(_timelock)
{}
// The following functions are overrides required by Solidity.
//this is my attempt to override the function but i dont know how to deal with the bytes
//memory argument
function _countVote(
uint256 proposalId,
address account,
uint8 support,
uint256 weight,
bytes memory // params
) internal override {
// super._countVote(proposalId,account,support,weight,*);
}
function votingDelay()
public
view
override(IGovernor, GovernorSettings)
returns (uint256)
{
// return super.votingDelay();
}
function votingPeriod()
public
view
override(IGovernor, GovernorSettings)
returns (uint256)
{
return super.votingPeriod();
}
function quorum(uint256 blockNumber)
public
view
override(IGovernor, GovernorVotesQuorumFraction)
returns (uint256)
{
return super.quorum(blockNumber);
}
function state(uint256 proposalId)
public
view
override(Governor, GovernorTimelockControl)
returns (ProposalState)
{
return super.state(proposalId);
}
function propose(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, string memory description)
public
override(Governor, IGovernor)
returns (uint256)
{
return super.propose(targets, values, calldatas, description);
}
function proposalThreshold()
public
view
override(Governor, GovernorSettings)
returns (uint256)
{
return super.proposalThreshold();
}
function _execute(uint256 proposalId, address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
internal
override(Governor, GovernorTimelockControl)
{
super._execute(proposalId, targets, values, calldatas, descriptionHash);
}
function _cancel(address[] memory targets, uint256[] memory values, bytes[] memory calldatas, bytes32 descriptionHash)
internal
override(Governor, GovernorTimelockControl)
returns (uint256)
{
return super._cancel(targets, values, calldatas, descriptionHash);
}
function _executor()
internal
view
override(Governor, GovernorTimelockControl)
returns (address)
{
return super._executor();
}
function supportsInterface(bytes4 interfaceId)
public
view
override(Governor, GovernorTimelockControl)
returns (bool)
{
return super.supportsInterface(interfaceId);
}
}

You was closer to the solution...
Just change this line: super._countVote(proposalId,account,support,weight,*);
Instead of using this symbol * to omit last parameter, try with single quotes.
This should work:
super._countVote(proposalId,account,support,weight,'');

Related

Is there a way to check how many times an ERC721 token has been minted?

I am using the OpenZeppelin's ERC721 Library, is there was a way to get the count of the number of times a token has been minted.
Is there a built-in mapping or function to check that?
You can use the Enumerable extension. The totalSupply() function returns the currently existing amount of tokens (minted minus burned), which in some cases might not the same thing as the total amount of minted tokens.
pragma solidity ^0.8;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
/*
* `totalSupply()` (defined in ERC721Enumerable) returns 1
* even though 2 tokens were minted, but 1 was also burned
*/
contract MyCollection is ERC721Enumerable, ERC721Burnable {
constructor() ERC721("CollectionName", "Symbol") {
_mint(msg.sender, 1);
_burn(1);
_mint(msg.sender, 2);
}
// multiple parents define the same function
// overriding here just to point at the expected parent class
// related to the combination of `Burnable` and `Enumerable` in the same contract - not to `Enumerable` alone
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
return ERC721Enumerable._beforeTokenTransfer(from, to, tokenId);
}
// same reason for overriding as above
function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721Enumerable) returns (bool) {
return ERC721Enumerable.supportsInterface(interfaceId);
}
}
Or you can override the _beforeTokenTransfer() hook and create a custom counter that takes into account only minting and ignores burning.
pragma solidity ^0.8;
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Enumerable.sol";
import "#openzeppelin/contracts/token/ERC721/extensions/ERC721Burnable.sol";
/*
* `mintCounter` returns 2, ignores the burned tokens
*/
contract MyCollection is ERC721Enumerable, ERC721Burnable {
uint256 public mintCounter;
constructor() ERC721("CollectionName", "Symbol") {
_mint(msg.sender, 1);
_burn(1);
_mint(msg.sender, 2);
}
function _beforeTokenTransfer(address from, address to, uint256 tokenId) internal override(ERC721, ERC721Enumerable) {
if (from == address(0)) {
mintCounter++;
}
return ERC721Enumerable._beforeTokenTransfer(from, to, tokenId);
}
function supportsInterface(bytes4 interfaceId) public view override(ERC721, ERC721Enumerable) returns (bool) {
return ERC721Enumerable.supportsInterface(interfaceId);
}
}

Solidity, is there gas-wise difference between transfer and call with 2300?

I am trying the below code, elementary reentrancy example.
The current code works but transfer or send doesn't work. It reverts. What are the reasons?
Note: I chose setCaller to reenter to test the fact that updating the dirty state variable (already modified var in the same tx) takes less gas.
// SPDX-License-Identifier: Unlicensed
pragma solidity 0.8.7;
contract TestFund {
// success, if caller is itself.
address public caller;
constructor() payable {}
function getBalance() public view returns (uint) {
return address(this).balance;
}
function emptyFn(address dummy) public {}
function setCaller(address _caller) public {
caller = _caller;
}
function withdraw() external {
caller = msg.sender;
// require(payable(msg.sender).send(1 wei));
// payable(msg.sender).transfer(1 wei);
(bool success, bytes memory data) = payable(msg.sender).call{gas: 2300, value:1 wei}("");
require(success);
}
}
contract Attack {
function getBalance() public view returns (uint) {
return address(this).balance;
}
function attack(address target) external {
TestFund(target).withdraw();
}
receive() external payable {
if (getBalance() == 1 wei) {
TestFund(msg.sender).setCaller(msg.sender);
}
}
}

What is the type of `this` object in solidity

In the following claimPayment function that is used to claim a payment made earlier to this contract, the line bytes32 message = prefixed(keccak256(abi.encodePacked(msg.sender, amount, nonce, this))); has this as part of the signed message. This makes me wonder what is this and what the type of this is. If I'm returning this in a function, what type is used for it? Thanks.
function claimPayment(uint256 amount, uint256 nonce, bytes memory signature) public {
require(!usedNonces[nonce]);
usedNonces[nonce] = true;
// this recreates the message that was signed on the client
bytes32 message = prefixed(keccak256(abi.encodePacked(msg.sender, amount, nonce, this)));
require(recoverSigner(message, signature) == owner);
payable(msg.sender).transfer(amount);
}
this is a pointer to the current class instance, as in many other programming languages. You can for example point to public methods:
pragma solidity ^0.8.0;
contract MyContract {
function foo() external {
this.bar();
}
function bar() public {
}
}
When this is typecasted, it takes a form of the address, where the current instance is deployed.
pragma solidity ^0.8.0;
contract MyContract {
function foo() external view returns (bytes memory) {
return abi.encodePacked(this);
}
function bar() external view returns (address) {
return address(this);
}
}

functions of deployed contract only return 0

[solidity] I deployed a simple smart-contract to a private ethereum network, All the functions return only zero, even one thar explictely "return 170" what may be wrong?
pragma solidity ^0.5.6;
contract Test {
uint256 myNumber = 70;
function setMyNumber(uint256 _myNumber)
external {
myNumber = _myNumber;
}
function getMyNumber ()
external view returns (uint256) {
return myNumber;
}
function return170 ()
external pure returns (uint256) {
return 170;
}
function returnNumber (uint256 _number)
external pure returns (uint256) {
return _number;
}
}
For anyone with the same problem. The solidity 0.5.X only works after Constantinople, so the private blockchain must be configurated with the " constantinopleBlock: ".

How to return array of address in solidity?

I am creating a smart contract in solidity ^0.5.1 in which I get the following error:
data location must be a memory for the return parameter in the function, but none was given.
In the below function I am getting error.
function getCitizen()public returns(address[]){
return citizenArray;
}
The smart contract that I have tried so far.
pragma solidity ^0.5.1;
contract Citizen{
struct Citizens{
uint age;
string fName;
string lName;
}
mapping(address => Citizens) citizenMap;
address [] citizenArray;
function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public{
//creating the object of the structure in solidity
Citizens storage citizen=citizenMap[_address];
citizen.age=_age;
citizen.fName=_fName;
citizen.lName=_lName;
citizenArray.push(_address) -1;
}
function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
function getCitizenAddress()public returns(address[]){
return citizenArray;
}
}
How can I return the array of addresses?
It make sense, as you are returning the storage array of address you cannot return it as it is, because it will try to return the actual address of citizenArray in the contract storage. You can send the array by making it in memory. Like this.
function getCitizenAddress()public view returns( address [] memory){
return citizenArray;
}
Once you put it as memory, you will get the warning for this which will state that as you are not changing any state in the function, you should mark it view, I already did that in the above code.
Lastly, when you resolved this error, you will get another error in this function:
function getCitizen(address _address) public pure returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
This error is because you mark this function as pure. There is a little but very important difference between pure and view.
view means you cannot change the state of the contract in that function.
pure means you cannot change the state in the function and not even can read the state or storage variables.
In the above function of getCitizen you are actually doing read operations in your return statement. You can fix this by just putting view instead of pure. Like So:
function getCitizen(address _address) public view returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
I hope it will resolve all your issues. Thanks
// SPDX-License-Identifier: MIT
// Version
pragma solidity >=0.8.0 < 0.9.0;
contract EstructuraDeDatos {
struct Customer {
string nameCustomer;
string idCustomer;
string emailCustomer;
}
mapping(address => Customer) public myClientes;
address[] public listClientes;
function registrationApp(string memory _name, string memory _id, string memory _email) public {
Customer memory customer = Customer(_name, _id, _email);
myClientes[msg.sender] = customer;
listClientes.push(msg.sender);
}
function retornarArrat() public view returns (address[] memory) {
return listClientes;
}
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Citizen {
struct Citizens {
uint age;
string fName;
string lName;
}
mapping(address => Citizens) citizenMap;
address [] citizenArray;
function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public {
// Citizens storage citizen=citizenMap[_address];
//creating the object of the structure in solidity
Citizens storage citizen;
citizen = citizenMap[_address];
citizen.age=_age;
citizen.fName=_fName;
citizen.lName=_lName;
citizenArray.push(_address);
}
function getCitizen(address _address) public view returns(uint,string memory ,string memory) {
return (citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
// function getCitizenAddress()public returns(address[]) {
// return citizenArray;
// }
}
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract Citizen{
struct Citizens{
uint age;
string fName;
string lName;
}
mapping(address => Citizens) citizenMap;
address [] citizenArray;
function setCitizen(address _address,uint _age,string memory _fName,string memory _lName) public{
// Citizens storage citizen=citizenMap[_address];
//creating the object of the structure in solidity
Citizens storage citizen;
citizen = citizenMap[_address];
citizen.age=_age;
citizen.fName=_fName;
citizen.lName=_lName;
citizenArray.push(_address);
}
function getCitizen(address _address) public view returns(uint,string memory ,string memory ){
return(citizenMap[_address].age,citizenMap[_address].fName,citizenMap[_address].lName);
}
function getCitizenAddress()public view returns(address[] memory){
return citizenArray;
}
}