Skip to content
This repository has been archived by the owner on Mar 1, 2024. It is now read-only.

Commit

Permalink
gas optimisation
Browse files Browse the repository at this point in the history
  • Loading branch information
simonDos committed Dec 6, 2023
1 parent 8ec76f0 commit 4e32c3d
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions contracts/root/depositManager/DepositManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ interface IPolygonMigration {
function migrate(uint256 amount) external;
}


contract DepositManager is DepositManagerStorage, IDepositManager, ERC721Holder {
using SafeMath for uint256;
using SafeERC20 for IERC20;
Expand Down Expand Up @@ -50,15 +51,16 @@ contract DepositManager is DepositManagerStorage, IDepositManager, ERC721Holder

function _migrateMatic(uint256 _amount) private {
IERC20 matic = IERC20(registry.contractMap(keccak256("matic")));
address polygonMigration = registry.contractMap(keccak256("polygonMigration"));

// check that _amount is not too high
require(matic.balanceOf(address(this)) >= _amount, "amount exceeds this contract's MATIC balance");

// approve
matic.approve(registry.contractMap(keccak256("polygonMigration")), _amount);
matic.approve(polygonMigration, _amount);

// call migrate function
IPolygonMigration(registry.contractMap(keccak256("polygonMigration"))).migrate(_amount);
IPolygonMigration(polygonMigration).migrate(_amount);
}

function updateMaxErc20Deposit(uint256 maxDepositAmount) public onlyGovernance {
Expand Down Expand Up @@ -161,17 +163,19 @@ contract DepositManager is DepositManagerStorage, IDepositManager, ERC721Holder
address _token,
uint256 _amountOrToken
) internal onlyWhenUnlocked isTokenMapped(_token) {
_createDepositBlock(_user, _token, _amountOrToken, rootChain.updateDepositId(1) /* returns _depositId */);
_createDepositBlock(_user, _token, _amountOrToken, rootChain.updateDepositId(1)); // returns _depositId
}

function _createDepositBlock(address _user, address _token, uint256 _amountOrToken, uint256 _depositId) internal {
address matic = registry.contractMap(keccak256("matic"));

// new: auto-migrate MATIC to POL
if (_token == registry.contractMap(keccak256("matic"))) {
if (_token == matic) {
_migrateMatic(_amountOrToken);
}
// new: bridge POL as MATIC, child chain behaviour does not change
else if (_token == registry.contractMap(keccak256("pol"))) {
_token = registry.contractMap(keccak256("matic"));
_token = matic;
}

deposits[_depositId] = DepositBlock(keccak256(abi.encodePacked(_user, _token, _amountOrToken)), now);
Expand Down

0 comments on commit 4e32c3d

Please sign in to comment.