/
AssetLedger.sol
149 lines (100 loc) · 4.2 KB
/
AssetLedger.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
pragma solidity ^0.4.0;
contract AssetLedger {
function AssetLedger(){}
function(){}
Asset[] public arr_of_asset;
struct Asset {
bytes32 assetTokenHash;
uint purchaseprice;
bytes32 payloadHash;
uint timestamp;
uint assetID;
}
function addAsset(bytes32 _assetTokenHash, uint _purchaseprice, bytes32 _payloadHash, uint _timestamp, uint _assetID)returns (bool){
Asset memory newAsset;
newAsset.assetTokenHash = _assetTokenHash;
newAsset.purchaseprice = _purchaseprice;
newAsset.payloadHash = _payloadHash;
newAsset.timestamp = _timestamp;
newAsset.assetID = _assetID;
arr_of_asset.push(newAsset);
return (true);
}
function getAllAssets() constant returns (bytes32[], uint[], uint[]){
uint length = arr_of_asset.length;
bytes32[] memory arr_assetTokenHash = new bytes32[](length);
uint[] memory arr_timestamp = new uint[](length);
uint[] memory arr_purchaseprice = new uint[](length);
Asset memory currentAsset;
for (uint i = 0; i < arr_of_asset.length; i++) {
currentAsset = arr_of_asset[i];
arr_assetTokenHash[i] = currentAsset.assetTokenHash;
arr_timestamp[i] = currentAsset.timestamp;
arr_purchaseprice[i] = currentAsset.purchaseprice;
}
return (arr_assetTokenHash, arr_timestamp, arr_purchaseprice);
}
function getAssetDetail(bytes32 _assetTokenHash) constant returns (uint, uint){
Asset memory currentAsset;
bytes32 temp_assetTokenHash;
for (uint i = 0; i < arr_of_asset.length; i++) {
currentAsset = arr_of_asset[i];
temp_assetTokenHash = currentAsset.assetTokenHash;
if (temp_assetTokenHash == _assetTokenHash) {
return (currentAsset.timestamp, currentAsset.purchaseprice);
}
}
return (0, 0);
}
function getAssetByTimeRange(uint _start, uint _end) constant returns (bytes32[], uint[], uint[]){
/*
start and end time must be sent from the Dapp interface.
Only thing here doing is if timestamp of any transaction lies between start and end
would be displayed .
*/
uint length = arr_of_asset.length;
bytes32[] memory arr_assetTokenHash = new bytes32[](length);
uint[] memory arr_timestamp = new uint[](length);
uint[] memory arr_purchaseprice = new uint[](length);
Asset memory currentAsset;
uint temp_timestamp;
for (uint i = 0; i < arr_of_asset.length; i++) {
currentAsset = arr_of_asset[i];
temp_timestamp = currentAsset.timestamp;
if ((temp_timestamp >= _start) && (temp_timestamp <= _end)) {
arr_assetTokenHash[i] = currentAsset.assetTokenHash;
arr_timestamp[i] = currentAsset.timestamp;
arr_purchaseprice[i] = currentAsset.purchaseprice;
}
}
return (arr_assetTokenHash, arr_timestamp, arr_purchaseprice);
}
function verifyPayloadByToken(bytes32 _payloadHash, bytes32 _assetTokenHash) constant returns (bool){
Asset memory currentAsset;
bytes32 temp_assetTokenHash;
bytes32 temp_payloadHash;
for (uint i = 0; i < arr_of_asset.length; i++) {
currentAsset = arr_of_asset[i];
temp_assetTokenHash = currentAsset.assetTokenHash;
temp_payloadHash = currentAsset.payloadHash;
if ((temp_assetTokenHash == _assetTokenHash) && (temp_payloadHash == _payloadHash)) {
return true;
}
}
return false;
}
function verifyPayloadByID(bytes32 _payloadHash, uint _assetID) constant returns (bool){
Asset memory currentAsset;
uint temp_assetID;
bytes32 temp_payloadHash;
for (uint i = 0; i < arr_of_asset.length; i++) {
currentAsset = arr_of_asset[i];
temp_assetID = currentAsset.assetID;
temp_payloadHash = currentAsset.payloadHash;
if ((temp_assetID == _assetID) && (temp_payloadHash == _payloadHash)) {
return true;
}
}
return false;
}
}