2
2
/// @dev TrimmedAmount is a utility library to handle token amounts with different decimals
3
3
pragma solidity >= 0.8.8 < 0.9.0 ;
4
4
5
+ import "openzeppelin-contracts/contracts/utils/math/SafeCast.sol " ;
6
+
5
7
/// @dev TrimmedAmount is a bit-packed representation of a token amount and its decimals.
6
8
/// @dev 64 bits: [0 - 64] amount
7
9
/// @dev 8 bits: [64 - 72] decimals
@@ -122,7 +124,7 @@ library TrimmedAmountLib {
122
124
saturatedSum = saturatedSum > type (uint64 ).max ? type (uint64 ).max : saturatedSum;
123
125
}
124
126
125
- return packTrimmedAmount (uint64 (saturatedSum), getDecimals (a));
127
+ return packTrimmedAmount (SafeCast. toUint64 (saturatedSum), getDecimals (a));
126
128
}
127
129
128
130
/// @dev scale the amount from original decimals to target decimals (base 10)
@@ -145,7 +147,7 @@ library TrimmedAmountLib {
145
147
function shift (TrimmedAmount amount , uint8 toDecimals ) internal pure returns (TrimmedAmount) {
146
148
uint8 actualToDecimals = minUint8 (TRIMMED_DECIMALS, toDecimals);
147
149
return packTrimmedAmount (
148
- uint64 (scale (getAmount (amount), getDecimals (amount), actualToDecimals)),
150
+ SafeCast. toUint64 (scale (getAmount (amount), getDecimals (amount), actualToDecimals)),
149
151
actualToDecimals
150
152
);
151
153
}
@@ -173,10 +175,7 @@ library TrimmedAmountLib {
173
175
174
176
// NOTE: amt after trimming must fit into uint64 (that's the point of
175
177
// trimming, as Solana only supports uint64 for token amts)
176
- if (amountScaled > type (uint64 ).max) {
177
- revert AmountTooLarge (amt);
178
- }
179
- return packTrimmedAmount (uint64 (amountScaled), actualToDecimals);
178
+ return packTrimmedAmount (SafeCast.toUint64 (amountScaled), actualToDecimals);
180
179
}
181
180
182
181
function untrim (TrimmedAmount amt , uint8 toDecimals ) internal pure returns (uint256 ) {
0 commit comments