Am new to coding and solidity. trying to build a simple contract with the use case register one company, register two employees. Get all employees for registered company. Have the following code but am getting only the first employee that I registered
pragma solidity 0.7.5;
contract Employee {
uint256 companyIndex;
uint256 employeeIndex;
struct Company {
string name;
}
struct Employee {
uint256 companyId;
string name;
}
mapping(uint256 => Company) Companies;
mapping(uint256 => Employee) Employees;
///***************************************Company*******************************************///
function addCompany(string memory name) public
{
uint256 companyId = companyIndex++;
Companies[companyId] = Company(name);
}
function getCompany(uint256 companyId) public view returns (uint256, string memory)
{
return (companyId, Companies[companyId].name);
}
///***************************************Employee*******************************************///
function addEmployee(uint256 companyId,string memory name) public
{
uint256 employeeId = employeeIndex++;
Employees[employeeId] = Employee(companyId,name);//get error undeclared identifier
}
function getEmployee(uint256 companyId) public view returns (uint256, string memory)
{
return (companyId, Employees[companyId].name);
}
}
I registered the company (company 1). I registered employees (employee1, employee2). Now when I try to get employees for company1, I get only employee1. How can I get both employee1 and employee2.
Appreciate any help.
Related
I want to create a new function based on these codes below, that it can just fill in the ordinal number (first student is started by 1) to track out the struct student info in this case.
That's mean after I give the data of this first student to the struct by addInfo function:["Simon", 20, "CA USA", 10] Then my expected new function is that I just fill in this unit: 1, then it will appear this student detail
\`
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract ReferenceTypes {
address public owner;
struct student {
string Name;
uint Age;
string BirthPlace;
uint8 Marks;
}
constructor() {
owner = msg.sender;
}
mapping (address => student) public Info;
address[] public student_Info;
function addInfo(student memory _student, address _address) public {
require(owner == msg.sender, "Only admin can add Info!!!");
Info[_address] = _student;
student_Info.push(_address);
}
function count() public view returns(uint) {
return student_Info.length;
}
}
\`
Thanks so much guys
I tried a new mapping but failed
You can make another mapping that leads to your mapping Info.
uint public studentsCounter;
mapping (uint => mapping (address => student)) public Info;
function addInfo(student memory _student, address _address) public returns(uint){
require(owner == msg.sender, "Only admin can add Info!!!");
Info[studentsCounter][_address] = _student;
studentsCounter++;
student_Info.push(_address);
return studentsCounter-1;
}
Thus you can reach your struct by passing studentId(studentCounter) which addInfo returns and students address.
Another approach is to remake you array of addresses.
student[] public student_Info;
And push struct in this array.
function addInfo(student memory _student, address _address) public {
require(owner == msg.sender, "Only admin can add Info!!!");
Info[_address] = _student;
student_Info.push(_student);
}
So now you can display studentsInfo buy passing index in student_Info array.
When I tried to update string value inside smart contract, it is done successfully like this:
string private test;
function edit(string memory account) public {
test = account;
}
But after I used object and tried to edit string inside the object, I can not do that:
Hospital [] hospital_list;
struct Hospital {
int id;
string name;
string account;
}
function addHospital(int hosid, string memory name) public { // worked correctly
Hospital memory hospital = Hospital(hosid, name, 'null');
hospital_list.push(hospital);
}
function editHospitalAccount(int hosid, string memory account) public {
Hospital memory hospital = getHospital(hosid); // worked correctly
hospital.account = account; // this is not worked
}
How I can update the value?
Thanks,
I created an array of each address and amount of all users who have previously deposited a certain amount of ETH, then used the 'transfer' function (within : retireMyCoins()) to retrieve the amount and address of the user who is using the contract from the list. The user can then withdraw his ETH.
When compiling the contract, in the last function "retireMyCoins" the console returns the following error: 'Undeclared identifier'.
pragma solidity ^0.4.17;
contract myVault {
address[] public users;
uint[] public totalDeposited;
function sendToken(address user, uint amount) public payable {
require(msg.value > 0.001 ether);
user = msg.sender;
amount = msg.value;
users.push(msg.sender);
totalDeposited.push(msg.value);
}
function getUsers() public view returns (address[]) {
return users;
}
function getAmount() public view returns (uint[]) {
return totalDeposited;
}
function retireMyCoins() public {
require(user[msg.sender]);
require(amount[msg.value]);
user.transfer(this.amount);
}
}
You have to create amount as a store variable in the beginning of your contract. Also, to make it work as you expect, you should map the balance of each user, like the following:
...
mapping( address => uint ) balances;
function sendToken(address user, uint amount) public payable {
balances[msg.sender] = amount;
...
}
and then you can allow the withdrawal:
function retireMyCoins() public {
uint amountToWithdraw = balances[msg.sender]
balances[msg.sender] = 0;
msg.sender.transfer(amountToWithdraw);
}
Remember to zero the user balance before the transfer as above.
I'm trying to understand inheritance, interfaces and the call of functions from other smart contracts in Solidity. So I did this example. It has two interfaces, one for struct type definition and other for functions definition. It also has two contracts for implementing logic of functions defined in the interface. Finally, there is a main contract that interacts with functions contracts via the interface.
My question is how to share data written and read through the contract, since when interacting with functions contracts on the main smart contract, the struct data type that is written in registerPerson function is not read on getPerson function. It returns me a blank struct data type.
I think maybe is something that's missing in the code, but I don't know what it is.
Thanks in advance.
StructInterface interface Smart Contract
interface StructInterface {
struct Person {
uint personId;
string name;
string surname;
uint age;
}
}
FunctionsInterface interface Smart Contract
interface FunctionsInterface {
function addPerson (uint, string memory, string memory,uint ) external;
function askPerson(uint) external view returns (uint, string memory, string memory, uint);
}
RegisterPerson Smart Contract
import "./StructInterface.sol";
contract RegisterPerson is StructInterface {
mapping (uint => Person) persons;
function addPerson (
uint _personId,
string memory _name,
string memory _surname,
uint _age
) external {
persons[_personId].personId = _personId;
persons[_personId].name = _name;
persons[_personId].surname =_surname;
persons[_personId].age = _age;
}
}
GetPerson Smart Contract
import "./StructInterface.sol";
contract GetPerson is StructInterface {
mapping (uint => Person) persons;
function askPerson(uint _personId) external view
returns (uint personId, string memory name, string memory surname, uint age) {
personId = persons[_personId].personId;
name = persons[_personId].name;
surname = persons[_personId].surname;
age = persons[_personId].age;
return (personId, name, surname, age);
}
}
Main Smart Contract
import "./StructInterface.sol";
import "./RegisterPerson.sol";
import "./GetPerson.sol";
import "./FunctionsInterface.sol";
contract Main is StructInterface{
address regPersonA;
address getPersonA;
Person myPersons;
constructor (address _regPersonA, address _getPersonA) {
regPersonA = _regPersonA;
getPersonA = _getPersonA;
}
function registerPerson(
uint _personId,
string memory _name,
string memory _surname,
uint _age
) external {
FunctionsInterface interf = FunctionsInterface(regPersonA);
interf.addPerson(_personId, _name, _surname, _age);
}
function getPerson(
uint _personId
) external view
returns (uint personId, string memory name, string memory surname, uint age)
{
FunctionsInterface interf = FunctionsInterface(getPersonA);
return interf.askPerson(_personId);
}
}
below is my code I am not sure why give me this error.
// SPDX-License-Identifier: MIT
pragma solidity ^0.5.16;
// creating the contract
contract Rating {
// creating structure to model the product
struct Product {
uint id;
string name;
uint RatingCount;
}
// use mapping to get or fetch the contestant details
mapping(uint => product) public products;
// add a public state variable to keep track of product count
uint public productsCount;
constructor () public {
addProduct("Nike");
addProduct("Adidas");
}
// add a function to add product
// for private variable we use underscore in the start of variable _name
function addProduct(string memory _name) private {
productsCount++;
products[productsCount] = Product(productsCount, _name, 0);
}
}
Solidity is case-sensitive.
You have defined a struct named Product (uppercase P), but your mapping is using product (lowercase p).
Solution: Use the correct form Product
mapping(uint => Product) public products;
sorry I found the answer the problem is in mapping
mapping(uint => product) public products;
I mistakenly user small p instead of capital P. This gives me an error while migrating my smart contract because my struct name starts from Capital P (Product).
so instead of small p just change the p to P.
mapping(uint => Product) public products;