@@ -4,51 +4,73 @@ pragma solidity ^0.8.0;
4
4
import "./interface/ITokenBundle.sol " ;
5
5
6
6
abstract contract TokenBundle is ITokenBundle {
7
- /// @dev UID => asset count, bundle uri, and tokens contained in the bundle
7
+
8
+ /// @dev Mapping from bundle UID => bundle info.
8
9
mapping (uint256 => BundleInfo) private bundle;
9
10
10
- /// @dev Returns the count of assets in a bundle, given a bundle Id .
11
- function getTokenCount (uint256 _bundleId ) public view returns (uint256 ) {
11
+ /// @dev Returns the total number of assets in a particular bundle.
12
+ function getTokenCountOfBundle (uint256 _bundleId ) public view returns (uint256 ) {
12
13
return bundle[_bundleId].count;
13
14
}
14
15
15
- /// @dev Returns a token contained in a bundle, given a bundle Id and index of token .
16
- function getToken (uint256 _bundleId , uint256 index ) public view returns (Token memory ) {
16
+ /// @dev Returns an asset contained in a particular bundle, at a particular index.
17
+ function getTokenOfBundle (uint256 _bundleId , uint256 index ) public view returns (Token memory ) {
17
18
return bundle[_bundleId].tokens[index];
18
19
}
19
20
20
- /// @dev Returns the uri of bundle for a particular bundle Id .
21
- function getUri (uint256 _bundleId ) public view returns (string memory ) {
21
+ /// @dev Returns the uri of a particular bundle.
22
+ function getUriOfBundle (uint256 _bundleId ) public view returns (string memory ) {
22
23
return bundle[_bundleId].uri;
23
24
}
24
25
25
26
/// @dev Lets the calling contract create/update a bundle, by passing in a list of tokens and a unique id.
26
27
function _setBundle (Token[] calldata _tokensToBind , uint256 _bundleId ) internal {
27
- // uint256 _bundleId = _getNextBundleId();
28
- require (_tokensToBind.length > 0 , "no tokens to bind " );
29
- for (uint256 i = 0 ; i < _tokensToBind.length ; i += 1 ) {
30
- bundle[_bundleId].tokens[i] = _tokensToBind[i];
28
+
29
+ require (_tokensToBind.length > 0 , "TokenBundle: no tokens to bind. " );
30
+
31
+ uint256 currentCount = bundle[_bundleId].count;
32
+ uint256 targetCount = _tokensToBind.length ;
33
+ uint256 check = currentCount > targetCount ? currentCount : targetCount;
34
+
35
+ for (uint256 i = 0 ; i < check; i += 1 ) {
36
+
37
+ if (i < targetCount) {
38
+ bundle[_bundleId].tokens[i] = _tokensToBind[i];
39
+ } else if (i < currentCount) {
40
+ delete bundle[_bundleId].tokens[i];
41
+ }
31
42
}
32
- bundle[_bundleId].count = _tokensToBind.length ;
43
+
44
+ bundle[_bundleId].count = targetCount;
45
+ }
46
+
47
+ /// @dev Lets the calling contract add a token to a bundle for a unique bundle id and index.
48
+ function _addTokenInBundle (
49
+ Token memory _tokenToBind ,
50
+ uint256 _bundleId
51
+ ) internal {
52
+ uint256 id = bundle[_bundleId].count;
53
+
54
+ bundle[_bundleId].tokens[id] = _tokenToBind;
55
+ bundle[_bundleId].count += 1 ;
33
56
}
34
57
35
- /// @dev Lets the calling contract set/ update a token in a bundle for a unique bundle id and index.
36
- function _setBundleToken (
58
+ /// @dev Lets the calling contract update a token in a bundle for a unique bundle id and index.
59
+ function _updateTokenInBundle (
37
60
Token memory _tokenToBind ,
38
61
uint256 _bundleId ,
39
- uint256 index ,
40
- bool isUpdate
62
+ uint256 _index
41
63
) internal {
42
- bundle[_bundleId].tokens[index] = _tokenToBind ;
43
- bundle[_bundleId].count += isUpdate ? 0 : 1 ;
64
+ require (_index < bundle[_bundleId].count, " TokenBundle: index DNE. " ) ;
65
+ bundle[_bundleId].tokens[_index] = _tokenToBind ;
44
66
}
45
67
46
- /// @dev Lets the calling contract set/update the bundle uri for a particular bundle id .
47
- function _setUri (string calldata _uri , uint256 _bundleId ) internal {
68
+ /// @dev Lets the calling contract set/update the uri of a particular bundle.
69
+ function _setUriOfBundle (string calldata _uri , uint256 _bundleId ) internal {
48
70
bundle[_bundleId].uri = _uri;
49
71
}
50
72
51
- /// @dev Lets the calling contract delete a bundle with a given id .
73
+ /// @dev Lets the calling contract delete a particular bundle .
52
74
function _deleteBundle (uint256 _bundleId ) internal {
53
75
delete bundle[_bundleId];
54
76
}
0 commit comments