Warnings by deploying ERC20 token sol, - deployment

I want to deploy my contract but I have so many warnings, I don't know every meaning.
Gas requirement of function SpastToken.increaseApproval(address,uint256) high. (11 times in other functions)
BasicToken.balanceOf(address) : Variables have very similar names balance and balances. Note: Modifiers are currently not considered by this static analysis.
Use assert(x) if you never ever want x to be false, not in any circumstance (apart from a bug in your code). Use require(x) if x can be false, due to e.g. invalid input or a failing external component.
contract Ownable {
address public owner;
event OwnershipTransferred(address indexed previousOwner, address indexed
newOwner);
constructor() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
require(newOwner != address(0));
emit OwnershipTransferred(owner, newOwner);
owner = newOwner;
}
}
contract ERC20Basic {
function totalSupply() public view returns (uint256);
function balanceOf(address who) public view returns (uint256);
function transfer(address to, uint256 value) public returns (bool);
event Transfer(address indexed from, address indexed to, uint256 value);
}
/**
* #title ERC20 interface
*/
contract ERC20 is ERC20Basic {
function allowance(address owner, address spender) public view returns
(uint256);
function transferFrom(address from, address to, uint256 value) public
returns (bool);
function approve(address spender, uint256 value) public returns (bool);
event Approval(address indexed owner, address indexed spender, uint256
value);
}
/**
* #title Basic token
*/
contract BasicToken is ERC20Basic {
using SafeMath for uint256;
mapping(address => uint256) balances;
uint256 totalSupply_;
function totalSupply() public view returns (uint256) {
return totalSupply_;
}
function transfer(address _to, uint256 _value) public returns (bool) {
require(_to != address(0));
require(_value <= balances[msg.sender]);
balances[msg.sender] = balances[msg.sender].sub(_value);
balances[_to] = balances[_to].add(_value);
emit Transfer(msg.sender, _to, _value);
return true;
}
function balanceOf(address _owner) public view returns (uint256 balance) {
return balances[_owner];
}
}
/**
* #title Standard ERC20 token
*/
contract StandardToken is ERC20, BasicToken {
mapping (address => mapping (address => uint256)) internal allowed;
function transferFrom(address _from, address _to, uint256 _value) public
returns (bool) {
require(_to != address(0));
require(_value <= balances[_from]);
require(_value <= allowed[_from][msg.sender]);
balances[_from] = balances[_from].sub(_value);
balances[_to] = balances[_to].add(_value);
allowed[_from][msg.sender] = allowed[_from][msg.sender].sub(_value);
emit Transfer(_from, _to, _value);
return true;
}
function approve(address _spender, uint256 _value) public returns (bool) {
allowed[msg.sender][_spender] = _value;
emit Approval(msg.sender, _spender, _value);
return true;
}
function allowance(address _owner, address _spender) public view returns
(uint256) {
return allowed[_owner][_spender];
}
function increaseApproval(address _spender, uint _addedValue) public returns
(bool) {
allowed[msg.sender][_spender] = allowed[msg.sender]
[_spender].add(_addedValue);
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
function decreaseApproval(address _spender, uint _subtractedValue) public
returns (bool) {
uint oldValue = allowed[msg.sender][_spender];
if (_subtractedValue > oldValue) {
allowed[msg.sender][_spender] = 0;
} else {
allowed[msg.sender][_spender] = oldValue.sub(_subtractedValue);
}
emit Approval(msg.sender, _spender, allowed[msg.sender][_spender]);
return true;
}
}
contract SpastToken is StandardToken, Ownable {
string public name;
string public symbol;
uint8 public decimals;
uint256 public initialSupply;
constructor() public {
name = 'SPAST-Coin';
symbol = 'SPAS';
decimals = 18;
initialSupply = 100000000 * 10 ** uint256(decimals);
totalSupply_ = initialSupply;
balances[owner] = initialSupply;
emit Transfer(0x0, owner, initialSupply);
}
}

I think these warnings are pretty self explanatory. Except maybe the last one about the assertion. That one basically says that you should use assert() to check for logic errors in your code(that would be something you did wrong), whereas you should use require() to check if the inputs are legal or not(that would be the callers mistake). I don't think the rest of the warnings can be explained further, consider googling them. The one with the variable names however is just there to help you avoid typos from similar variable names and will not affect execution in any way. The modifiers warnings that do not appear here try to tell you that you could have stricter modifiers for your functions which again would make reading the code simpler.

Related

Partial calldata using ethers

I'd like to serialize a call using ethers, but only partially.
contract MockCall {
struct VoteResults {
uint256 votes;
bytes data;
}
event LogParam(uint256 param);
event LogParam(string name);
event LogParam(VoteResults[] results);
bool public isAction1 = false;
bool public isAction2 = false;
//I've only created this method so type chain would generate method stub
function Action(uint256 param1, string calldata name) public {
isAction1 = true;
}
// Intentionally leave 2 calldata parameters to verify appending calldata can work with trailing dynamic parameter
function Action(uint256 param1, string calldata name, VoteResults[] calldata voteResults) public {
isAction2 = true;
emit LogParam(param1);
emit LogParam(name);
emit LogParam(voteResults);
}
}
I would like to partially build the calldata, and my contract is going to handle injecting the rest of the parameters.
I've tried a few ways:
This way throws an error, in hexify, and sometimes json
let encodedFunctionCall = '';
try {
const rawInterface = new Interface("Action(uint256,string,(uint256,bytes)[]");
encodedFunctionCall = rawInterface.encodeFunctionData("Action(uint256,string,(uint256,bytes)[]", [BigNumber.from(5), "Abra"]);
}
catch(err) {
console.log(err);
}
This doesn't compile since it expects 3 parameters
const encodedFunctionCall = mockCall.interface.encodeFunctionData("Action(uint256,string,(uint256,bytes)[])", [BigNumber.from(5), "Abra"])
How do I partially construct calldata?

How to manage and communicate with two smart contracts

I am working on an election project. initially, I created a smart contract (Election) where an admin will start the election and voters can vote. the problem is there will be multiple elections at the same time and voters must be able to vote for all of the elections. how can I do that by creating another contract?
here is the election contract: Election.sol
//SPDX-License-Identifier: UNLICENSED
pragma solidity >= 0.5.0<0.9.0;
***contract ElectionList{
mapping (address => Election)public electionsmap;
}***
//contract for election
contract Election{
//candidatte name,numvotes
struct Candidate{
string name;
uint numVotes;
}
//voter name,authorised,whom(voting to),voted(or not)
struct Voter{
string name;
bool authorised;
uint whom;
bool voted;
}
address public owner; //is the admin starts election
string public electionName; //the name of the election
bool public electionStarted = false; //election started or not
mapping(address => Voter) public voters; // voters = i.voter[adres]
Candidate[] public candidates; //array of candidate
uint public totalVotes; //total votes voted
//modifiers to limit or require
modifier ownerOnly(){
require(msg.sender == owner);
_;
}
modifier ifnoElection(){
require(electionStarted==false);
_;
}
//function to start election
function startElection(string memory _electionName)ifnoElection public{
owner = msg.sender;
electionName = _electionName;
electionStarted = true;
}
//adding candidate only admin candidate(name, number of votes)
function addCandidate(string memory _candidateName)ownerOnly public{
candidates.push(Candidate(_candidateName, 0));
}
//authorize voter admin only can authorize voter needs voter adress
function authorizeVoter(address _voterAddress) ownerOnly public{
voters[_voterAddress].authorised= true;
}
//function to get total candidates number
function getNumCandidates() public view returns(uint){
return candidates.length;
}
//function to vote requires the index of candidate to vote
function vote(uint candidateIndex) public{
require(!voters[msg.sender].voted);
require(voters[msg.sender].authorised);
voters[msg.sender].whom = candidateIndex;
voters[msg.sender].voted = true;
candidates[candidateIndex].numVotes++;
totalVotes++;
}
//to get total votes
function getTotalVotes()public view returns(uint) {
return totalVotes;
}
//get info of candidate
function candidateInfo(uint index) public view returns(Candidate memory){
return candidates[index];
}
function closeElection()public {
remove();
// electionStarted = false;
}
function remove() public ownerOnly{
uint n = candidates.length;
for(uint i=0;i<n;i++){
delete candidates[i];
}
// // Reset the value to the default value.
}
}
you can create a Factory contract which deploys Election contract
contract ElectionFactory {
address[] public deployedElections;
uint public electionsCount;
address public owner;
modifier ownerOnly(){
require(msg.sender == owner);
_;
}
constructor(){
owner=msg.sender();
}
// If Election contract had constructor parameters you would pass them to this function
// you want onlyOwner who is the creator of this contract create new Election
function createElection() public ownerOnly {
Election newElection = new Campaign();
deployedElections.push(address(newElection));
electionsCount++;
}
function getDeployedCampaign(uint index) public view returns (address) {
return deployedElections[index];
}
function getCampaignCounts() public view returns (uint) {
return campaignsCount;
}
}
Your Election contract has no constructor function. You could add constructor to make your contract more specific. Maybe each election has different duration so you can specify it during creation of Election contract

Interfaces. Why am I getting a type error when trying to use an external function?

Here is the code for my main smart contract.
Errors on the last two functions.
from solidity: TypeError: Invalid type for argument in function call.
Invalid implicit conversion from type(uint256) to uint256 requested.
--> contracts/DIV4.sol:39:57: | 39 | randomness_interface(_random).fulfillRandomness(uint256); |
^^^^^^
from solidity: TypeError: Invalid type for argument in function call.
Invalid implicit conversion from type(bytes32) to bytes32 requested.
--> contracts/DIV4.sol:43:55: | 43 | randomness_interface(_random).getRandomNumber(bytes32); |
^^^^^^^
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/token/ERC1155/ERC1155.sol";
import {randomness_interface} from "./randomness_interface.sol";
contract Divergence is ERC1155, Ownable {
uint256 public constant One = 0;
uint256 public constant Two = 1;
uint256 public constant Three = 2;
uint256 public constant Four = 3;
constructor() ERC1155 ("https://ipfs.io/ipfs/dsfgijdsfklj348rue0ur099045948.json"){
_mint(msg.sender, item1, 1000, "" );
_mint(msg.sender, item2, 130, "" );
_mint(msg.sender, item3, 65, "" );
_mint(msg.sender, item4, 3, "" );
}
function uri(uint256 _tokenId) override public view returns (string memory) {
return string(
abi.encodePacked(
"https://ipfs.io/ipfs/Qmf4WrTGA2fYJXitigvqJ7FDVMPreJQW4of8HZ1k5Wzkd3?filename=Divergence1",
Strings.toString(_tokenId),
".json"
)
);
}
function generateRandomNumber(address _random) external(bytes32 requestId) {
randomness_interface(_random).fulfillRandomness(uint256);
}
function getRandomNumberfromOutside(address _random) external {
randomness_interface(_random).getRandomNumber(bytes32);
}
Interface.sol file.
pragma solidity ^0.8.0;
interface randomness_interface {
function fulfillRandomness(uint256 randomness) external view returns (uint);
function getRandomNumber(bytes32 requestId) external;
}
Finally, the file with all the randomization happening.
pragma solidity ^0.6.6;
import "https://github.com/smartcontractkit/chainlink/blob/develop/contracts/src/v0.8/VRFConsumerBase.sol";
contract RandomNumber is VRFConsumerBase {
bytes32 public keyHash;
uint256 public fee;
uint256 public randomResult;
constructor() VRFConsumerBase(0xdD3782915140c8f3b190B5D67eAc6dc5760C46E9, 0xa36085F69e2889c224210F603D836748e7dC0088) public {
keyHash = 0x6c3699283bda56ad74f6b855546325b68d482e983852a7a82979cc4807b641f4;
fee = 0.1 * 10 ** 18; //0.1 LINK
}
function getRandomNumber() public returns (bytes32 requestId) {
return requestRandomness(keyHash, fee);
}
function fulfillRandomness(bytes32 requestId, uint256 randomness) internal override {
randomResult = randomness.mod(100).add(1);
}
}
You are not passing correct arguments to the functions.
-
function generateRandomNumber(address _random) external(bytes32 requestId) {
randomness_interface(_random).fulfillRandomness(uint256);
}
You are calling randomness_interface(_random).fulfillRandomness and fulfillRandomness expects
function fulfillRandomness(bytes32 requestId, uint256 randomness)
I am not sure if this is the correct way of calling fulfillRandomness
function generateRandomNumber(address _random) external(bytes32 requestId) {
randomness_interface(_random).fulfillRandomness(uint256);
}
Because as far as I know, fulfillRandomness gets called by the chainlink node, so you make request, get the random number and then chainlink will call fulfillRandomness. I think you should have written like this:
function fulfillRandomness(bytes32 requestId,uint256 randomNumber) internal override{
}

How can I dynamically make entity properties read-only?

I'm working with EF 4.5 and DbContext. At business rules layer level, I should implement checks to avoid change entity value properties in some entity scenarios. Sample: StartProjecteDate should be readonly if ProjectIsStarted but not in other status.
I follow DRY principle, for this reason, I should be able to inspect readonly properties list from context and also from UI.
My question:
Is there a DataAnnotation validator that can dynamically set properties as readonly?
(and if not, is there a different / better solution to this problem?)
Notice than I'm working with Web Forms (and Telerik) architecture, a clean and elegant pattern will be welcome.
I'm trying to set and get at run time EditableAttribute as Jesse Webb explains, but I'm not able to get dataannotation attributes from property, my code:
<EditableAttribute(False)>
<MaxLength(400, ErrorMessage:="Màxim 400 caracters")>
Public Property NomInvertebrat As String
Edited Nov 8 2013 after digging docs, it seems that dataanottions if for class but for instance object itself. Perhaps an iReadonlyableProperties interface may be a way.
I have a class containing extension methods that lets me read data annotations like this:
int maxRefLen = ReflectionAPI.GetProperty<Organisation, String>(x => x.Name)
.GetAttribute<StringLengthAttribute>()
.GetValueOrDefault(x => x.MaximumLength, 256);
So if you use it you should be able to do get the value of the EditableAttribute like this:
bool isEditable = ReflectionAPI.GetProperty<Foo, String>(x => x.NomInvertebrat)
.GetAttribute<EditableAttribute>()
.GetValueOrDefault(x => x.AllowEdit, true);
As for setting the data annotations at run-time, I haven't done it myself but I have read that there is a solution here: Setting data-annotations at runtime
Getting a list of all data annotations of a particular type I think would entail reading the entity framework metadata. Again I haven't tried this.
If you add that together I personally think it feels clunky rather than elegant, but you have asked for a solution using DataAnnotations and something more elegant would probably mean getting into your architecture.
I would be inclined to do this:
public bool StartDateIsReadOnly
{
//use this property client-side to disable the input
get{ return Project.IsStarted;}
}
//Implement IValidatable object to do server side validation
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext
{
bool startdateIsChanged = // I'll leave you to work out this bit
var results = new List<ValidationResult>();
if(StartDateIsReadOnly && startdateIsChanged)
results.Add(new ValidationResult("Start Date cannot be changed after project is started");
}
Here is the ReflectionAPI class:
Please note that the class includes part of a hack that #JonSkeet posted and described as "evil". I personally think this bit ain't so bad, but you should read the following references:
Override a generic method for value types and reference types.
Evil code - overload resolution workaround
public static class ReflectionAPI
{
public static int GetValueOrDefault<TInput>(this TInput a, Func<TInput, int> func, int defaultValue)
where TInput : Attribute
//Have to restrict to struct or you get the error:
//The type 'R' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
{
if (a == null)
return defaultValue;
return func(a);
}
public static Nullable<TResult> GetValueOrDefault<TInput, TResult>(this TInput a, Func<TInput, TResult> func, Nullable<TResult> defaultValue)
where TInput : Attribute
where TResult : struct
//Have to restrict to struct or you get the error:
//The type 'R' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method 'System.Nullable<T>'
{
if (a == null)
return defaultValue;
return func(a);
}
//In order to constrain to a class without interfering with the overload that has a generic struct constraint
//we need to add a parameter to the signature that is a reference type restricted to a class
public class ClassConstraintHack<T> where T : class { }
//The hack means we have an unused parameter in the signature
//http://msmvps.com/blogs/jon_skeet/archive/2010/11/02/evil-code-overload-resolution-workaround.aspx
public static TResult GetValueOrDefault<TInput, TResult>(this TInput a, Func<TInput, TResult> func, TResult defaultValue, ClassConstraintHack<TResult> ignored = default(ClassConstraintHack<TResult>))
where TInput : Attribute
where TResult : class
{
if (a == null)
return defaultValue;
return func(a);
}
//I don't go so far as to use the inheritance trick decribed in the evil code overload resolution blog,
//just create some overloads that take nullable types - and I will just keep adding overloads for other nullable type
public static bool? GetValueOrDefault<TInput>(this TInput a, Func<TInput, bool?> func, bool? defaultValue)
where TInput : Attribute
{
if (a == null)
return defaultValue;
return func(a);
}
public static int? GetValueOrDefault<TInput>(this TInput a, Func<TInput, int?> func, int? defaultValue)
where TInput : Attribute
{
if (a == null)
return defaultValue;
return func(a);
}
public static T GetAttribute<T>(this PropertyInfo p) where T : Attribute
{
if (p == null)
return null;
return p.GetCustomAttributes(false).OfType<T>().LastOrDefault();
}
public static PropertyInfo GetProperty<T, R>(Expression<Func<T, R>> expression)
{
if (expression == null)
return null;
MemberExpression memberExpression = expression.Body as MemberExpression;
if (memberExpression == null)
return null;
return memberExpression.Member as PropertyInfo;
}
}
.NET allows you to dynamically change structure of Class by implementing System.ComponentModel.ICustomTypeDescriptor. Most serializers support this interface.
// Sample Serialization
foreach(PropertyDescriptor p in TypeDescriptor.GetProperties(obj)){
string name = p.PropertyName;
object value = p.GetValue(obj);
}
Internally TypeDescriptor uses Reflection, but the implementation allows us to override reflection attributes easily.
Here are three steps of implementation,
// Implement System.ComponentModel.ICustomTypeDescriptor Interface on
// your Entity
public class MyEntity: System.ComponentModel.ICustomTypeDescriptor
{
....
// most methods needs only call to default implementation as shown below
System.ComponentModel.AttributeCollection
System.ComponentModel.ICustomTypeDescriptor.GetAttributes()
{
return TypeDescriptor.GetAttributes(this, true);
}
string System.ComponentModel.ICustomTypeDescriptor.GetClassName()
{
return TypeDescriptor.GetClassName(this, true);
}
string System.ComponentModel.ICustomTypeDescriptor.GetComponentName()
{
return TypeDescriptor.GetComponentName(this, true);
}
System.ComponentModel.TypeConverter System.ComponentModel.ICustomTypeDescriptor.GetConverter()
{
return TypeDescriptor.GetConverter(this, true);
}
System.ComponentModel.EventDescriptor System.ComponentModel.ICustomTypeDescriptor.GetDefaultEvent()
{
return TypeDescriptor.GetDefaultEvent(this, true);
}
System.ComponentModel.PropertyDescriptor System.ComponentModel.ICustomTypeDescriptor.GetDefaultProperty()
{
return TypeDescriptor.GetDefaultProperty(this, true);
}
object System.ComponentModel.ICustomTypeDescriptor.GetEditor(Type editorBaseType)
{
return TypeDescriptor.GetEditor(this, editorBaseType, true);
}
System.ComponentModel.EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents(Attribute[] attributes)
{
return TypeDescriptor.GetEvents(this, attributes, true);
}
System.ComponentModel.EventDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetEvents()
{
return TypeDescriptor.GetEvents(this, true);
}
System.ComponentModel.PropertyDescriptorCollection System.ComponentModel.ICustomTypeDescriptor.GetProperties(Attribute[] attributes)
{
return TypeDescriptor.GetProperties(this, attributes, true);
}
object System.ComponentModel.ICustomTypeDescriptor.GetPropertyOwner(System.ComponentModel.PropertyDescriptor pd)
{
return this;
}
// The Only method that needs different implementation is below
System.ComponentModel.PropertyDescriptorCollection
System.ComponentModel.ICustomTypeDescriptor.GetProperties()
{
// ... you are supposed to create new instance of
// PropertyDescriptorCollection with PropertyDescriptor
PropertyDescriptorCollection pdc = new PropertyDescriptorCollection();
foreach(PropertyDescriptor p in TypeDescriptor.GetProperties(this,true)){
// if readonly..
AtomPropertyDescriptor ap = new AtomPropertyDescriptor(p, p.Name);
// or
AtomPropertyDescriptor ap = new AtomPropertyDescriptor(p, p.Name,
true,
new XmlIgnoreAttribute(),
new ScriptIgnoreAttribute(),
new ReadOnlyAttribute());
pdc.Add(ap);
}
return pdc;
}
}
// And here is the AtomPropertyDescriptorClass
public class AtomPropertyDescriptor : PropertyDescriptor
{
PropertyDescriptor desc;
bool? readOnly = null;
public AtomPropertyDescriptor(PropertyDescriptor pd, string name,
bool? readOnly, params Attribute[] attrs) :
base(name, attrs)
{
desc = pd;
this.readOnly = readOnly;
}
public override bool CanResetValue(object component)
{
return desc.CanResetValue(component);
}
public override Type ComponentType
{
get
{
return desc.ComponentType;
}
}
public override object GetValue(object component)
{
return desc.GetValue(component);
}
public override bool IsReadOnly
{
get
{
if (readOnly.HasValue)
return readOnly.Value;
return desc.IsReadOnly;
}
}
public override Type PropertyType
{
get { return desc.PropertyType; }
}
public override void ResetValue(object component)
{
desc.ResetValue(component);
}
public override void SetValue(object component, object value)
{
desc.SetValue(component, value);
}
public override bool ShouldSerializeValue(object component)
{
return desc.ShouldSerializeValue(component);
}
}
I think what you are looking for is a custom Annotation Attribute like this:
<DisableEditAttribute(this.IsProjectStarted)>
Public Property NomInvertebrat As String
public override bool IsValid(bool value)
{
bool result = true;
// Add validation logic here.
if(value)
{
//Compare Current Value Against DB Value.
}
return result;
}
See MSDN: http://msdn.microsoft.com/en-us/library/cc668224(v=vs.98).aspx

accessing request parameters from inside validator

Is there a proper way to access other form fields from inside a validator?
Is there another solution than:
context.getViewRoot().findComponent("formid:exampleField:example")?
f.e I want to validate a city field inside a custom validator and checking if country is US.
Re-read your question and I am going to interpret it as this:
"You would like to write a custom validator that checks that if a city field exists, the country field is equal to 'US'"
So, I would look at going about this in the following fashion:
First create a validator interface:
#Documented
#ValidatorClass(value=CountryEqualsUSValidator.class)
#Target(ElementType.TYPE)
#Retention(RetentionPolicy.RUNTIME)
public #interface CountryEqualsUS {
String message() default "The country should be US for the city provided";
}
Then create a validator class:
public class CountryEqualsUSValidator implements Validator<CountryEqualsUS> {
public void initialize(CountryEqualsUS arg0) {
}
public boolean isValid(Object value) {
if(value != null && value instanceof YourBeanClass) {
YourBeanClass yourBeanClass = (YourBeanClass) value;
if(/*some test logic here*/) {
return true;
else {
return false;
}
}
return false;
}
}
Then on the class that you want to validate:
#CountryEqualsUS
public class YourBeanClass {
...
}
Then, finally, on your controller/action class, when the form is submitted, the city is a value for which you want to check the country, add this method and call it:
public boolean doValidation(YourBeanClass yourBeanClass) {
ClassValidator requestValidator = new ClassValidator(yourBeanClass.getClass());
InvalidValue[] validationMessages = requestValidator.getInvalidValues(yourBeanClass);
if (validationMessages != null && validationMessages.length > 0) {
for (int i = 0; i < validationMessages.length; i++) {
//Add a validation message to be displayed to the user
}
return false;
}
return true;
}