Skip to content

Commit

Permalink
Merge pull request #34 from coderbunker/issue5-to_blockchain
Browse files Browse the repository at this point in the history
Issue5 to blockchain
  • Loading branch information
rngadam authored Jan 11, 2020
2 parents d9c8c67 + 678fec0 commit 2b95c0b
Show file tree
Hide file tree
Showing 6 changed files with 420 additions and 0 deletions.
39 changes: 39 additions & 0 deletions to_blockchain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# timesheet-backend/to_blockchain

Basic "skeleton" implementation of storage smart contract + simple interface to store data on smart contract

To test the following code you need Node.js and NPM

run

```bash
npm install -g ethereumjs-testrpc
```

then start it

```bash
testrpc
```
In different terminal window in to_blockchain directory run

```bash
npm init
```

```bash
npm install ethereum/web3.js --save
```

copy code from data_storage_contract.sol to http://remix.ethereum.org/ compile code and deploy it to "Web3 Provider" of your test network

change

```
var data_storage = data_storage_contract.at("YOU SMART CONTRACT'S ADDRESS");
```
run index.html

now you can store data in smart contract in the format : address, string, string

run getProjects on Remix to see smart contracts of projects in the "database"
75 changes: 75 additions & 0 deletions to_blockchain/data_storage_contract.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
pragma solidity ^0.4.23;

contract data_storage_contract {

struct Freelancer {
string id; //freelancer id hash
string name; //freelancer's name
address projectAddress; //project's ethereum address
uint128 hoursSpent;
}

// struct for storing projects related information
struct Project {
string id; // project id hash
string name; //project name
}

mapping (address => Project) projects; // bind project's address to Project type struct
address[] public projectAccounts;

mapping (address => Freelancer) freelancers; // bind freelancer's address to freelancer type struct
address[] public freelancerAccounts;

function addProject(address _address, string _id, string _name) public {
Project storage project = projects[_address];

project.id = _id;
project.name = _name;

projectAccounts.push(_address) -1;
}

function getProjects() view public returns(address[]){ // get list of all project's wallets
return projectAccounts;
}

function getProject(address _address) view public returns(string, string){ // get data for a specific project
return (projects[_address].id, projects[_address].name);
}

function addFreelancer(address _address,
string _id,
string _name,
address _projectAddress,
uint128 _hoursSpent) public{
Freelancer storage freelancer = freelancers[_address];

freelancer.id = _id;
freelancer.name = _name;
freelancer.projectAddress = _projectAddress;
freelancer.hoursSpent = _hoursSpent;
}

function getFreelancers() view public returns(address[]){ // get list of all freelancer's wallets
return freelancerAccounts;
}

function getFreelancer(address _address) view public returns(string, string, address, uint128){ // get data for a specific freelancer
return (freelancers[_address].id,
freelancers[_address].name,
freelancers[_address].projectAddress,
freelancers[_address].hoursSpent);
}

function freelancerToProject(address _address) view public returns(address){
return freelancers[_address].projectAddress;
}



}




62 changes: 62 additions & 0 deletions to_blockchain/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>

<link rel="stylesheet" type="text/css" href="main.css">

<script src="./node_modules/web3/dist/web3.min.js"></script>
<script src="storage_abi.js"></script>

</head>
<body>
<div class="container">

<h1>Add Project</h1>


<label for="name" class="col-lg-2 control-label">Project's Ethereum address</label>
<input id="wallet" type="text"> <!-- project's wallet address input -->

<label for="name" class="col-lg-2 control-label">ID</label>
<input id="id" type="text"> <!-- project's ID input -->

<label for="name" class="col-lg-2 control-label">Project Name</label>
<input id="name" type="text"> <!-- project's name input -->


<button id="button">Update Project</button>


</div>

<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>

<script>

if (typeof web3 !== 'undefined') {
web3 = new Web3(web3.currentProvider);
} else {
// set the provider you want from Web3.providers
web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
}


var data_storage_contract = web3.eth.contract(storageABI);

var data_storage = data_storage_contract.at('0xad1bd8707cb092d85c1b5b67515a0b5aec762a15'); //storage smart contract's address on testnet, put address on your testnet in order test the code


$("#button").click(function() {
data_storage.addProject($("#wallet").val(), $("#id").val(), $("#name").val(), {from: web3.eth.accounts[1], gas:3000000});
}); //generate transaction to storage smart contract containing project's wallet, id and name from account number 1 on our testnet


</script>

</body>
</html>

29 changes: 29 additions & 0 deletions to_blockchain/main.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
body {
background-color:#F0F0F0;
padding: 2em;
font-family: 'Raleway','Source Sans Pro', 'Arial';
}
.container {
width: 50%;
margin: 0 auto;
}
label {
display:block;
margin-bottom:10px;
}
input {
padding:10px;
width: 50%;
margin-bottom: 1em;
}
button {
margin: 2em 0;
padding: 1em 4em;
display:block;
}

#project {
padding:1em;
background-color:#fff;
margin: 1em 0;
}
14 changes: 14 additions & 0 deletions to_blockchain/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "to_blockchain",
"version": "1.0.0",
"description": "Basic \"skeleton\" implementation of storage smart contract",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "",
"license": "ISC",
"dependencies": {
"web3": "github:ethereum/web3.js"
}
}
Loading

0 comments on commit 2b95c0b

Please sign in to comment.