ChainID solidity version 0.6.12 - constructor

I've seen this code working in many factory contracts:
constructor() public {
uint chainId;
assembly {
chainId := chainid
}
DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
keccak256(bytes(name)),
keccak256(bytes('1')),
chainId,
address(this)
)
);
}
However, this works when I use
pragma solidity =0.5.16;
But if I use pragma solidity =0.6.12;, I get an error:
ParseError: Expected '(' but got '}' } ^
I tried to do the following:
constructor() public {
uint chainId;
assembly {
chainId := chainid()
}...
But then I get multiple errors on variables and functions saying:
TypeError: overriding public state variable is missing string public constant name = ...
How do I write the assembly line in solidity version 0.6.12?
Thank you,
Using version 0.5.16 it's not an option because many imports use 0.6.12 and they won't work...

//SPDX-License-Identifier: MIT
pragma solidity =0.6.12;
contract Test {
string public constant name = "Your Contract name";
constructor() public {
uint chainId;
assembly {
chainId := chainid()
}
bytes32 DOMAIN_SEPARATOR;
DOMAIN_SEPARATOR = keccak256(
abi.encode(
keccak256('EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)'),
keccak256(bytes(name)),
keccak256(bytes('1')),
chainId,
address(this)
)
);}}
Try this. If you upgrade your code to version 0.8.0 and above there is a variable available in global namespace called
block.chainid

Related

How to return a struct array in solidity with respect to web3j?

I've a smartcontract in which I've a function getLevels() that returns an array of struct type MLevel, defined at the beginning of smartcontract.
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.16;
struct MLevel{
address id;
string levelPrize;
uint levelNo;
uint levelCriteria;
}
contract Test{
MLevel[] public mlevels;
function addLevel(uint _levelCriteria, string memory _levelPrize)public payable{
MLevel memory level;
level.id = msg.sender;
level.levelCriteria = _levelCriteria;
level.levelPrize = _levelPrize;
mlevels.push(level);
}
function getLevels() public view returns(MLevel [] memory){
return mlevels;
}
}
The above smartcontract works absolutely fine in Remix ide. But when I try to create its java wrapper class using web3j, wrapper class does not generate and web3j ends with a weird error part 'struct MLevel' is keyword as shown below in the image

Contract "ItemMint" should be marked as abstract

code
trying to create contract using KIP37 as base contract. its works fine for the previous version of #klaytn/contracts - 0.9.0, but it is not working for #klaytn/contracts - 1.0.0
//SPDX-License-Identifier: Unlincesed
pragma solidity ^0.8.0;
import "#klaytn/contracts/contracts/KIP/token/KIP37/KIP37.sol";
// this line showing error "Contract ItemMint should be abstract
contract ItemMint is KIP37 {
string public name;
constructor(string memory _name) {
name = _name;
}
function mint(address to) public pure returns (bool) {
require(msg.sender == to , "your are authorized");
return true;
}
}
using npm i #klaytn/contracts - 1.0.0
The parent KIP37 contract defines a constructor that takes one argument - a string named uri_, so you need to invoke the parent constructor as well.
constructor(string memory _name, string memory uri_) KIP37(uri_) {
name = _name;
}

I get a declaration error in solidity when I create a variable with "var"

I'm trying to create a variable "project" to store data from a mapping but I get "Decalration error, undefined identifier" on project = projects[addr]
function getProjectInfo(address addr) public view returns (string memory name, string memory url, uint funds){
var project = projects[addr];
}```
Use explicit variable type definition:
pragma solidity ^0.5.8;
contract Test
{
struct Project
{
bytes32 name ;
}
mapping (address => Project) projects ;
constructor () public {
}
function getProjectInfo(address addr) public view returns (string memory name, string memory url, uint funds)
{
Project memory project = projects[addr];
// ...
}
}

How to declare constants in Solidity

I am really new to Solidity and smart contracts and would really appreciate some help. I am following a tutorial and this is the exact code they use. But when I compile the code I get this error:
ParserError: Expected primary expression.
address public constant approver = ;
pragma solidity ^0.6.0;
contract ApprovalContract {
address public sender;
address public receiver;
address public constant approver = ;
function deposit(address _receiver) external payable {
require(msg.value > 0);
sender = msg.sender;
receiver = _receiver;
}
function viewApprover() external pure returns(address) {
return(approver);
}
function approve() external {
require(msg.sender == approver);
receiver.transfer(address(this).balance);
}
}
The constant needs to be initialized
address public constant approver = YOURADDRESS;
There are 2 types of constant variables in Solidity:
Constants: a variable that is hardcoded in the smart contract and that you cannot change the value
Immutables: variables you can only define the value in the constructor and that cannot be updated afterwards
Here is an example:
uint256 public constant EXAMPLE_NUMBER = 123;
unit256 public immutable EXAMPLE_NUMBER_2;
constructor(uint256 _number) {
EXAMPLE_NUMBER_2 = _number;
}
You can read more about it here.

An error when executing truffle.migrate, Invalid number of parameters for "undefined"

I'am trying to deploy a smart contract :
`
pragma solidity >=0.4.21;
contract SimpleStorage {
uint public storedData;
constructor(uint initVal) public {
storedData = initVal;
}
function set(uint x) public {
storedData = x;
}
function get() view public returns (uint retVal) {
return storedData;
}
}`
I have created 2_deploy.js
var SimpleStorage = artifacts.require("SimpleStorage");
module.exports = function(deployer) {
// Pass 42 to the contract as the first constructor parameter
deployer.deploy(SimpleStorage, 42, {privateFor:
["ROAZBWtSacxXQrOe3FGAqJDyJjFePR5ce4TSIzmJ0Bc="]})
};
but when I execute truffle.migration I obtain this error:
'Error encountered, bailing. Network state unknown. Review
successful transactions manually.
Error: Invalid number of parameters for "undefined". Got 2
expected 1!
at Object.InvalidNumberOfParams
(/usr/lib/node_modules/truffle/build/webpack:/~/web3-eth-
contract/~/web3-core-helpers/src/errors.js:32:1)
at Object._createTxObject
(/usr/lib/node_modules/truffle/build/webpack:/~/web3-eth-
contract/src/index.js:699:1)
at Contract.deploy
(/usr/lib/node_modules/truffle/build/webpack:/~/web3-eth-
contract/src/index.js:504:1)
at Function.deploy
(/usr/lib/node_modules/truffle/build/webpack:/packages/truffle-
contract/lib/execute.js:214:1)
at constructor.detectNetwork.then.network
(/usr/lib/node_modules/truffle/build/webpack:/packages/truffle-
contract/lib/contract/constructorMethods.js:56:1)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:188:7)
Truffle v5.0.13 (core: 5.0.13)
Node v8.9.4''
do anyone knows how to deal with the problem?
Truffle has only recently added support for quorum private transactions. So you need Truffle version 5.0.14.
I hope that helps.