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

Commit

Permalink
Add BRR migration contract
Browse files Browse the repository at this point in the history
  • Loading branch information
kphed committed Apr 16, 2024
1 parent a380358 commit c97a344
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/on-push.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: forge-test

on: [push]

env:
FOUNDRY_PROFILE: ci

jobs:
check:
name: Foundry project
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
with:
submodules: recursive

- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly

- name: Run Forge tests
env:
FOUNDRY_PROFILE: default
RPC_URL: ${{ secrets.BASE_RPC_URL }}
run: |
forge test -vvv --fuzz-runs 1000 --rpc-url "$RPC_URL"
id: test
30 changes: 30 additions & 0 deletions src/BRRMigrator.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";

contract BRRMigrator {
using SafeTransferLib for address;

// The old BRR token which can be converted 1:1 into the new BRR token.
address private constant _OLD_TOKEN =
0x6d80d90ce251985bF41A98c6FDd6b7b975Fff884;

/// @notice The new and final BRR token.
address public immutable newToken;

constructor(address _newToken) {
newToken = _newToken;
}

/**
* @notice Migrate the old BRR token to the new BRR token.
* @param amount uint256 Token amount.
*/
function migrate(uint256 amount) external {
// Burn the old token, and transfer the new token to `msg.sender`.
_OLD_TOKEN.safeTransferFrom(msg.sender, address(0xdead), amount);

newToken.safeTransfer(msg.sender, amount);
}
}
81 changes: 81 additions & 0 deletions test/BRRMigrator.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "forge-std/Test.sol";
import {ERC20} from "solady/tokens/ERC20.sol";
import {SafeTransferLib} from "solady/utils/SafeTransferLib.sol";
import {BRR} from "src/BRR.sol";
import {BRRMigrator} from "src/BRRMigrator.sol";

contract BRRMigratorTest is Test {
using SafeTransferLib for address;

address public constant OLD_TOKEN =
0x6d80d90ce251985bF41A98c6FDd6b7b975Fff884;
uint256 public constant TEST_SUPPLY = 1_000_000e18;
BRR public immutable token = new BRR(address(this));
BRRMigrator public immutable migrator = new BRRMigrator(address(token));

constructor() {
token.mint(address(migrator), TEST_SUPPLY);

vm.prank(BRR(OLD_TOKEN).owner());

BRR(OLD_TOKEN).mint(address(this), TEST_SUPPLY);
address(OLD_TOKEN).safeApprove(address(migrator), type(uint256).max);
}

function testCannotMigrateTransferFromFailed() external {
uint256 amount = TEST_SUPPLY + 1;

vm.expectRevert(SafeTransferLib.TransferFromFailed.selector);

migrator.migrate(amount);
}

function testMigrateZero() external {
uint256 amount = 0;
uint256 oldTokenBalanceBefore = OLD_TOKEN.balanceOf(address(this));
uint256 newTokenBalanceBefore = token.balanceOf(address(this));

migrator.migrate(amount);

// Should be unchanged if `amount` is zero.
assertEq(oldTokenBalanceBefore, OLD_TOKEN.balanceOf(address(this)));
assertEq(newTokenBalanceBefore, token.balanceOf(address(this)));
}

function testMigrate() external {
uint256 amount = 1;
uint256 oldTokenBalanceBefore = OLD_TOKEN.balanceOf(address(this));
uint256 newTokenBalanceBefore = token.balanceOf(address(this));

migrator.migrate(amount);

assertEq(
oldTokenBalanceBefore - amount,
OLD_TOKEN.balanceOf(address(this))
);
assertEq(
newTokenBalanceBefore + amount,
token.balanceOf(address(this))
);
}

function testMigrateFuzz(uint256 amount) external {
amount = bound(amount, 0, TEST_SUPPLY);
uint256 oldTokenBalanceBefore = OLD_TOKEN.balanceOf(address(this));
uint256 newTokenBalanceBefore = token.balanceOf(address(this));

migrator.migrate(amount);

assertEq(
oldTokenBalanceBefore - amount,
OLD_TOKEN.balanceOf(address(this))
);
assertEq(
newTokenBalanceBefore + amount,
token.balanceOf(address(this))
);
}
}

0 comments on commit c97a344

Please sign in to comment.