Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2018
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.85 KB | None | 0 0
  1. pragma solidity ^0.4.20;
  2.  
  3. contract Marketplace {
  4.  
  5. uint256 transferBudget = 1 ether;
  6. bytes32[] ids;
  7. enum Position{
  8. Goalkeeper, Defender, Midfielder, Attacker, Coach
  9. }
  10.  
  11. struct Footballer{
  12. bytes32 id;
  13. string name;
  14. address owner;
  15. Position position;
  16. uint price;
  17. }
  18. Footballer[] footballers;
  19.  
  20. //function buy(uint ID, uint quantity) public payable {}
  21.  
  22. function deposit() public payable {
  23. if(msg.value>0){
  24. transferBudget = transferBudget+msg.value;
  25. }
  26. }
  27.  
  28. function getTransferBudget() public view returns(uint256){
  29. return transferBudget;
  30. }
  31.  
  32. function update(bytes32 ID, uint newPrice) public {
  33. for(uint i=0; i<footballers.length; i++){
  34. if(footballers[i].id==ID && msg.sender==footballers[i].owner){
  35. footballers[i].price = newPrice;
  36. break;
  37. }
  38. }
  39. }
  40.  
  41. //creates a new product and returns its ID
  42. function newProduct(string name, uint price, Position position) public returns(bytes32) {
  43. bytes32 id = keccak256(name,price,position);
  44. Footballer memory footballer = Footballer(id, name, msg.sender,position,price);
  45. footballers.push(footballer);
  46. ids.push(id);
  47. return id;
  48. }
  49.  
  50. function bytes32ToString(bytes32 x) internal pure returns (string) {
  51. bytes memory bytesString = new bytes(32);
  52. uint charCount = 0;
  53. for (uint j = 0; j < 32; j++) {
  54. byte char = byte(bytes32(uint(x) * 2 ** (8 * j)));
  55. if (char != 0) {
  56. bytesString[charCount] = char;
  57. charCount++;
  58. }
  59. }
  60. bytes memory bytesStringTrimmed = new bytes(charCount);
  61. for (j = 0; j < charCount; j++) {
  62. bytesStringTrimmed[j] = bytesString[j];
  63. }
  64. return string(bytesStringTrimmed);
  65. }
  66.  
  67. function bytesToBytes32(bytes b, uint offset) private pure returns (bytes32) {
  68. bytes32 out;
  69. for (uint i = 0; i < 32; i++) {
  70. out |= bytes32(b[offset + i] & 0xFF) >> (i * 8);
  71. }
  72. return out;
  73. }
  74. function getProduct(bytes32 ID) public view returns(string name, uint price ,Position position) {
  75. for(uint i=0; i<footballers.length; i++){
  76. if(footballers[i].id==ID){
  77. return (footballers[i].name , footballers[i].price, footballers[i].position);
  78. }
  79. }
  80. }
  81.  
  82.  
  83.  
  84. function getProducts() public view returns(bytes32[]) {
  85. return ids;
  86. }
  87.  
  88. function getPrice(bytes32 ID) public view returns (uint) {
  89. for(uint i=0; i<footballers.length; i++){
  90. if(footballers[i].id==ID){
  91. return footballers[i].price;
  92. }
  93. }
  94. }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement