Created
October 11, 2020 06:31
-
-
Save gyan0890/859a2907ec8ae0f13c3e39bbb93a59c5 to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.6.12+commit.27d51765.js&optimize=false&gist=
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
address delegate; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
enum Stage {Init,Reg, Vote, Done} | |
Stage public stage = Stage.Init; | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
event votingCompleted(); | |
uint startTime; | |
//modifiers | |
modifier validStage(Stage reqStage) | |
{ require(stage == reqStage); | |
_; | |
} | |
/// Create a new ballot with $(_numProposals) different proposals. | |
constructor(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 2; // weight is 2 for testing purposes | |
proposals.length = _numProposals; | |
stage = Stage.Reg; | |
startTime = now; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function register(address toVoter) public validStage(Stage.Reg) { | |
//if (stage != Stage.Reg) {return;} | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
voters[toVoter].voted = false; | |
if (now > (startTime+ 30 seconds)) {stage = Stage.Vote; } | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public validStage(Stage.Vote) { | |
// if (stage != Stage.Vote) {return;} | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
if (now > (startTime+ 30 seconds)) {stage = Stage.Done; emit votingCompleted();} | |
} | |
function winningProposal() public view validStage(Stage.Done) returns (uint8 _winningProposal) { | |
//if(stage != Stage.Done) {return;} | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
assert (winningVoteCount > 0); | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
// address delegate; | |
} | |
struct Proposal { | |
uint voteCount; // could add other data about proposal | |
} | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
constructor(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 2; | |
proposals.length = _numProposals; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function register(address toVoter) public { | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
voters[toVoter].voted = false; | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public { | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
} | |
function winningProposal() public view returns (uint8 _winningProposal) { | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract Coin { | |
// The keyword "public" makes those variables | |
// readable from outside. | |
address public minter; | |
mapping (address => uint) public balances; | |
// Events allow light clients to react on | |
// changes efficiently. | |
event Sent(address from, address to, uint amount); | |
// This is the constructor whose code is | |
// run only when the contract is created. | |
constructor() public{ | |
minter = msg.sender; | |
} | |
function mint(address receiver, uint amount) public { | |
if (msg.sender != minter) return; | |
balances[receiver] += amount; | |
} | |
function send(address receiver, uint amount) public { | |
if (balances[msg.sender] < amount) return; | |
balances[msg.sender] -= amount; | |
balances[receiver] += amount; | |
emit Sent(msg.sender, receiver, amount); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
// Imagine a big integer that the whole world could share | |
contract SimpleStorage { | |
uint storedData; | |
function set(uint x) public { | |
storedData = x; | |
} | |
function get() constant public returns (uint) { | |
return storedData; | |
} | |
function increment (uint n) public { | |
storedData = storedData + n; | |
return; | |
} | |
function decrement (uint n) public { | |
storedData = storedData - n; | |
return; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
enum Stage {Init,Reg, Vote, Done} | |
Stage public stage = Stage.Init; | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
uint startTime; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
constructor(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 2; // weight is 2 for testing purposes | |
proposals.length = _numProposals; | |
stage = Stage.Reg; | |
startTime = now; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function register(address toVoter) public { | |
if (stage != Stage.Reg) {return;} | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
voters[toVoter].voted = false; | |
if (now > (startTime+ 10 seconds)) {stage = Stage.Vote; startTime = now;} | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public { | |
if (stage != Stage.Vote) {return;} | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
if (now > (startTime+ 10 seconds)) {stage = Stage.Done;} | |
} | |
function winningProposal() public constant returns (uint8 _winningProposal) { | |
if(stage != Stage.Done) {return;} | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.0; | |
contract Greeter { | |
string public yourName; // data | |
/* This runs when the contract is executed */ | |
constructor() public { | |
yourName = "World"; | |
} | |
function set(string name)public { | |
yourName = name; | |
} | |
function hello() constant public returns (string) { | |
return yourName; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: educationwithgyan42@gmail.com | |
pragma solidity ^0.6.0; | |
contract Bidder { | |
string public name; | |
uint public bidAmount = 20000; | |
bool public eligible; | |
uint constant minBid = 1000; | |
function setName(string memory _name, uint _bidAmount) public { | |
name = _name; | |
bidAmount = _bidAmount; | |
} | |
function determineEligibility() public { | |
if(bidAmount > minBid){ | |
eligible = true; | |
} | |
else | |
eligible = false; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.6; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/token/ERC20/ERC20.sol"; | |
contract AaveLendingTrial { | |
// Retrieve LendingPool address | |
LendingPoolAddressesProvider provider = LendingPoolAddressesProvider(address(0x24a42fD28C976A61Df5D00D0599C34c4f90748c8)); // mainnet address, for other addresses: https://docs.aave.com/developers/developing-on-aave/deployed-contract-instances | |
LendingPool lendingPool = LendingPool(provider.getLendingPool()); | |
// Input variables | |
address ethAddress = address(0xAb8483F64d9C6d1EcF9b849Ae677dD3315835cb2); // mainnet DAI | |
uint256 amount = 10 * 1e18; | |
uint16 referral = 0; | |
// Approve LendingPool contract to move your DAI | |
function approveTheLoan() public { | |
IERC20(ethAddress).approve(provider.getLendingPoolCore(), amount); | |
// Deposit 1000 DAI | |
lendingPool.deposit(ethAddress, amount, referral); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.6; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.5.0/contracts/token/ERC20/ERC20.sol"; | |
contract Trial { | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Addition { | |
uint256 public a; | |
uint256 public b; | |
constructor(uint256 _a, uint256 _b) public{ | |
a = _a; | |
b = _b; | |
} | |
function Add(uint256 var1, uint256 var2) public pure returns(uint256) { | |
uint256 c = var1 + var2; | |
return c; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract AssignmentERC20 { | |
string public constant name = "AssignmentERC20"; | |
string public constant symbol = "WNXT"; | |
uint8 public constant decimals = 0; | |
event Approval(address indexed tokenOwner, address indexed spender, uint tokens); | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
mapping(address => uint256) balances; | |
mapping(address => mapping (address => uint256)) allowed; | |
uint256 totalSupply_; | |
using SafeMath for uint256; | |
constructor(uint256 total) public { | |
totalSupply_ = total; | |
balances[msg.sender] = totalSupply_; | |
} | |
function totalSupply() public view returns (uint256) { | |
return totalSupply_; | |
} | |
function balanceOf(address tokenOwner) public view returns (uint) { | |
return balances[tokenOwner]; | |
} | |
function transfer(address receiver, uint numTokens) public returns (bool) { | |
require(numTokens <= balances[msg.sender]); | |
balances[msg.sender] = balances[msg.sender].sub(numTokens); | |
balances[receiver] = balances[receiver].add(numTokens); | |
emit Transfer(msg.sender, receiver, numTokens); | |
return true; | |
} | |
function approve(address delegate, uint numTokens) public returns (bool) { | |
allowed[msg.sender][delegate] = numTokens; | |
emit Approval(msg.sender, delegate, numTokens); | |
return true; | |
} | |
function allowance(address owner, address delegate) public view returns (uint) { | |
return allowed[owner][delegate]; | |
} | |
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) { | |
require(numTokens <= balances[owner]); | |
require(numTokens <= allowed[owner][msg.sender]); | |
balances[owner] = balances[owner].sub(numTokens); | |
allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens); | |
balances[buyer] = balances[buyer].add(numTokens); | |
emit Transfer(owner, buyer, numTokens); | |
return true; | |
} | |
} | |
library SafeMath { | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract Ballot { | |
struct Voter { | |
uint weight; | |
bool voted; | |
uint8 vote; | |
address delegate; | |
} | |
struct Proposal { | |
uint voteCount; | |
} | |
address chairperson; | |
mapping(address => Voter) voters; | |
Proposal[] proposals; | |
/// Create a new ballot with $(_numProposals) different proposals. | |
constructor(uint8 _numProposals) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
proposals.length = _numProposals; | |
} | |
/// Give $(toVoter) the right to vote on this ballot. | |
/// May only be called by $(chairperson). | |
function giveRightToVote(address toVoter) public { | |
if (msg.sender != chairperson || voters[toVoter].voted) return; | |
voters[toVoter].weight = 1; | |
} | |
/// Delegate your vote to the voter $(to). | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; // assigns reference | |
if (sender.voted) return; | |
while (voters[to].delegate != address(0) && voters[to].delegate != msg.sender) | |
to = voters[to].delegate; | |
if (to == msg.sender) return; | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegateTo = voters[to]; | |
if (delegateTo.voted) | |
proposals[delegateTo.vote].voteCount += sender.weight; | |
else | |
delegateTo.weight += sender.weight; | |
} | |
/// Give a single vote to proposal $(toProposal). | |
function vote(uint8 toProposal) public { | |
Voter storage sender = voters[msg.sender]; | |
if (sender.voted || toProposal >= proposals.length) return; | |
sender.voted = true; | |
sender.vote = toProposal; | |
proposals[toProposal].voteCount += sender.weight; | |
} | |
function winningProposal() public view returns (uint8 _winningProposal) { | |
uint256 winningVoteCount = 0; | |
for (uint8 prop = 0; prop < proposals.length; prop++) | |
if (proposals[prop].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[prop].voteCount; | |
_winningProposal = prop; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () public { | |
ballotToTest = new Ballot(2); | |
} | |
function checkWinningProposal () public { | |
ballotToTest.vote(1); | |
Assert.equal(ballotToTest.winningProposal(), uint(1), "1 should be the winning proposal"); | |
} | |
function checkWinninProposalWithReturnValue () public view returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract BasicERC20 { | |
string public constant name = "AssignmentERC20"; | |
string public constant symbol = "GYAN"; | |
uint8 public constant decimals = 0; | |
//The indexed parameters for logged events will allow you | |
//to search for these events using the indexed parameters as filters. | |
event Approval(address indexed tokenOwner, address indexed spender, uint tokens); | |
event Transfer(address indexed from, address indexed to, uint tokens); | |
mapping(address => uint256) balances; | |
mapping(address => mapping (address => uint256)) allowed; | |
uint256 totalSupply_; | |
using SafeMath for uint256; | |
constructor(uint256 total) public { | |
totalSupply_ = total; | |
balances[msg.sender] = totalSupply_; | |
} | |
function totalSupply() public view returns (uint256) { | |
return totalSupply_; | |
} | |
function balanceOf(address tokenOwner) public view returns (uint) { | |
return balances[tokenOwner]; | |
} | |
function transfer(address receiver, uint numTokens) public returns (bool) { | |
require(numTokens <= balances[msg.sender]); | |
balances[msg.sender] = balances[msg.sender].sub(numTokens); | |
balances[receiver] = balances[receiver].add(numTokens); | |
emit Transfer(msg.sender, receiver, numTokens); | |
return true; | |
} | |
function approve(address delegate, uint numTokens) public returns (bool) { | |
allowed[msg.sender][delegate] = numTokens; | |
emit Approval(msg.sender, delegate, numTokens); | |
return true; | |
} | |
function allowance(address owner, address delegate) public view returns (uint) { | |
return allowed[owner][delegate]; | |
} | |
function transferFrom(address owner, address buyer, uint numTokens) public returns (bool) { | |
require(numTokens <= balances[owner]); | |
require(numTokens <= allowed[owner][msg.sender]); | |
balances[owner] = balances[owner].sub(numTokens); | |
allowed[owner][msg.sender] = allowed[owner][msg.sender].sub(numTokens); | |
balances[buyer] = balances[buyer].add(numTokens); | |
emit Transfer(owner, buyer, numTokens); | |
return true; | |
} | |
} | |
library SafeMath { | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Beginner { | |
uint256 public a; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract BuySell { | |
struct Commodity{ | |
//Owner of the commodity | |
address owner; | |
uint256 uid; | |
uint256 price; | |
uint256 quantity; | |
bool availability; | |
} | |
//Each commodity will have a unique id | |
mapping(uint256 => Commodity ) commodities; | |
modifier quantityGreaterThanZero(uint256 _quantity) { | |
require(_quantity > 0, "Quantity of a commodity cannot be zero"); | |
_; | |
} | |
modifier ownerIsModifer(uint256 _uid) { | |
require(commodities[_uid].owner == msg.sender); | |
_; | |
} | |
constructor(uint256 _uid, uint256 _price, uint256 _quantity) public { | |
newCommodity(_uid, _price, _quantity); | |
} | |
function newCommodity(uint256 _uid, uint256 _price, uint256 _quantity) public quantityGreaterThanZero(_quantity) { | |
commodities[_uid] = Commodity(msg.sender, _uid, _price, _quantity, true); | |
} | |
function updateCommodity(uint256 _uid, uint256 _quantity) public quantityGreaterThanZero(_quantity) ownerIsModifer(_uid){ | |
commodities[_uid].quantity += _quantity; | |
if(commodities[_uid].availability == false) | |
commodities[_uid].availability = true; | |
} | |
function sellCommodity(uint256 _uid, uint256 _quantity) public quantityGreaterThanZero(_quantity) ownerIsModifer(_uid){ | |
commodities[_uid].quantity -= _quantity; | |
if(commodities[_uid].quantity == 0) | |
commodities[_uid].availability = false; | |
} | |
function isAvailable(uint256 _uid) public view returns(bool) { | |
return (commodities[_uid].availability); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.4.0; | |
contract Candidate { | |
struct candidate { | |
uint256 id; | |
string name; | |
string state; | |
uint256 funds; | |
bool active; | |
} | |
mapping (uint256 => candidate) public candidateidentification; | |
function registerCandidate(uint256 _id, string memory _name, string memory _state, uint256 _funds) public { | |
candidate memory newCandidate = candidate(_id, _name, _state, _funds, true); | |
if(_funds > 100) | |
newCandidate.funds = _funds; | |
else | |
newCandidate.funds = 0; | |
if(newCandidate.funds > 0) | |
newCandidate.active = true; | |
else | |
newCandidate.active = false; | |
candidateidentification[_id] = newCandidate; | |
} | |
function getCandidate(uint256 _id) public view returns(uint256){ | |
candidate storage candidate1 = candidateidentification[_id]; | |
return candidate1.funds; | |
} | |
function performCampaign(uint256 _id, uint256 campaignType) public returns(bool) { | |
if(checkCandidate(_id)){ | |
candidate storage candidate1 = candidateidentification[_id]; | |
if(campaignType == 1) | |
candidate1.funds = candidate1.funds-50; | |
else if(campaignType == 2) | |
candidate1.funds = candidate1.funds-20; | |
} | |
else | |
return false; | |
return true; | |
} | |
function checkCandidate(uint256 _id) public returns(bool) { | |
candidate storage candidate2 = candidateidentification[_id]; | |
if(candidate2.funds > 0) | |
candidate2.active = true; | |
else | |
candidate2.active = false; | |
return(candidate2.active); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//SPDX-License-Identifier: educationwithgyan42@gmail.com | |
pragma solidity ^0.6.6; | |
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/FlashLoanReceiverBase.sol"; | |
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPoolAddressesProvider.sol"; | |
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPool.sol"; | |
contract CrowdBank is FlashLoanReceiverBase { | |
enum ProposalState { | |
WAITING, | |
ACCEPTED, | |
REPAID | |
} | |
struct Proposal { | |
address payable lender; | |
uint loanId; | |
ProposalState state; | |
uint rate; | |
uint amount; | |
} | |
enum LoanState { | |
ACCEPTING, | |
LOCKED, | |
SUCCESSFUL, | |
FAILED | |
} | |
struct Loan { | |
address borrower; | |
LoanState state; | |
uint dueDate; | |
uint amount; | |
uint proposalCount; | |
uint collected; | |
uint startDate; | |
bytes32 mortgage; | |
mapping (uint=>uint) proposal; | |
} | |
Loan[] public loanList; | |
Proposal[] public proposalList; | |
mapping (address=>uint[]) public loanMap; | |
mapping (address=>uint[]) public lendMap; | |
constructor(address _addressProvider) FlashLoanReceiverBase(_addressProvider) public { | |
} | |
/** | |
This function is called after your contract has received the flash loaned amount | |
*/ | |
function executeOperation( | |
address _reserve, | |
uint256 _amount, | |
uint256 _fee, | |
bytes calldata _params | |
) | |
external | |
override | |
{ | |
require(_amount <= getBalanceInternal(address(this), _reserve), "Invalid balance, was the flashLoan successful?"); | |
// | |
// Your logic goes here. | |
// !! Ensure that *this contract* has enough of `_reserve` funds to payback the `_fee` !! | |
// | |
uint totalDebt = _amount.add(_fee); | |
transferFundsBackToPoolInternal(_reserve, totalDebt); | |
} | |
function hasActiveLoan(address borrower) public view returns(bool) { | |
uint validLoans = loanMap[borrower].length; | |
if(validLoans == 0) return false; | |
Loan storage obj = loanList[loanMap[borrower][validLoans-1]]; | |
if(loanList[validLoans-1].state == LoanState.ACCEPTING) return true; | |
if(loanList[validLoans-1].state == LoanState.LOCKED) return true; | |
return false; | |
} | |
function newLoan(uint amount, uint dueDate, bytes32 mortgage) public { | |
if(hasActiveLoan(msg.sender)) return; | |
uint currentDate = block.timestamp; | |
loanList.push(Loan(msg.sender, LoanState.ACCEPTING, dueDate, amount, 0, 0, currentDate, mortgage)); | |
loanMap[msg.sender].push(loanList.length-1); | |
} | |
function newProposal(uint loanId, uint rate) public payable { | |
if(loanList[loanId].borrower == address(0) || loanList[loanId].state != LoanState.ACCEPTING) | |
return; | |
proposalList.push(Proposal(msg.sender, loanId, ProposalState.WAITING, rate, msg.value)); | |
lendMap[msg.sender].push(proposalList.length-1); | |
loanList[loanId].proposalCount++; | |
loanList[loanId].proposal[loanList[loanId].proposalCount-1] = proposalList.length-1; | |
} | |
function getActiveLoanId(address borrower) public view returns(uint) { | |
uint numLoans = loanMap[borrower].length; | |
if(numLoans == 0) return (2**64 - 1); | |
uint lastLoanId = loanMap[borrower][numLoans-1]; | |
if(loanList[lastLoanId].state != LoanState.ACCEPTING) return (2**64 - 1); | |
return lastLoanId; | |
} | |
function revokeMyProposal(uint id) public { | |
uint proposeId = lendMap[msg.sender][id]; | |
if(proposalList[proposeId].state != ProposalState.WAITING) return; | |
uint loanId = proposalList[proposeId].loanId; | |
if(loanList[loanId].state == LoanState.ACCEPTING) { | |
// Lender wishes to revoke his ETH when proposal is still WAITING | |
proposalList[proposeId].state = ProposalState.REPAID; | |
msg.sender.transfer(proposalList[proposeId].amount); | |
} | |
else if(loanList[loanId].state == LoanState.LOCKED) { | |
// The loan is locked/accepting and the due date passed : transfer the mortgage | |
if(loanList[loanId].dueDate < now) return; | |
loanList[loanId].state = LoanState.FAILED; | |
for(uint i = 0; i < loanList[loanId].proposalCount; i++) { | |
uint numI = loanList[loanId].proposal[i]; | |
if(proposalList[numI].state == ProposalState.ACCEPTED) { | |
// transfer mortgage | |
} | |
} | |
} | |
} | |
function lockLoan(uint loanId) public { | |
//contract will send money to msg.sender | |
//states of proposals would be finalized, not accepted proposals would be reimbursed | |
if(loanList[loanId].state == LoanState.ACCEPTING) | |
{ | |
loanList[loanId].state = LoanState.LOCKED; | |
for(uint i = 0; i < loanList[loanId].proposalCount; i++) | |
{ | |
uint numI = loanList[loanId].proposal[i]; | |
if(proposalList[numI].state == ProposalState.ACCEPTED) | |
{ | |
msg.sender.transfer(proposalList[numI].amount); //Send to borrower | |
} | |
else | |
{ | |
proposalList[numI].state = ProposalState.REPAID; | |
proposalList[numI].lender.transfer(proposalList[numI].amount); //Send back to lender | |
} | |
} | |
} | |
else | |
return; | |
} | |
//Amount to be Repaid | |
function getRepayValue(uint loanId) public view returns(uint) { | |
if(loanList[loanId].state == LoanState.LOCKED) | |
{ | |
uint time = loanList[loanId].startDate; | |
uint finalamount = 0; | |
for(uint i = 0; i < loanList[loanId].proposalCount; i++) | |
{ | |
uint numI = loanList[loanId].proposal[i]; | |
if(proposalList[numI].state == ProposalState.ACCEPTED) | |
{ | |
uint original = proposalList[numI].amount; | |
uint rate = proposalList[numI].rate; | |
uint now = block.timestamp; | |
uint interest = (original*rate*(now - time))/(365*24*60*60*100); | |
finalamount += interest; | |
finalamount += original; | |
} | |
} | |
return finalamount; | |
} | |
else | |
return (2**64 -1); | |
} | |
function repayLoan(uint loanId) public payable { | |
uint now = block.timestamp; | |
uint toBePaid = getRepayValue(loanId); | |
uint time = loanList[loanId].startDate; | |
uint paid = msg.value; | |
if(paid >= toBePaid) | |
{ | |
uint remain = paid - toBePaid; | |
loanList[loanId].state = LoanState.SUCCESSFUL; | |
for(uint i = 0; i < loanList[loanId].proposalCount; i++) | |
{ | |
uint numI = loanList[loanId].proposal[i]; | |
if(proposalList[numI].state == ProposalState.ACCEPTED) | |
{ | |
uint original = proposalList[numI].amount; | |
uint rate = proposalList[numI].rate; | |
uint interest = (original*rate*(now - time))/(365*24*60*60*100); | |
uint finalamount = interest + original; | |
proposalList[numI].lender.transfer(finalamount); | |
proposalList[numI].state = ProposalState.REPAID; | |
} | |
} | |
msg.sender.transfer(remain); | |
} | |
else | |
{ | |
msg.sender.transfer(paid); | |
} | |
} | |
function acceptProposal(uint proposeId) public | |
{ | |
uint loanId = getActiveLoanId(msg.sender); | |
if(loanId == (2**64 - 1)) return; | |
Proposal storage pObj = proposalList[proposeId]; | |
if(pObj.state != ProposalState.WAITING) return; | |
Loan storage lObj = loanList[loanId]; | |
if(lObj.state != LoanState.ACCEPTING) return; | |
if(lObj.collected + pObj.amount <= lObj.amount) | |
{ | |
loanList[loanId].collected += pObj.amount; | |
proposalList[proposeId].state = ProposalState.ACCEPTED; | |
} | |
} | |
function totalProposalsBy(address lender) public view returns(uint) { | |
return lendMap[lender].length; | |
} | |
function getProposalAtPosFor(address lender, uint pos) public view returns(address, uint, ProposalState, uint, uint, uint, uint, bytes32) { | |
Proposal storage prop = proposalList[lendMap[lender][pos]]; | |
return (prop.lender, prop.loanId, prop.state, prop.rate, prop.amount, loanList[prop.loanId].amount, loanList[prop.loanId].dueDate, loanList[prop.loanId].mortgage); | |
} | |
// BORROWER ACTIONS AVAILABLE | |
function totalLoansBy(address borrower) public view returns(uint) { | |
return loanMap[borrower].length; | |
} | |
function getLoanDetailsByAddressPosition(address borrower, uint pos) public view returns(LoanState, uint, uint, uint, uint,bytes32) { | |
Loan storage obj = loanList[loanMap[borrower][pos]]; | |
return (obj.state, obj.dueDate, obj.amount, obj.collected, loanMap[borrower][pos], obj.mortgage); | |
} | |
function getLastLoanState(address borrower) public view returns(LoanState) { | |
uint loanLength = loanMap[borrower].length; | |
if(loanLength == 0) | |
return LoanState.SUCCESSFUL; | |
return loanList[loanMap[borrower][loanLength -1]].state; | |
} | |
function getLastLoanDetails(address borrower) public view returns(LoanState, uint, uint, uint, uint) { | |
uint loanLength = loanMap[borrower].length; | |
Loan storage obj = loanList[loanMap[borrower][loanLength -1]]; | |
return (obj.state, obj.dueDate, obj.amount, obj.proposalCount, obj.collected); | |
} | |
function getProposalDetailsByLoanIdPosition(uint loanId, uint numI) public view returns(ProposalState, uint, uint, uint, address) { | |
Proposal storage obj = proposalList[loanList[loanId].proposal[numI]]; | |
return (obj.state, obj.rate, obj.amount, loanList[loanId].proposal[numI],obj.lender); | |
} | |
function numTotalLoans() public view returns(uint) { | |
return loanList.length; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract DappToken { | |
string public name = "DApp Token"; | |
string public symbol = "DAPP"; | |
string public standard = "DApp Token v1.0"; | |
uint256 public totalSupply; | |
event Transfer( | |
address indexed _from, | |
address indexed _to, | |
uint256 _value | |
); | |
event Approval( | |
address indexed _owner, | |
address indexed _spender, | |
uint256 _value | |
); | |
mapping(address => uint256) public balanceOf; | |
mapping(address => mapping(address => uint256)) public allowance; | |
constructor(uint256 _initialSupply) public { | |
balanceOf[msg.sender] = _initialSupply; | |
totalSupply = _initialSupply; | |
} | |
function transfer(address _to, uint256 _value) public returns (bool success) { | |
require(balanceOf[msg.sender] >= _value); | |
balanceOf[msg.sender] -= _value; | |
balanceOf[_to] += _value; | |
emit Transfer(msg.sender, _to, _value); | |
return true; | |
} | |
function approve(address _spender, uint256 _value) public returns (bool success) { | |
allowance[msg.sender][_spender] = _value; | |
emit Approval(msg.sender, _spender, _value); | |
return true; | |
} | |
function transferFrom(address _from, address _to, uint256 _value) public returns (bool success) { | |
require(_value <= balanceOf[_from]); | |
require(_value <= allowance[_from][msg.sender]); | |
balanceOf[_from] -= _value; | |
balanceOf[_to] += _value; | |
allowance[_from][msg.sender] -= _value; | |
emit Transfer(_from, _to, _value); | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
import "./DappToken.sol"; | |
contract DappTokenSale { | |
address payable admin; | |
DappToken public tokenContract; | |
uint256 public tokenPrice; | |
uint256 public tokensSold; | |
event Sell(address _buyer, uint256 _amount); | |
constructor (DappToken _tokenContract, uint256 _tokenPrice) public { | |
admin = msg.sender; | |
tokenContract = _tokenContract; | |
tokenPrice = _tokenPrice; | |
} | |
function multiply(uint x, uint y) internal pure returns (uint z) { | |
require(y == 0 || (z = x * y) / y == x); | |
} | |
function buyTokens(uint256 _numberOfTokens) public payable { | |
require(msg.value == multiply(_numberOfTokens, tokenPrice)); | |
require(tokenContract.balanceOf(address(this)) >= _numberOfTokens); | |
require(tokenContract.transfer(msg.sender, _numberOfTokens)); | |
tokensSold += _numberOfTokens; | |
emit Sell(msg.sender, _numberOfTokens); | |
} | |
function endSale() public { | |
require(msg.sender == admin); | |
require(tokenContract.transfer(admin, tokenContract.balanceOf(address(this)))); | |
// Just transfer the balance to the admin | |
admin.transfer(address(this).balance); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.4.4; | |
contract Token { | |
/// @return total amount of tokens | |
function totalSupply() constant returns (uint256 supply) {} | |
/// @param _owner The address from which the balance will be retrieved | |
/// @return The balance | |
function balanceOf(address _owner) constant returns (uint256 balance) {} | |
/// @notice send `_value` token to `_to` from `msg.sender` | |
/// @param _to The address of the recipient | |
/// @param _value The amount of token to be transferred | |
/// @return Whether the transfer was successful or not | |
function transfer(address _to, uint256 _value) returns (bool success) {} | |
/// @notice send `_value` token to `_to` from `_from` on the condition it is approved by `_from` | |
/// @param _from The address of the sender | |
/// @param _to The address of the recipient | |
/// @param _value The amount of token to be transferred | |
/// @return Whether the transfer was successful or not | |
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) {} | |
/// @notice `msg.sender` approves `_addr` to spend `_value` tokens | |
/// @param _spender The address of the account able to transfer the tokens | |
/// @param _value The amount of wei to be approved for transfer | |
/// @return Whether the approval was successful or not | |
function approve(address _spender, uint256 _value) returns (bool success) {} | |
/// @param _owner The address of the account owning tokens | |
/// @param _spender The address of the account able to transfer the tokens | |
/// @return Amount of remaining tokens allowed to spent | |
function allowance(address _owner, address _spender) constant returns (uint256 remaining) {} | |
event Transfer(address indexed _from, address indexed _to, uint256 _value); | |
event Approval(address indexed _owner, address indexed _spender, uint256 _value); | |
} | |
contract StandardToken is Token { | |
function transfer(address _to, uint256 _value) returns (bool success) { | |
//Default assumes totalSupply can't be over max (2^256 - 1). | |
//If your token leaves out totalSupply and can issue more tokens as time goes on, you need to check if it doesn't wrap. | |
//Replace the if with this one instead. | |
//if (balances[msg.sender] >= _value && balances[_to] + _value > balances[_to]) { | |
if (balances[msg.sender] >= _value && _value > 0) { | |
balances[msg.sender] -= _value; | |
balances[_to] += _value; | |
Transfer(msg.sender, _to, _value); | |
return true; | |
} else { return false; } | |
} | |
function transferFrom(address _from, address _to, uint256 _value) returns (bool success) { | |
//same as above. Replace this line with the following if you want to protect against wrapping uints. | |
//if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && balances[_to] + _value > balances[_to]) { | |
if (balances[_from] >= _value && allowed[_from][msg.sender] >= _value && _value > 0) { | |
balances[_to] += _value; | |
balances[_from] -= _value; | |
allowed[_from][msg.sender] -= _value; | |
Transfer(_from, _to, _value); | |
return true; | |
} else { return false; } | |
} | |
function balanceOf(address _owner) constant returns (uint256 balance) { | |
return balances[_owner]; | |
} | |
function approve(address _spender, uint256 _value) returns (bool success) { | |
allowed[msg.sender][_spender] = _value; | |
Approval(msg.sender, _spender, _value); | |
return true; | |
} | |
function allowance(address _owner, address _spender) constant returns (uint256 remaining) { | |
return allowed[_owner][_spender]; | |
} | |
mapping (address => uint256) balances; | |
mapping (address => mapping (address => uint256)) allowed; | |
uint256 public totalSupply; | |
} | |
//name this contract whatever you'd like | |
contract ERC20Token is StandardToken { | |
function () { | |
//if ether is sent to this address, send it back. | |
throw; | |
} | |
/* Public variables of the token */ | |
/* | |
NOTE: | |
The following variables are OPTIONAL vanities. One does not have to include them. | |
They allow one to customise the token contract & in no way influences the core functionality. | |
Some wallets/interfaces might not even bother to look at this information. | |
*/ | |
string public name; //fancy name: eg Simon Bucks | |
uint8 public decimals; //How many decimals to show. ie. There could 1000 base units with 3 decimals. Meaning 0.980 SBX = 980 base units. It's like comparing 1 wei to 1 ether. | |
string public symbol; //An identifier: eg SBX | |
string public version = 'H1.0'; //human 0.1 standard. Just an arbitrary versioning scheme. | |
// | |
// CHANGE THESE VALUES FOR YOUR TOKEN | |
// | |
//make sure this function name matches the contract name above. So if you're token is called TutorialToken, make sure the //contract name above is also TutorialToken instead of ERC20Token | |
function ERC20Token( | |
) { | |
balances[msg.sender] = 10000; // Give the creator all initial tokens (100000 for example) | |
totalSupply = 10000; // Update total supply (100000 for example) | |
name = "NAME OF YOUR TOKEN HERE"; // Set the name for display purposes | |
decimals = 0; // Amount of decimals for display purposes | |
symbol = "SYM"; // Set the symbol for display purposes | |
} | |
/* Approves and then calls the receiving contract */ | |
function approveAndCall(address _spender, uint256 _value, bytes _extraData) returns (bool success) { | |
allowed[msg.sender][_spender] = _value; | |
Approval(msg.sender, _spender, _value); | |
//call the receiveApproval function on the contract you want to be notified. This crafts the function signature manually so one doesn't have to include a contract in here just for this. | |
//receiveApproval(address _from, uint256 _value, address _tokenContract, bytes _extraData) | |
//it is assumed that when does this that the call *should* succeed, otherwise one would use vanilla approve instead. | |
if(!_spender.call(bytes4(bytes32(sha3("receiveApproval(address,uint256,address,bytes)"))), msg.sender, _value, this, _extraData)) { throw; } | |
return true; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract FindMax{ | |
mapping (address => int) addrmap; | |
address[] public addr; | |
function add(address adr, int value) public { | |
if(addrmap[adr] > 0){ | |
addrmap[adr] += value; | |
} | |
else { | |
addrmap[adr] = value; | |
addr.push(adr); | |
} | |
} | |
function findmax(address adr, int value) public returns(address){ | |
add(adr, value); | |
uint len = addr.length; | |
int max = 0; | |
address maxAddr; | |
for (uint i = 0; i < len; i++){ | |
if(addrmap[addr[i]] > max){ | |
max = addrmap[addr[i]]; | |
maxAddr = addr[i]; | |
} | |
} | |
return maxAddr; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract FirstContract { | |
uint256 var1; | |
bytes32 var2; | |
//address var3; | |
constructor(uint256 a, bytes32 b) public{ | |
var1 = a; | |
var2 = b; | |
} | |
function getval() public view returns(uint256, bytes32){ | |
return(var1, var2); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
//pragma solidity ^0.5.12; | |
import "SmartMeter.sol"; | |
//Contract structure and basic syntax | |
contract FirstContract1 { | |
int var1; | |
function setVar(int var2) public { | |
var1 = var2; | |
} | |
function getVar() public view returns (int) { | |
return var1; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
*Submitted for verification at Etherscan.io on 2020-01-31 | |
*/ | |
/** | |
* | |
* ,d8888b | |
* 88P' | |
*d888888P | |
* ?88' d8888b 88bd88b .d888b, d888b8b d888b8b d8888b | |
* 88P d8P' ?88 88P' ` ?8b, d8P' ?88 d8P' ?88 d8b_,dP | |
* d88 88b d88 d88 `?8b 88b ,88b 88b ,88b 88b | |
*d88' `?8888P'd88' `?888P' `?88P'`88b`?88P'`88b`?888P' | |
* )88 | |
* ,88P | |
* `?8888P | |
* | |
* | |
* SmartWay Forsage | |
* https://forsage.smartway.run | |
* (only for SmartWay.run members) | |
* | |
**/ | |
pragma solidity >=0.4.23 <0.6.0; | |
contract SmartMatrixForsage { | |
struct User { | |
uint id; | |
address referrer; | |
uint partnersCount; | |
mapping(uint8 => bool) activeX3Levels; | |
mapping(uint8 => bool) activeX6Levels; | |
mapping(uint8 => X3) x3Matrix; | |
mapping(uint8 => X6) x6Matrix; | |
} | |
struct X3 { | |
address currentReferrer; | |
address[] referrals; | |
bool blocked; | |
uint reinvestCount; | |
} | |
struct X6 { | |
address currentReferrer; | |
address[] firstLevelReferrals; | |
address[] secondLevelReferrals; | |
bool blocked; | |
uint reinvestCount; | |
address closedPart; | |
} | |
uint8 public constant LAST_LEVEL = 12; | |
mapping(address => User) public users; | |
mapping(uint => address) public idToAddress; | |
mapping(uint => address) public userIds; | |
mapping(address => uint) public balances; | |
uint public lastUserId = 2; | |
address public owner; | |
mapping(uint8 => uint) public levelPrice; | |
event Registration(address indexed user, address indexed referrer, uint indexed userId, uint referrerId); | |
event Reinvest(address indexed user, address indexed currentReferrer, address indexed caller, uint8 matrix, uint8 level); | |
event Upgrade(address indexed user, address indexed referrer, uint8 matrix, uint8 level); | |
event NewUserPlace(address indexed user, address indexed referrer, uint8 matrix, uint8 level, uint8 place); | |
event MissedEthReceive(address indexed receiver, address indexed from, uint8 matrix, uint8 level); | |
event SentExtraEthDividends(address indexed from, address indexed receiver, uint8 matrix, uint8 level); | |
constructor(address ownerAddress) public { | |
levelPrice[1] = 0.025 ether; | |
for (uint8 i = 2; i <= LAST_LEVEL; i++) { | |
levelPrice[i] = levelPrice[i-1] * 2; | |
} | |
owner = ownerAddress; | |
User memory user = User({ | |
id: 1, | |
referrer: address(0), | |
partnersCount: uint(0) | |
}); | |
users[ownerAddress] = user; | |
idToAddress[1] = ownerAddress; | |
for (uint8 i = 1; i <= LAST_LEVEL; i++) { | |
users[ownerAddress].activeX3Levels[i] = true; | |
users[ownerAddress].activeX6Levels[i] = true; | |
} | |
userIds[1] = ownerAddress; | |
} | |
function() external payable { | |
if(msg.data.length == 0) { | |
return registration(msg.sender, owner); | |
} | |
registration(msg.sender, bytesToAddress(msg.data)); | |
} | |
function registrationExt(address referrerAddress) external payable { | |
registration(msg.sender, referrerAddress); | |
} | |
function buyNewLevel(uint8 matrix, uint8 level) external payable { | |
require(isUserExists(msg.sender), "user is not exists. Register first."); | |
require(matrix == 1 || matrix == 2, "invalid matrix"); | |
require(msg.value == levelPrice[level], "invalid price"); | |
require(level > 1 && level <= LAST_LEVEL, "invalid level"); | |
if (matrix == 1) { | |
require(!users[msg.sender].activeX3Levels[level], "level already activated"); | |
if (users[msg.sender].x3Matrix[level-1].blocked) { | |
users[msg.sender].x3Matrix[level-1].blocked = false; | |
} | |
address freeX3Referrer = findFreeX3Referrer(msg.sender, level); | |
users[msg.sender].x3Matrix[level].currentReferrer = freeX3Referrer; | |
users[msg.sender].activeX3Levels[level] = true; | |
updateX3Referrer(msg.sender, freeX3Referrer, level); | |
emit Upgrade(msg.sender, freeX3Referrer, 1, level); | |
} else { | |
require(!users[msg.sender].activeX6Levels[level], "level already activated"); | |
if (users[msg.sender].x6Matrix[level-1].blocked) { | |
users[msg.sender].x6Matrix[level-1].blocked = false; | |
} | |
address freeX6Referrer = findFreeX6Referrer(msg.sender, level); | |
users[msg.sender].activeX6Levels[level] = true; | |
updateX6Referrer(msg.sender, freeX6Referrer, level); | |
emit Upgrade(msg.sender, freeX6Referrer, 2, level); | |
} | |
} | |
function registration(address userAddress, address referrerAddress) private { | |
require(msg.value == 0.05 ether, "registration cost 0.05"); | |
require(!isUserExists(userAddress), "user exists"); | |
require(isUserExists(referrerAddress), "referrer not exists"); | |
uint32 size; | |
assembly { | |
size := extcodesize(userAddress) | |
} | |
require(size == 0, "cannot be a contract"); | |
User memory user = User({ | |
id: lastUserId, | |
referrer: referrerAddress, | |
partnersCount: 0 | |
}); | |
users[userAddress] = user; | |
idToAddress[lastUserId] = userAddress; | |
users[userAddress].referrer = referrerAddress; | |
users[userAddress].activeX3Levels[1] = true; | |
users[userAddress].activeX6Levels[1] = true; | |
userIds[lastUserId] = userAddress; | |
lastUserId++; | |
users[referrerAddress].partnersCount++; | |
address freeX3Referrer = findFreeX3Referrer(userAddress, 1); | |
users[userAddress].x3Matrix[1].currentReferrer = freeX3Referrer; | |
updateX3Referrer(userAddress, freeX3Referrer, 1); | |
updateX6Referrer(userAddress, findFreeX6Referrer(userAddress, 1), 1); | |
emit Registration(userAddress, referrerAddress, users[userAddress].id, users[referrerAddress].id); | |
} | |
function updateX3Referrer(address userAddress, address referrerAddress, uint8 level) private { | |
users[referrerAddress].x3Matrix[level].referrals.push(userAddress); | |
if (users[referrerAddress].x3Matrix[level].referrals.length < 3) { | |
emit NewUserPlace(userAddress, referrerAddress, 1, level, uint8(users[referrerAddress].x3Matrix[level].referrals.length)); | |
return sendETHDividends(referrerAddress, userAddress, 1, level); | |
} | |
emit NewUserPlace(userAddress, referrerAddress, 1, level, 3); | |
//close matrix | |
users[referrerAddress].x3Matrix[level].referrals = new address[](0); | |
if (!users[referrerAddress].activeX3Levels[level+1] && level != LAST_LEVEL) { | |
users[referrerAddress].x3Matrix[level].blocked = true; | |
} | |
//create new one by recursion | |
if (referrerAddress != owner) { | |
//check referrer active level | |
address freeReferrerAddress = findFreeX3Referrer(referrerAddress, level); | |
if (users[referrerAddress].x3Matrix[level].currentReferrer != freeReferrerAddress) { | |
users[referrerAddress].x3Matrix[level].currentReferrer = freeReferrerAddress; | |
} | |
users[referrerAddress].x3Matrix[level].reinvestCount++; | |
emit Reinvest(referrerAddress, freeReferrerAddress, userAddress, 1, level); | |
updateX3Referrer(referrerAddress, freeReferrerAddress, level); | |
} else { | |
sendETHDividends(owner, userAddress, 1, level); | |
users[owner].x3Matrix[level].reinvestCount++; | |
emit Reinvest(owner, address(0), userAddress, 1, level); | |
} | |
} | |
function updateX6Referrer(address userAddress, address referrerAddress, uint8 level) private { | |
require(users[referrerAddress].activeX6Levels[level], "500. Referrer level is inactive"); | |
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length < 2) { | |
users[referrerAddress].x6Matrix[level].firstLevelReferrals.push(userAddress); | |
emit NewUserPlace(userAddress, referrerAddress, 2, level, uint8(users[referrerAddress].x6Matrix[level].firstLevelReferrals.length)); | |
//set current level | |
users[userAddress].x6Matrix[level].currentReferrer = referrerAddress; | |
if (referrerAddress == owner) { | |
return sendETHDividends(referrerAddress, userAddress, 2, level); | |
} | |
address ref = users[referrerAddress].x6Matrix[level].currentReferrer; | |
users[ref].x6Matrix[level].secondLevelReferrals.push(userAddress); | |
uint len = users[ref].x6Matrix[level].firstLevelReferrals.length; | |
if ((len == 2) && | |
(users[ref].x6Matrix[level].firstLevelReferrals[0] == referrerAddress) && | |
(users[ref].x6Matrix[level].firstLevelReferrals[1] == referrerAddress)) { | |
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length == 1) { | |
emit NewUserPlace(userAddress, ref, 2, level, 5); | |
} else { | |
emit NewUserPlace(userAddress, ref, 2, level, 6); | |
} | |
} else if ((len == 1 || len == 2) && | |
users[ref].x6Matrix[level].firstLevelReferrals[0] == referrerAddress) { | |
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length == 1) { | |
emit NewUserPlace(userAddress, ref, 2, level, 3); | |
} else { | |
emit NewUserPlace(userAddress, ref, 2, level, 4); | |
} | |
} else if (len == 2 && users[ref].x6Matrix[level].firstLevelReferrals[1] == referrerAddress) { | |
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals.length == 1) { | |
emit NewUserPlace(userAddress, ref, 2, level, 5); | |
} else { | |
emit NewUserPlace(userAddress, ref, 2, level, 6); | |
} | |
} | |
return updateX6ReferrerSecondLevel(userAddress, ref, level); | |
} | |
users[referrerAddress].x6Matrix[level].secondLevelReferrals.push(userAddress); | |
if (users[referrerAddress].x6Matrix[level].closedPart != address(0)) { | |
if ((users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] == | |
users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]) && | |
(users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] == | |
users[referrerAddress].x6Matrix[level].closedPart)) { | |
updateX6(userAddress, referrerAddress, level, true); | |
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level); | |
} else if (users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] == | |
users[referrerAddress].x6Matrix[level].closedPart) { | |
updateX6(userAddress, referrerAddress, level, true); | |
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level); | |
} else { | |
updateX6(userAddress, referrerAddress, level, false); | |
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level); | |
} | |
} | |
if (users[referrerAddress].x6Matrix[level].firstLevelReferrals[1] == userAddress) { | |
updateX6(userAddress, referrerAddress, level, false); | |
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level); | |
} else if (users[referrerAddress].x6Matrix[level].firstLevelReferrals[0] == userAddress) { | |
updateX6(userAddress, referrerAddress, level, true); | |
return updateX6ReferrerSecondLevel(userAddress, referrerAddress, level); | |
} | |
if (users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.length <= | |
users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.length) { | |
updateX6(userAddress, referrerAddress, level, false); | |
} else { | |
updateX6(userAddress, referrerAddress, level, true); | |
} | |
updateX6ReferrerSecondLevel(userAddress, referrerAddress, level); | |
} | |
function updateX6(address userAddress, address referrerAddress, uint8 level, bool x2) private { | |
if (!x2) { | |
users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.push(userAddress); | |
emit NewUserPlace(userAddress, users[referrerAddress].x6Matrix[level].firstLevelReferrals[0], 2, level, uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.length)); | |
emit NewUserPlace(userAddress, referrerAddress, 2, level, 2 + uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]].x6Matrix[level].firstLevelReferrals.length)); | |
//set current level | |
users[userAddress].x6Matrix[level].currentReferrer = users[referrerAddress].x6Matrix[level].firstLevelReferrals[0]; | |
} else { | |
users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.push(userAddress); | |
emit NewUserPlace(userAddress, users[referrerAddress].x6Matrix[level].firstLevelReferrals[1], 2, level, uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.length)); | |
emit NewUserPlace(userAddress, referrerAddress, 2, level, 4 + uint8(users[users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]].x6Matrix[level].firstLevelReferrals.length)); | |
//set current level | |
users[userAddress].x6Matrix[level].currentReferrer = users[referrerAddress].x6Matrix[level].firstLevelReferrals[1]; | |
} | |
} | |
function updateX6ReferrerSecondLevel(address userAddress, address referrerAddress, uint8 level) private { | |
if (users[referrerAddress].x6Matrix[level].secondLevelReferrals.length < 4) { | |
return sendETHDividends(referrerAddress, userAddress, 2, level); | |
} | |
address[] memory x6 = users[users[referrerAddress].x6Matrix[level].currentReferrer].x6Matrix[level].firstLevelReferrals; | |
if (x6.length == 2) { | |
if (x6[0] == referrerAddress || | |
x6[1] == referrerAddress) { | |
users[users[referrerAddress].x6Matrix[level].currentReferrer].x6Matrix[level].closedPart = referrerAddress; | |
} else if (x6.length == 1) { | |
if (x6[0] == referrerAddress) { | |
users[users[referrerAddress].x6Matrix[level].currentReferrer].x6Matrix[level].closedPart = referrerAddress; | |
} | |
} | |
} | |
users[referrerAddress].x6Matrix[level].firstLevelReferrals = new address[](0); | |
users[referrerAddress].x6Matrix[level].secondLevelReferrals = new address[](0); | |
users[referrerAddress].x6Matrix[level].closedPart = address(0); | |
if (!users[referrerAddress].activeX6Levels[level+1] && level != LAST_LEVEL) { | |
users[referrerAddress].x6Matrix[level].blocked = true; | |
} | |
users[referrerAddress].x6Matrix[level].reinvestCount++; | |
if (referrerAddress != owner) { | |
address freeReferrerAddress = findFreeX6Referrer(referrerAddress, level); | |
emit Reinvest(referrerAddress, freeReferrerAddress, userAddress, 2, level); | |
updateX6Referrer(referrerAddress, freeReferrerAddress, level); | |
} else { | |
emit Reinvest(owner, address(0), userAddress, 2, level); | |
sendETHDividends(owner, userAddress, 2, level); | |
} | |
} | |
function findFreeX3Referrer(address userAddress, uint8 level) public view returns(address) { | |
while (true) { | |
if (users[users[userAddress].referrer].activeX3Levels[level]) { | |
return users[userAddress].referrer; | |
} | |
userAddress = users[userAddress].referrer; | |
} | |
} | |
function findFreeX6Referrer(address userAddress, uint8 level) public view returns(address) { | |
while (true) { | |
if (users[users[userAddress].referrer].activeX6Levels[level]) { | |
return users[userAddress].referrer; | |
} | |
userAddress = users[userAddress].referrer; | |
} | |
} | |
function usersActiveX3Levels(address userAddress, uint8 level) public view returns(bool) { | |
return users[userAddress].activeX3Levels[level]; | |
} | |
function usersActiveX6Levels(address userAddress, uint8 level) public view returns(bool) { | |
return users[userAddress].activeX6Levels[level]; | |
} | |
function usersX3Matrix(address userAddress, uint8 level) public view returns(address, address[] memory, bool) { | |
return (users[userAddress].x3Matrix[level].currentReferrer, | |
users[userAddress].x3Matrix[level].referrals, | |
users[userAddress].x3Matrix[level].blocked); | |
} | |
function usersX6Matrix(address userAddress, uint8 level) public view returns(address, address[] memory, address[] memory, bool, address) { | |
return (users[userAddress].x6Matrix[level].currentReferrer, | |
users[userAddress].x6Matrix[level].firstLevelReferrals, | |
users[userAddress].x6Matrix[level].secondLevelReferrals, | |
users[userAddress].x6Matrix[level].blocked, | |
users[userAddress].x6Matrix[level].closedPart); | |
} | |
function isUserExists(address user) public view returns (bool) { | |
return (users[user].id != 0); | |
} | |
function findEthReceiver(address userAddress, address _from, uint8 matrix, uint8 level) private returns(address, bool) { | |
address receiver = userAddress; | |
bool isExtraDividends; | |
if (matrix == 1) { | |
while (true) { | |
if (users[receiver].x3Matrix[level].blocked) { | |
emit MissedEthReceive(receiver, _from, 1, level); | |
isExtraDividends = true; | |
receiver = users[receiver].x3Matrix[level].currentReferrer; | |
} else { | |
return (receiver, isExtraDividends); | |
} | |
} | |
} else { | |
while (true) { | |
if (users[receiver].x6Matrix[level].blocked) { | |
emit MissedEthReceive(receiver, _from, 2, level); | |
isExtraDividends = true; | |
receiver = users[receiver].x6Matrix[level].currentReferrer; | |
} else { | |
return (receiver, isExtraDividends); | |
} | |
} | |
} | |
} | |
function sendETHDividends(address userAddress, address _from, uint8 matrix, uint8 level) private { | |
(address receiver, bool isExtraDividends) = findEthReceiver(userAddress, _from, matrix, level); | |
if (!address(uint160(receiver)).send(levelPrice[level])) { | |
return address(uint160(receiver)).transfer(address(this).balance); | |
} | |
if (isExtraDividends) { | |
emit SentExtraEthDividends(_from, receiver, matrix, level); | |
} | |
} | |
function bytesToAddress(bytes memory bys) private pure returns (address addr) { | |
assembly { | |
addr := mload(add(bys, 20)) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
pragma experimental ABIEncoderV2; | |
contract FreelancerDotCom { | |
struct Freelancer { | |
uint id; | |
address payable freelancerAddr; | |
string skills; | |
uint num_experience; | |
} | |
Freelancer[] freelancers; | |
address payable[] freelancer_addr; | |
mapping(address => Freelancer) freelancerMap; | |
struct Org{ | |
uint orgId; | |
address payable orgAddr; | |
} | |
struct Project{ | |
uint projectId; | |
uint orgId; | |
string skillsReq; | |
uint num_experience; | |
uint cost; | |
bool allocated; | |
} | |
Org[] orgs; | |
Project[] projects; | |
address payable[] org_addr; | |
mapping(address => Org) orgMap; | |
function registerFreelancer(address payable _address, uint _id, string memory _skills, uint _num_experience) public { | |
Freelancer memory freelancer; | |
freelancer.id = _id; | |
freelancer.freelancerAddr = _address; | |
freelancer.skills = _skills; | |
freelancer.num_experience = _num_experience; | |
freelancerMap[_address] = freelancer; | |
freelancers.push(freelancer); | |
} | |
function registerOrg(address payable _address, uint _orgId) public { | |
Org memory org; | |
org.orgAddr = _address; | |
org.orgId = _orgId; | |
orgMap[_address] = org; | |
orgs.push(org); | |
} | |
function registerProject(uint _projectId, uint _orgId, string memory _skillsReq, uint _numExperience, uint _cost) public returns( Project memory) { | |
Project memory project; | |
project.projectId = _projectId; | |
project.orgId = _orgId; | |
project.skillsReq = _skillsReq; | |
project.num_experience = _numExperience; | |
project.cost = _cost; | |
projects.push(project); | |
return project; | |
} | |
event matchedProject(address freelancer, uint cost, bool allocated); | |
function allocateProjects(uint _projectId, uint _orgId, string memory _skillsReq, uint _numExperience, uint _cost) public { | |
Project memory project = registerProject(_projectId, _orgId, _skillsReq, _numExperience, _cost); | |
uint length = freelancer_addr.length; | |
address payable assignedFreelancer; | |
uint param = 0; | |
for(uint i = 0; i < length; i++){ | |
Freelancer memory freelancer = freelancerMap[freelancer_addr[i]]; | |
if(compareStrings(freelancer.skills,_skillsReq) && (freelancer.num_experience >= _numExperience)){ | |
param += 1; | |
assignedFreelancer = freelancer_addr[i]; | |
break; | |
} | |
} | |
if (param == 1){ | |
project.allocated = true; | |
assignedFreelancer.transfer(_cost); | |
emit matchedProject(assignedFreelancer, _cost, project.allocated); | |
} | |
else{ | |
project.allocated = false; | |
emit matchedProject(assignedFreelancer, _cost, project.allocated); | |
} | |
} | |
function compareStrings (string memory a, string memory b) public pure | |
returns (bool) { | |
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))) ); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.4; | |
pragma experimental ABIEncoderV2; | |
contract Investment { | |
struct Investor{ | |
uint investorId; | |
string subRegion; | |
uint numSolarPanels; | |
uint investmentAmount; | |
bool closed; | |
} | |
mapping (address => Investor) investors; | |
address payable[] public investorAccts; | |
mapping(address => string) investorRegion; | |
uint vendorId; | |
constructor(address payable _address, uint _investorId, | |
uint _vendorId, string memory _subRegion, uint _numSolarPanels) public { | |
Investor memory investor = investors[_address]; | |
investorRegion[_address] = _subRegion; | |
investor.investorId = _investorId; | |
investor.subRegion = _subRegion; | |
investor.numSolarPanels = _numSolarPanels; | |
investor.investmentAmount = _numSolarPanels*29; | |
vendorId = _vendorId; | |
investor.closed = false; | |
investors[_address] = investor; | |
investorAccts.push(_address) -1; | |
} | |
function amountInvested(address _address) public view | |
returns(uint) { | |
Investor memory investor = investors[_address]; | |
return investor.investmentAmount; | |
} | |
function addInvestor(address payable _address, uint _investorId, | |
uint _vendorId, string memory _subRegion, uint _numSolarPanels) public { | |
Investor memory investorNew = investors[_address]; | |
investorRegion[_address] = _subRegion; | |
investorNew.investorId = _investorId; | |
investorNew.subRegion = _subRegion; | |
investorNew.numSolarPanels = _numSolarPanels; | |
investorNew.investmentAmount = _numSolarPanels*29; | |
vendorId = _vendorId; | |
investorNew.closed = false; | |
investors[_address] = investorNew; | |
investorAccts.push(_address); | |
} | |
function compareStrings (string memory a, string memory b) public pure | |
returns (bool) { | |
return (keccak256(abi.encodePacked((a))) == keccak256(abi.encodePacked((b))) ); | |
} | |
function updateActivePanels(uint _activePanels, string memory _subRegion) public view { | |
for(uint i = 0; i < investorAccts.length; i++){ | |
Investor memory investor = investors[investorAccts[i]]; | |
if(compareStrings(investor.subRegion,_subRegion)){ | |
investor.numSolarPanels = _activePanels; | |
} | |
} | |
} | |
function ifInvestorClosed(string memory _subRegion) public view returns(address payable) { | |
for(uint i = 0; i < investorAccts.length; i++){ | |
Investor memory investor = investors[investorAccts[i]]; | |
if(compareStrings(investor.subRegion, _subRegion)) { | |
if(investor.numSolarPanels == 0) { | |
investor.closed = true; | |
return(investorAccts[i]); | |
} | |
} | |
} | |
} | |
} | |
contract Readings is Investment { | |
string subRegion; | |
uint activePanels; | |
uint energyConsumed; | |
Investment ic; | |
mapping(string => uint) panelsRemaining; | |
mapping(string => uint) energyPerRegion; | |
constructor(string memory _subRegion, uint _activePanels, uint _energyConsumed) public{ | |
subRegion = _subRegion; | |
activePanels = _activePanels; | |
energyConsumed = _energyConsumed; | |
energyPerRegion[_subRegion] += _energyConsumed; | |
panelsRemaining[_subRegion] = activePanels; | |
} | |
function getEnergyPerRegion(string memory region) public view returns(uint){ | |
return energyPerRegion[region]; | |
} | |
function updateEnergyConsumed(string memory _subRegion, uint _activePanels, | |
uint _energyConsumed ) public { | |
energyPerRegion[_subRegion] += _energyConsumed; | |
panelsRemaining[_subRegion] = _activePanels; | |
ic.updateActivePanels(_activePanels, _subRegion); | |
} | |
function calculateCarbonCredits(string memory _subRegion) public { | |
address payable investorAddress = ic.ifInvestorClosed(_subRegion); | |
uint credits = energyPerRegion[_subRegion]; | |
//Paying the investors in ethere for the credits they have collected | |
investorAddress.transfer(credits); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
import "SmartMeter.sol"; | |
//Contract structure and basic syntax | |
contract Lab1 { | |
int var1; | |
function setVar(int var2) public { | |
var1 = var2; | |
} | |
function getVar() public view returns (int) { | |
return var1; | |
} | |
function whileLoop() public pure returns(uint) { | |
uint j; | |
// while(i > 0){ //10 , 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 | |
// j = i; | |
// i--; //10-- = 9-- = 8-- = 7,.. 1-- = 0 | |
// } | |
// return j; | |
for(uint i = 10; i > 0; i--) { | |
j = i; | |
} | |
return j; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.6; | |
contract Lab2{ | |
//This is a comment in solidity | |
/* | |
This is a multiline comment | |
*/ | |
/* | |
* These are also multiline comments | |
* Hello, I am a multiline comment | |
*/ | |
//Integers | |
uint256 var1 = 45; //same as uint | |
int256 var2 = 56; //same as int | |
int8 var3 = 20; | |
uint8 var4 = 72; | |
//Boolean | |
bool boolVar; | |
bool boolVar1 = true; | |
bool boolVar2 = false; | |
//Fixed point number | |
fixed varFixed; | |
//address | |
address varAddr = 0x081f21A43e7E9EC5307c8bE89b2366E1466e208A; //20 byte value | |
uint storedData; // State variable | |
constructor() public { | |
storedData = 100; | |
} | |
function getVar() public view returns(uint){ | |
return storedData; | |
} | |
function getMessageSenderVar() public view returns(address){ | |
return msg.sender; | |
} | |
function getResult(uint a, uint b) public pure returns(uint){ | |
uint result = a + b; | |
return result; //access the local variable | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.6; | |
contract Lab3{ | |
//Operators | |
uint a1 = 10; | |
uint b1 = 5; | |
//Arithmetic Operators | |
function arithmeticOp(uint a, uint b) public pure returns(uint){ | |
// uint c = a+b; | |
//return c; | |
//uint d = a-b; | |
//return d; | |
uint e = a*b; | |
return e; | |
//uint f = a/b; | |
//uint g = b%a; | |
//uint h = a++; | |
//uint i = b--; | |
} | |
function logicalOp(uint a, uint b) public pure returns(uint){ | |
if(a < b){ | |
return 1; | |
} | |
else if(a==b){ | |
return 2; | |
} | |
else if(a != b){ | |
return 3; | |
} | |
else if(a > b){ | |
return 4; | |
} | |
else | |
return 5; | |
} | |
function bitwiseOp(uint a, uint b) public pure returns(uint){ | |
//Students to do - EXAMPLE | |
uint c = a&b; | |
return c; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.6; | |
contract Lab4{ | |
address var1; | |
address payable var2; | |
function transferTo(address payable a, address payable b) public { | |
if(a.balance < 100 && b.balance > 50){ | |
a.transfer(10); | |
b.transfer(10); | |
} | |
//uint c = balanceOf(a); | |
} | |
function balanceOf(address payable a) internal view returns(uint){ | |
return a.balance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Lab5{ | |
uint storedData; //State variable | |
constructor() public{ | |
storedData = 10; | |
} | |
function getResult() public pure returns(string memory){ | |
uint a = 10; | |
uint b = 2; | |
uint result = a + b; | |
return integerToString(result); | |
} | |
function integerToString(uint _i) internal pure | |
returns (string memory) { | |
if (_i == 0) { | |
return "0"; | |
} | |
uint j = _i; | |
uint len; | |
while (j != 0) { | |
len++;//1, 2 len = 2 | |
j /= 10; //j = j/10 = 12/10 = 1, j= 1/10 = 0 | |
} | |
bytes memory bstr = new bytes(len); //bstr = length(2) | |
uint k = len - 1; // k = 1 | |
while (_i != 0) { // while loop //12!=0 | |
bstr[k--] = byte(uint8(48 + _i % 10)); //bstr[1] = byte(50) = "2". bstr[0] = byte(49) = "1" | |
_i /= 10; //_i = 1/10 = 12/10 = 1, _i = 1/10 = 0 | |
} | |
return string(bstr); //["1","2"],"12" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract SolidityTest { | |
uint storedData; | |
constructor() public { | |
storedData = 10; | |
} | |
function getResult() public pure returns(string memory){ | |
uint a = 1; | |
uint b = 2; | |
uint result = a + b; | |
return integerToString(result); | |
} | |
function integerToString(uint _i) internal pure | |
returns (string memory) { | |
if (_i == 0) { // if statement | |
return "0"; | |
} | |
uint j = _i; | |
uint len; | |
while (j != 0) { | |
len++; | |
j /= 10; | |
} | |
bytes memory bstr = new bytes(len); | |
uint k = len - 1; | |
while (_i != 0) { | |
bstr[k--] = byte(uint8(48 + _i % 10)); | |
_i /= 10; | |
} | |
return string(bstr);//access local variable | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Lab7 { | |
struct dummyArr{ | |
uint a; | |
} | |
mapping(address => uint) public balances; | |
mapping(uint => uint) public demoMapping; | |
mapping(uint => dummyArr) public arrayMapping; | |
mapping(string => uint) public mobile; | |
//var["Google_Pixel"] = 80000; | |
//"GOogle" : 80000 | |
//"Lenovo" : 75000 | |
//"iPhone" : 60000 | |
function updateBalance(uint newBalance) public { | |
balances[msg.sender] = newBalance; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract Owner { | |
address owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner { | |
require(msg.sender == owner); | |
_; | |
} | |
modifier costs(uint price) { | |
if (msg.value >= price) { | |
_; | |
} | |
} | |
} | |
contract Register is Owner { | |
mapping (address => bool) registeredAddresses; | |
uint price; //State variable | |
constructor(uint initialPrice) public { price = initialPrice; } | |
function register() public payable costs(price) { | |
registeredAddresses[msg.sender] = true; | |
} | |
function changePrice(uint _price) public onlyOwner { | |
price = _price; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.6.6; | |
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/FlashLoanReceiverBase.sol"; | |
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPoolAddressesProvider.sol"; | |
import "https://github.com/aave/flashloan-box/blob/Remix/contracts/aave/ILendingPool.sol"; | |
contract Flashloan is FlashLoanReceiverBase { | |
constructor(address _addressProvider) FlashLoanReceiverBase(_addressProvider) public { | |
} | |
function flashloan(address _asset) public onlyOwner { | |
bytes memory data = ""; | |
uint amount = 1 ether; | |
ILendingPool lendingPool = ILendingPool(addressesProvider.getLendingPool()); | |
lendingPool.flashLoan(address(this), _asset, amount, data); | |
} | |
/** | |
This function is called after your contract has received the flash loaned amount | |
*/ | |
function executeOperation( | |
address _reserve, | |
uint256 _amount, | |
uint256 _fee, | |
bytes calldata _params | |
) | |
external | |
override | |
{ | |
require(_amount <= getBalanceInternal(address(this), _reserve), "Invalid balance, was the flashLoan successful?"); | |
// | |
// Your logic goes here. | |
// !! Ensure that *this contract* has enough of `_reserve` funds to payback the `_fee` !! | |
// | |
uint totalDebt = _amount.add(_fee); | |
transferFundsBackToPoolInternal(_reserve, totalDebt); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.4.0; | |
contract Readings { | |
string subRegion; | |
uint activePanels; | |
uint energyConsumed; | |
mapping(string => uint) panelsRemaining; | |
mapping(string => uint) energyPerRegion; | |
constructor(string memory _subRegion, uint _activePanels, uint _energyConsumed) public{ | |
subRegion = _subRegion; | |
activePanels = _activePanels; | |
energyConsumed = _energyConsumed; | |
energyPerRegion[_subRegion] += _energyConsumed; | |
panelsRemaining[_subRegion] = activePanels; | |
} | |
function getEnergyPerRegion(string memory region) public view returns(uint){ | |
return energyPerRegion[region]; | |
} | |
function updateEnergyConsumed(string memory _subRegion, uint _activePanels, | |
uint _energyConsumed ) public { | |
energyPerRegion[_subRegion] += _energyConsumed; | |
panelsRemaining[_subRegion] = _activePanels; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity >=0.4.22 <0.6.0; | |
contract SMContract { | |
address private utility; | |
uint private counter; | |
struct Reading{ | |
address uAddr; | |
uint256 energy_consumed; | |
uint256 readingNo; | |
} | |
event readingEvent(address _user, uint256 _reading); | |
mapping (uint256=> Reading) public _readings; | |
uint256[] public readingsArr; | |
constructor() public { | |
utility = msg.sender; | |
counter = 1; | |
} | |
function newReading(uint256 energy) public { | |
_readings[counter].uAddr = msg.sender; | |
_readings[counter].energy_consumed = energy; | |
_readings[counter].readingNo = readingsArr.length++; | |
readingsArr.push(counter); | |
counter++; | |
emit readingEvent(msg.sender, energy); | |
} | |
function returnReadingArr() public view returns(uint256[] memory) { | |
return readingsArr; | |
} | |
function returnCarbonCredits() public view returns(uint) { | |
uint carbonCredits = 0; | |
if(readingsArr.length >= 10){ | |
for(uint256 i = 0; i < readingsArr.length; i++){ | |
carbonCredits += readingsArr[i]*2; | |
} | |
} | |
return carbonCredits; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.4.0; | |
contract TurnBasedGame { | |
//0 - PLAYING, TIED - 1, WON - 2 | |
enum GameState {PLAYING, TIED, WON} | |
//Advanced | |
//enum GameState {PLAYING, TIED, PLAYER1WON, PLAYER2WON} | |
// game variables - Number 1 | |
struct Game { | |
address player1; | |
address player2; | |
uint256 score1; | |
uint256 score2; | |
GameState state; | |
} | |
Game game; | |
// Mapping of key vs games | |
mapping (uint256 => Game) games; | |
event NewGame(address player1, address player2, uint256 score); | |
constructor(uint256 uid, address _player2, uint256 _baseScore) public { | |
newGame(uid, _player2, _baseScore); | |
} | |
modifier callerMustBeAPlayer(uint256 uid) { | |
require(games[uid].player1 == msg.sender || games[uid].player2 == msg.sender, "Caller must be a player"); | |
_; | |
} | |
function newGame(uint256 uid, address _player2, uint256 _baseScore) public { | |
require(games[uid].player1 == address(0) | |
&& games[uid].player2 == address(0), "Game must not exist already!"); | |
games[uid] = Game(msg.sender, _player2, _baseScore, _baseScore, GameState.PLAYING); | |
emit NewGame(msg.sender, _player2, _baseScore); | |
} | |
function updateScore(uint256 uid, uint256 _score1, uint256 _score2) public callerMustBeAPlayer(uid) { | |
games[uid].score1 = _score1; | |
games[uid].score2 = _score2; | |
} | |
function tieGame(uint256 uid) public callerMustBeAPlayer(uid) { //game - tied | |
games[uid].state = GameState.TIED; | |
} | |
function wonGame(uint256 uid) public callerMustBeAPlayer(uid) { // game - over | |
games[uid].state = GameState.WON; | |
} | |
function getInfo(uint256 uid) public view returns(address, address, uint256, uint256, GameState){ | |
return(games[uid].player1, games[uid].player2, games[uid].score1, | |
games[uid].score2, games[uid].state); | |
} | |
function returnScorePlayer1(uint256 uid) public view returns(uint256){ | |
return (games[uid].score1); | |
} | |
function returnScorePlayer2(uint256 uid) public view returns(uint256){ | |
return (games[uid].score2); | |
} | |
//Advanced | |
/* | |
function decideResult(uint256 uid) public view returns(GameState){ | |
uint256 scorePlayer1 = returnScorePlayer1(uid); | |
uint256 scorePlayer2 = returnScorePlayer2(uid); | |
if(scorePlayer2 > scorePlayer1) | |
}*/ | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0x692a70d2e424a56d2c6c27aa97d1a86395877b3a |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
pragma solidity ^0.5.0; | |
contract webinar { | |
uint public a; | |
bytes32 public b; | |
int[] public c; | |
address public var1; | |
address var2; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Contracts from my remix IDE.