Skip to content

Commit

Permalink
fix: withdraw bep20 error
Browse files Browse the repository at this point in the history
  • Loading branch information
owen-reorg committed Nov 21, 2023
1 parent 59a5613 commit 0ad4bb4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 2 deletions.
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-solidity and test':
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");
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
50 changes: 50 additions & 0 deletions contracts/test/L2StandardBridgeBot.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
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;

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;
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);
bot.withdrawTo{value: withdrawFee}(usdt, user, amount, 200000, "");
}
}

0 comments on commit 0ad4bb4

Please sign in to comment.