Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Program 1:
/* Another very basic contract. It demonstrates that a contract can retrieve and store the balance of its creator address.
Note that the creatorbalance value captured in the constructor is a snapshot in time. Later on, creator.balance will reflect whatever
it is now.
*/
pragma solidity >=0.4.0 <0.6.0;
contractcreatorBalanceChecker {
address creator;
uintcreatorbalance;
functioncreatorBalanceChecker() public
{
creator = msg.sender;
creatorbalance = creator.balance;
}
/**********
Standard kill() function to recover funds
**********/
}
=====================================================================================================
pragma
solidity
^0.4.18;
contract Election {
// Model a Candidate
struct Candidate {
uint id;
string name;
uintvoteCount;
}
// Store accounts that have voted
mapping(address => bool) public voters;
// Store Candidates
// Fetch Candidate
mapping(uint => Candidate) public candidates;
// Store Candidates Count
uint public candidatesCount;
// voted event
event votedEvent (
uint indexed _candidateId
);
function Election () public {
addCandidate("KamalHaasan");
addCandidate("RajniKanth");
}
function addCandidate (string _name) private {
candidatesCount ++;
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
}
function vote (uint _candidateId) public {
// require that they haven't voted before
require(!voters[msg.sender]);
// require a valid candidate
require(_candidateId> 0 && _candidateId<= candidatesCount);
// record that voter has voted
voters[msg.sender] = true;
// update candidate vote Count
candidates[_candidateId].voteCount ++;
// trigger voted event
votedEvent(_candidateId);
}
}
//section 1
pragma solidity ^0.4.18;
contractmysecondcontract{
string private name;
uint private age;
functionsetname(string newName) {
name=newName;
}
functiongetName() returns (string) {
return name;
}
functionsetAge(uintnewAge) {
age=newAge;
}
functiongetAge() returns (uint) {
return age;
}
contractmyfirstcontract{
functionmyfirstcontract(string _greeting) {
greeting= _greeting;
owner=msg.sender;
functionsetGreeting(string _greeting) {
require(owner ==msg.sender);
GreetingChanged(greeting, _greeting);
greeting= _greeting;
contract Counter {
uint256 count; // persistent contract storage
contractRevenueSharing {
address public creator;
mapping(uint => address) public shareholders;
uint public numShareholders;
functionRevenueSharing(address[] addresses) {
creator = msg.sender;
numShareholders = addresses.length;
for (uint i=0; i<addresses.length; i++) {
shareholders[i] = addresses[i];
}
}
function kill() {
if (msg.sender == creator) selfdestruct(creator);
}
}
pragma solidity ^0.4.18;
contractshapeCalc {
function rectangle(uint h, uint w) public view returns (uint p, uint s) {
p = 2 * (h + w);
s = h * w;
}
}
pragma solidity^0.4.21;
contractStack{
uint[]stack;
functionpush(uintdata)public{
stack.push(data);
}
functionpop()publicreturns(uintdata){
data=stack[stack.length-1];
stack.length-=1;
}
}
https://medium.com/openberry/deploying-smart-contracts-with-truffle-1c056b452cde