Skip to content

Commit 92f9e84

Browse files
committed
polish to a more auditable state
Signed-off-by: Ihor Farion <ihor@umaproject.org>
1 parent 8cc0c5a commit 92f9e84

File tree

3 files changed

+13
-41
lines changed

3 files changed

+13
-41
lines changed

contracts/external/libraries/SharedDecimalsLib.sol renamed to contracts/external/libraries/OFTCoreMath.sol

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
pragma solidity ^0.8.0;
33

44
/**
5-
* @title SharedDecimalsLib
5+
* @title OFTCoreMath
66
* @notice Copied from LZ implementation here:
77
* `https://github.com/LayerZero-Labs/devtools/blob/16daaee36fe802d11aa99b89c29bb74447354483/packages/oft-evm/contracts/OFTCore.sol#L364`
8-
* Code was not modified beyond removing unrelated OFT/OApp concerns.
8+
* Code was not modified beyond adding `uint8 _sharedDecimals` to constructor args and substituting `sharedDecimals()` calls with it
99
*/
10-
abstract contract SharedDecimalsLib {
10+
abstract contract OFTCoreMath {
1111
error InvalidLocalDecimals();
1212
error AmountSDOverflowed(uint256 amountSD);
1313

@@ -25,43 +25,15 @@ abstract contract SharedDecimalsLib {
2525
// @dev To preserve the dust that would otherwise be lost on that conversion,
2626
// we need to unify a denomination that can be represented on ALL chains inside of the OFT mesh
2727
uint256 public immutable decimalConversionRate;
28-
uint8 internal immutable _sharedDecimals;
2928

3029
/**
3130
* @dev Constructor.
3231
* @param _localDecimals The decimals of the token on the local chain (this chain).
33-
* @param _sharedDecimalsArg The shared decimals used by the OFT.
32+
* @param _sharedDecimals The shared decimals used by the OFT.
3433
*/
35-
constructor(uint8 _localDecimals, uint8 _sharedDecimalsArg) {
36-
_sharedDecimals = _sharedDecimalsArg;
37-
if (_localDecimals < _sharedDecimalsArg) revert InvalidLocalDecimals();
38-
decimalConversionRate = 10 ** (_localDecimals - _sharedDecimalsArg);
39-
}
40-
41-
/**
42-
* @dev Retrieves the shared decimals of the OFT.
43-
* @return The shared decimals of the OFT.
44-
*
45-
* @dev Sets an implicit cap on the amount of tokens, over uint64.max() will need some sort of outbound cap / totalSupply cap
46-
* Lowest common decimal denominator between chains.
47-
* Defaults to 6 decimal places to provide up to 18,446,744,073,709.551615 units (max uint64).
48-
* For tokens exceeding this totalSupply(), they will need to override the sharedDecimals function with something smaller.
49-
* ie. 4 sharedDecimals would be 1,844,674,407,370,955.1615
50-
*/
51-
function sharedDecimals() public view virtual returns (uint8) {
52-
return _sharedDecimals;
53-
}
54-
55-
/**
56-
* @dev Internal function to remove dust from the given local decimal amount.
57-
* @param _amountLD The amount in local decimals.
58-
* @return amountLD The amount after removing dust.
59-
*
60-
* @dev Prevents the loss of dust when moving amounts between chains with different decimals.
61-
* @dev eg. uint(123) with a conversion rate of 100 becomes uint(100).
62-
*/
63-
function _removeDust(uint256 _amountLD) internal view virtual returns (uint256 amountLD) {
64-
return (_amountLD / decimalConversionRate) * decimalConversionRate;
34+
constructor(uint8 _localDecimals, uint8 _sharedDecimals) {
35+
if (_localDecimals < _sharedDecimals) revert InvalidLocalDecimals();
36+
decimalConversionRate = 10 ** (_localDecimals - _sharedDecimals);
6537
}
6638

6739
/**

contracts/periphery/mintburn/sponsored-oft/DstOFTHandler.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import { IOFT, IOAppCore } from "../../../interfaces/IOFT.sol";
1111
import { HyperCoreFlowExecutor } from "../HyperCoreFlowExecutor.sol";
1212
import { ArbitraryEVMFlowExecutor } from "../ArbitraryEVMFlowExecutor.sol";
1313
import { CommonFlowParams, EVMFlowParams, AccountCreationMode } from "../Structs.sol";
14-
import { SharedDecimalsLib } from "../../../external/libraries/SharedDecimalsLib.sol";
14+
import { OFTCoreMath } from "../../../external/libraries/OFTCoreMath.sol";
1515

1616
import { IERC20 } from "@openzeppelin/contracts-v4/token/ERC20/IERC20.sol";
1717
import { IERC20Metadata } from "@openzeppelin/contracts-v4/token/ERC20/extensions/IERC20Metadata.sol";
@@ -23,7 +23,7 @@ import { SafeERC20 } from "@openzeppelin/contracts-v4/token/ERC20/utils/SafeERC2
2323
* @dev IMPORTANT. `BaseModuleHandler` should always be the first contract in inheritance chain. Read
2424
`BaseModuleHandler` contract code to learn more.
2525
*/
26-
contract DstOFTHandler is BaseModuleHandler, SharedDecimalsLib, ILayerZeroComposer, ArbitraryEVMFlowExecutor {
26+
contract DstOFTHandler is BaseModuleHandler, OFTCoreMath, ILayerZeroComposer, ArbitraryEVMFlowExecutor {
2727
using ComposeMsgCodec for bytes;
2828
using Bytes32ToAddress for bytes32;
2929
using AddressToBytes32 for address;
@@ -87,7 +87,7 @@ contract DstOFTHandler is BaseModuleHandler, SharedDecimalsLib, ILayerZeroCompos
8787
)
8888
BaseModuleHandler(_donationBox, _baseToken, DEFAULT_ADMIN_ROLE)
8989
ArbitraryEVMFlowExecutor(_multicallHandler)
90-
SharedDecimalsLib(IERC20Metadata(_baseToken).decimals(), IOFT(_ioft).sharedDecimals())
90+
OFTCoreMath(IERC20Metadata(_baseToken).decimals(), IOFT(_ioft).sharedDecimals())
9191
{
9292
baseToken = _baseToken;
9393

contracts/periphery/mintburn/sponsored-oft/SponsoredOFTSrcPeriphery.sol

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { ComposeMsgCodec } from "./ComposeMsgCodec.sol";
88
import { IOFT, IOAppCore, SendParam, MessagingFee } from "../../../interfaces/IOFT.sol";
99
import { AddressToBytes32 } from "../../../libraries/AddressConverters.sol";
1010
import { MinimalLZOptions } from "../../../external/libraries/MinimalLZOptions.sol";
11-
import { SharedDecimalsLib } from "../../../external/libraries/SharedDecimalsLib.sol";
11+
import { OFTCoreMath } from "../../../external/libraries/OFTCoreMath.sol";
1212

1313
import { Ownable } from "@openzeppelin/contracts-v4/access/Ownable.sol";
1414
import { IERC20 } from "@openzeppelin/contracts-v4/token/ERC20/IERC20.sol";
@@ -17,7 +17,7 @@ import { SafeERC20 } from "@openzeppelin/contracts-v4/token/ERC20/utils/SafeERC2
1717

1818
/// @notice Source chain periphery contract for users to interact with to start a sponsored or a non-sponsored flow
1919
/// that allows custom Accross-supported flows on destination chain. Uses LayzerZero's OFT as an underlying bridge
20-
contract SponsoredOFTSrcPeriphery is Ownable, SharedDecimalsLib {
20+
contract SponsoredOFTSrcPeriphery is Ownable, OFTCoreMath {
2121
using AddressToBytes32 for address;
2222
using MinimalLZOptions for bytes;
2323
using SafeERC20 for IERC20;
@@ -95,7 +95,7 @@ contract SponsoredOFTSrcPeriphery is Ownable, SharedDecimalsLib {
9595
address _oftMessenger,
9696
uint32 _srcEid,
9797
address _signer
98-
) SharedDecimalsLib(IERC20Metadata(_token).decimals(), IOFT(_oftMessenger).sharedDecimals()) {
98+
) OFTCoreMath(IERC20Metadata(_token).decimals(), IOFT(_oftMessenger).sharedDecimals()) {
9999
TOKEN = _token;
100100
OFT_MESSENGER = _oftMessenger;
101101
SRC_EID = _srcEid;

0 commit comments

Comments
 (0)