Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.4.20;
- contract Marketplace {
- uint256 transferBudget = 1 ether;
- bytes32[] ids;
- enum Position{
- Goalkeeper, Defender, Midfielder, Attacker, Coach
- }
- struct Footballer{
- bytes32 id;
- string name;
- address owner;
- Position position;
- uint price;
- }
- Footballer[] footballers;
- //function buy(uint ID, uint quantity) public payable {}
- function deposit() public payable {
- if(msg.value>0){
- transferBudget = transferBudget+msg.value;
- }
- }
- function getTransferBudget() public view returns(uint256){
- return transferBudget;
- }
- function update(bytes32 ID, uint newPrice) public {
- for(uint i=0; i<footballers.length; i++){
- if(footballers[i].id==ID && msg.sender==footballers[i].owner){
- footballers[i].price = newPrice;
- break;
- }
- }
- }
- //creates a new product and returns its ID
- function newProduct(string name, uint price, Position position) public returns(bytes32) {
- bytes32 id = keccak256(name,price,position);
- Footballer memory footballer = Footballer(id, name, msg.sender,position,price);
- footballers.push(footballer);
- ids.push(id);
- return id;
- }
- function bytes32ToString(bytes32 x) internal pure returns (string) {
- bytes memory bytesString = new bytes(32);
- uint charCount = 0;
- for (uint j = 0; j < 32; j++) {
- byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
- if (char != 0) {
- bytesString[charCount] = char;
- charCount++;
- }
- }
- bytes memory bytesStringTrimmed = new bytes(charCount);
- for (j = 0; j < charCount; j++) {
- bytesStringTrimmed[j] = bytesString[j];
- }
- return string(bytesStringTrimmed);
- }
- function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) {
- bytes32 out;
- for (uint i = 0; i < 32; i++) {
- out |= bytes32(b[offset + i] & 0xFF) >> (i * 8);
- }
- return out;
- }
- function getProduct(bytes32 ID) public view returns(string name, uint price ,Position position) {
- for(uint i=0; i<footballers.length; i++){
- if(footballers[i].id==ID){
- return (footballers[i].name , footballers[i].price, footballers[i].position);
- }
- }
- }
- function getProducts() public view returns(bytes32[]) {
- return ids;
- }
- function getPrice(bytes32 ID) public view returns (uint) {
- for(uint i=0; i<footballers.length; i++){
- if(footballers[i].id==ID){
- return footballers[i].price;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement