forked from Synthetixio/synthetix
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathExchangeState.sol
102 lines (88 loc) · 3.01 KB
/
ExchangeState.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
pragma solidity ^0.5.16;
// Inheritance
import "./Owned.sol";
import "./State.sol";
import "./interfaces/IExchangeState.sol";
// https://docs.synthetix.io/contracts/source/contracts/exchangestate
contract ExchangeState is Owned, State, IExchangeState {
mapping(address => mapping(bytes32 => IExchangeState.ExchangeEntry[])) public exchanges;
uint public maxEntriesInQueue = 12;
constructor(address _owner, address _associatedContract) public Owned(_owner) State(_associatedContract) {}
/* ========== SETTERS ========== */
function setMaxEntriesInQueue(uint _maxEntriesInQueue) external onlyOwner {
maxEntriesInQueue = _maxEntriesInQueue;
}
/* ========== MUTATIVE FUNCTIONS ========== */
function appendExchangeEntry(
address account,
bytes32 src,
uint amount,
bytes32 dest,
uint amountReceived,
uint exchangeFeeRate,
uint timestamp,
uint roundIdForSrc,
uint roundIdForDest
) external onlyAssociatedContract {
require(exchanges[account][dest].length < maxEntriesInQueue, "Max queue length reached");
exchanges[account][dest].push(
ExchangeEntry({
src: src,
amount: amount,
dest: dest,
amountReceived: amountReceived,
exchangeFeeRate: exchangeFeeRate,
timestamp: timestamp,
roundIdForSrc: roundIdForSrc,
roundIdForDest: roundIdForDest
})
);
}
function removeEntries(address account, bytes32 currencyKey) external onlyAssociatedContract {
delete exchanges[account][currencyKey];
}
/* ========== VIEWS ========== */
function getLengthOfEntries(address account, bytes32 currencyKey) external view returns (uint) {
return exchanges[account][currencyKey].length;
}
function getEntryAt(
address account,
bytes32 currencyKey,
uint index
)
external
view
returns (
bytes32 src,
uint amount,
bytes32 dest,
uint amountReceived,
uint exchangeFeeRate,
uint timestamp,
uint roundIdForSrc,
uint roundIdForDest
)
{
ExchangeEntry storage entry = exchanges[account][currencyKey][index];
return (
entry.src,
entry.amount,
entry.dest,
entry.amountReceived,
entry.exchangeFeeRate,
entry.timestamp,
entry.roundIdForSrc,
entry.roundIdForDest
);
}
function getMaxTimestamp(address account, bytes32 currencyKey) external view returns (uint) {
ExchangeEntry[] storage userEntries = exchanges[account][currencyKey];
uint timestamp = 0;
for (uint i = 0; i < userEntries.length; i++) {
if (userEntries[i].timestamp > timestamp) {
timestamp = userEntries[i].timestamp;
}
}
return timestamp;
}
}