@@ -4,51 +4,73 @@ pragma solidity ^0.8.0;
44import "./interface/ITokenBundle.sol " ;
55
66abstract 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.
89 mapping (uint256 => BundleInfo) private bundle;
910
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 ) {
1213 return bundle[_bundleId].count;
1314 }
1415
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 ) {
1718 return bundle[_bundleId].tokens[index];
1819 }
1920
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 ) {
2223 return bundle[_bundleId].uri;
2324 }
2425
2526 /// @dev Lets the calling contract create/update a bundle, by passing in a list of tokens and a unique id.
2627 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+ }
3142 }
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 ;
3356 }
3457
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 (
3760 Token memory _tokenToBind ,
3861 uint256 _bundleId ,
39- uint256 index ,
40- bool isUpdate
62+ uint256 _index
4163 ) 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 ;
4466 }
4567
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 {
4870 bundle[_bundleId].uri = _uri;
4971 }
5072
51- /// @dev Lets the calling contract delete a bundle with a given id .
73+ /// @dev Lets the calling contract delete a particular bundle .
5274 function _deleteBundle (uint256 _bundleId ) internal {
5375 delete bundle[_bundleId];
5476 }
0 commit comments