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

fix: withdraw bep20 error #2

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
- name: Make build-go
run: make build-go

'build-solidity':
'build-test-solidity':
name: make build-solidity
runs-on: ubuntu-latest
steps:
Expand All @@ -38,4 +38,4 @@ jobs:
uses: foundry-rs/foundry-toolchain@v1

- name: Make build-solidity
run: cd contracts && forge build
run: cd contracts && forge build && forge test
6 changes: 6 additions & 0 deletions contracts/src/L2StandardBridgeBot.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
pragma solidity 0.8.20;

import { Ownable } from "openzeppelin-contracts/contracts/access/Ownable.sol";
import { IERC20 } from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";

interface IL2StandardBridge {
function withdrawTo(
Expand Down Expand Up @@ -45,6 +46,11 @@ contract L2StandardBridgeBot is Ownable {
} else {
require(msg.value == delegationFee, "BEP20 withdrawal: msg.value does not equal to delegationFee");

IERC20 l2Token = IERC20(_l2Token);
bool approveSuccess = l2Token.approve(L2_STANDARD_BRIDGE_ADDRESS, _amount + l2Token.allowance(address(this), L2_STANDARD_BRIDGE_ADDRESS));
require(approveSuccess, "BEP20 withdrawal: approve failed");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to confirm that whether this two require are necessary.

bool transferSuccess = l2Token.transferFrom(msg.sender, address(this), _amount);
require(transferSuccess, "BEP20 withdrawal: transferFrom failed");
L2_STANDARD_BRIDGE.withdrawTo{value: 0}(_l2Token, _to, _amount, _minGasLimit, _extraData);
}

Expand Down
56 changes: 56 additions & 0 deletions contracts/test/L2StandardBridgeBot.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
pragma solidity 0.8.20;

import "forge-std/Test.sol";
import { IERC20 } from "openzeppelin-contracts/contracts/token/ERC20/IERC20.sol";
import { Ownable } from "openzeppelin-contracts/contracts/access/Ownable.sol";
import "../src/L2StandardBridgeBot.sol";

contract L2StandardBridgeBotTest is Test {
L2StandardBridgeBot bot;
uint256 opbnbMainnetFork;
address deployer = address(0x1234);
address usdt = 0xCF712f20c85421d00EAa1B6F6545AaEEb4492B75;
address user = 0x3977f9B1F4912a783B44aBa813dA388AC73a1428;
uint withdrawFee = 10000;
address constant LEGACY_ERC20_ETH = 0xDeadDeAddeAddEAddeadDEaDDEAdDeaDDeAD0000;

event WithdrawTo(address indexed from, address l2Token, address to, uint256 amount, uint32 minGasLimit, bytes extraData);

function setUp() public {
opbnbMainnetFork = vm.createFork("https://opbnb-testnet-rpc.bnbchain.org");
vm.selectFork(opbnbMainnetFork);
vm.rollFork(opbnbMainnetFork, 7125821);
bot = new L2StandardBridgeBot(payable(deployer), withdrawFee);
}

function test_RevertSetDelegationFeeNotOwner() public {
vm.expectRevert();
bot.setDelegationFee(100);
}

function test_setDelegationFee() public {
vm.prank(deployer);
bot.setDelegationFee(100);
assertEq(bot.delegationFee(), 100);
}

function test_withdrawBNB() public {
vm.prank(user);
uint amount = 100;
vm.expectEmit(true, false, false, true);
emit WithdrawTo(user, LEGACY_ERC20_ETH, user, amount, 200000, "");
bot.withdrawTo{value: withdrawFee + amount}(LEGACY_ERC20_ETH, user, amount, 200000, "");
}

function test_ERC20() public {
uint amount = 100;
uint balanceBefore = IERC20(usdt).balanceOf(user);
console2.log("balanceBefore", balanceBefore);
vm.prank(user);
IERC20(usdt).approve(address(bot), amount);
vm.prank(user);
vm.expectEmit(true, false, false, true);
emit WithdrawTo(user, usdt, user, amount, 200000, "");
bot.withdrawTo{value: withdrawFee}(usdt, user, amount, 200000, "");
}
}
Loading