Skip to content

Commit 98be37e

Browse files
code refactor + erc1410 issuer facet
Signed-off-by: Alberto Molina <[email protected]>
1 parent 284118a commit 98be37e

File tree

54 files changed

+817
-758
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

54 files changed

+817
-758
lines changed

packages/ats/contracts/Configuration.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ export const CONTRACT_NAMES = [
236236
'ERC20Votes', //TODO
237237
'ERC1410ReadFacet',
238238
'ERC1410ManagementFacet',
239+
'ERC1410IssuerFacet',
239240
'ERC1410TokenHolderFacet',
240241
'ERC1594Facet',
241242
'ERC1643Facet',
@@ -246,7 +247,7 @@ export const CONTRACT_NAMES = [
246247
'BondUSARead', //TODO
247248
'ScheduledSnapshotsFacet',
248249
'ScheduledBalanceAdjustmentsFacet',
249-
'ScheduledTasksFacet',
250+
'scheduledCrossOrderedTasksFacet',
250251
'SnapshotsFacet',
251252
'CorporateActionsFacet',
252253
'TransferAndLockFacet',

packages/ats/contracts/contracts/layer_0/ERC1400/ERC1410/ERC1410StandardStorageWrapper.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ abstract contract ERC1410StandardStorageWrapper is
7676
address _from,
7777
address _to
7878
) internal {
79-
_triggerScheduledTasks(0);
79+
_triggerScheduledCrossOrderedTasks(0);
8080
_syncBalanceAdjustments(_partition, _from, _to);
8181
}
8282

packages/ats/contracts/contracts/layer_0/ERC1400/ERC20Votes/ERC20VotesStorageWrapper.sol

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ abstract contract ERC20VotesStorageWrapper is ERC1594StorageWrapper {
9393
}
9494

9595
function _delegate(address delegator, address delegatee) internal virtual {
96-
_triggerScheduledTasks(0);
96+
_triggerScheduledCrossOrderedTasks(0);
9797

9898
_takeAbafCheckpoint();
9999

packages/ats/contracts/contracts/layer_0/adjustBalances/AdjustBalancesStorageWrapper1.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import {
55
_ADJUST_BALANCES_STORAGE_POSITION
66
} from '../constants/storagePositions.sol';
77
import {
8-
ScheduledBalanceAdjustmentsStorageWrapper
9-
} from '../scheduledTasks/scheduledBalanceAdjustments/ScheduledBalanceAdjustmentsStorageWrapper.sol';
8+
ScheduledCrossOrderedTasksStorageWrapper
9+
} from '../../layer_0/scheduledTasks/scheduledCrossOrderedTasks/ScheduledCrossOrderedTasksStorageWrapper.sol';
1010
import {
1111
IAdjustBalancesStorageWrapper
1212
} from '../../layer_2/interfaces/adjustBalances/IAdjustBalancesStorageWrapper.sol';
1313
import {IClearing} from '../../layer_1/interfaces/clearing/IClearing.sol';
1414

1515
abstract contract AdjustBalancesStorageWrapper1 is
1616
IAdjustBalancesStorageWrapper,
17-
ScheduledBalanceAdjustmentsStorageWrapper
17+
ScheduledCrossOrderedTasksStorageWrapper
1818
{
1919
struct AdjustBalancesStorage {
2020
// Mapping from investor to their partitions labaf

packages/ats/contracts/contracts/layer_0/adjustBalances/AdjustBalancesStorageWrapper2.sol

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,10 @@ abstract contract AdjustBalancesStorageWrapper2 is
1111
IAdjustBalancesStorageWrapper,
1212
ClearingStorageWrapper2
1313
{
14-
function _adjustBalances(uint256 _factor, uint8 _decimals) internal {
14+
function _adjustBalances(
15+
uint256 _factor,
16+
uint8 _decimals
17+
) internal override {
1518
_updateDecimalsSnapshot();
1619
_updateAbafSnapshot();
1720
_updateAssetTotalSupplySnapshot();

packages/ats/contracts/contracts/layer_0/bond/BondStorageWrapper.sol

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,11 @@ pragma solidity >=0.8.0 <0.9.0;
44
import {
55
_BOND_STORAGE_POSITION
66
} from '../../layer_2/constants/storagePositions.sol';
7-
import {COUPON_CORPORATE_ACTION_TYPE} from '../../layer_2/constants/values.sol';
7+
import {
8+
COUPON_CORPORATE_ACTION_TYPE,
9+
SNAPSHOT_RESULT_ID,
10+
SNAPSHOT_TASK_TYPE
11+
} from '../../layer_2/constants/values.sol';
812
import {IBondRead} from '../../layer_2/interfaces/bond/IBondRead.sol';
913
import {
1014
IBondStorageWrapper
@@ -50,10 +54,35 @@ abstract contract BondStorageWrapper is
5054
internal
5155
returns (bool success_, bytes32 corporateActionId_, uint256 couponID_)
5256
{
57+
bytes memory data = abi.encode(_newCoupon);
58+
5359
(success_, corporateActionId_, couponID_) = _addCorporateAction(
5460
COUPON_CORPORATE_ACTION_TYPE,
55-
abi.encode(_newCoupon)
61+
data
62+
);
63+
64+
_initCoupon(success_, corporateActionId_, data);
65+
}
66+
67+
function _initCoupon(
68+
bool _success,
69+
bytes32 _actionId,
70+
bytes memory _data
71+
) internal {
72+
if (!_success) {
73+
revert IBondStorageWrapper.CouponCreationFailed();
74+
}
75+
76+
IBondRead.Coupon memory newCoupon = abi.decode(
77+
_data,
78+
(IBondRead.Coupon)
5679
);
80+
81+
_addScheduledCrossOrderedTask(
82+
newCoupon.recordDate,
83+
abi.encode(SNAPSHOT_TASK_TYPE)
84+
);
85+
_addScheduledSnapshot(newCoupon.recordDate, abi.encode(_actionId));
5786
}
5887

5988
/**
@@ -97,7 +126,10 @@ abstract contract BondStorageWrapper is
97126
(registeredCoupon_.coupon) = abi.decode(data, (IBondRead.Coupon));
98127
}
99128

100-
registeredCoupon_.snapshotId = _getSnapshotID(actionId);
129+
registeredCoupon_.snapshotId = _getUintResultAt(
130+
actionId,
131+
SNAPSHOT_RESULT_ID
132+
);
101133
}
102134

103135
function _getCouponFor(

packages/ats/contracts/contracts/layer_0/constants/storagePositions.sol

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ bytes32 constant _SCHEDULED_SNAPSHOTS_STORAGE_POSITION = 0xe5334ddaa6268d55c7efe
3636
// keccak256('security.token.standard.scheduledBalanceAdjustments.storage');
3737
bytes32 constant _SCHEDULED_BALANCE_ADJUSTMENTS_STORAGE_POSITION = 0xaf4aaa3de473ec9b58645d40f5a2fe4e176157e247b2d875db61f1a70935ac68;
3838

39-
// keccak256('security.token.standard.scheduledTasks.storage');
40-
bytes32 constant _SCHEDULED_TASKS_STORAGE_POSITION = 0x2352c39c61abb4e922588a7d46fb5c925b9a205577be135537479031648c86fe;
39+
// keccak256('security.token.standard.scheduledCrossOrderedTasks.storage');
40+
bytes32 constant _SCHEDULED_CROSS_ORDERED_TASKS_STORAGE_POSITION = 0x07c301a048b8fa80688acfab6d93f7e94a43ce454031a02cdd132b92ca943a70;
4141

4242
// keccak256('security.token.standard.hold.storage');
4343
bytes32 constant _HOLD_STORAGE_POSITION = 0x80346b80475a6f26abb9f460d81c6dbe6a8dd5d1acfb0827cfe37c4263a562ca;

packages/ats/contracts/contracts/layer_0/corporateActions/CorporateActionsStorageWrapper1.sol renamed to packages/ats/contracts/contracts/layer_0/corporateActions/CorporateActionsStorageWrapper.sol

Lines changed: 66 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,8 @@ import {
1717
_CORPORATE_ACTION_STORAGE_POSITION
1818
} from '../constants/storagePositions.sol';
1919
import {ClearingStorageWrapper1} from '../clearing/ClearingStorageWrapper1.sol';
20-
import {SNAPSHOT_RESULT_ID} from '../constants/values.sol';
2120

22-
abstract contract CorporateActionsStorageWrapper1 is ClearingStorageWrapper1 {
21+
abstract contract CorporateActionsStorageWrapper is ClearingStorageWrapper1 {
2322
using LibCommon for EnumerableSet.Bytes32Set;
2423
using EnumerableSet for EnumerableSet.Bytes32Set;
2524

@@ -28,24 +27,38 @@ abstract contract CorporateActionsStorageWrapper1 is ClearingStorageWrapper1 {
2827
_;
2928
}
3029

31-
function _onScheduledSnapshotTriggered(
32-
uint256 _snapShotID,
33-
bytes memory _data
34-
) internal {
35-
if (_data.length > 0) {
36-
bytes32 actionId = abi.decode(_data, (bytes32));
37-
_addSnapshotToAction(actionId, _snapShotID);
38-
}
30+
modifier onlyMatchingActionType(bytes32 _actionType, uint256 _index) {
31+
_checkMatchingActionType(_actionType, _index);
32+
_;
3933
}
4034

41-
function _addSnapshotToAction(
42-
bytes32 _actionId,
43-
uint256 _snapshotId
44-
) internal {
45-
_updateCorporateActionResult(
46-
_actionId,
47-
SNAPSHOT_RESULT_ID,
48-
abi.encodePacked(_snapshotId)
35+
// Internal
36+
function _addCorporateAction(
37+
bytes32 _actionType,
38+
bytes memory _data
39+
)
40+
internal
41+
returns (
42+
bool success_,
43+
bytes32 corporateActionId_,
44+
uint256 corporateActionIndexByType_
45+
)
46+
{
47+
CorporateActionDataStorage
48+
storage corporateActions_ = _corporateActionsStorage();
49+
corporateActionId_ = bytes32(corporateActions_.actions.length() + 1);
50+
// TODO: Review when it can return false.
51+
success_ =
52+
corporateActions_.actions.add(corporateActionId_) &&
53+
corporateActions_.actionsByType[_actionType].add(
54+
corporateActionId_
55+
);
56+
corporateActions_
57+
.actionsData[corporateActionId_]
58+
.actionType = _actionType;
59+
corporateActions_.actionsData[corporateActionId_].data = _data;
60+
corporateActionIndexByType_ = _getCorporateActionCountByType(
61+
_actionType
4962
);
5063
}
5164

@@ -120,12 +133,14 @@ abstract contract CorporateActionsStorageWrapper1 is ClearingStorageWrapper1 {
120133
.getFromSet(_pageIndex, _pageLength);
121134
}
122135

123-
function _getResult(
136+
function _getCorporateActionResult(
124137
bytes32 actionId,
125138
uint256 resultId
126139
) internal view returns (bytes memory result_) {
127140
if (_getCorporateActionResultCount(actionId) > resultId)
128-
result_ = _getCorporateActionResult(actionId, resultId);
141+
result_ = _corporateActionsStorage().actionsData[actionId].results[
142+
resultId
143+
];
129144
}
130145

131146
function _getCorporateActionResultCount(
@@ -134,25 +149,32 @@ abstract contract CorporateActionsStorageWrapper1 is ClearingStorageWrapper1 {
134149
return _corporateActionsStorage().actionsData[actionId].results.length;
135150
}
136151

137-
/**
138-
* @dev returns a corporate action result.
139-
*
140-
* @param actionId The corporate action Id
141-
*/
142-
function _getCorporateActionResult(
143-
bytes32 actionId,
144-
uint256 resultId
145-
) internal view returns (bytes memory) {
146-
return
147-
_corporateActionsStorage().actionsData[actionId].results[resultId];
148-
}
149-
150152
function _getCorporateActionData(
151153
bytes32 actionId
152154
) internal view returns (bytes memory) {
153155
return _corporateActionsStorage().actionsData[actionId].data;
154156
}
155157

158+
function _getUintResultAt(
159+
bytes32 _actionId,
160+
uint256 resultId
161+
) internal view returns (uint256) {
162+
bytes memory data = _getCorporateActionResult(_actionId, resultId);
163+
164+
uint256 bytesLength = data.length;
165+
166+
if (bytesLength < 32) return 0;
167+
168+
uint256 value;
169+
170+
// solhint-disable-next-line no-inline-assembly
171+
assembly {
172+
value := mload(add(data, 0x20))
173+
}
174+
175+
return value;
176+
}
177+
156178
function _corporateActionsStorage()
157179
internal
158180
pure
@@ -165,6 +187,17 @@ abstract contract CorporateActionsStorageWrapper1 is ClearingStorageWrapper1 {
165187
}
166188
}
167189

190+
function _checkMatchingActionType(
191+
bytes32 _actionType,
192+
uint256 _index
193+
) private view {
194+
if (_getCorporateActionCountByType(_actionType) <= _index)
195+
revert ICorporateActionsStorageWrapper.WrongIndexForAction(
196+
_index,
197+
_actionType
198+
);
199+
}
200+
168201
function _checkDates(uint256 _firstDate, uint256 _secondDate) private pure {
169202
if (_secondDate < _firstDate) {
170203
revert ICorporateActionsStorageWrapper.WrongDates(

0 commit comments

Comments
 (0)