Skip to content

Commit

Permalink
review: refactor Contract from UpgradableOwnable to Ownable, add chai…
Browse files Browse the repository at this point in the history
…nID for Alfajores
  • Loading branch information
GigaHierz committed Sep 18, 2023
1 parent 70d2e13 commit f411e27
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 27 deletions.
40 changes: 17 additions & 23 deletions contracts/OffsetHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ import "./interfaces/IToucanContractRegistry.sol";
*/
contract OffsetHelper is OffsetHelperStorage {
using SafeERC20 for IERC20;
// Chain ID of Celo mainnet
// As Celo allows to pay the gas fees with other tokens than the native token,
// it's not possible ot use the swapETH (swap native tokens) functions.
uint256 private constant CELO_MAINNET_CHAINID = 42220;
uint256 private constant ALFAJORES_MAINNET_CHAINID = 44787;

/**
* @notice Emitted upon successful redemption of TCO2 tokens from a Toucan
Expand Down Expand Up @@ -88,9 +90,10 @@ contract OffsetHelper is OffsetHelperStorage {
_;
}

modifier isCelo() {
modifier isNotCelo() {
require(
block.chainid != CELO_MAINNET_CHAINID,
block.chainid != CELO_MAINNET_CHAINID &&
block.chainid != ALFAJORES_MAINNET_CHAINID,
"The function is not available on Celo."
);

Expand Down Expand Up @@ -121,29 +124,17 @@ contract OffsetHelper is OffsetHelperStorage {
paths = _paths;
dexRouterAddress = _dexRouterAddress;

uint256 i = 0;
uint256 eligibleSwapPathsBySymbolLen = _tokenSymbolsForPaths.length;
while (i < eligibleSwapPathsBySymbolLen) {
for (uint256 i; i < eligibleSwapPathsBySymbolLen; i++) {
eligibleSwapPaths[_paths[i][0]] = _paths[i];
eligibleSwapPathsBySymbol[_tokenSymbolsForPaths[i]] = _paths[i];
i++;
}
}

// The receive and fallback method are needed to fix the situation where transfering dust native tokens
// in the native tokens to token swap fails
receive() external payable {}

fallback() external payable {}

// ----------------------------------------
// Upgradable related functions
// ----------------------------------------

function initialize() external virtual initializer {
__Ownable_init_unchained();
}

// ----------------------------------------
// Public functions
// ----------------------------------------
Expand Down Expand Up @@ -263,7 +254,7 @@ contract OffsetHelper is OffsetHelperStorage {
)
public
payable
isCelo
isNotCelo
returns (address[] memory tco2s, uint256[] memory amounts)
{
// swap native tokens for BCT / NCT
Expand Down Expand Up @@ -299,7 +290,7 @@ contract OffsetHelper is OffsetHelperStorage {
)
public
payable
isCelo
isNotCelo
returns (address[] memory tco2s, uint256[] memory amounts)
{
// swap native tokens for BCT / NCT
Expand Down Expand Up @@ -373,8 +364,11 @@ contract OffsetHelper is OffsetHelperStorage {
// update balances
balances[msg.sender][_fromToken] -= _amount;
uint256 tco2sLen = tco2s.length;
for (uint256 index = 0; index < tco2sLen; index++) {
balances[msg.sender][tco2s[index]] += amounts[index];
for (uint256 i; i < tco2sLen; ) {
balances[msg.sender][tco2s[i]] += amounts[i];
unchecked {
i++;
}
}

emit Redeemed(msg.sender, _fromToken, tco2s, amounts);
Expand Down Expand Up @@ -510,7 +504,7 @@ contract OffsetHelper is OffsetHelperStorage {
function swapExactOutETH(
address _poolToken,
uint256 _toAmount
) public payable isCelo onlyRedeemable(_poolToken) {
) public payable isNotCelo onlyRedeemable(_poolToken) {
// create path & amounts
// wrap the native token
address fromToken = eligibleSwapPathsBySymbol["WMATIC"][0];
Expand Down Expand Up @@ -548,7 +542,7 @@ contract OffsetHelper is OffsetHelperStorage {
)
public
payable
isCelo
isNotCelo
onlyRedeemable(_poolToken)
returns (uint256 amountOut)
{
Expand Down Expand Up @@ -705,7 +699,7 @@ contract OffsetHelper is OffsetHelperStorage {
)
public
view
isCelo
isNotCelo
onlyRedeemable(_poolToken)
returns (uint256 amountOut)
{
Expand Down
4 changes: 2 additions & 2 deletions contracts/OffsetHelperStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

pragma solidity ^0.8.0;

import "@openzeppelin/contracts-upgradeable/access/OwnableUpgradeable.sol";
import "@openzeppelin/contracts/access/Ownable.sol";

contract OffsetHelperStorage is OwnableUpgradeable {
contract OffsetHelperStorage is Ownable {
// token symbol => token address
mapping(address => address[]) public eligibleSwapPaths;
mapping(string => address[]) public eligibleSwapPathsBySymbol;
Expand Down
2 changes: 0 additions & 2 deletions test/OffsetHelper.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,6 @@ describe("OffsetHelper", function () {
routerAddress
);

await offsetHelper.initialize();

const bct = IToucanPoolToken__factory.connect(
networkPoolAddress.BCT,
owner
Expand Down

0 comments on commit f411e27

Please sign in to comment.