-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathlibraries.sol
More file actions
134 lines (108 loc) · 5.23 KB
/
libraries.sol
File metadata and controls
134 lines (108 loc) · 5.23 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
pragma solidity ^0.4.2;
// Unfortunately libraries can not inherit from other libraries, otherwise the IterableAddressMapping
// and IterableAddressBalanceMapping could inherit from a shared base.
/// @dev Models an address mapping where it is possible to both iterate through the values, and quickly look up to see if a particular address is in mapping
library IterableAddressMapping {
struct iterableAddressMap {
mapping(address => IndexValue) data;
address[] keys;
uint size;
}
struct IndexValue { uint keyIndex; }
function add(iterableAddressMap storage self, address mappedAddress) {
if (self.data[mappedAddress].keyIndex == 0) {
uint key = self.keys.push(mappedAddress);
self.data[mappedAddress].keyIndex = key;
self.size ++;
}
}
function remove(iterableAddressMap storage self, address mappedAddress) {
if (tx.origin != mappedAddress) {
if (self.data[mappedAddress].keyIndex != 0) {
delete self.keys[self.data[mappedAddress].keyIndex - 1];
delete self.data[mappedAddress];
self.size --;
}
}
}
function contains(iterableAddressMap storage self, address mappedAddress) returns (bool) {
return self.data[mappedAddress].keyIndex != 0;
}
function iterateStart(iterableAddressMap storage self) returns (uint keyIndex) {
return iterateNext(self, 0);
}
function iterateValid(iterableAddressMap storage self, uint keyIndex) returns (bool) {
return keyIndex < self.keys.length;
}
function iterateNext(iterableAddressMap storage self, uint keyIndex) returns (uint r_keyIndex) {
return keyIndex++;
}
function iterateGet(iterableAddressMap storage self, uint keyIndex) returns (address mappedAddress) {
mappedAddress = self.keys[keyIndex];
}
}
/// @dev Models address => balances mapping where it is possible to both iterate through the values, and quickly look up to see if a particular address is in mapping
library IterableAddressBalanceMapping {
struct iterableAddressBalanceMap {
mapping(address => IndexValue) data;
address[] keys;
uint size;
}
struct IndexValue { uint keyIndex; uint32 coinBalance; int160 dollarBalance;}
function add(iterableAddressBalanceMap storage self, address mappedAddress, uint32 coinBalance, int160 dollarBalance) {
if (self.data[mappedAddress].keyIndex == 0) {
uint key = self.keys.push(mappedAddress);
self.data[mappedAddress].keyIndex = key;
self.data[mappedAddress].coinBalance = coinBalance;
self.data[mappedAddress].dollarBalance = dollarBalance;
self.size ++;
}
}
function remove(iterableAddressBalanceMap storage self, address mappedAddress) {
if (self.data[mappedAddress].keyIndex != 0) {
delete self.keys[self.data[mappedAddress].keyIndex - 1];
delete self.data[mappedAddress];
self.size --;
}
}
function contains(iterableAddressBalanceMap storage self, address mappedAddress) returns (bool) {
return self.data[mappedAddress].keyIndex != 0;
}
function valueOf(iterableAddressBalanceMap storage self, address mappedAddress) returns (uint32 coinBalance, int160 dollarBalance) {
coinBalance = self.data[mappedAddress].coinBalance;
dollarBalance = self.data[mappedAddress].dollarBalance;
}
function valueOfCoinBalance(iterableAddressBalanceMap storage self, address mappedAddress) returns (uint32 coinBalance) {
return self.data[mappedAddress].coinBalance;
}
function valueOfDollarBalance(iterableAddressBalanceMap storage self, address mappedAddress) returns (int160 dollarBalance) {
return self.data[mappedAddress].dollarBalance;
}
function setCoinBalance(iterableAddressBalanceMap storage self, address mappedAddress, uint32 coinBalance) {
self.data[mappedAddress].coinBalance = coinBalance;
}
function setDollarBalance(iterableAddressBalanceMap storage self, address mappedAddress, int160 dollarBalance) {
self.data[mappedAddress].dollarBalance = dollarBalance;
}
function addCoinAmount(iterableAddressBalanceMap storage self, address mappedAddress, uint32 coinAmount) {
self.data[mappedAddress].coinBalance += coinAmount;
}
function addDollarAmount(iterableAddressBalanceMap storage self, address mappedAddress, int160 dollarAmount) {
self.data[mappedAddress].dollarBalance += dollarAmount;
}
function iterateStart(iterableAddressBalanceMap storage self) returns (uint keyIndex) {
return iterateNext(self, 0);
}
function iterateValid(iterableAddressBalanceMap storage self, uint keyIndex) returns (bool) {
return keyIndex < self.keys.length;
}
function iterateNext(iterableAddressBalanceMap storage self, uint keyIndex) returns (uint r_keyIndex) {
return keyIndex++;
}
function iterateGet(iterableAddressBalanceMap storage self, uint keyIndex) returns (address mappedAddress) {
mappedAddress = self.keys[keyIndex];
}
function length(iterableAddressBalanceMap storage self) returns (uint) {
return self.keys.length;
}
}