Created
December 11, 2020 08:57
-
-
Save rafat/56faa551d4510c372658a0970673372c 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.5.17+commit.d19bba13.js&optimize=false&runs=200&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.4.22 <0.7.0; | |
/** | |
* @title Storage | |
* @dev Store & retrieve value in a variable | |
*/ | |
contract Storage { | |
uint256 number; | |
/** | |
* @dev Store value in variable | |
* @param num value to store | |
*/ | |
function store(uint256 num) public { | |
number = num; | |
} | |
/** | |
* @dev Return value | |
* @return value of 'number' | |
*/ | |
function retrieve() public view returns (uint256){ | |
return number; | |
} | |
} |
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.7.0; | |
/** | |
* @title Owner | |
* @dev Set & change owner | |
*/ | |
contract Owner { | |
address private owner; | |
// event for EVM logging | |
event OwnerSet(address indexed oldOwner, address indexed newOwner); | |
// modifier to check if caller is owner | |
modifier isOwner() { | |
// If the first argument of 'require' evaluates to 'false', execution terminates and all | |
// changes to the state and to Ether balances are reverted. | |
// This used to consume all gas in old EVM versions, but not anymore. | |
// It is often a good idea to use 'require' to check if functions are called correctly. | |
// As a second argument, you can also provide an explanation about what went wrong. | |
require(msg.sender == owner, "Caller is not owner"); | |
_; | |
} | |
/** | |
* @dev Set contract deployer as owner | |
*/ | |
constructor() public { | |
owner = msg.sender; // 'msg.sender' is sender of current call, contract deployer for a constructor | |
emit OwnerSet(address(0), owner); | |
} | |
/** | |
* @dev Change owner | |
* @param newOwner address of new owner | |
*/ | |
function changeOwner(address newOwner) public isOwner { | |
emit OwnerSet(owner, newOwner); | |
owner = newOwner; | |
} | |
/** | |
* @dev Return owner address | |
* @return address of owner | |
*/ | |
function getOwner() external view returns (address) { | |
return owner; | |
} | |
} |
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.7.0; | |
/** | |
* @title Ballot | |
* @dev Implements voting process along with vote delegation | |
*/ | |
contract Ballot { | |
struct Voter { | |
uint weight; // weight is accumulated by delegation | |
bool voted; // if true, that person already voted | |
address delegate; // person delegated to | |
uint vote; // index of the voted proposal | |
} | |
struct Proposal { | |
// If you can limit the length to a certain number of bytes, | |
// always use one of bytes1 to bytes32 because they are much cheaper | |
bytes32 name; // short name (up to 32 bytes) | |
uint voteCount; // number of accumulated votes | |
} | |
address public chairperson; | |
mapping(address => Voter) public voters; | |
Proposal[] public proposals; | |
/** | |
* @dev Create a new ballot to choose one of 'proposalNames'. | |
* @param proposalNames names of proposals | |
*/ | |
constructor(bytes32[] memory proposalNames) public { | |
chairperson = msg.sender; | |
voters[chairperson].weight = 1; | |
for (uint i = 0; i < proposalNames.length; i++) { | |
// 'Proposal({...})' creates a temporary | |
// Proposal object and 'proposals.push(...)' | |
// appends it to the end of 'proposals'. | |
proposals.push(Proposal({ | |
name: proposalNames[i], | |
voteCount: 0 | |
})); | |
} | |
} | |
/** | |
* @dev Give 'voter' the right to vote on this ballot. May only be called by 'chairperson'. | |
* @param voter address of voter | |
*/ | |
function giveRightToVote(address voter) public { | |
require( | |
msg.sender == chairperson, | |
"Only chairperson can give right to vote." | |
); | |
require( | |
!voters[voter].voted, | |
"The voter already voted." | |
); | |
require(voters[voter].weight == 0); | |
voters[voter].weight = 1; | |
} | |
/** | |
* @dev Delegate your vote to the voter 'to'. | |
* @param to address to which vote is delegated | |
*/ | |
function delegate(address to) public { | |
Voter storage sender = voters[msg.sender]; | |
require(!sender.voted, "You already voted."); | |
require(to != msg.sender, "Self-delegation is disallowed."); | |
while (voters[to].delegate != address(0)) { | |
to = voters[to].delegate; | |
// We found a loop in the delegation, not allowed. | |
require(to != msg.sender, "Found loop in delegation."); | |
} | |
sender.voted = true; | |
sender.delegate = to; | |
Voter storage delegate_ = voters[to]; | |
if (delegate_.voted) { | |
// If the delegate already voted, | |
// directly add to the number of votes | |
proposals[delegate_.vote].voteCount += sender.weight; | |
} else { | |
// If the delegate did not vote yet, | |
// add to her weight. | |
delegate_.weight += sender.weight; | |
} | |
} | |
/** | |
* @dev Give your vote (including votes delegated to you) to proposal 'proposals[proposal].name'. | |
* @param proposal index of proposal in the proposals array | |
*/ | |
function vote(uint proposal) public { | |
Voter storage sender = voters[msg.sender]; | |
require(sender.weight != 0, "Has no right to vote"); | |
require(!sender.voted, "Already voted."); | |
sender.voted = true; | |
sender.vote = proposal; | |
// If 'proposal' is out of the range of the array, | |
// this will throw automatically and revert all | |
// changes. | |
proposals[proposal].voteCount += sender.weight; | |
} | |
/** | |
* @dev Computes the winning proposal taking all previous votes into account. | |
* @return winningProposal_ index of winning proposal in the proposals array | |
*/ | |
function winningProposal() public view | |
returns (uint winningProposal_) | |
{ | |
uint winningVoteCount = 0; | |
for (uint p = 0; p < proposals.length; p++) { | |
if (proposals[p].voteCount > winningVoteCount) { | |
winningVoteCount = proposals[p].voteCount; | |
winningProposal_ = p; | |
} | |
} | |
} | |
/** | |
* @dev Calls winningProposal() function to get the index of the winner contained in the proposals array and then | |
* @return winnerName_ the name of the winner | |
*/ | |
function winnerName() public view | |
returns (bytes32 winnerName_) | |
{ | |
winnerName_ = proposals[winningProposal()].name; | |
} | |
} |
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.0; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/math/SafeMath.sol"; | |
contract Allowance is Ownable { | |
using SafeMath for uint; | |
event allowanceChanged(address indexed _from, address indexed _to, uint _oldAmount, uint _newAmount); | |
mapping(address => uint) public allowance; | |
function isOwner() internal view returns(bool) { | |
return owner() == msg.sender; | |
} | |
modifier OwnerorUser(address _to) { | |
require(isOwner() || msg.sender == _to, "You are not allowed"); | |
_; | |
} | |
function setAllowance(address _who, uint _amount) public onlyOwner { | |
emit allowanceChanged(msg.sender,_who,allowance[_who],_amount); | |
allowance[_who] = _amount; | |
} | |
function reduceAllowance(address _who, uint _amount) internal OwnerorUser(_who) { | |
emit allowanceChanged(msg.sender,_who,allowance[_who],allowance[_who].sub(_amount)); | |
allowance[_who] = allowance[_who].sub(_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.6.0; | |
contract SimpleAuction { | |
address payable public beneficiary; | |
uint public auctionEnd; | |
// Current state of the auction. | |
address public highestBidder; | |
uint public highestBid; | |
mapping(address => uint) pendingReturns; | |
bool ended; | |
event HighestBidIncreased(address bidder, uint amount); | |
event AuctionEnded(address winner, uint amount); | |
constructor( | |
uint _biddingTime, | |
address payable _beneficiary | |
) public { | |
beneficiary = _beneficiary; | |
auctionEnd = block.timestamp + _biddingTime; | |
} | |
function bid() public payable { | |
// No arguments are necessary, all | |
// information is already part of | |
// the transaction. The keyword payable | |
// is required for the function to | |
// be able to receive Ether. | |
// Revert the call if the bidding | |
// period is over. | |
require( | |
block.timestamp <= auctionEnd, | |
"Auction already ended." | |
); | |
// If the bid is not higher, send the | |
// money back. | |
require( | |
msg.value > highestBid, | |
"There already is a higher bid." | |
); | |
if (highestBid != 0) { | |
pendingReturns[highestBidder] += highestBid; | |
} | |
highestBidder = msg.sender; | |
highestBid = msg.value; | |
emit HighestBidIncreased(msg.sender, msg.value); | |
} | |
/// Withdraw a bid that was overbid. | |
function withdraw() public returns (bool) { | |
uint amount = pendingReturns[msg.sender]; | |
if (amount > 0) { | |
pendingReturns[msg.sender] = 0; | |
if (!msg.sender.send(amount)) { | |
// No need to call throw here, just reset the amount owing | |
pendingReturns[msg.sender] = amount; | |
return false; | |
} | |
} | |
return true; | |
} | |
/// End the auction and send the highest bid | |
/// to the beneficiary. | |
function auctionFinished() public { | |
// 1. Conditions | |
require(block.timestamp >= auctionEnd, "Auction not yet ended."); | |
require(!ended, "auctionEnd has already been called."); | |
// 2. Effects | |
ended = true; | |
emit AuctionEnded(highestBidder, highestBid); | |
// 3. Interaction | |
beneficiary.transfer(highestBid); | |
} | |
} |
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.0; | |
contract checkContract { | |
address public address1 = 0x2EB8291d3a6b72cf55B000610B6485b68783D330; | |
address public address2 = 0x56123d3026134107A4241ce6cD8B5aABdBd0b1F6; | |
address public addressToken = 0xa2c90542Cb9E2fD85d69fe658fD92168156E7F17; | |
} |
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.24; | |
// File: openzeppelin-solidity/contracts/introspection/ERC165.sol | |
/** | |
* @title ERC165 | |
* @dev https://github.com/ethereum/EIPs/blob/master/EIPS/eip-165.md | |
*/ | |
interface ERC165 { | |
/** | |
* @notice Query if a contract implements an interface | |
* @param _interfaceId The interface identifier, as specified in ERC-165 | |
* @dev Interface identification is specified in ERC-165. This function | |
* uses less than 30,000 gas. | |
*/ | |
function supportsInterface(bytes4 _interfaceId) | |
external | |
view | |
returns (bool); | |
} | |
// File: openzeppelin-solidity/contracts/token/ERC721/ERC721Basic.sol | |
/** | |
* @title ERC721 Non-Fungible Token Standard basic interface | |
* @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721Basic is ERC165 { | |
bytes4 internal constant InterfaceId_ERC721 = 0x80ac58cd; | |
/* | |
* 0x80ac58cd === | |
* bytes4(keccak256('balanceOf(address)')) ^ | |
* bytes4(keccak256('ownerOf(uint256)')) ^ | |
* bytes4(keccak256('approve(address,uint256)')) ^ | |
* bytes4(keccak256('getApproved(uint256)')) ^ | |
* bytes4(keccak256('setApprovalForAll(address,bool)')) ^ | |
* bytes4(keccak256('isApprovedForAll(address,address)')) ^ | |
* bytes4(keccak256('transferFrom(address,address,uint256)')) ^ | |
* bytes4(keccak256('safeTransferFrom(address,address,uint256)')) ^ | |
* bytes4(keccak256('safeTransferFrom(address,address,uint256,bytes)')) | |
*/ | |
bytes4 internal constant InterfaceId_ERC721Exists = 0x4f558e79; | |
/* | |
* 0x4f558e79 === | |
* bytes4(keccak256('exists(uint256)')) | |
*/ | |
bytes4 internal constant InterfaceId_ERC721Enumerable = 0x780e9d63; | |
/** | |
* 0x780e9d63 === | |
* bytes4(keccak256('totalSupply()')) ^ | |
* bytes4(keccak256('tokenOfOwnerByIndex(address,uint256)')) ^ | |
* bytes4(keccak256('tokenByIndex(uint256)')) | |
*/ | |
bytes4 internal constant InterfaceId_ERC721Metadata = 0x5b5e139f; | |
/** | |
* 0x5b5e139f === | |
* bytes4(keccak256('name()')) ^ | |
* bytes4(keccak256('symbol()')) ^ | |
* bytes4(keccak256('tokenURI(uint256)')) | |
*/ | |
event Transfer( | |
address indexed _from, | |
address indexed _to, | |
uint256 indexed _tokenId | |
); | |
event Approval( | |
address indexed _owner, | |
address indexed _approved, | |
uint256 indexed _tokenId | |
); | |
event ApprovalForAll( | |
address indexed _owner, | |
address indexed _operator, | |
bool _approved | |
); | |
function balanceOf(address _owner) public view returns (uint256 _balance); | |
function ownerOf(uint256 _tokenId) public view returns (address _owner); | |
function exists(uint256 _tokenId) public view returns (bool _exists); | |
function approve(address _to, uint256 _tokenId) public; | |
function getApproved(uint256 _tokenId) | |
public view returns (address _operator); | |
function setApprovalForAll(address _operator, bool _approved) public; | |
function isApprovedForAll(address _owner, address _operator) | |
public view returns (bool); | |
function transferFrom(address _from, address _to, uint256 _tokenId) public; | |
function safeTransferFrom(address _from, address _to, uint256 _tokenId) | |
public; | |
function safeTransferFrom( | |
address _from, | |
address _to, | |
uint256 _tokenId, | |
bytes _data | |
) | |
public; | |
} | |
// File: openzeppelin-solidity/contracts/token/ERC721/ERC721.sol | |
/** | |
* @title ERC-721 Non-Fungible Token Standard, optional enumeration extension | |
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721Enumerable is ERC721Basic { | |
function totalSupply() public view returns (uint256); | |
function tokenOfOwnerByIndex( | |
address _owner, | |
uint256 _index | |
) | |
public | |
view | |
returns (uint256 _tokenId); | |
function tokenByIndex(uint256 _index) public view returns (uint256); | |
} | |
/** | |
* @title ERC-721 Non-Fungible Token Standard, optional metadata extension | |
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721Metadata is ERC721Basic { | |
function name() external view returns (string _name); | |
function symbol() external view returns (string _symbol); | |
function tokenURI(uint256 _tokenId) public view returns (string); | |
} | |
/** | |
* @title ERC-721 Non-Fungible Token Standard, full implementation interface | |
* @dev See https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721 is ERC721Basic, ERC721Enumerable, ERC721Metadata { | |
} | |
// File: openzeppelin-solidity/contracts/token/ERC721/ERC721Receiver.sol | |
/** | |
* @title ERC721 token receiver interface | |
* @dev Interface for any contract that wants to support safeTransfers | |
* from ERC721 asset contracts. | |
*/ | |
contract ERC721Receiver { | |
/** | |
* @dev Magic value to be returned upon successful reception of an NFT | |
* Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`, | |
* which can be also obtained as `ERC721Receiver(0).onERC721Received.selector` | |
*/ | |
bytes4 internal constant ERC721_RECEIVED = 0x150b7a02; | |
/** | |
* @notice Handle the receipt of an NFT | |
* @dev The ERC721 smart contract calls this function on the recipient | |
* after a `safetransfer`. This function MAY throw to revert and reject the | |
* transfer. Return of other than the magic value MUST result in the | |
* transaction being reverted. | |
* Note: the contract address is always the message sender. | |
* @param _operator The address which called `safeTransferFrom` function | |
* @param _from The address which previously owned the token | |
* @param _tokenId The NFT identifier which is being transferred | |
* @param _data Additional data with no specified format | |
* @return `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` | |
*/ | |
function onERC721Received( | |
address _operator, | |
address _from, | |
uint256 _tokenId, | |
bytes _data | |
) | |
public | |
returns(bytes4); | |
} | |
// File: openzeppelin-solidity/contracts/math/SafeMath.sol | |
/** | |
* @title SafeMath | |
* @dev Math operations with safety checks that throw on error | |
*/ | |
library SafeMath { | |
/** | |
* @dev Multiplies two numbers, throws on overflow. | |
*/ | |
function mul(uint256 _a, uint256 _b) internal pure returns (uint256 c) { | |
// Gas optimization: this is cheaper than asserting 'a' not being zero, but the | |
// benefit is lost if 'b' is also tested. | |
// See: https://github.com/OpenZeppelin/openzeppelin-solidity/pull/522 | |
if (_a == 0) { | |
return 0; | |
} | |
c = _a * _b; | |
assert(c / _a == _b); | |
return c; | |
} | |
/** | |
* @dev Integer division of two numbers, truncating the quotient. | |
*/ | |
function div(uint256 _a, uint256 _b) internal pure returns (uint256) { | |
// assert(_b > 0); // Solidity automatically throws when dividing by 0 | |
// uint256 c = _a / _b; | |
// assert(_a == _b * c + _a % _b); // There is no case in which this doesn't hold | |
return _a / _b; | |
} | |
/** | |
* @dev Subtracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). | |
*/ | |
function sub(uint256 _a, uint256 _b) internal pure returns (uint256) { | |
assert(_b <= _a); | |
return _a - _b; | |
} | |
/** | |
* @dev Adds two numbers, throws on overflow. | |
*/ | |
function add(uint256 _a, uint256 _b) internal pure returns (uint256 c) { | |
c = _a + _b; | |
assert(c >= _a); | |
return c; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/AddressUtils.sol | |
/** | |
* Utility library of inline functions on addresses | |
*/ | |
library AddressUtils { | |
/** | |
* Returns whether the target address is a contract | |
* @dev This function will return false if invoked during the constructor of a contract, | |
* as the code is not actually created until after the constructor finishes. | |
* @param _addr address to check | |
* @return whether the target address is a contract | |
*/ | |
function isContract(address _addr) internal view returns (bool) { | |
uint256 size; | |
// XXX Currently there is no better way to check if there is a contract in an address | |
// than to check the size of the code at that address. | |
// See https://ethereum.stackexchange.com/a/14016/36603 | |
// for more details about how this works. | |
// TODO Check this again before the Serenity release, because all addresses will be | |
// contracts then. | |
// solium-disable-next-line security/no-inline-assembly | |
assembly { size := extcodesize(_addr) } | |
return size > 0; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/introspection/SupportsInterfaceWithLookup.sol | |
/** | |
* @title SupportsInterfaceWithLookup | |
* @author Matt Condon (@shrugs) | |
* @dev Implements ERC165 using a lookup table. | |
*/ | |
contract SupportsInterfaceWithLookup is ERC165 { | |
bytes4 public constant InterfaceId_ERC165 = 0x01ffc9a7; | |
/** | |
* 0x01ffc9a7 === | |
* bytes4(keccak256('supportsInterface(bytes4)')) | |
*/ | |
/** | |
* @dev a mapping of interface id to whether or not it's supported | |
*/ | |
mapping(bytes4 => bool) internal supportedInterfaces; | |
/** | |
* @dev A contract implementing SupportsInterfaceWithLookup | |
* implement ERC165 itself | |
*/ | |
constructor() | |
public | |
{ | |
_registerInterface(InterfaceId_ERC165); | |
} | |
/** | |
* @dev implement supportsInterface(bytes4) using a lookup table | |
*/ | |
function supportsInterface(bytes4 _interfaceId) | |
external | |
view | |
returns (bool) | |
{ | |
return supportedInterfaces[_interfaceId]; | |
} | |
/** | |
* @dev private method for registering an interface | |
*/ | |
function _registerInterface(bytes4 _interfaceId) | |
internal | |
{ | |
require(_interfaceId != 0xffffffff); | |
supportedInterfaces[_interfaceId] = true; | |
} | |
} | |
// File: openzeppelin-solidity/contracts/token/ERC721/ERC721BasicToken.sol | |
/** | |
* @title ERC721 Non-Fungible Token Standard basic implementation | |
* @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721BasicToken is SupportsInterfaceWithLookup, ERC721Basic { | |
using SafeMath for uint256; | |
using AddressUtils for address; | |
// Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))` | |
// which can be also obtained as `ERC721Receiver(0).onERC721Received.selector` | |
bytes4 private constant ERC721_RECEIVED = 0x150b7a02; | |
// Mapping from token ID to owner | |
mapping (uint256 => address) internal tokenOwner; | |
// Mapping from token ID to approved address | |
mapping (uint256 => address) internal tokenApprovals; | |
// Mapping from owner to number of owned token | |
mapping (address => uint256) internal ownedTokensCount; | |
// Mapping from owner to operator approvals | |
mapping (address => mapping (address => bool)) internal operatorApprovals; | |
constructor() | |
public | |
{ | |
// register the supported interfaces to conform to ERC721 via ERC165 | |
_registerInterface(InterfaceId_ERC721); | |
_registerInterface(InterfaceId_ERC721Exists); | |
} | |
/** | |
* @dev Gets the balance of the specified address | |
* @param _owner address to query the balance of | |
* @return uint256 representing the amount owned by the passed address | |
*/ | |
function balanceOf(address _owner) public view returns (uint256) { | |
require(_owner != address(0)); | |
return ownedTokensCount[_owner]; | |
} | |
/** | |
* @dev Gets the owner of the specified token ID | |
* @param _tokenId uint256 ID of the token to query the owner of | |
* @return owner address currently marked as the owner of the given token ID | |
*/ | |
function ownerOf(uint256 _tokenId) public view returns (address) { | |
address owner = tokenOwner[_tokenId]; | |
require(owner != address(0)); | |
return owner; | |
} | |
/** | |
* @dev Returns whether the specified token exists | |
* @param _tokenId uint256 ID of the token to query the existence of | |
* @return whether the token exists | |
*/ | |
function exists(uint256 _tokenId) public view returns (bool) { | |
address owner = tokenOwner[_tokenId]; | |
return owner != address(0); | |
} | |
/** | |
* @dev Approves another address to transfer the given token ID | |
* The zero address indicates there is no approved address. | |
* There can only be one approved address per token at a given time. | |
* Can only be called by the token owner or an approved operator. | |
* @param _to address to be approved for the given token ID | |
* @param _tokenId uint256 ID of the token to be approved | |
*/ | |
function approve(address _to, uint256 _tokenId) public { | |
address owner = ownerOf(_tokenId); | |
require(_to != owner); | |
require(msg.sender == owner || isApprovedForAll(owner, msg.sender)); | |
tokenApprovals[_tokenId] = _to; | |
emit Approval(owner, _to, _tokenId); | |
} | |
/** | |
* @dev Gets the approved address for a token ID, or zero if no address set | |
* @param _tokenId uint256 ID of the token to query the approval of | |
* @return address currently approved for the given token ID | |
*/ | |
function getApproved(uint256 _tokenId) public view returns (address) { | |
return tokenApprovals[_tokenId]; | |
} | |
/** | |
* @dev Sets or unsets the approval of a given operator | |
* An operator is allowed to transfer all tokens of the sender on their behalf | |
* @param _to operator address to set the approval | |
* @param _approved representing the status of the approval to be set | |
*/ | |
function setApprovalForAll(address _to, bool _approved) public { | |
require(_to != msg.sender); | |
operatorApprovals[msg.sender][_to] = _approved; | |
emit ApprovalForAll(msg.sender, _to, _approved); | |
} | |
/** | |
* @dev Tells whether an operator is approved by a given owner | |
* @param _owner owner address which you want to query the approval of | |
* @param _operator operator address which you want to query the approval of | |
* @return bool whether the given operator is approved by the given owner | |
*/ | |
function isApprovedForAll( | |
address _owner, | |
address _operator | |
) | |
public | |
view | |
returns (bool) | |
{ | |
return operatorApprovals[_owner][_operator]; | |
} | |
/** | |
* @dev Transfers the ownership of a given token ID to another address | |
* Usage of this method is discouraged, use `safeTransferFrom` whenever possible | |
* Requires the msg sender to be the owner, approved, or operator | |
* @param _from current owner of the token | |
* @param _to address to receive the ownership of the given token ID | |
* @param _tokenId uint256 ID of the token to be transferred | |
*/ | |
function transferFrom( | |
address _from, | |
address _to, | |
uint256 _tokenId | |
) | |
public | |
{ | |
require(isApprovedOrOwner(msg.sender, _tokenId)); | |
require(_from != address(0)); | |
require(_to != address(0)); | |
clearApproval(_from, _tokenId); | |
removeTokenFrom(_from, _tokenId); | |
addTokenTo(_to, _tokenId); | |
emit Transfer(_from, _to, _tokenId); | |
} | |
/** | |
* @dev Safely transfers the ownership of a given token ID to another address | |
* If the target address is a contract, it must implement `onERC721Received`, | |
* which is called upon a safe transfer, and return the magic value | |
* `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, | |
* the transfer is reverted. | |
* | |
* Requires the msg sender to be the owner, approved, or operator | |
* @param _from current owner of the token | |
* @param _to address to receive the ownership of the given token ID | |
* @param _tokenId uint256 ID of the token to be transferred | |
*/ | |
function safeTransferFrom( | |
address _from, | |
address _to, | |
uint256 _tokenId | |
) | |
public | |
{ | |
// solium-disable-next-line arg-overflow | |
safeTransferFrom(_from, _to, _tokenId, ""); | |
} | |
/** | |
* @dev Safely transfers the ownership of a given token ID to another address | |
* If the target address is a contract, it must implement `onERC721Received`, | |
* which is called upon a safe transfer, and return the magic value | |
* `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`; otherwise, | |
* the transfer is reverted. | |
* Requires the msg sender to be the owner, approved, or operator | |
* @param _from current owner of the token | |
* @param _to address to receive the ownership of the given token ID | |
* @param _tokenId uint256 ID of the token to be transferred | |
* @param _data bytes data to send along with a safe transfer check | |
*/ | |
function safeTransferFrom( | |
address _from, | |
address _to, | |
uint256 _tokenId, | |
bytes _data | |
) | |
public | |
{ | |
transferFrom(_from, _to, _tokenId); | |
// solium-disable-next-line arg-overflow | |
require(checkAndCallSafeTransfer(_from, _to, _tokenId, _data)); | |
} | |
/** | |
* @dev Returns whether the given spender can transfer a given token ID | |
* @param _spender address of the spender to query | |
* @param _tokenId uint256 ID of the token to be transferred | |
* @return bool whether the msg.sender is approved for the given token ID, | |
* is an operator of the owner, or is the owner of the token | |
*/ | |
function isApprovedOrOwner( | |
address _spender, | |
uint256 _tokenId | |
) | |
internal | |
view | |
returns (bool) | |
{ | |
address owner = ownerOf(_tokenId); | |
// Disable solium check because of | |
// https://github.com/duaraghav8/Solium/issues/175 | |
// solium-disable-next-line operator-whitespace | |
return ( | |
_spender == owner || | |
getApproved(_tokenId) == _spender || | |
isApprovedForAll(owner, _spender) | |
); | |
} | |
/** | |
* @dev Internal function to mint a new token | |
* Reverts if the given token ID already exists | |
* @param _to The address that will own the minted token | |
* @param _tokenId uint256 ID of the token to be minted by the msg.sender | |
*/ | |
function _mint(address _to, uint256 _tokenId) internal { | |
require(_to != address(0)); | |
addTokenTo(_to, _tokenId); | |
emit Transfer(address(0), _to, _tokenId); | |
} | |
/** | |
* @dev Internal function to burn a specific token | |
* Reverts if the token does not exist | |
* @param _tokenId uint256 ID of the token being burned by the msg.sender | |
*/ | |
function _burn(address _owner, uint256 _tokenId) internal { | |
clearApproval(_owner, _tokenId); | |
removeTokenFrom(_owner, _tokenId); | |
emit Transfer(_owner, address(0), _tokenId); | |
} | |
/** | |
* @dev Internal function to clear current approval of a given token ID | |
* Reverts if the given address is not indeed the owner of the token | |
* @param _owner owner of the token | |
* @param _tokenId uint256 ID of the token to be transferred | |
*/ | |
function clearApproval(address _owner, uint256 _tokenId) internal { | |
require(ownerOf(_tokenId) == _owner); | |
if (tokenApprovals[_tokenId] != address(0)) { | |
tokenApprovals[_tokenId] = address(0); | |
} | |
} | |
/** | |
* @dev Internal function to add a token ID to the list of a given address | |
* @param _to address representing the new owner of the given token ID | |
* @param _tokenId uint256 ID of the token to be added to the tokens list of the given address | |
*/ | |
function addTokenTo(address _to, uint256 _tokenId) internal { | |
require(tokenOwner[_tokenId] == address(0)); | |
tokenOwner[_tokenId] = _to; | |
ownedTokensCount[_to] = ownedTokensCount[_to].add(1); | |
} | |
/** | |
* @dev Internal function to remove a token ID from the list of a given address | |
* @param _from address representing the previous owner of the given token ID | |
* @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address | |
*/ | |
function removeTokenFrom(address _from, uint256 _tokenId) internal { | |
require(ownerOf(_tokenId) == _from); | |
ownedTokensCount[_from] = ownedTokensCount[_from].sub(1); | |
tokenOwner[_tokenId] = address(0); | |
} | |
/** | |
* @dev Internal function to invoke `onERC721Received` on a target address | |
* The call is not executed if the target address is not a contract | |
* @param _from address representing the previous owner of the given token ID | |
* @param _to target address that will receive the tokens | |
* @param _tokenId uint256 ID of the token to be transferred | |
* @param _data bytes optional data to send along with the call | |
* @return whether the call correctly returned the expected magic value | |
*/ | |
function checkAndCallSafeTransfer( | |
address _from, | |
address _to, | |
uint256 _tokenId, | |
bytes _data | |
) | |
internal | |
returns (bool) | |
{ | |
if (!_to.isContract()) { | |
return true; | |
} | |
bytes4 retval = ERC721Receiver(_to).onERC721Received( | |
msg.sender, _from, _tokenId, _data); | |
return (retval == ERC721_RECEIVED); | |
} | |
} | |
// File: openzeppelin-solidity/contracts/token/ERC721/ERC721Token.sol | |
/** | |
* @title Full ERC721 Token | |
* This implementation includes all the required and some optional functionality of the ERC721 standard | |
* Moreover, it includes approve all functionality using operator terminology | |
* @dev see https://github.com/ethereum/EIPs/blob/master/EIPS/eip-721.md | |
*/ | |
contract ERC721Token is SupportsInterfaceWithLookup, ERC721BasicToken, ERC721 { | |
// Token name | |
string internal name_; | |
// Token symbol | |
string internal symbol_; | |
// Mapping from owner to list of owned token IDs | |
mapping(address => uint256[]) internal ownedTokens; | |
// Mapping from token ID to index of the owner tokens list | |
mapping(uint256 => uint256) internal ownedTokensIndex; | |
// Array with all token ids, used for enumeration | |
uint256[] internal allTokens; | |
// Mapping from token id to position in the allTokens array | |
mapping(uint256 => uint256) internal allTokensIndex; | |
// Optional mapping for token URIs | |
mapping(uint256 => string) internal tokenURIs; | |
/** | |
* @dev Constructor function | |
*/ | |
constructor(string _name, string _symbol) public { | |
name_ = _name; | |
symbol_ = _symbol; | |
// register the supported interfaces to conform to ERC721 via ERC165 | |
_registerInterface(InterfaceId_ERC721Enumerable); | |
_registerInterface(InterfaceId_ERC721Metadata); | |
} | |
/** | |
* @dev Gets the token name | |
* @return string representing the token name | |
*/ | |
function name() external view returns (string) { | |
return name_; | |
} | |
/** | |
* @dev Gets the token symbol | |
* @return string representing the token symbol | |
*/ | |
function symbol() external view returns (string) { | |
return symbol_; | |
} | |
/** | |
* @dev Returns an URI for a given token ID | |
* Throws if the token ID does not exist. May return an empty string. | |
* @param _tokenId uint256 ID of the token to query | |
*/ | |
function tokenURI(uint256 _tokenId) public view returns (string) { | |
require(exists(_tokenId)); | |
return tokenURIs[_tokenId]; | |
} | |
/** | |
* @dev Gets the token ID at a given index of the tokens list of the requested owner | |
* @param _owner address owning the tokens list to be accessed | |
* @param _index uint256 representing the index to be accessed of the requested tokens list | |
* @return uint256 token ID at the given index of the tokens list owned by the requested address | |
*/ | |
function tokenOfOwnerByIndex( | |
address _owner, | |
uint256 _index | |
) | |
public | |
view | |
returns (uint256) | |
{ | |
require(_index < balanceOf(_owner)); | |
return ownedTokens[_owner][_index]; | |
} | |
/** | |
* @dev Gets the total amount of tokens stored by the contract | |
* @return uint256 representing the total amount of tokens | |
*/ | |
function totalSupply() public view returns (uint256) { | |
return allTokens.length; | |
} | |
/** | |
* @dev Gets the token ID at a given index of all the tokens in this contract | |
* Reverts if the index is greater or equal to the total number of tokens | |
* @param _index uint256 representing the index to be accessed of the tokens list | |
* @return uint256 token ID at the given index of the tokens list | |
*/ | |
function tokenByIndex(uint256 _index) public view returns (uint256) { | |
require(_index < totalSupply()); | |
return allTokens[_index]; | |
} | |
/** | |
* @dev Internal function to set the token URI for a given token | |
* Reverts if the token ID does not exist | |
* @param _tokenId uint256 ID of the token to set its URI | |
* @param _uri string URI to assign | |
*/ | |
function _setTokenURI(uint256 _tokenId, string _uri) internal { | |
require(exists(_tokenId)); | |
tokenURIs[_tokenId] = _uri; | |
} | |
/** | |
* @dev Internal function to add a token ID to the list of a given address | |
* @param _to address representing the new owner of the given token ID | |
* @param _tokenId uint256 ID of the token to be added to the tokens list of the given address | |
*/ | |
function addTokenTo(address _to, uint256 _tokenId) internal { | |
super.addTokenTo(_to, _tokenId); | |
uint256 length = ownedTokens[_to].length; | |
ownedTokens[_to].push(_tokenId); | |
ownedTokensIndex[_tokenId] = length; | |
} | |
/** | |
* @dev Internal function to remove a token ID from the list of a given address | |
* @param _from address representing the previous owner of the given token ID | |
* @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address | |
*/ | |
function removeTokenFrom(address _from, uint256 _tokenId) internal { | |
super.removeTokenFrom(_from, _tokenId); | |
// To prevent a gap in the array, we store the last token in the index of the token to delete, and | |
// then delete the last slot. | |
uint256 tokenIndex = ownedTokensIndex[_tokenId]; | |
uint256 lastTokenIndex = ownedTokens[_from].length.sub(1); | |
uint256 lastToken = ownedTokens[_from][lastTokenIndex]; | |
ownedTokens[_from][tokenIndex] = lastToken; | |
// This also deletes the contents at the last position of the array | |
ownedTokens[_from].length--; | |
// Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to | |
// be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping | |
// the lastToken to the first position, and then dropping the element placed in the last position of the list | |
ownedTokensIndex[_tokenId] = 0; | |
ownedTokensIndex[lastToken] = tokenIndex; | |
} | |
/** | |
* @dev Internal function to mint a new token | |
* Reverts if the given token ID already exists | |
* @param _to address the beneficiary that will own the minted token | |
* @param _tokenId uint256 ID of the token to be minted by the msg.sender | |
*/ | |
function _mint(address _to, uint256 _tokenId) internal { | |
super._mint(_to, _tokenId); | |
allTokensIndex[_tokenId] = allTokens.length; | |
allTokens.push(_tokenId); | |
} | |
/** | |
* @dev Internal function to burn a specific token | |
* Reverts if the token does not exist | |
* @param _owner owner of the token to burn | |
* @param _tokenId uint256 ID of the token being burned by the msg.sender | |
*/ | |
function _burn(address _owner, uint256 _tokenId) internal { | |
super._burn(_owner, _tokenId); | |
// Clear metadata (if any) | |
if (bytes(tokenURIs[_tokenId]).length != 0) { | |
delete tokenURIs[_tokenId]; | |
} | |
// Reorg all tokens array | |
uint256 tokenIndex = allTokensIndex[_tokenId]; | |
uint256 lastTokenIndex = allTokens.length.sub(1); | |
uint256 lastToken = allTokens[lastTokenIndex]; | |
allTokens[tokenIndex] = lastToken; | |
allTokens[lastTokenIndex] = 0; | |
allTokens.length--; | |
allTokensIndex[_tokenId] = 0; | |
allTokensIndex[lastToken] = tokenIndex; | |
} | |
} | |
// File: crypto-collectible.sol | |
pragma solidity^0.4.24; | |
contract Collectible is ERC721Token { | |
constructor() ERC721Token("Collectible", "COL") public {} | |
struct Color { | |
uint8 red; | |
uint8 green; | |
uint8 blue; | |
} | |
Color[] colors; | |
function mint() public { | |
Color memory _color = Color(uint8(now), uint8(now-1000), uint8(now-5500)); | |
uint _id = colors.push(_color) - 1; | |
_mint(msg.sender, _id); | |
} | |
function getColorFromId(uint id) public view returns(uint8, uint8, uint8) { | |
return (colors[id].red, colors[id].green, colors[id].blue); | |
} | |
} |
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.11; | |
contract EventExample { | |
mapping(address => uint) public tokenBalance; | |
event TokensSent(address _from, address _to, uint _amount); | |
constructor() public { | |
tokenBalance[msg.sender] = 100; | |
} | |
function sendToken(address _to, uint _amount) public returns(bool) { | |
require(tokenBalance[msg.sender] >= _amount, "Not enough tokens"); | |
assert(tokenBalance[_to] + _amount >= tokenBalance[_to]); | |
assert(tokenBalance[msg.sender] - _amount <= tokenBalance[msg.sender]) | |
; | |
tokenBalance[msg.sender] -= _amount; | |
tokenBalance[_to] += _amount; | |
emit TokensSent(msg.sender,_to,_amount); | |
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.6.0; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol"; | |
contract Item { | |
uint public priceInWei; | |
uint publicIndex; | |
uint public paid; | |
ItemManager parentContract; | |
constructor(ItemManager _parentContract, uint _priceInWei, uint _index) public { | |
priceInWei = _priceInWei; | |
publicIndex = _index; | |
parentContract = _parentContract; | |
} | |
receive() external payable { | |
require(paid == 0, "Price is already paid"); | |
require(priceInWei == msg.value, "Only Full Payments Allowed"); | |
paid += msg.value; | |
(bool success,) = address(parentContract).call{value:msg.value}(abi.encodeWithSignature("triggerPayment(uint256)",publicIndex)); | |
require(success,"the transaction was unsuccessful"); | |
} | |
fallback() external {} | |
} | |
contract ItemManager is Ownable { | |
enum SupplyChainState{created,paid,delivered} | |
struct S_Item { | |
Item item; | |
string identifier; | |
uint price; | |
ItemManager.SupplyChainState state; | |
} | |
uint _itemIndex; | |
mapping (uint => S_Item) public items; | |
event supplychainStep(uint _itemIndex, uint _step,address _itemAddress); | |
function isOwner() internal view returns(bool) { | |
return owner() == msg.sender; | |
} | |
function createItem(string memory _identifier, uint _itemPrice) public onlyOwner { | |
Item item = new Item(this,_itemPrice,_itemIndex); | |
items[_itemIndex].item = item; | |
items[_itemIndex].identifier = _identifier; | |
items[_itemIndex].price = _itemPrice; | |
items[_itemIndex].state = SupplyChainState.created; | |
emit supplychainStep(_itemIndex,uint(items[_itemIndex].state),address(item)); | |
_itemIndex++; | |
} | |
function triggerPayment(uint itemIndex) public payable { | |
require(items[itemIndex].price == msg.value,"The price is incorrect."); | |
require (items[itemIndex].state == SupplyChainState.created, "Item is not available for sale."); | |
items[itemIndex].state = SupplyChainState.paid; | |
emit supplychainStep(itemIndex,uint(items[itemIndex].state),address(items[itemIndex].item)); | |
} | |
function triggerDelivery(uint itemIndex) public onlyOwner { | |
require (items[itemIndex].state == SupplyChainState.paid, "Item is further in the chain"); | |
items[itemIndex].state = SupplyChainState.delivered; | |
emit supplychainStep(itemIndex,uint(items[itemIndex].state),address(items[itemIndex].item)); | |
} | |
} |
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.13; | |
contract ExceptionExample { | |
mapping (address => uint) public balanceReceived; | |
function receiveMoney() public payable { | |
assert(balanceReceived[msg.sender] + msg.value >= balanceReceived[msg.sender]); | |
balanceReceived[msg.sender] += msg.value; | |
} | |
function withdrawMoney(address payable _to, uint _amount) public { | |
require(_amount <= balanceReceived[msg.sender],"You don't have enough ether"); | |
assert(balanceReceived[msg.sender] - _amount <= balanceReceived[msg.sender]); | |
balanceReceived[msg.sender] -= _amount; | |
_to.transfer(_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.5.13; | |
contract ExceptionExample { | |
mapping (address => uint) public balanceReceived; | |
address payable public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
function destroyContract() public { | |
require(msg.sender == owner); | |
selfdestruct(owner); | |
} | |
function getOwner() public view returns (address) { | |
return owner; | |
} | |
function receiveMoney() public payable { | |
assert(balanceReceived[msg.sender] + msg.value >= balanceReceived[msg.sender]); | |
balanceReceived[msg.sender] += msg.value; | |
} | |
function withdrawMoney(address payable _to, uint _amount) public { | |
require(_amount <= balanceReceived[msg.sender],"You don't have enough ether"); | |
assert(balanceReceived[msg.sender] - _amount <= balanceReceived[msg.sender]); | |
balanceReceived[msg.sender] -= _amount; | |
_to.transfer(_amount); | |
} | |
receive () external payable { | |
receiveMoney(); | |
} | |
} |
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.10; | |
contract incone { | |
uint256 private value; | |
/// Constructor that initializes the `uint256` value to the given `init_value`. | |
constructor (uint256 initvalue) public { | |
value = initvalue; | |
} | |
/// Adds one to a uint | |
function inc() public { | |
value = value+1; | |
} | |
/// Simply returns the current value of 'uint256' | |
function get() public view returns (uint256) { | |
return value; | |
} | |
} |
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.13; | |
import './owned.sol'; | |
contract InheritanceModifierExample is Owned { | |
mapping (address => uint) public tokenBalance; | |
uint tokenPrice = 1 ether; | |
constructor() { | |
tokenBalance[owner] = 100; | |
} | |
function createNewToken() public onlyOwner { | |
tokenBalance[owner]++; | |
} | |
function burnToken() public onlyOwner { | |
tokenBalance[owner]--; | |
} | |
function purchaseToken() public payable { | |
require(tokenBalance[owner] >= msg.value/tokenPrice, "Not enough Tokens available to purchase"); | |
tokenBalance[owner] -= msg.value/tokenPrice; | |
tokenBalance[msg.sender] += msg.value/tokenPrice; | |
} | |
function sendToken(address payable _to, uint _number) public { | |
require(tokenBalance[msg.sender] >= _number, "Not enough Tokens available to send"); | |
assert(tokenBalance[_to] + _number >= tokenBalance[_to]); | |
assert(tokenBalance[msg.sender] >= tokenBalance[msg.sender] - _number); | |
tokenBalance[msg.sender] -= _number; | |
tokenBalance[_to] += _number; | |
} | |
} |
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.0; | |
import "https://raw.githubusercontent.com/OpenZeppelin/openzeppelin-contracts/master/contracts/math/SafeMath.sol"; | |
contract LibrariesExample { | |
using SafeMath for uint; | |
mapping(address => uint) public tokenBalance; | |
constructor() public { | |
tokenBalance[msg.sender] = 1; | |
} | |
function sendToken(address _to, uint _amount) public returns(bool) { | |
tokenBalance[msg.sender] = tokenBalance[msg.sender].sub(_amount); | |
tokenBalance[_to] = tokenBalance[_to].add(_amount); | |
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.13; | |
contract MappingExample { | |
mapping(uint256 => bool) public myMapping; | |
mapping(address => bool) public myAddress; | |
function setValue(uint256 _index) public { | |
myMapping[_index] = true; | |
} | |
function setAddressMapping() public { | |
myAddress[msg.sender] = 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.13; | |
contract MappingStructExample { | |
struct Payment { | |
uint amount; | |
uint timestamp; | |
} | |
struct Balance { | |
uint totalBalance; | |
uint numPayments; | |
mapping(uint => Payment) payments; | |
} | |
mapping(address => Balance) public balanceReceived; | |
function sendMoney() public payable { | |
balanceReceived[msg.sender].totalBalance += msg.value; | |
Payment memory payment = Payment(msg.value,now); | |
balanceReceived[msg.sender].payments[balanceReceived[msg.sender].numPayments] = payment; | |
balanceReceived[msg.sender].numPayments += 1; | |
} | |
function getBalance() public view returns (uint) { | |
return address(this).balance; | |
} | |
function withdrawAllMoney(address payable _to) public { | |
uint balanceToSend = balanceReceived[msg.sender].totalBalance; | |
balanceReceived[msg.sender].totalBalance = 0; | |
_to.transfer(balanceToSend); | |
} | |
function withdrawMoney(address payable _to, uint _amount) public { | |
require(balanceReceived[msg.sender].totalBalance >= _amount, "Not enough funds to excute transaction"); | |
balanceReceived[msg.sender].totalBalance -= _amount; | |
_to.transfer(_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
// SPDX-License-Identifier: MIT | |
pragma solidity >=0.4.21 <= 0.7.0; | |
contract Migrations { | |
address public owner; | |
uint public last_completed_migration; | |
modifier restricted() { | |
if (msg.sender == owner) _; | |
} | |
constructor() public { | |
owner = msg.sender; | |
} | |
function setCompleted(uint completed) public restricted { | |
last_completed_migration = completed; | |
} | |
} |
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.15; | |
contract MyContract { | |
string public myString = "Hello World"; | |
uint public myUint = 10; | |
function setString(string memory _mystring) public { | |
myString = _mystring; | |
} | |
function setUint(uint _myuint) public { | |
myUint = _myuint; | |
} | |
} |
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 "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20.sol"; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/v2.3.0/contracts/token/ERC20/ERC20Detailed.sol"; | |
// This ERC-20 contract mints the specified amount of tokens to the contract creator. | |
contract MyToken is ERC20, ERC20Detailed { | |
constructor(uint256 initialSupply) | |
public | |
ERC20Detailed("RTOKEN", "RTOK", 18) | |
{ | |
_mint(msg.sender, initialSupply); | |
} | |
} |
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.13; | |
contract Owned { | |
address public owner; | |
constructor() public { | |
owner = msg.sender; | |
} | |
modifier onlyOwner() { | |
require(owner == msg.sender, "You are not allowed"); | |
_; | |
} | |
} |
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 PayToPlay { | |
event messageWrite(string _text); | |
string message; | |
function write(string memory _message) public payable { | |
require(msg.value == 10000000000000000, "Error Require 0.01 BNB to write message"); | |
message = _message; | |
emit messageWrite(message); | |
} | |
function read() public view returns(string memory) { | |
return(message); | |
} | |
} | |
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.13; | |
contract SendMoneyExample { | |
uint public balanceReceived; | |
function receiveMoney() public payable { | |
balanceReceived += msg.value; | |
} | |
function getBalance() public view returns (uint) { | |
return address(this).balance; | |
} | |
function withdrawMoney() public { | |
address payable to = msg.sender; | |
to.transfer(this.getBalance()); | |
balanceReceived = 0; | |
} | |
function withdrawMoneyTo(address payable _sendAddress) public { | |
_sendAddress.transfer(this.getBalance()); | |
balanceReceived = 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.6.0; | |
import "https://github.com/OpenZeppelin/openzeppelin-contracts/blob/master/contracts/access/Ownable.sol"; | |
contract SimpleWallet is Ownable { | |
mapping(address => uint) public allowance; | |
function isOwner() internal view returns(bool) { | |
return owner() == msg.sender; | |
} | |
modifier OwnerorUser(address _to) { | |
require(isOwner() || msg.sender == _to, "You are not allowed"); | |
_; | |
} | |
function getBalance() public view returns (uint) { | |
return address(this).balance; | |
} | |
function reduceAllowance(address _who, uint _amount) internal OwnerorUser(_who) { | |
allowance[_who] -= _amount; | |
} | |
function withdraw(address payable _to, uint _amount) public payable OwnerorUser(_to) { | |
require(address(this).balance >= _amount, "Not enough funds available for this transaction"); | |
require(allowance[_to] >= _amount, "requested amount exceeds the allowance"); | |
reduceAllowance(_to,_amount); | |
_to.transfer(_amount); | |
} | |
function setAllowance(address _who, uint _amount) public onlyOwner { | |
allowance[_who] = _amount; | |
} | |
receive() external payable { | |
} | |
} |
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.13; | |
contract StartStopUpdateExample { | |
address owner; | |
bool public paused; | |
constructor() public { | |
owner = msg.sender; | |
} | |
function sendMoney() public payable { | |
} | |
function withdrawAllMoney(address payable _to) public { | |
require(msg.sender == owner, "You are not the owner"); | |
require(paused == false,"The Contract is Paused"); | |
_to.transfer(address(this).balance); | |
} | |
function setPaused(bool _paused) public { | |
require(msg.sender == owner, "You are not the owner"); | |
paused = _paused; | |
} | |
function destroySmartContract(address payable _to) public { | |
require(msg.sender == owner,"You are not the owner"); | |
selfdestruct(_to); | |
} | |
} |
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.10; | |
contract WorkingWithVariables { | |
uint256 public myVar; | |
function setMyVar(uint256 _myVar) public { | |
myVar = _myVar; | |
} | |
bool public myBool; | |
function setMyBool(bool _myBool) public { | |
myBool = _myBool; | |
} | |
uint8 public myUint8; | |
function incrementuint8() public { | |
myUint8++; | |
} | |
function decrementuint8() public { | |
myUint8--; | |
} | |
address public myAddress; | |
function setAddress(address _address) public { | |
myAddress = _address; | |
} | |
function getBalance() public view returns (uint) { | |
return myAddress.balance; | |
} | |
string public myString = "hello World"; | |
function setString(string memory _myString) public { | |
myString = _myString; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment