I tried to write a smart contract which is able to complaining. Here is the code
pragma solidity ^0.4.2;
contract Complain {
//Model Complain
struct compalins {
uint id;
string category;
string desc;
string complainer;
}
mapping( uint => complains) public newComplain;
uint public complainCount;
function Complain () public {
addComplain("c1","bhbh","bybhb");
addComplain("c2","bhbh","bybhb");
}
function addComplain (string _category,string desc,string complainer){
complainCount ++;
// newComplain[ComplainCount] = complains(complainCount,_category,desc,complainer);
}
}
in this mapping function gives an error and say structure of the complains haven't Unique value. But id is Unique.
Please help me to solve this issue
You misspelled "complains" when you declared your struct. (You spelled it "compalins" there.) So the error on the mapping line is that there's no such identifier "complains". If you fix the typo, the code will compile.
Related
pragma solidity ^0.8.0;
contract Counter {
uint public number;
string public name;
constructor(string memory input_name, uint input_number){
name = input_name;
number = input_number;
}
function add () public {
number ++;
}
function subtract() public {
number --;
}
function update_name(string memory update) public {
name = update;
}
I wrote a very basic contract here and I'm wondering why I should write a return statement for each of my functions as it is already returns a value.
Could someone explain my error or the point of the return statement here?
Solidity have the "implicit return" feature, so as you noticed, you may skip the "return" statement in some cases. However, it's discourages, since you're not explicit enough and may cause a lot of confusion and issues.
For details please check the following links:
https://medium.com/#pareshmasani/ethereum-smart-contract-implicit-return-can-go-wrong-33b41c93dbba
https://github.com/ethereum/solidity/issues/3134
https://blog.openzeppelin.com/instadapp-audit/
This might be a beginner question, but the thing is confusing
I can see from samples like bellow
https://github.com/acloudfan/Blockchain-Course-Basic-Solidity/blob/93ca256bcf8c436c144425291257dcff5c3b269f/test/TestMetacoin.sol#L18
MetaCoin meta = new MetaCoin();
That a contract can be created/instantiated inside another contract, I want to understand what is happening here? does this create a new contract and deploy, therefore having a new address? or does it works or behaves like an ordinary class like in C++ or java, etc?
I dont know what is the context of the meta instance at this point.
Can someone help explain?
Secondly, this might be related,
What does it mean when two contracts are defined in a single Sol file and the second contract instantiated the first one like below?
pragma solidity 0.4.21;
contract Base {
uint public dataA;
bytes32 public dataB;
function setAB(uint a, bytes32 b) public {
dataA = a;
dataB = b;
}
function getA() public view returns(uint) {
return dataA ;
}
function getB() public view returns(bytes32) {
return dataB ;
}
}
contract Extra {
Base base;
function Extra() public {
base = new Base();
}
...
}
Does the contract base deployed and will have its own address?
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];
// ...
}
}
I've been doing some research around this issue, but I couldn't find a definite answer. I'm using solidity 0.4.24.
I have a contract like this:
contract {
struct FutureOperation is Ownable {
uint256 date;
uint256 price;
uint256 amount;
string name;
}
FutureOperation[] futureOperations;
// ...
function getAllFutureOperations() public onlyOwner returns (FutureOperation[]) {
return futureOperations;
}
}
When I compile this in Remix I get the following error:
browser/myfuturetoken.sol:53:64: TypeError: This type is only supported in the new experimental ABI encoder. Use "pragma experimental ABIEncoderV2;" to enable the feature.
I found some blog posts saying that I should de-structure the fields in the struct to return them as arrays of the primitive types. So, in this case, it would look something like this:
function getAllFutureOperations() public onlyOwner returns (uint256[] dates, uint256[] prices, uint256[] amounts, string[] names) {
return futureOperations;
}
Is there an alternative for that? Are the newer compilers capable of returning an array of structs?
Thanks.
As error stated, returning dynamic array is not supported by compiler yet. However, experiment feature supported it. To use experimental compiler, you need to make some changes as follows,
pragma experimental ABIEncoderV2;
contract myContract{
struct FutureOperation {
uint256 date;
uint256 price;
uint256 amount;
string name;
}
string[] futureOperations;
function getAllFutureOperations() public view returns (string[] memory) {
return futureOperations;
}
}
Note: make sure to not use experimental things in production version
This is my contract code. Here I'm trying to store the coordinates of a particular trip. While storing the information contract executing fine. But When I retrieve the data, It should give the array of coordinates. But it is throwing an error.
reason: 'insufficient data for uint256 type'
contract TripHistory {
struct Trip {
string lat;
string lon;
}
mapping(string => Trip[]) trips;
function getTrip(string _trip_id) public view returns (Trip[]) {
return trips[_trip_id];
}
function storeTrip(string _trip_id, string _lat, string _lon) public {
trips[_trip_id].push(Trip(_lat, _lon));
}
}
What I'm missing here. Is there any other way to achieve what I'm trying here?
P.S: I'm new to solidity.
First of returning structs is not supported in Solidity directly. Instead you need to return every individual element in the struct as below.
Function xyz(uint256 _value) returns(uint256 User.x, uint256 User.y)
public {}
But then there’s an experimental feature that will help you with returning struct. All that you need to do is add the following after your first pragma line
pragma experimental ABIEncoderV2;
then continue with your code. That should work with no changes to your code.
An example of abiencoderv2 returning struct can be found at this link
It is not possible in solidity to return struct array.
As jlo said in this link, after version 0.8.0, it is possible to return a struct. jlo describes how to set and return an element of array of struct. Here I describe how to set, reset, and return a struct type variable.
I tested it and my test environment is:
private Ethereum network
Geth version 1.10.9-stable (for private network)
Slocjs Compiler version 0.8.7
web3js version 1.5.1
Note, you have to first define the struct type outside of any function inside the contract.
A supper intuitive example code is as follows:
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract testContract {
struct funcResultType {
uint[] var1;
string[] var2;
string message;
}
funcResultType private funcResult;
function testSetFunc(string memory inputVar) public payable {
funcResult.var1.push(123);
funcResult.var2.push(inputVar);
funcResult.message = "Done!";
}
function testResetFunc() public payable {
delete funcResult; // reset variales
}
function testGetFunc() public view returns (funcResultType memory){
return funcResult;
}
}
The result of the test with Web3js in the console is as follow:
As you can see the whole variables are accessible. I upload its web3js code in Github in this link.