How to make a new contract using remix with a function in which I want to get the user details from my already deployed contract on the blockchain
Please be elaborate on the answer with some code as I am very new to solidity.
//deployed contract
struct User {
address wallet_address;
string userId;
string first_name;
string last_name;
}
mapping(address => User) users;
mapping(string => address) walletAccount;
mapping(address => uint) public balanceOf;
User public newUser;
uint public length;
address public owner;
constructor() public {
owner = msg.sender;
}
event userCreated (
address wallet_address,
string userId,
string first_name,
string last_name
);
function createNewUser(address userAcc, string memory _id, string memory firstName, string memory lastName) public {
User storage user = users[userAcc];
users[userAcc] = User(userAcc,_id,firstName,lastName);
}
function getUser(address _userAddress, string memory _lastName) public view returns (address, string memory, string memory, string memory) {
User memory user = users[_userAddress];
return (users[_userAddress].wallet_address, users[_userAddress].userId, users[_userAddress].first_name, users[_userAddress].last_name);
}
pragma solidity ^0.4.0;
contract crr019_week4assignment {
// Student Account Structure
struct StudentAccount {
string Username;
address Metamask;
uint Age;
string EmailAddress;
bool BlockchainTrackEnrolled;
StudentType ClassLevel;
}
constructor() public {
}
StudentAccount [] public StudentInformation;
enum StudentType {
Freshman,
Sophomore,
Junior,
Senior,
Graduate
}
function AddStudent (string _Username, address _Metamask, uint _Age, string _EmailAddress, bool _BlockchainTrackEnrolled, StudentType _ClassLevel ) public {
// Anything inside of this function can use any of the parameters
StudentAccount memory NewStudent = StudentAccount({Username:_Username, Metamask:_Metamask, Age:_Age, Email:_EmailAddress, BlockchainTrackEnrolled:_BlockchainTrackEnrolled, Student:_ClassLevel});
}
}
Getting TypeError: Named argument does not match functions declaration.
what you should correct in your smartcontract
you must add memory after your string arguments
replace email and student with EmailAddress and ClassLevel
correct code
You have a typo when you are creating your struct instance : Use EmailAddress instead of Email.
Each property name used in your json ({}) must match names declared in the struct.
The following correction has to be done in arguments and function declaration:
instead of email you must use EmailAddress
instead of Student you must use ClassLevel
for string arguments you should use memory
The corrected segment is as follow:
function AddStudent (string memory _Username, address _Metamask, uint _Age, string memory _EmailAddress, bool _BlockchainTrackEnrolled, StudentType _ClassLevel ) public {
// Anything inside of this function can use any of the parameters
StudentAccount memory NewStudent = StudentAccount({Username:_Username, Metamask:_Metamask, Age:_Age, EmailAddress:_EmailAddress, BlockchainTrackEnrolled:_BlockchainTrackEnrolled, ClassLevel:_ClassLevel});
Inside function AddStudent correct the naming convention from email to EmailAddress. Also you can add memory after each string variable(not mandatory).
Naming should be done properly.
Corrected code :
pragma solidity 0.6.12;
contract crr019_week4assignment {
// Student Account Structure
struct StudentAccount {
string Username;
address Metamask;
uint Age;
string EmailAddress;
bool BlockchainTrackEnrolled;
StudentType ClassLevel;
}
constructor() public { }
StudentAccount [] public StudentInformation;
enum StudentType {
Freshman,
Sophomore,
Junior,
Senior,
Graduate
}
function AddStudent (string memory _Username, address _Metamask, uint _Age, string memory _EmailAddress, bool _BlockchainTrackEnrolled, StudentType _ClassLevel ) public {
// Anything inside of this function can use any of the parameters
StudentAccount memory NewStudent = StudentAccount({Username:_Username, Metamask:_Metamask, Age:_Age, EmailAddress:_EmailAddress, BlockchainTrackEnrolled:_BlockchainTrackEnrolled, ClassLevel:_ClassLevel});
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
contract crr019_week4assignment {
// Student Account Structure
struct StudentAccount {
string Username;
address Metamask;
uint Age;
string EmailAddress;
bool BlockchainTrackEnrolled;
StudentType ClassLevel;
}
constructor() {}
StudentAccount [] public StudentInformation;
enum StudentType {
Freshman,
Sophomore,
Junior,
Senior,
Graduate
}
function addStudent (string memory _Username, address _Metamask, uint _Age, string memory _EmailAddress, bool _BlockchainTrackEnrolled, StudentType _Classlevel) public {
StudentAccount memory NewStudent = StudentAccount(_Username, _Metamask, _Age, _EmailAddress, _BlockchainTrackEnrolled, _Classlevel);
StudentInformation.push(NewStudent);
}
}
I have the above code
mapping(string => Owner) public Owners;
function addOwner( string memory name, string memory surname, string memory fathername, string memory mothername, string memory ethnicityId, string memory id) public returns(int8)
{
Owner memory owner;
owner.name = name;
owner.surname = surname;
owner.fathername = fathername;
owner.mothername = mothername;
owner.ethnicityId = ethnicityId;
owner.id = id;
Owners[id] = owner;
return 0;
}
function getOwnerName(string memory id) public returns(string memory, string memory) {
Owner storage owner = Owners[id];
return (owner.name, owner.surname);
}
I am inserting an Owner with addOwner and try to retrieve with getOwnerName but Ringeby network is not returning the data I have inserted. The function returns the right data when I use the javascript VM on remix IDE
I am creating a contract for identification person, and I need to verify if there are some contracts with the same address, email, or phone number.
Example:
contract Person {
//date of create
uint public dateCreate;
//name of person
string public name;
//variables to be validates
string public email;
string public phone;
// constructor
function Person(string _name, string _email, string _phone) public {
name = _name;
email = _email;
phone = _phone;
owner = msg.sender;
}
}
I have the option to save the address contract in a mapping with the key email or phone.
contract RegisterPerson {
//save the contract address person using the key of the email
mapping(bytes32=>address) public addressEmail;
}
There is this solution, but I believe it's not the better because the mapping it will be very big and the contract expensive.
Does anybody know another solution?
You shouldn't be using a contract to represent an object like you're attempting to do here. Not only is it very costly as contract deployments are usually much more expensive than transactions, but you also can't guarantee uniqueness.
You should use a struct to represent the individual.
contract PersonStorage {
struct Person {
uint dateCreate;
string name;
string email;
string phone;
}
mapping(bytes32 => Person) persons;
function addPerson(string _name, string _email, string _phone) public {
Person memory person;
person.name = _name;
person.email = _email;
person.phone = _phone;
persons[keccak256(person.email)] = person;
}
...
}
Now, your contract is data storage for all Persons. You can deploy this version and pass the contract address to whatever contracts need access to it. You'll also have all your data centralized in case you need to allow multiple business logic contracts to use it or if you need to upgrade your business contract.
EDIT - I should note that if this is in its own contract, you'll have to change from string to bytes32. You can't send strings between contracts.
I'm having an entity class according to the database, which includes a Date type field called start_date. There's a search bar within a jsp page where the user will be able to search for the data using the name or the date. But the search value which we're passing, into the method should be a String value.
But where as the start_date is a type of Date, and I'm unable to use that to search for a data since the search parameter is a string. What could I do to convert Date type into String? These are the sample entity fields I've got.
P.S: This is a Maven Hibernate Spring MVC project with MySQL
#Basic
#Column(name = "name")
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
#Basic
#Column(name = "start_date")
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
Any help would be appreciated.
Add an <input type="date"/> and bind it in a controller method with #RequestParam Date searchDate. The line format is yyyy-mm-dd so make sure your binder supports, because I'm not sure if it does so out of the box.
pubilc Static getFromattedDate(date,dateFormatter){
SimpleDateFormat formatter = new SimpleDateFormat(dateFormat);
return formatter.format(date);
}
Creating this method in your CalendarUtil.java you can call in from your controller.then you can format your inputs and return as string as you want.