Skip to content

Commit fa7137f

Browse files
author
0xIryna
committed
updated remove dust logic to return dust to the sender
1 parent c2688f9 commit fa7137f

File tree

3 files changed

+30
-10
lines changed

3 files changed

+30
-10
lines changed

.solcover.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
module.exports = {
2-
skipFiles: ["interfaces", "test"]
2+
skipFiles: ["interfaces", "test", "wrappedTokens"]
33
};

contracts/OriginalTokenBridge.sol

+13-8
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,29 @@ contract OriginalTokenBridge is TokenBridgeBase {
7474
/// @dev Locks an ERC20 on the source chain and sends LZ message to the remote chain to mint a wrapped token
7575
function bridge(address token, uint amountLD, address to, LzLib.CallParams calldata callParams, bytes memory adapterParams) external payable nonReentrant {
7676
require(supportedTokens[token], "OriginalTokenBridge: token is not supported");
77-
amountLD = _removeDust(token, amountLD);
78-
77+
7978
// Supports tokens with transfer fee
8079
uint balanceBefore = IERC20(token).balanceOf(address(this));
8180
IERC20(token).safeTransferFrom(msg.sender, address(this), amountLD);
8281
uint balanceAfter = IERC20(token).balanceOf(address(this));
83-
amountLD = balanceAfter - balanceBefore;
82+
(uint amountWithoutDustLD, uint dust) = _removeDust(token, balanceAfter - balanceBefore);
83+
84+
// return dust to the sender
85+
if (dust > 0) {
86+
IERC20(token).safeTransfer(msg.sender, dust);
87+
}
8488

85-
_bridge(token, amountLD, to, msg.value, callParams, adapterParams);
89+
_bridge(token, amountWithoutDustLD, to, msg.value, callParams, adapterParams);
8690
}
8791

8892
/// @notice Bridges ETH to the remote chain
8993
/// @dev Locks WETH on the source chain and sends LZ message to the remote chain to mint a wrapped token
9094
function bridgeETH(uint amountLD, address to, LzLib.CallParams calldata callParams, bytes memory adapterParams) external payable nonReentrant {
9195
require(supportedTokens[weth], "OriginalTokenBridge: token is not supported");
9296
require(msg.value >= amountLD, "OriginalTokenBridge: not enough value sent");
93-
amountLD = _removeDust(weth, amountLD);
97+
(uint amountWithoutDustLD, ) = _removeDust(weth, amountLD);
9498
IWETH(weth).deposit{value: amountLD}();
95-
_bridge(weth, amountLD, to, msg.value - amountLD, callParams, adapterParams);
99+
_bridge(weth, amountLD, to, msg.value - amountWithoutDustLD, callParams, adapterParams);
96100
}
97101

98102
function _bridge(address token, uint amountLD, address to, uint nativeFee, LzLib.CallParams calldata callParams, bytes memory adapterParams) private {
@@ -153,8 +157,9 @@ contract OriginalTokenBridge is TokenBridgeBase {
153157
return amountLD / LDtoSDConversionRate[token];
154158
}
155159

156-
function _removeDust(address token, uint amountLD) internal view returns (uint) {
157-
return _amountSDtoLD(token, _amountLDtoSD(token, amountLD));
160+
function _removeDust(address token, uint amountLD) internal view returns (uint amountWithoutDustLD, uint dust) {
161+
dust = amountLD % LDtoSDConversionRate[token];
162+
amountWithoutDustLD = amountLD - dust;
158163
}
159164

160165
/// @dev Allows receiving ETH when calling WETH.withdraw()

test/OriginalTokenBridge.test.js

+16-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { expect } = require("chai")
22
const { ethers } = require("hardhat")
3-
const { utils, constants } = require("ethers")
3+
const { utils, constants, BigNumber } = require("ethers")
44

55
describe("OriginalTokenBridge", () => {
66
const originalTokenChainId = 0
@@ -146,6 +146,21 @@ describe("OriginalTokenBridge", () => {
146146
expect(await originalToken.balanceOf(originalTokenBridge.address)).to.be.eq(amount)
147147
expect(await originalToken.balanceOf(user.address)).to.be.eq(0)
148148
})
149+
150+
it("locks tokens in the contract and returns dust to the sender", async () => {
151+
const dust = BigNumber.from("12345")
152+
const amountWithDust = amount.add(dust)
153+
154+
await originalTokenBridge.registerToken(originalToken.address, sharedDecimals)
155+
await originalToken.mint(user.address, dust)
156+
await originalToken.connect(user).approve(originalTokenBridge.address, amountWithDust)
157+
await originalTokenBridge.connect(user).bridge(originalToken.address, amountWithDust, user.address, callParams, adapterParams, { value: fee })
158+
const LDtoSD = await originalTokenBridge.LDtoSDConversionRate(originalToken.address)
159+
160+
expect(await originalTokenBridge.totalValueLockedSD(originalToken.address)).to.be.eq(amount.div(LDtoSD))
161+
expect(await originalToken.balanceOf(originalTokenBridge.address)).to.be.eq(amount)
162+
expect(await originalToken.balanceOf(user.address)).to.be.eq(dust)
163+
})
149164
})
150165

151166
describe("bridgeETH", () => {

0 commit comments

Comments
 (0)