Repository navigation
Expand file tree
/
Copy pathLoan.sol
More file actions
361 lines (287 loc) · 12.8 KB
/
Copy pathLoan.sol
File metadata and controls
361 lines (287 loc) · 12.8 KB
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
pragma solidity ^0.4.22;
contract Loan {
struct Arbitral {
string ArbitralName;
address ArbitralInstitutionAddress;
}
struct Bank {
string bankName;
uint[] ReceiptIndex_loanMoneyToCompany;
uint[] ReceiptIndex_authorizeCreditAssetToCompany;
uint[] ReceiptIndex_getRepaidMoneyFromTrustedCompany;
mapping (string => uint) companyDebtAmount;
}
struct Company {
string companyName;
string companyType;
bool isTrusted;
uint creditAsset;
uint[] ReceiptIndex_getCreditAssetFromCompanyOrBank;
uint[] ReceiptIndex_giveCreditAssetToCompany;
uint realMoney;
uint[] ReceiptIndex_getRealMoneyFromBank;
uint[] ReceiptIndex_repayRealMoneyToBank;
}
struct Receipt_A_gives_things_to_B {
string Name_A;
string Name_B;
bool bankParticipation;
bool isRealMoney;
uint amount;
}
Arbitral public arbitralInstitution;
Company[] public companies;
Bank[] public banks;
Receipt_A_gives_things_to_B[] public receipts;
bool public trustedCompanyHasFinishedWithBank = false;
constructor(string arbitralInstitutionName) {
arbitralInstitution = Arbitral({
ArbitralName: arbitralInstitutionName,
ArbitralInstitutionAddress: msg.sender
});
}
function addCompany(string cpName, string cpType, bool whetherTrusted) returns (bool) {
require(msg.sender == arbitralInstitution.ArbitralInstitutionAddress, "Only arbitral institution can add a company.");
require(keccak256(cpType) != keccak256("bank"), "Please use addBank method to add a bank.") ;
for(uint i = 0; i < companies.length; i++) {
require(keccak256(cpName) != keccak256(companies[i].companyName), "This company name already exsits.");
}
for(uint j = 0; j < banks.length; j++) {
require(keccak256(cpName) != keccak256(banks[j].bankName), "Cannot add the company because a bank of the same name exsits.");
}
companies.push(Company({
companyName: cpName,
companyType: cpType,
isTrusted: whetherTrusted,
creditAsset: 0,
ReceiptIndex_getCreditAssetFromCompanyOrBank: new uint[](0),
ReceiptIndex_giveCreditAssetToCompany: new uint[](0),
realMoney: 0,
ReceiptIndex_getRealMoneyFromBank: new uint[](0),
ReceiptIndex_repayRealMoneyToBank: new uint[](0)
}));
return true;
}
function addBank(string bName) returns(bool) {
require(msg.sender == arbitralInstitution.ArbitralInstitutionAddress, "Only arbitral institution can add a bank.");
for(uint i = 0; i < banks.length; i++) {
require(keccak256(bName) != keccak256(banks[i].bankName), "This bank name already exsits.");
}
for(uint j = 0; j < companies.length; j++) {
require(keccak256(bName) != keccak256(companies[j].companyName), "Cannot add the bank because a company of the same name exsits.");
}
banks.push(Bank({
bankName: bName,
ReceiptIndex_loanMoneyToCompany: new uint[](0),
ReceiptIndex_authorizeCreditAssetToCompany: new uint[](0),
ReceiptIndex_getRepaidMoneyFromTrustedCompany: new uint[](0)
}));
return true;
}
function newReceipt(string nameA, string nameB, uint amt, bool bankParticipates, bool realMoney) internal returns(uint) {
receipts.push(Receipt_A_gives_things_to_B({
Name_A: nameA,
Name_B: nameB,
amount: amt,
bankParticipation: bankParticipates,
isRealMoney: realMoney
}));
return receipts.length - 1;
}
function bank_giveCreditAssetTo_trustedCompany(string bName, string cpName, uint amt) returns(bool) {
require(amt > 0, "Amount must be positive!");
bool bName_Valid = false;
bool cpName_Valid = false;
uint b_Index = 0;
uint cp_Index = 0;
for(uint j = 0; j < banks.length; j++) {
if(keccak256(bName) == keccak256(banks[j].bankName)) {
bName_Valid = true;
b_Index = j;
}
}
for(uint i = 0; i < companies.length; i++) {
if(keccak256(cpName) == keccak256(companies[i].companyName)) {
cpName_Valid = true;
cp_Index = i;
}
}
require(bName_Valid == true, "Bank name is invalid!");
require(cpName_Valid == true, "Company name is invalid!");
require(companies[cp_Index].isTrusted == true, "Company is not trusted!");
uint currentReceiptIndex = newReceipt(bName, cpName, amt, true, false);
banks[b_Index].ReceiptIndex_authorizeCreditAssetToCompany.push(currentReceiptIndex);
banks[b_Index].companyDebtAmount[cpName] += amt;
companies[cp_Index].creditAsset += amt;
companies[cp_Index].ReceiptIndex_getCreditAssetFromCompanyOrBank.push(currentReceiptIndex);
return true;
}
function companyA_giveCreditAssetTo_companyB(string companyName_A, string companyName_B, uint amt) returns(bool) {
require(keccak256(companyName_A) != keccak256(companyName_B), "Company A and Company B should be different!");
require(amt > 0, "Amount must be positive!");
bool Name_A_Valid = false;
bool Name_B_Valid = false;
uint companyA_Index = 0;
uint companyB_Index = 0;
for(uint i = 0; i < companies.length; i++) {
if(keccak256(companyName_A) == keccak256(companies[i].companyName)) {
Name_A_Valid = true;
companyA_Index = i;
}
if(keccak256(companyName_B) == keccak256(companies[i].companyName)) {
Name_B_Valid = true;
companyB_Index = i;
}
}
require(Name_A_Valid == true, "Company A name is invalid!");
require(Name_B_Valid == true, "Company B name is invalid!");
require(companies[companyA_Index].creditAsset >= amt, "Company A's creditAsset should be more than the loan amount!");
uint currentReceiptIndex = newReceipt(companyName_A, companyName_B, amt, false, false);
companies[companyA_Index].creditAsset -= amt;
companies[companyA_Index].ReceiptIndex_giveCreditAssetToCompany.push(currentReceiptIndex);
companies[companyB_Index].creditAsset += amt;
companies[companyB_Index].ReceiptIndex_getCreditAssetFromCompanyOrBank.push(currentReceiptIndex);
return true;
}
function bank_giveRealMoneyTo_company(string bName, string cpName, uint amt) returns(bool) {
require(amt > 0, "Amount must be positive!");
bool bName_Valid = false;
bool cpName_Valid = false;
uint b_Index = 0;
uint cp_Index = 0;
for(uint j = 0; j < banks.length; j++) {
if(keccak256(bName) == keccak256(banks[j].bankName)) {
bName_Valid = true;
b_Index = j;
}
}
for(uint i = 0; i < companies.length; i++) {
if(keccak256(cpName) == keccak256(companies[i].companyName)) {
cpName_Valid = true;
cp_Index = i;
}
}
require(bName_Valid == true, "Bank name is invalid!");
require(cpName_Valid == true, "Company name is invalid!");
require(companies[cp_Index].creditAsset >= amt, "Company's credit assest is not enough!");
uint currentReceiptIndex = newReceipt(bName, cpName, amt, true, true);
banks[b_Index].ReceiptIndex_authorizeCreditAssetToCompany.push(currentReceiptIndex);
companies[cp_Index].creditAsset -= amt;
companies[cp_Index].realMoney += amt;
companies[cp_Index].ReceiptIndex_getRealMoneyFromBank.push(currentReceiptIndex);
return true;
}
function trustedCompany_repaidTo_bank(string cpName, string bName, uint amt) returns(bool) {
require(amt > 0, "Amount must be positive!");
bool bName_Valid = false;
bool cpName_Valid = false;
uint b_Index = 0;
uint cp_Index = 0;
for(uint j = 0; j < banks.length; j++) {
if(keccak256(bName) == keccak256(banks[j].bankName)) {
bName_Valid = true;
b_Index = j;
}
}
for(uint i = 0; i < companies.length; i++) {
if(keccak256(cpName) == keccak256(companies[i].companyName)) {
cpName_Valid = true;
cp_Index = i;
}
}
require(bName_Valid == true, "Bank name is invalid!");
require(cpName_Valid == true, "Company name is invalid!");
require(companies[cp_Index].isTrusted == true, "Only trusted company can repay to bank!");
require(amt <= (banks[b_Index].companyDebtAmount[cpName] - companies[cp_Index].creditAsset), "Repay amount is more than the needed!");
uint currentReceiptIndex = newReceipt(cpName, bName, amt, true, true);
banks[b_Index].ReceiptIndex_getRepaidMoneyFromTrustedCompany.push(currentReceiptIndex);
banks[b_Index].companyDebtAmount[cpName] -= amt;
companies[cp_Index].ReceiptIndex_repayRealMoneyToBank.push(currentReceiptIndex);
return true;
}
function trustedCompany_FinishWith_Bank(string cpName, string bName) returns(bool) {
bool bName_Valid = false;
bool cpName_Valid = false;
uint b_Index = 0;
uint cp_Index = 0;
for(uint j = 0; j < banks.length; j++) {
if(keccak256(bName) == keccak256(banks[j].bankName)) {
bName_Valid = true;
b_Index = j;
}
}
for(uint i = 0; i < companies.length; i++) {
if(keccak256(cpName) == keccak256(companies[i].companyName)) {
cpName_Valid = true;
cp_Index = i;
}
}
require(bName_Valid == true, "Bank name is invalid!");
require(cpName_Valid == true, "Company name is invalid!");
require(companies[cp_Index].isTrusted == true, "Only trusted company can finish business with bank!");
require(companies[cp_Index].creditAsset == banks[b_Index].companyDebtAmount[cpName], "The trusted company still has unrepaid money!");
companies[cp_Index].creditAsset = 0;
banks[b_Index].companyDebtAmount[cpName] = 0;
trustedCompanyHasFinishedWithBank = true;
return true;
}
function getCompanyInfoByName(string cpName) returns(string, string, bool, uint, uint) {
bool Name_A_Valid = false;
uint companyA_Index = 0;
for(uint i = 0; i < companies.length; i++) {
if(keccak256(cpName) == keccak256(companies[i].companyName)) {
Name_A_Valid = true;
companyA_Index = i;
}
}
require(Name_A_Valid == true, "Company A name is invalid!");
return (companies[companyA_Index].companyName,
companies[companyA_Index].companyType,
companies[companyA_Index].isTrusted,
companies[companyA_Index].creditAsset,
companies[companyA_Index].realMoney);
}
function getBankInfoByName(string bName) returns(string) {
bool bName_Valid = false;
uint b_Index = 0;
for(uint j = 0; j < banks.length; j++) {
if(keccak256(bName) == keccak256(banks[j].bankName)) {
bName_Valid = true;
b_Index = j;
}
}
require(bName_Valid == true, "Bank name is invalid!");
return banks[b_Index].bankName;
}
function getAmountOfCreditAsset_bankGiveToTrustedCompany(string bName, string cpName) returns(uint) {
bool bName_Valid = false;
bool cpName_Valid = false;
uint b_Index = 0;
uint cp_Index = 0;
for(uint j = 0; j < banks.length; j++) {
if(keccak256(bName) == keccak256(banks[j].bankName)) {
bName_Valid = true;
b_Index = j;
}
}
for(uint i = 0; i < companies.length; i++) {
if(keccak256(cpName) == keccak256(companies[i].companyName)) {
cpName_Valid = true;
cp_Index = i;
}
}
require(bName_Valid == true, "Bank name is invalid!");
require(cpName_Valid == true, "Company name is invalid!");
require(companies[cp_Index].isTrusted == true, "Company is not trusted!");
return banks[b_Index].companyDebtAmount[cpName];
}
function getCompanyNum() returns(uint) {
return companies.length;
}
function getBankNum() returns (uint) {
return banks.length;
}
function getReceiptNum() returns (uint) {
return receipts.length;
}
}