Skip to content

Commit

Permalink
Merge pull request ToucanProtocol#26 from lazaralex98/prt-212-more-co…
Browse files Browse the repository at this point in the history
…mplete-tests

PRT-215 Wrote tests with USDC & WMATIC
  • Loading branch information
mauricedesaxe authored Jun 14, 2022
2 parents cd85e75 + c78a3e5 commit 2306556
Show file tree
Hide file tree
Showing 8 changed files with 2,134 additions and 914 deletions.
61 changes: 37 additions & 24 deletions contracts/OffsetHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ pragma solidity ^0.8.0;

import "hardhat/console.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@uniswap/v2-periphery/contracts/interfaces/IUniswapV2Router02.sol";
import "./OffsetHelperStorage.sol";
import "./interfaces/IToucanPoolToken.sol";
import "./interfaces/IUniswapV2Router02.sol";
import "./interfaces/IToucanCarbonOffsets.sol";
import "./interfaces/IToucanContractRegistry.sol";
import "hardhat/console.sol";
Expand Down Expand Up @@ -264,11 +264,8 @@ contract OffsetHelper is OffsetHelperStorage {
// instantiate router
IUniswapV2Router02 routerSushi = IUniswapV2Router02(sushiRouterAddress);

// establish path
address[] memory path = new address[](3);
path[0] = _fromToken;
path[1] = eligibleTokenAddresses["USDC"];
path[2] = _toToken;
// generate path
address[] memory path = generatePath(_fromToken, _toToken);

// get expected amountsIn
uint256[] memory amountsIn = routerSushi.getAmountsIn(_amount, path);
Expand Down Expand Up @@ -296,11 +293,8 @@ contract OffsetHelper is OffsetHelperStorage {
// instantiate router
IUniswapV2Router02 routerSushi = IUniswapV2Router02(sushiRouterAddress);

// establish path
address[] memory path = new address[](3);
path[0] = _fromToken;
path[1] = eligibleTokenAddresses["USDC"];
path[2] = _toToken;
// generate path
address[] memory path = generatePath(_fromToken, _toToken);

// estimate amountsIn
uint256[] memory expectedAmountsIn = routerSushi.getAmountsIn(
Expand All @@ -321,14 +315,14 @@ contract OffsetHelper is OffsetHelperStorage {
// swap
routerSushi.swapTokensForExactTokens(
_amount,
expectedAmountsIn[2],
expectedAmountsIn[0],
path,
address(this),
block.timestamp
);

// update balances
balances[msg.sender][path[2]] += _amount;
balances[msg.sender][_toToken] += _amount;
}

// apparently I need a fallback and a receive method to fix the situation where transfering dust MATIC
Expand Down Expand Up @@ -358,11 +352,11 @@ contract OffsetHelper is OffsetHelperStorage {
// instantiate router
IUniswapV2Router02 routerSushi = IUniswapV2Router02(sushiRouterAddress);

// establish path
address[] memory path = new address[](3);
path[0] = eligibleTokenAddresses["WMATIC"];
path[1] = eligibleTokenAddresses["USDC"];
path[2] = _toToken;
// generate path
address[] memory path = generatePath(
eligibleTokenAddresses["WMATIC"],
_toToken
);

// get expectedAmountsIn
uint256[] memory amounts = routerSushi.getAmountsIn(_amount, path);
Expand All @@ -381,11 +375,11 @@ contract OffsetHelper is OffsetHelperStorage {
// instantiate router
IUniswapV2Router02 routerSushi = IUniswapV2Router02(sushiRouterAddress);

// estabilish path
address[] memory path = new address[](3);
path[0] = eligibleTokenAddresses["WMATIC"];
path[1] = eligibleTokenAddresses["USDC"];
path[2] = _toToken;
// generate path
address[] memory path = generatePath(
eligibleTokenAddresses["WMATIC"],
_toToken
);

// estimate amountsIn
uint256[] memory expectedAmountsIn = routerSushi.getAmountsIn(
Expand All @@ -412,7 +406,7 @@ contract OffsetHelper is OffsetHelperStorage {
}

// update balances
balances[msg.sender][path[2]] += _amount;
balances[msg.sender][_toToken] += _amount;
}

/**
Expand Down Expand Up @@ -505,6 +499,25 @@ contract OffsetHelper is OffsetHelperStorage {
}
}

function generatePath(address _fromToken, address _toToken)
internal
view
returns (address[] memory)
{
if (_fromToken == eligibleTokenAddresses["USDC"]) {
address[] memory path = new address[](2);
path[0] = _fromToken;
path[1] = _toToken;
return path;
} else {
address[] memory path = new address[](3);
path[0] = _fromToken;
path[1] = eligibleTokenAddresses["USDC"];
path[2] = _toToken;
return path;
}
}

// ----------------------------------------
// Admin methods
// ----------------------------------------
Expand Down
162 changes: 0 additions & 162 deletions contracts/interfaces/IUniswapV2Router01.sol

This file was deleted.

52 changes: 0 additions & 52 deletions contracts/interfaces/IUniswapV2Router02.sol

This file was deleted.

Loading

0 comments on commit 2306556

Please sign in to comment.