-
Notifications
You must be signed in to change notification settings - Fork 76
feat: adds mta redeemer #380
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
doncesarts
wants to merge
6
commits into
master
Choose a base branch
from
feat/mtaTokenRedeemer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8fff7b5
feat: adds mta redeemer
doncesarts b45b544
feat: adds mta redeemer
doncesarts b19e0fc
feat: adds periods to mta token redeemer
doncesarts 12eebdc
fix: lint issues on mta redeemer
doncesarts 827d465
fix: lint issues on mta redeemer
doncesarts aa3f2c4
fix: mta redeemer improves gass
doncesarts File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| // SPDX-License-Identifier: AGPL-3.0-or-later | ||
| pragma solidity 0.8.6; | ||
|
|
||
| // External | ||
| import { Initializable } from "@openzeppelin/contracts/proxy/utils/Initializable.sol"; | ||
| import { IERC20 } from "@openzeppelin/contracts/token/ERC20/IERC20.sol"; | ||
| import { SafeERC20 } from "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol"; | ||
|
|
||
| /** | ||
| * @notice Allows to redeem MTA for WETH at a fixed rate. | ||
| * @author mStable | ||
| * @dev VERSION: 1.0 | ||
| * DATE: 2023-03-08 | ||
| */ | ||
| contract MetaTokenRedeemer { | ||
| using SafeERC20 for IERC20; | ||
|
|
||
| uint256 public constant RATE_SCALE = 1e18; | ||
| address public immutable MTA; | ||
| address public immutable WETH; | ||
| uint256 public immutable RATE; | ||
|
|
||
| /** | ||
| * @notice Emits event whenever a user funds and amount. | ||
| */ | ||
| event Funded(address indexed from, uint256 amount); | ||
|
|
||
| /** | ||
| * @notice Emits event whenever a user redeems an amount. | ||
| */ | ||
| event Redeemed(address indexed sender, uint256 fromAssetAmount, uint256 toAssetAmount); | ||
|
|
||
| /** | ||
| * @notice Crates a new instance of the contract | ||
| * @param _mta MTA Token Address | ||
| * @param _weth WETH Token Address | ||
| * @param _rate The exchange rate with 18 decimal numbers, for example 1 MTA = 0.00002 ETH rate is 20000000000000; | ||
| */ | ||
| constructor( | ||
| address _mta, | ||
| address _weth, | ||
| uint256 _rate | ||
| ) { | ||
| MTA = _mta; | ||
| WETH = _weth; | ||
| RATE = _rate; | ||
| } | ||
|
|
||
| /// @notice Funds the contract with WETH. | ||
| /// @param amount Amount of WETH to be transfer to the contract | ||
| function fund(uint256 amount) external { | ||
|
naddison36 marked this conversation as resolved.
|
||
| IERC20(WETH).safeTransferFrom(msg.sender, address(this), amount); | ||
| emit Funded(msg.sender, amount); | ||
| } | ||
|
|
||
| /// @notice Redeems MTA for WETH at a fixed rate. | ||
| /// @param fromAssetAmount a parameter just like in doxygen (must be followed by parameter name) | ||
| /// @return toAssetAmount The amount of WETH received. | ||
| function redeem(uint256 fromAssetAmount) external returns (uint256 toAssetAmount) { | ||
| IERC20(MTA).safeTransferFrom(msg.sender, address(this), fromAssetAmount); | ||
|
naddison36 marked this conversation as resolved.
Outdated
|
||
|
|
||
| // calculate to asset amount | ||
| toAssetAmount = (fromAssetAmount * RATE) / RATE_SCALE; | ||
|
|
||
| // transfer out the to asset | ||
| require(IERC20(WETH).balanceOf(address(this)) >= toAssetAmount, "not enough WETH"); | ||
|
|
||
| IERC20(WETH).safeTransfer(msg.sender, toAssetAmount); | ||
|
|
||
| emit Redeemed(msg.sender, fromAssetAmount, toAssetAmount); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| import "ts-node/register" | ||
| import "tsconfig-paths/register" | ||
| import { task, types } from "hardhat/config" | ||
| import { MetaTokenRedeemer__factory } from "types/generated" | ||
| import { BigNumber } from "ethers" | ||
| import { deployContract } from "./utils/deploy-utils" | ||
| import { getSigner } from "./utils/signerFactory" | ||
| import { verifyEtherscan } from "./utils/etherscan" | ||
| import { MTA } from "./utils" | ||
|
|
||
| task("deploy-MetaTokenRedeemer") | ||
| .addParam("rate", "Redemption rate with 18 decimal points", types.string) | ||
| .addOptionalParam("speed", "Defender Relayer speed param: 'safeLow' | 'average' | 'fast' | 'fastest'", "fast", types.string) | ||
| .setAction(async (taskArgs, hre) => { | ||
| const signer = await getSigner(hre, taskArgs.speed) | ||
| const mtaAddr = MTA.address | ||
| const wethAddr = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2" | ||
|
|
||
| const metaTokenRedeemer = await deployContract(new MetaTokenRedeemer__factory(signer), "MetaTokenRedeemer", [ | ||
| mtaAddr, | ||
| wethAddr, | ||
| BigNumber.from(taskArgs.rate), | ||
| ]) | ||
|
|
||
| await verifyEtherscan(hre, { | ||
| address: metaTokenRedeemer.address, | ||
| contract: "contracts/shared/MetaTokenRedeemer.sol:MetaTokenRedeemer", | ||
| }) | ||
| }) | ||
| module.exports = {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| import { BN, simpleToExactAmount } from "@utils/math" | ||
| import { ethers } from "hardhat" | ||
| import { ERC20, MetaTokenRedeemer, MetaTokenRedeemer__factory, MockERC20__factory, MockRoot__factory } from "types/generated" | ||
| import { expect } from "chai" | ||
| import { Signer } from "ethers" | ||
| import { ZERO } from "@utils/constants" | ||
|
|
||
| describe("MetaTokenRedeemer", () => { | ||
| let redeemer: MetaTokenRedeemer | ||
| let deployer: Signer | ||
| let alice: Signer | ||
| let aliceAddress: string | ||
| let mta: ERC20 | ||
| let weth: ERC20 | ||
| const rate = BN.from("20000000000000") // 1 MTA = 0.00002 ETH (Rate to simplify tests) | ||
| const wethAmount = simpleToExactAmount(20) | ||
|
|
||
| before(async () => { | ||
| const accounts = await ethers.getSigners() | ||
| deployer = accounts[0] | ||
| alice = accounts[1] | ||
| aliceAddress = await alice.getAddress() | ||
| mta = await new MockERC20__factory(deployer).deploy( | ||
| "Meta Token", | ||
| "mta", | ||
| 18, | ||
| await deployer.getAddress(), | ||
| simpleToExactAmount(10_000_000), | ||
| ) | ||
| weth = await new MockERC20__factory(deployer).deploy( | ||
| "WETH Token", | ||
| "weth", | ||
| 18, | ||
| await deployer.getAddress(), | ||
| simpleToExactAmount(1_000_000), | ||
| ) | ||
| redeemer = await new MetaTokenRedeemer__factory(deployer).deploy(mta.address, weth.address, rate) | ||
| // send mta to alice | ||
| mta.transfer(aliceAddress, simpleToExactAmount(10_000)) | ||
| }) | ||
| it("deposits WETH into redeemer", async () => { | ||
| await weth.approve(redeemer.address, wethAmount) | ||
| const tx = await redeemer.fund(wethAmount) | ||
| expect(tx) | ||
| .to.emit(redeemer, "Funded") | ||
| .withArgs(await deployer.getAddress(), wethAmount) | ||
| }) | ||
| it("anyone can redeem MTA multiple times", async () => { | ||
| const aliceBalanceBefore = await mta.balanceOf(aliceAddress) | ||
| const aliceWethBalanceBefore = await weth.balanceOf(aliceAddress) | ||
| const redeemerWethBalanceBefore = await weth.balanceOf(redeemer.address) | ||
|
|
||
| const amount = aliceBalanceBefore.div(2) | ||
| const wethAmount = amount.mul(rate).div(simpleToExactAmount(1)) | ||
|
|
||
| expect(aliceBalanceBefore, "balance").to.be.gt(ZERO) | ||
| await mta.connect(alice).approve(redeemer.address, ethers.constants.MaxUint256) | ||
|
|
||
| const tx1 = await redeemer.connect(alice).redeem(amount) | ||
| expect(tx1).to.emit(redeemer, "Redeemed").withArgs(aliceAddress, amount, wethAmount) | ||
|
|
||
| const tx2 = await redeemer.connect(alice).redeem(amount) | ||
| expect(tx2).to.emit(redeemer, "Redeemed").withArgs(aliceAddress, amount, wethAmount) | ||
|
|
||
| const aliceBalanceAfter = await mta.balanceOf(aliceAddress) | ||
| const aliceWethBalanceAfter = await weth.balanceOf(aliceAddress) | ||
| const redeemerWethBalanceAfter = await weth.balanceOf(redeemer.address) | ||
|
|
||
| expect(aliceBalanceAfter, "alice mta balance").to.be.eq(ZERO) | ||
| expect(aliceWethBalanceAfter, "alice weth balance").to.be.eq(aliceWethBalanceBefore.add(wethAmount.mul(2))) | ||
| expect(redeemerWethBalanceAfter, "redeemer weth balance").to.be.eq(redeemerWethBalanceBefore.sub(wethAmount.mul(2))) | ||
| }) | ||
| it("fails if there is not enough WETH (non realistic example) ", async () => { | ||
| const mtaAmount = await mta.balanceOf(await deployer.getAddress()) | ||
| const wethAmount = mtaAmount.mul(rate).div(simpleToExactAmount(1)) | ||
| expect(wethAmount).to.be.gt(simpleToExactAmount(20)) | ||
| await mta.approve(redeemer.address, mtaAmount) | ||
|
|
||
| await expect(redeemer.redeem(mtaAmount)).to.be.revertedWith("not enough WETH") | ||
| }) | ||
| }) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.