Created
August 29, 2018 17:46
-
-
Save ZainaliSyed/10b0e4650f23f7a72b792d017ef73063 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.4.24+commit.e67f0147.js&optimize=false&gist=
This file contains hidden or 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.21; | |
contract MyContract { | |
event Log(address addr); | |
constructor() public { | |
emit Log(this); | |
} | |
function add(uint256 a, uint256 b) public pure returns (uint256) { | |
return a + b; | |
} | |
} |
This file contains hidden or 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; | |
/** | |
* 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 _account address of the account to check | |
* @return whether the target address is a contract | |
*/ | |
function isContract(address _account) 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(_account) } | |
return size > 0; | |
} | |
} |
This file contains hidden or 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.2; | |
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. | |
function Ballot(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 constant 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 hidden or 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.2; | |
import "remix_tests.sol"; // this import is automatically injected by Remix. | |
import "./ballot.sol"; | |
contract test3 { | |
Ballot ballotToTest; | |
function beforeAll () { | |
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 constant returns (bool) { | |
return ballotToTest.winningProposal() == 1; | |
} | |
} |
This file contains hidden or 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; | |
import "./ERC721BasicToken.sol"; | |
contract CryptoMonester is ERC721BasicToken{ | |
struct Monster{ | |
string name ; | |
uint level; | |
uint attactPower; | |
uint defencePower; | |
} | |
Monster [] public monsters; | |
address public owner; | |
constructor () public { | |
owner = msg.sender; | |
} | |
// function CryptoMonester() public{ | |
// owner = msg.sender; | |
// } | |
function createMonster(string _name , address _to) public { | |
require(owner == msg.sender); | |
uint id = monsters.length; | |
monsters.push(Monster(_name, 1,100,100)); | |
_mint(_to,id); | |
} | |
function battle (uint _monsterId, uint _targetId) public{ | |
Monster storage monsters1 = monsters[_monsterId]; | |
Monster storage monsters2 = monsters[_targetId]; | |
if(monsters1.attactPower >= monsters2.defencePower){ | |
monsters1.level +=1; | |
monsters1.attactPower +=10; | |
} | |
else{ | |
monsters2.level +=1; | |
monsters2.attactPower +=10; | |
} | |
} | |
} |
This file contains hidden or 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.2; | |
contract DataTypes { | |
bool myBool = false; | |
int8 myInt = -128; | |
uint8 myUInt = 255; | |
string myString; | |
uint8[] myStringArr; | |
byte myValue; | |
bytes1 myBytes1; | |
bytes32 myBytes32; | |
// fixed256x8 myFixed = 1; // 255.0 | |
// ufixed myFixed = 1; | |
enum Action {ADD, REMOVE, UPDATE} | |
Action myAction = Action.ADD; | |
address myAddress; | |
function assignAddress() { | |
myAddress = msg.sender; | |
myAddress.balance; | |
myAddress.transfer(10); | |
} | |
uint[] myIntArr = [1,2,3]; | |
function arrFunc() { | |
myIntArr.push(1); | |
myIntArr.length; | |
myIntArr[0]; | |
} | |
uint[10] myFixedArr; | |
struct Account { | |
uint balance; | |
uint dailyLimit; | |
} | |
Account myAccount; | |
function structFunc() { | |
myAccount.balance = 100; | |
} | |
mapping (address => Account) _accounts; | |
function () payable { | |
_accounts[msg.sender].balance += msg.value; | |
} | |
function getBalance() returns (uint) { | |
return _accounts[msg.sender].balance; | |
} | |
} |
This file contains hidden or 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; | |
import "../../introspection/ERC165.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_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 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; | |
} |
This file contains hidden or 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; | |
import "./ERC721Basic.sol"; | |
import "./ERC721Receiver.sol"; | |
import "../../math/SafeMath.sol"; | |
import "../../AddressUtils.sol"; | |
import "../../introspection/SupportsInterfaceWithLookup.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); | |
} | |
/** | |
* @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 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(_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 specified token exists | |
* @param _tokenId uint256 ID of the token to query the existence of | |
* @return whether the token exists | |
*/ | |
function _exists(uint256 _tokenId) internal view returns (bool) { | |
address owner = tokenOwner[_tokenId]; | |
return owner != address(0); | |
} | |
/** | |
* @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); | |
} | |
} |
This file contains hidden or 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; | |
/** | |
* @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); | |
} |
This file contains hidden or 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.2; | |
contract Transaction { | |
event SenderLogger(address); | |
event ValueLogger(uint); | |
address private owner; | |
modifier isOwner { | |
require(owner == msg.sender); | |
_; | |
} | |
modifier validValue { | |
assert(msg.value >= 1 ether); | |
_; | |
} | |
function Transaction() { | |
owner = msg.sender; | |
} | |
function () payable isOwner validValue { | |
SenderLogger(msg.sender); | |
ValueLogger(msg.value); | |
} | |
} |
This file contains hidden or 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.2; | |
interface Regulator { | |
function checkValue(uint amount) constant external returns (bool); | |
function loan () constant external returns (bool); | |
} | |
contract Bank is Regulator { | |
uint private value; | |
address private owner; | |
modifier ownerFunc { | |
require(owner == msg.sender); | |
_; | |
} | |
constructor(uint amount) public{ | |
value = amount; | |
owner = msg.sender; | |
} | |
function deposit(uint amount) public ownerFunc { | |
value += amount; | |
} | |
function withdraw(uint amount) public ownerFunc { | |
if (checkValue(amount)) { | |
value -= amount; | |
} | |
} | |
function balance() constant public returns (uint) { | |
return value; | |
} | |
function checkValue(uint amount) constant public returns (bool) { | |
// Classic mistake in the tutorial value should be above the amount | |
return value >= amount; | |
} | |
function loan() constant public returns (bool) { | |
return value > 0; | |
} | |
} | |
contract MyFirstContract is Bank(10) { | |
string private name; | |
uint private age; | |
function setName(string newName) public{ | |
name = newName; | |
} | |
function getName() constant public returns (string) { | |
return name; | |
} | |
function setAge(uint newAge) public { | |
age = newAge; | |
} | |
function getAge() constant public returns (uint){ | |
return age; | |
} | |
} | |
contract TestTrow { | |
function testAssert() public { | |
assert(1==2); | |
} | |
function testRequire() public { | |
require(2==1); | |
} | |
function testRevert() public { | |
revert(); | |
} | |
function testThrow() public { | |
throw; | |
} | |
} |
This file contains hidden or 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.2; | |
library IntExtended { | |
function increament (uint _self) constant public returns (uint){ | |
return _self+1; | |
} | |
function decreament (uint _self) constant public returns (uint){ | |
return _self-1; | |
} | |
function increamentByValue(uint _self, uint _value) constant public returns(uint){ | |
return _self +_value; | |
} | |
function decreamentByValue(uint _self, uint _value) constant public returns(uint){ | |
return _self -_value; | |
} | |
} |
This file contains hidden or 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.6; | |
contract Practise { | |
uint public age = 24; | |
string public name = "zain"; | |
string constant fName = "Rizwan"; | |
constructor () public { | |
age = 25; | |
name = "ali"; | |
} | |
function setInstructor(string _name,uint _age) public { | |
name = _name; | |
age = _age; | |
} | |
function getInstructor () public constant returns(string, uint){ | |
return (name , age); | |
} | |
} |
This file contains hidden or 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.6; | |
// web3.utils.toAscii(val) | |
contract Quiz { | |
// struct Answer | |
// { | |
// bytes32 text; | |
// uint voteCount; // number of accumulated votes | |
// // add more non-key fields as needed | |
// } | |
struct Answer | |
{ | |
bytes32 [] text; | |
uint voteCount; // number of accumulated votes | |
// add more non-key fields as needed | |
} | |
struct Question | |
{ | |
bytes32 text; | |
bytes32[] answerList; // list of answer keys so we can look them up | |
mapping(bytes32 => Answer) answerStructs; // random access by question key and answer key | |
// add more non-key fields as needed | |
} | |
mapping(bytes32 => Question) questionStructs; // random access by question key | |
bytes32[] questionList; // list of question keys so we can enumerate them | |
function newQuestion(bytes32 questionKey, bytes32 text) public | |
// onlyOwner | |
returns(bool success) | |
{ | |
// not checking for duplicates | |
questionStructs[questionKey].text = text; | |
questionList.push(questionKey); | |
return true; | |
} | |
function getQuestion(bytes32 questionKey) | |
public | |
constant | |
returns(bytes32 wording, uint answerCount) | |
{ | |
return(questionStructs[questionKey].text, questionStructs[questionKey].answerList.length); | |
} | |
// function addAnswer(bytes32 questionKey, bytes32 answerKey, bytes32 answerText) | |
// // onlyOwner | |
// public | |
// returns(bool success) | |
// { | |
// questionStructs[questionKey].answerList.push(answerKey); | |
// questionStructs[questionKey].answerStructs[answerKey].text = answerText; | |
// // answer vote will init to 0 without our help | |
// return true; | |
// } | |
function addAnswerArray(bytes32 questionKey, bytes32 [] answerText, bytes32 answerLength) | |
// onlyOwner | |
public | |
returns(bool success) | |
{ | |
uint data = uint(answerLength); | |
for( uint i = 0; i < data; i++ ){ | |
bytes32 loopIndex = bytes32(i); | |
questionStructs[questionKey].answerList.push(loopIndex); | |
questionStructs[questionKey].answerStructs[loopIndex].text = answerText; | |
} | |
// answer vote will init to 0 without our help | |
return true; | |
} | |
// function getQuestionAnswer(bytes32 questionKey, bytes32 answerKey) | |
// public | |
// constant | |
// returns(bytes32 answerText, uint answerVoteCount) | |
// { | |
// return( | |
// questionStructs[questionKey].answerStructs[answerKey].text, | |
// questionStructs[questionKey].answerStructs[answerKey].voteCount); | |
// } | |
function getQuestionCount() | |
public | |
constant | |
returns(uint questionCount) | |
{ | |
return questionList.length; | |
} | |
function getQuestionAtIndex(uint row) | |
public | |
constant | |
returns(bytes32 questionkey) | |
{ | |
return questionList[row]; | |
} | |
function getQuestionAnswerCount(bytes32 questionKey) | |
public | |
constant | |
returns(uint answerCount) | |
{ | |
return(questionStructs[questionKey].answerList.length); | |
} | |
function getQuestionAnswerAtIndex(bytes32 questionKey, uint answerRow) | |
public | |
constant | |
returns(bytes32 answerKey) | |
{ | |
return(questionStructs[questionKey].answerList[answerRow]); | |
} | |
} |
This file contains hidden or 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; | |
/** | |
* @title SafeMath | |
* @dev Math operations with safety checks that revert on error | |
*/ | |
library SafeMath { | |
/** | |
* @dev Multiplies two numbers, reverts on overflow. | |
*/ | |
function mul(uint256 _a, uint256 _b) internal pure returns (uint256) { | |
// Gas optimization: this is cheaper than requiring '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; | |
} | |
uint256 c = _a * _b; | |
require(c / _a == _b); | |
return c; | |
} | |
/** | |
* @dev Integer division of two numbers truncating the quotient, reverts on division by zero. | |
*/ | |
function div(uint256 _a, uint256 _b) internal pure returns (uint256) { | |
require(_b > 0); // Solidity only automatically asserts 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 c; | |
} | |
/** | |
* @dev Subtracts two numbers, reverts on overflow (i.e. if subtrahend is greater than minuend). | |
*/ | |
function sub(uint256 _a, uint256 _b) internal pure returns (uint256) { | |
require(_b <= _a); | |
uint256 c = _a - _b; | |
return c; | |
} | |
/** | |
* @dev Adds two numbers, reverts on overflow. | |
*/ | |
function add(uint256 _a, uint256 _b) internal pure returns (uint256) { | |
uint256 c = _a + _b; | |
require(c >= _a); | |
return c; | |
} | |
/** | |
* @dev Divides two numbers and returns the remainder (unsigned integer modulo), | |
* reverts when dividing by zero. | |
*/ | |
function mod(uint256 a, uint256 b) internal pure returns (uint256) { | |
require(b != 0); | |
return a % b; | |
} | |
} |
This file contains hidden or 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.2; | |
import "browser/library.sol"; | |
contract UseLibraries{ | |
// to call datatypes string,uint | |
// using IntExtended for *; | |
using IntExtended for uint; | |
function testIncrement(uint _base) returns (uint){ | |
// return _base.increament(); | |
return IntExtended.increament(_base); | |
} | |
function testDecrement(uint _base) returns (uint){ | |
// return _base.decreament(); | |
return IntExtended.decreament(_base); | |
} | |
function testIncrementByValue(uint _base , uint _value) returns (uint){ | |
return _base.increamentByValue(_value); | |
// return IntExtended.increamentByValue(_base,_value); | |
} | |
function testDecrementByValue(uint _base , uint _value) returns (uint){ | |
return _base.decreamentByValue(_value); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment