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

Add Fee to OriginalTokenBridge #6

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
31 changes: 25 additions & 6 deletions contracts/OriginalTokenBridgeUpgradable.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@ import {IWETH} from "./interfaces/IWETH.sol";
contract OriginalTokenBridgeUpgradable is TokenBridgeBaseUpgradable {
using SafeERC20 for IERC20;

/// @notice Total bps representing 100%
uint16 public constant TOTAL_BPS = 10000;

/// @notice An optional fee charged on withdrawal, expressed in bps. E.g., 1bps = 0.01%
uint16 public withdrawalFeeBps;

/// @notice Tokens that can be bridged to the remote chain
mapping(address => bool) public supportedTokens;

Expand All @@ -32,6 +38,7 @@ contract OriginalTokenBridgeUpgradable is TokenBridgeBaseUpgradable {
event SetRemoteChainId(uint16 remoteChainId);
event RegisterToken(address token);
event WithdrawFee(address indexed token, address to, uint amount);
event SetWithdrawalFeeBps(uint16 withdrawalFeeBps);

function __OriginalTokenBridgeBaseUpgradable_init(address _endpoint, uint16 _remoteChainId, address _weth) internal onlyInitializing {
require(_weth != address(0), "OriginalTokenBridge: invalid WETH address");
Expand All @@ -40,7 +47,7 @@ contract OriginalTokenBridgeUpgradable is TokenBridgeBaseUpgradable {
weth = _weth;
}

function initialize(address _endpoint, uint16 _remoteChainId, address _weth) virtual external initializer {
function initialize(address _endpoint, uint16 _remoteChainId, address _weth) external virtual initializer {
__OriginalTokenBridgeBaseUpgradable_init(_endpoint, _remoteChainId, _weth);
}

Expand Down Expand Up @@ -75,6 +82,12 @@ contract OriginalTokenBridgeUpgradable is TokenBridgeBaseUpgradable {
return lzEndpoint.estimateFees(remoteChainId, address(this), payload, useZro, adapterParams);
}

function setWithdrawalFeeBps(uint16 _withdrawalFeeBps) external onlyOwner {
require(_withdrawalFeeBps < TOTAL_BPS, "OriginalTokenBridge: invalid withdrawal fee bps");
withdrawalFeeBps = _withdrawalFeeBps;
emit SetWithdrawalFeeBps(_withdrawalFeeBps);
}

/// @notice Bridges ERC20 to the remote chain
/// @dev Locks an ERC20 on the source chain and sends LZ message to the remote chain to mint a wrapped token
function bridge(address token, uint amountLD, address to, LzLib.CallParams calldata callParams, bytes memory adapterParams) external payable nonReentrant {
Expand All @@ -89,7 +102,6 @@ contract OriginalTokenBridgeUpgradable is TokenBridgeBaseUpgradable {
if (dust > 0) {
IERC20(token).safeTransfer(msg.sender, dust);
}

_bridge(token, amountWithoutDustLD, to, msg.value, callParams, adapterParams);
}

Expand All @@ -107,13 +119,20 @@ contract OriginalTokenBridgeUpgradable is TokenBridgeBaseUpgradable {
require(to != address(0), "OriginalTokenBridge: invalid to");
_checkAdapterParams(remoteChainId, PT_MINT, adapterParams);

uint withdrawalAmountLD = amountLD;
if (withdrawalFeeBps > 0) {
uint withdrawalFee = (amountLD * withdrawalFeeBps) / TOTAL_BPS;
Copy link
Member

Choose a reason for hiding this comment

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

Please check this flow with max and min values for uint

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@leonprou why is that needed?

Copy link
Member

Choose a reason for hiding this comment

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

The user sends a really big amount of tokens, when it gets multiplied by withdrawalFeeBps an integer overflow might happen.

withdrawalAmountLD -= withdrawalFee;
}

uint amountSD = _amountLDtoSD(token, amountLD);
require(amountSD > 0, "OriginalTokenBridge: invalid amount");
uint withdrawalAmountSD = _amountLDtoSD(token, withdrawalAmountLD);
require(amountSD > 0 && withdrawalAmountSD > 0, "OriginalTokenBridge: invalid amount");

totalValueLockedSD[token] += amountSD;
bytes memory payload = abi.encode(PT_MINT, token, to, amountSD);
totalValueLockedSD[token] += withdrawalAmountSD;
bytes memory payload = abi.encode(PT_MINT, token, to, withdrawalAmountSD);
_lzSend(remoteChainId, payload, callParams.refundAddress, callParams.zroPaymentAddress, adapterParams, nativeFee);
emit SendToken(token, msg.sender, to, amountLD);
emit SendToken(token, msg.sender, to, withdrawalAmountSD);
}

function withdrawFee(address token, address to, uint amountLD) public onlyOwner {
Expand Down
54 changes: 54 additions & 0 deletions test/OriginalTokenBridge.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,60 @@ describe("OriginalTokenBridge", () => {
})
})

describe("sets withdrawal fee", () => {
const withdrawalFeeBps = 100
it("reverts when called by non owner", async () => {
await expect(originalTokenBridge.connect(user).setWithdrawalFeeBps(withdrawalFeeBps)).to.be.revertedWith("Ownable: caller is not the owner")
})

it("reverts when withdrawal fee is greater than 10000", async () => {
const invalidWithdrawalFeeBps = 10001
await expect(originalTokenBridge.setWithdrawalFeeBps(invalidWithdrawalFeeBps)).to.be.revertedWith("OriginalTokenBridge: invalid withdrawal fee")
})

it("sets withdrawal fee", async () => {
MayankMittal1 marked this conversation as resolved.
Show resolved Hide resolved
await originalTokenBridge.setWithdrawalFeeBps(withdrawalFeeBps)
expect(await originalTokenBridge.withdrawalFeeBps()).to.be.eq(withdrawalFeeBps)
})
})

describe("bridges and withdraw fee", () => {
const withdrawalFeeBps = 100
let fee
let totalAmount
beforeEach(async () => {
await originalTokenBridge.setWithdrawalFeeBps(withdrawalFeeBps)
fee = (await originalTokenBridge.estimateBridgeFee(false, adapterParams)).nativeFee
totalAmount = amount + fee
await originalToken.connect(user).approve(originalTokenBridge.target, amount)
})

it("bridges ERC20 token and withdraws fees", async () => {
await originalTokenBridge.registerToken(originalToken.target, sharedDecimals)
await originalTokenBridge.connect(user).bridge(originalToken.target, amount, user.address, callParams, adapterParams, { value: fee })
const LDtoSD = await originalTokenBridge.LDtoSDConversionRate(originalToken.target)

const withdrawalFee = (amount * BigInt(withdrawalFeeBps)) / BigInt(10000) / LDtoSD
const withdrawalAmount = amount / LDtoSD - withdrawalFee

await originalTokenBridge.connect(owner).withdrawFee(originalToken.target, owner.address, withdrawalFee)
expect(await originalToken.balanceOf(owner.address)).to.be.eq(withdrawalFee)
Copy link
Member

Choose a reason for hiding this comment

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

Can you check the following:
when fee is 1%, while bridging X user receives 99%, the fees are 1%.
I see you only checked the fee part

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@leonprou I have updated the test which checks the total value locked for a token which is essentially the actual amount of token sent in the LZ message to be minted on the dest chain

expect(await originalTokenBridge.totalValueLockedSD(originalToken.target)).to.be.eq(withdrawalAmount)
})

it("bridges WETH and withdraws fees", async () => {
Copy link
Member

Choose a reason for hiding this comment

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

that's actually bridging native, no?

Copy link
Collaborator Author

@MayankMittal1 MayankMittal1 Nov 12, 2023

Choose a reason for hiding this comment

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

yes, this is what we will be using for FUSE token

await originalTokenBridge.registerToken(weth.target, wethSharedDecimals)
await originalTokenBridge.connect(user).bridgeNative(amount, user.address, callParams, adapterParams, { value: totalAmount })

const withdrawalFee = (amount * BigInt(withdrawalFeeBps)) / BigInt(10000)
const withdrawalAmount = amount - withdrawalFee

await originalTokenBridge.connect(owner).withdrawFee(weth.target, owner.address, withdrawalFee)
expect(await weth.balanceOf(owner.address)).to.be.eq(withdrawalFee)
expect(await originalTokenBridge.totalValueLockedSD(weth.target)).to.be.eq(withdrawalAmount)
})
})

describe("Upgrades Contract", () => {
beforeEach(async () => {
originalTokenBridgeV2Factory = await ethers.getContractFactory("OriginalTokenBridgeHarnessUpgradableV2")
Expand Down