Skip to content
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

feat: adds mta redeemer #380

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions contracts/shared/MetaTokenRedeemer.sol
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;
naddison36 marked this conversation as resolved.
Show resolved Hide resolved
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.
Show resolved Hide 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.
Show resolved Hide resolved

// 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);
}
}
30 changes: 30 additions & 0 deletions tasks/deployShared.ts
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 = {}
81 changes: 81 additions & 0 deletions test/shared/meta-token-redeemer.spec.ts
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")
})
})