Skip to content

Instantly share code, notes, and snippets.

@lildata
Created April 17, 2019 20:57
Show Gist options
  • Save lildata/4fef503aa626c095901d200f19115506 to your computer and use it in GitHub Desktop.
Save lildata/4fef503aa626c095901d200f19115506 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.7+commit.6da8b019.js&optimize=false&gist=
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;
}
}
}
# Voting with delegation.
# Information about voters
struct Voter:
# weight is accumulated by delegation
weight: int128
# if true, that person already voted (which includes voting by delegating)
voted: bool
# person delegated to
delegate: address
# index of the voted proposal, which is not meaningful unless 'voted' is True.
vote: int128
# Users can create proposals
struct Proposal:
# short name (up to 32 bytes)
name: bytes32
# number of accumulated votes
voteCount: int128
voters: public(map(address, Voter))
proposals: public(map(int128, Proposal))
voterCount: public(int128)
chairperson: public(address)
int128Proposals: public(int128)
@public
@constant
def delegated(addr: address) -> bool:
return self.voters[addr].delegate != ZERO_ADDRESS
@public
@constant
def directlyVoted(addr: address) -> bool:
return self.voters[addr].voted and (self.voters[addr].delegate == ZERO_ADDRESS)
# Setup global variables
@public
def __init__(_proposalNames: bytes32[2]):
self.chairperson = msg.sender
self.voterCount = 0
for i in range(2):
self.proposals[i] = Proposal({
name: _proposalNames[i],
voteCount: 0
})
self.int128Proposals += 1
# Give a 'voter' the right to vote on this ballot.
# This may only be called by the 'chairperson'.
@public
def giveRightToVote(voter: address):
# Throws if the sender is not the chairperson.
assert msg.sender == self.chairperson
# Throws if the voter has already voted.
assert not self.voters[voter].voted
# Throws if the voter's voting weight isn't 0.
assert self.voters[voter].weight == 0
self.voters[voter].weight = 1
self.voterCount += 1
# Used by 'delegate' below, and can be called by anyone.
@public
def forwardWeight(delegate_with_weight_to_forward: address):
assert self.delegated(delegate_with_weight_to_forward)
# Throw if there is nothing to do:
assert self.voters[delegate_with_weight_to_forward].weight > 0
target: address = self.voters[delegate_with_weight_to_forward].delegate
for i in range(4):
if self.delegated(target):
target = self.voters[target].delegate
# The following effectively detects cycles of length <= 5,
# in which the delegation is given back to the delegator.
# This could be done for any int128ber of loops,
# or even infinitely with a while loop.
# However, cycles aren't actually problematic for correctness;
# they just result in spoiled votes.
# So, in the production version, this should instead be
# the responsibility of the contract's client, and this
# check should be removed.
assert target != delegate_with_weight_to_forward
else:
# Weight will be moved to someone who directly voted or
# hasn't voted.
break
weight_to_forward: int128 = self.voters[delegate_with_weight_to_forward].weight
self.voters[delegate_with_weight_to_forward].weight = 0
self.voters[target].weight += weight_to_forward
if self.directlyVoted(target):
self.proposals[self.voters[target].vote].voteCount += weight_to_forward
self.voters[target].weight = 0
# To reiterate: if target is also a delegate, this function will need
# to be called again, similarly to as above.
# Delegate your vote to the voter 'to'.
@public
def delegate(to: address):
# Throws if the sender has already voted
assert not self.voters[msg.sender].voted
# Throws if the sender tries to delegate their vote to themselves or to
# the default address value of 0x0000000000000000000000000000000000000000
# (the latter might not be problematic, but I don't want to think about it).
assert to != msg.sender
assert to != ZERO_ADDRESS
self.voters[msg.sender].voted = True
self.voters[msg.sender].delegate = to
# This call will throw if and only if this delegation would cause a loop
# of length <= 5 that ends up delegating back to the delegator.
self.forwardWeight(msg.sender)
# Give your vote (including votes delegated to you)
# to proposal 'proposals[proposal].name'.
@public
def vote(proposal: int128):
# can't vote twice
assert not self.voters[msg.sender].voted
# can only vote on legitimate proposals
assert proposal < self.int128Proposals
self.voters[msg.sender].vote = proposal
self.voters[msg.sender].voted = True
# transfer msg.sender's weight to proposal
self.proposals[proposal].voteCount += self.voters[msg.sender].weight
self.voters[msg.sender].weight = 0
# Computes the winning proposal taking all
# previous votes into account.
@public
@constant
def winningProposal() -> int128:
winning_vote_count: int128 = 0
winning_proposal: int128 = 0
for i in range(2):
if self.proposals[i].voteCount > winning_vote_count:
winning_vote_count = self.proposals[i].voteCount
winning_proposal = i
return winning_proposal
# Calls winningProposal() function to get the index
# of the winner contained in the proposals array and then
# returns the name of the winner
@public
@constant
def winnerName() -> bytes32:
return self.proposals[self.winningProposal()].name
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;
}
}
pragma solidity ^0.4.0;
contract Courses {
string fName;
uint age;
function setInstructor(string _fName, uint _age) public {
fName = _fName;
age = _age;
}
function getInstructor() public constant returns (string, uint) {
return (fName, age);
}
}
pragma solidity ^0.5.6;
contract owned {
address payable owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner, "only the owner can call this fn");
_;
}
}
contract mortal is owned {
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
contract Courses is mortal {
string fName;
uint age;
function setInstructor(string memory _fName, uint _age) public {
fName = _fName;
age = _age;
}
function getInstructor() public view returns (string memory, uint) {
return (fName, age);
}
}
fName: public(string[20])
age: public(int128)
@public
def setInstructor(_fName: string[20], _age: int128):
self.fName = _fName
self.age = _age
@public
def getInstructor() -> string[20]:
return self.fName
pragma solidity ^0.5.2;
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC721/IERC721.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC721/IERC721Receiver.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/utils/Address.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/drafts/Counters.sol";
import "github.com/OpenZeppelin/openzeppelin-solidity/contracts/introspection/ERC165.sol";
/**
* @title ERC721 Non-Fungible Token Standard basic implementation
* @dev see https://eips.ethereum.org/EIPS/eip-721
*/
contract ERC721 is ERC165, IERC721 {
using SafeMath for uint256;
using Address for address;
using Counters for Counters.Counter;
// Equals to `bytes4(keccak256("onERC721Received(address,address,uint256,bytes)"))`
// which can be also obtained as `IERC721Receiver(0).onERC721Received.selector`
bytes4 private constant _ERC721_RECEIVED = 0x150b7a02;
// Mapping from token ID to owner
mapping (uint256 => address) private _tokenOwner;
// Mapping from token ID to approved address
mapping (uint256 => address) private _tokenApprovals;
// Mapping from owner to number of owned token
mapping (address => Counters.Counter) private _ownedTokensCount;
// Mapping from owner to operator approvals
mapping (address => mapping (address => bool)) private _operatorApprovals;
bytes4 private constant _INTERFACE_ID_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)'))
*/
constructor () public {
// register the supported interfaces to conform to ERC721 via ERC165
_registerInterface(_INTERFACE_ID_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].current();
}
/**
* @dev Gets the owner of the specified token ID.
* @param tokenId uint256 ID of the token to query the owner of
* @return 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
* Reverts if the token ID does not exist.
* @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) {
require(_exists(tokenId));
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));
_transferFrom(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 {
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 memory _data) public {
transferFrom(from, to, tokenId);
require(_checkOnERC721Received(from, to, tokenId, _data));
}
/**
* @dev Returns whether the specified token exists.
* @param tokenId uint256 ID of the token to query the existence of
* @return bool 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);
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
*/
function _mint(address to, uint256 tokenId) internal {
require(to != address(0));
require(!_exists(tokenId));
_tokenOwner[tokenId] = to;
_ownedTokensCount[to].increment();
emit Transfer(address(0), to, tokenId);
}
/**
* @dev Internal function to burn a specific token.
* Reverts if the token does not exist.
* Deprecated, use _burn(uint256) instead.
* @param owner owner of the token to burn
* @param tokenId uint256 ID of the token being burned
*/
function _burn(address owner, uint256 tokenId) internal {
require(ownerOf(tokenId) == owner);
_clearApproval(tokenId);
_ownedTokensCount[owner].decrement();
_tokenOwner[tokenId] = address(0);
emit Transfer(owner, address(0), 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
*/
function _burn(uint256 tokenId) internal {
_burn(ownerOf(tokenId), tokenId);
}
/**
* @dev Internal function to transfer ownership of a given token ID to another address.
* As opposed to transferFrom, this imposes no restrictions on msg.sender.
* @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) internal {
require(ownerOf(tokenId) == from);
require(to != address(0));
_clearApproval(tokenId);
_ownedTokensCount[from].decrement();
_ownedTokensCount[to].increment();
_tokenOwner[tokenId] = to;
emit Transfer(from, to, tokenId);
}
/**
* @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 bool whether the call correctly returned the expected magic value
*/
function _checkOnERC721Received(address from, address to, uint256 tokenId, bytes memory _data)
internal returns (bool)
{
if (!to.isContract()) {
return true;
}
bytes4 retval = IERC721Receiver(to).onERC721Received(msg.sender, from, tokenId, _data);
return (retval == _ERC721_RECEIVED);
}
/**
* @dev Private function to clear current approval of a given token ID.
* @param tokenId uint256 ID of the token to be transferred
*/
function _clearApproval(uint256 tokenId) private {
if (_tokenApprovals[tokenId] != address(0)) {
_tokenApprovals[tokenId] = address(0);
}
}
}
pragma solidity ^0.5.6;
contract owned {
address payable owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner, "only the owner can call this fn");
_;
}
}
contract mortal is owned {
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
contract Faucet is mortal {
event Withdrawal(address indexed to, uint amount);
event Deposit(address indexed from, uint amount);
function withdraw(uint withdraw_amount) public {
require(withdraw_amount <= 0.01 ether);
require(address(this).balance >= withdraw_amount, "not enought eth in the faucet");
msg.sender.transfer(withdraw_amount);
emit Withdrawal(msg.sender, withdraw_amount);
}
function () external payable {
emit Deposit(msg.sender, msg.value);
}
}
pragma solidity ^0.5.0;
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";
contract HelloWorld {
//string defaultName;
/*constructor() public{
defaultName = 'World';
}*/
function getMessage() public view returns(string memory){
return "Hello";
}
}
pragma solidity ^0.5.1;
// We have to specify what version of compiler this code will compile with
contract Rating {
/* mapping field below is equivalent to an associative array or hash.
*/
mapping (bytes32 => uint8) public ratingsReceived;
/* We will use an array of bytes32 to store the list of movies
*/
bytes32[] public movieList;
/* This is the constructor which will be called once when you
deploy the contract to the blockchain. When we deploy the contract,
we will pass an array of movies for which users will give ratings
*/
constructor(bytes32[] memory movieNames) public {
movieList = movieNames;
}
// This function returns the total ratings a movie has received so far
function totalVotesFor(bytes32 movie) view public returns (uint8) {
return ratingsReceived[movie];
}
// This function increments the vote count for the specified movie. Equivalent to upvoting
function voteForMovie(bytes32 movie) public {
ratingsReceived[movie] += 1;
}
}
pragma solidity ^0.5.7;
contract owned {
address payable owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner, "only the owner can call this fn");
_;
}
}
contract mortal is owned {
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
contract Things is mortal {
// string fName;
// uint tid;
mapping(uint24 => address payable) public owners;
mapping(uint24 => string) public extid;
mapping(uint24 => string) public description;
uint24[] public locked; //
mapping(uint24 => string) public instructionslib;
mapping(uint24 => uint) public instructions;
modifier onlyThingOwner(uint24 _tid) {
require(msg.sender == owners[_tid], "only the owner of the thing can call this fn");
_;
}
function newThing(uint24 _tid) public {
newThing(_tid, msg.sender);
}
function newThing(uint24 _tid, address payable _owner) internal {
require(owners[_tid] == address(0), "new thing required, this identifier is already registered");
owners[_tid] = _owner;
}
function getThing(uint24 _tid) public view returns (uint, address payable, string memory) {
require(owners[_tid] != address(0), "this thing is not registered, it has no owner");
return (_tid, owners[_tid], description[_tid]);
}
/**
@notice Lock a thing to prevent any future exchange, should never be reverted
@dev always check using this function when relevant
@param _tid the thing ID
*/
function lockThing(uint24 _tid) public onlyThingOwner(_tid) {
locked.push(_tid);
}
/**
@notice Remove a thing
@dev we need to work on that one
@param _tid the thing ID
@return { "value" : "Ether equivalent balance of account" }
*/
function deleteThing(uint24 _tid) public onlyThingOwner(_tid) {
delete owners[_tid]; //FIXME i'm not sure about this one https://ethereum.stackexchange.com/questions/44433/delete-an-entry-in-a-mapping
}
/**
@notice Count number of things managed by the system
@return { "count" : "Total number of things managed in the system" }
*/
function countThings() view public returns (uint count) {
return 0; //owners. ; //FIXME it seems we can't get the size of map ????
}
/*
function setName(uint24 _tid, string memory _tname) public {
tnames[_tid] = _tname;
}
function getName(uint24 _tid) public view returns (uint, string memory) {
return (_tid, tnames[_tid]);
}
*/
}
pragma solidity ^0.5.7;
contract owned {
address payable owner;
constructor() public {
owner = msg.sender;
}
modifier onlyOwner {
require(msg.sender == owner, "only the owner can call this fn");
_;
}
}
contract mortal is owned {
function destroy() public onlyOwner {
selfdestruct(owner);
}
}
/**
* @title Thing basic implementation
* @dev refactored to stay kind of aligned with https://github.com/OpenZeppelin/openzeppelin-solidity/blob/721-interface/contracts/token/ERC721/ERC721.sol
*/
contract Things is mortal {
event Transfer(address indexed from, address indexed to, uint24 indexed tid);
event Request(address indexed from, address indexed to, uint24 indexed tid, uint time);
// Mapping from thing ID to owner
mapping (uint24 => address payable) public _thingOwner; //FIXME to be change to private at some point
mapping (uint24 => address payable) public _bearers;
// Mapping from thing ID to approved address
//mapping (uint256 => address) private _tokenApprovals;
// Mapping from owner to number of owned token
//mapping (address => Counters.Counter) private _ownedTokensCount;
// Mapping from owner to operator approvals
//mapping (address => mapping (address => bool)) private _operatorApprovals;
mapping(uint24 => string) private extid;
mapping(uint24 => string) private description;
mapping(uint24 => bytes32) private _pictures; //bytes32 is enough for an MD5
mapping(uint24 => bool) _locked; //array would be very inefficient for this since there is no contains function
mapping(uint24 => string) public instructionslib;
mapping(uint24 => uint) public instructions;
mapping(uint24 => address payable) public _requests;
modifier onlyThingOwner(uint24 tid) {
require(msg.sender == _thingOwner[tid], "only the owner of the thing can call this fn");
_;
}
/**
* @dev Gets the owner of the specified thing ID
* @param tid ID of the thing to query the owner of
* @return address currently marked as the owner of the given thing ID
*/
function ownerOf(uint24 tid) public view returns (address payable) {
address owner = _thingOwner[tid];
require(owner != address(0));
return address(uint160(address(owner))); // need to cast from address to address payable
}
/**
* @dev Returns whether the specified thing exists
* @param tid uint24 ID of the token to query the existence of
* @return bool whether the thing exists
*/
function _exists(uint24 tid) internal view returns (bool) {
address owner = _thingOwner[tid];
return owner != address(0);
}
function newThing(uint24 tid) public {
_mint(msg.sender, tid);
}
/**
* @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 tid ID of the token to be minted
*/
function _mint(address payable to, uint24 tid) internal {
require(to != address(0));
require(!_exists(tid));
_thingOwner[tid] = to;
//_ownedTokensCount[to].increment();
emit Transfer(address(0), to, tid);
}
/**
* @dev Internal function to burn a specific thing
* Reverts if the thing does not exist
* @param tid ID of the thing being burned
*/
function _burn(uint24 tid) internal {
_burn(ownerOf(tid), tid);
}
/**
* @dev Internal function to burn a specific thing
* Reverts if the thing does not exist
* Deprecated, use _burn(uint256) instead.
* @param owner owner of the thing to burn
* @param tid ID of the thing being burned
*/
function _burn(address payable owner, uint24 tid) internal {
require(ownerOf(tid) == owner);
//TODO _clearApproval(tokenId);
//_ownedTokensCount[owner].decrement();
_thingOwner[tid] = address(0);
emit Transfer(owner, address(0), tid);
}
function getBearer(uint24 tid) public view returns (address payable) {
require(_exists(tid));
if(_bearers[tid] != address(0)) {
return _bearers[tid];
} else {
return _thingOwner[tid];
}
}
/**
* @dev Returns the hash/md5 of the photo of a thing
* @param tid thing id
*/
function getPicture(uint24 tid) external view returns (bytes32) {
require(_exists(tid));
return _pictures[tid];
}
/**
* @param tid thing id
* @param hash md5 of the photo
*/
function setPicture(uint24 tid, bytes32 hash) public {
require(_exists(tid));
require(ownerOf(tid) == owner);
_pictures[tid] = hash;
}
function getThing(uint24 tid) public view returns (uint, address payable, string memory) {
require(_thingOwner[tid] != address(0), "this thing is not registered, it has no owner");
return (tid, _thingOwner[tid], description[tid]);
}
/**
@notice Lock a thing to prevent any future exchange, should never be reverted
@dev always check using this function when relevant
@param tid the thing ID
*/
function lockThing(uint24 tid) public onlyThingOwner(tid) {
_locked[tid]=true;
}
function isLocked(uint24 tid) public view returns (bool) {
return _locked[tid];
}
function requestThing(uint24 tid) public {
require(_exists(tid));
require(!_locked[tid]);
require(msg.sender != _bearers[tid]);
address payable b;
//to be replaced by getBearer
if(_bearers[tid] != address(0)) {
b = _bearers[tid];
} else {
b = _thingOwner[tid];
}
_requests[tid] = msg.sender;
emit Request(msg.sender, b, tid, now);
}
/**
@notice Count number of things managed by the system
@return { "count" : "Total number of things managed in the system" }
*/
function countThings() view public returns (uint count) {
return 0; //owners. ; //FIXME it seems we can't get the size of map ????
}
}
pragma solidity >=0.4.0 <0.6.0;
import "remix_tests.sol"; // this import is automatically injected by Remix.
// file name has to end with '_test.sol'
contract test_1 {
function beforeAll() public {
// here should instantiate tested contract
Assert.equal(uint(4), uint(3), "error in before all function");
}
function check1() public {
// use 'Assert' to test the contract
Assert.equal(uint(2), uint(1), "error message");
Assert.equal(uint(2), uint(2), "error message");
}
function check2() public view returns (bool) {
// use the return value (true or false) to test the contract
return true;
}
}
contract test_2 {
function beforeAll() public {
// here should instantiate tested contract
Assert.equal(uint(4), uint(3), "error in before all function");
}
function check1() public {
// use 'Assert' to test the contract
Assert.equal(uint(2), uint(1), "error message");
Assert.equal(uint(2), uint(2), "error message");
}
function check2() public view returns (bool) {
// use the return value (true or false) to test the contract
return true;
}
}
pragma solidity ^0.4.24;
contract e0x {
event Log(string message);
event LinkAdded(uint linkId, string url);
struct LinkTemplate {
address userAddress;
string url;
}
uint lastLinkId;
mapping (uint => LinkTemplate) public linkMapping;
constructor() public {
lastLinkId = 0;
}
function createNewLink(string url) public returns (uint) {
lastLinkId++;
linkMapping[lastLinkId] = LinkTemplate(msg.sender, url);
emit LinkAdded(lastLinkId, url);
return lastLinkId;
}
modifier linkExists(uint linkId) {
//link with the given hash does not exist
if(linkMapping[linkId].userAddress == 0x0000000000000000000000000000000000000000) {
revert();
}
_;
}
function getLink(uint linkId) linkExists(linkId) public constant
returns(
address,
string
) {
LinkTemplate memory link = linkMapping[linkId];
return(
link.userAddress,
link.url
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment