-
Notifications
You must be signed in to change notification settings - Fork 3
Smart Contract: Model Divided Version with Different Associations
bunnie-xia edited this page May 13, 2020
·
1 revision
Solidity is an object-oriented, high-level language for implementing smart contracts. Smart contracts are programs which govern the behaviour of accounts within the Ethereum state.
Below is a different version of the smart contracts for our project, it is written with solidity v0.6.7.
With models of User, Charity, Project and Donation.
And associations between model as:
- Project belongs to one Charity
- Charity has many Project
- Project has many Donation
- Donation belongs to one Project
pragma solidity >=0.4.21 <0.7.0;
contract Users {
// using metamask login
struct User {
string email;
string name;
}
mapping(address => User) public users;
//msg.sender: the person who currently connecting with the contract/ the account currently connecting with
function createUser(string memory email, string memory name) public {
address user = msg.sender;
require(
!users[user],
'User is already exist'
);
users[user] = User(email, name);
}
}
pragma solidity >=0.4.21 <0.7.0;
contract Charities {
struct Charity {
string name;
string pictureUrl;
string description;
}
mapping(address => Charity) public charities;
function createOrg(string memory organization, string memory info) public {
require(
!charities[msg.sender],
'Charity is already exist'
);
charities[msg.sender] = Charity(organization, info);
}
}
pragma solidity >=0.4.21 <0.7.0;
contract Projects {
struct Project {
string name;
string info;
uint donatedAmount; // reached amount
uint goal; // target amount
address payable charity;
Donation[] donations; // project has many donations
}
struct Donation {
address payable user;
uint amount;
bool isTransfered;
}
Project[] public projects; // charity has many projects
function createPro(string memory name, string memory info, uint donatedAmount, uint memory goal) public {
projects.push(Project({
name: name,
info: info,
donatedAmount: 0,
goal: goal,
chairty: msg.sender, // owner's wallet id..
donations: []
// address: address payable charity
}));
}
function donate(uint memory projectId, uint memory amount) public payable {
require(projectId < projects.length, 'Project does not exist');
projects[projectId].donations.push(Donation({
user: msg.sender, // donor's wallet id..
amount: amount,
isTransfered: false
}));
}
}
/*
going futher:
- verify if receiver/owner is correct before receiving transactions
- refund if expire
*/
Sources to look up:
solidity documentation: https://solidity.readthedocs.io/en/v0.6.7/