-
Notifications
You must be signed in to change notification settings - Fork 0
/
SplitWise.sol
59 lines (45 loc) · 1.72 KB
/
SplitWise.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
pragma solidity ^0.6.6;
contract Splitwise {
mapping (address => mapping(address => uint32)) IOU; //IOU[debtor][creditor]
mapping (address => uint32) totalOwed;
event new_IOU(
address indexed debtor,
address indexed creditor,
uint32 amount
);
function add_IOU (address creditor, uint32 amount) external {
IOU[msg.sender][creditor] += amount;
totalOwed[msg.sender] += amount;
emit new_IOU(msg.sender,creditor,amount);
}
function lookup(address debtor, address creditor) external view returns (uint32 ret){
return IOU[debtor][creditor];
}
function viewTotalOwed(address debtor) external view returns (uint32 ret){
return totalOwed[debtor];
}
function resolveCycle (address[] calldata cycle, uint32 _min) external returns (bool ret){
require(checkCycle(cycle,_min),"either cycle addresse is or min value is wrong");
for (uint i = 0; i < cycle.length-1; i++) {
IOU[cycle[i]][cycle[i+1]] -= _min;
totalOwed[cycle[i]] -= _min;
}
return true;
}
function checkCycle (address[] memory cycle, uint32 _min) private view returns (bool ret) {
uint32 min = 0xFFFFFFFF;
for (uint i = 0; i < cycle.length-1; i++) {
uint32 val = IOU[cycle[i]][cycle[i+1]];
if(val == 0){
return false;
}
if(val < min){
min = val;
}
}
if(_min != min || cycle[0] != cycle[cycle.length-1]){ // if min is wrong and cycle doesn't connect to the begining
return false;
}
return true;
}
}