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(bridge): Token Vault sendEther messages with processing fees are impossible to send #277

Merged
merged 12 commits into from
Nov 18, 2022
Merged
4 changes: 2 additions & 2 deletions packages/protocol/contracts/bridge/TokenVault.sol
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ contract TokenVault is EssentialContract {
to != address(0) && to != resolve(destChainId, "token_vault"),
"V:to"
);
require(msg.value > 0, "V:msgValue");
require(msg.value > processingFee, "V:msgValue");

IBridge.Message memory message;
message.destChainId = destChainId;
Expand All @@ -131,7 +131,7 @@ contract TokenVault is EssentialContract {

message.gasLimit = gasLimit;
message.processingFee = processingFee;
message.depositValue = msg.value;
message.depositValue = msg.value - processingFee;
dantaik marked this conversation as resolved.
Show resolved Hide resolved
message.refundAddress = refundAddress;
message.memo = memo;

Expand Down
16 changes: 16 additions & 0 deletions packages/protocol/contracts/test/thirdparty/TestMessageSender.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.9;

import "../../bridge/IBridge.sol";

contract TestMessageSender {

bytes32 signal = 0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab;

function sendMessage(
IBridge.Message calldata message
) external payable returns (bytes32) {
message;
return signal;
}
}
145 changes: 145 additions & 0 deletions packages/protocol/test/bridge/TokenVault.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,18 @@ describe("TokenVault", function () {

await tokenVault.init(tokenVaultAddressManager.address)

const network = await ethers.provider.getNetwork()

const TestMessageSenderFactory = await ethers.getContractFactory(
"TestMessageSender"
)

const testMessageSender = await TestMessageSenderFactory.deploy()

await tokenVaultAddressManager.setAddress(
`${network.chainId}.bridge`,
testMessageSender.address
)
return {
owner,
nonOwner,
Expand All @@ -62,4 +74,137 @@ describe("TokenVault", function () {
).to.be.revertedWith(ADDRESS_RESOLVER_DENIED)
})
})

describe("sendEther()", async () => {
it("throws when msg.value is 0", async () => {
const { owner, tokenVault } = await deployTokenVaultFixture()

const processingFee = 10

await expect(
tokenVault.sendEther(
167001,
owner.address,
10000,
processingFee,
owner.address,
""
)
).to.be.revertedWith("V:msgValue")
})

it("throws when msg.value - processing fee is 0", async () => {
const { owner, tokenVault } = await deployTokenVaultFixture()

const processingFee = 10

await expect(
tokenVault.sendEther(
167001,
owner.address,
10000,
processingFee,
owner.address,
"",
{
value: processingFee,
}
)
).to.be.revertedWith("V:msgValue")
})

it("throws when msg.value is < processingFee", async () => {
const { owner, tokenVault } = await deployTokenVaultFixture()

const processingFee = 10

await expect(
tokenVault.sendEther(
167001,
owner.address,
10000,
processingFee,
owner.address,
"",
{
value: processingFee - 1,
}
)
).to.be.revertedWith("V:msgValue")
})

it("throws when to is 0", async () => {
const { owner, tokenVault } = await deployTokenVaultFixture()

const processingFee = 10

await expect(
tokenVault.sendEther(
167001,
ethers.constants.AddressZero,
10000,
processingFee,
owner.address,
"",
{
value: processingFee - 1,
}
)
).to.be.revertedWith("V:to")
})

it("succeeds with processingFee", async () => {
const { owner, tokenVault } = await deployTokenVaultFixture()

const processingFee = 10
const depositValue = 1000
const destChainId = 167001

const testSignal =
"0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab"

await expect(
tokenVault.sendEther(
destChainId,
owner.address,
10000,
processingFee,
owner.address,
"",
{
value: depositValue,
}
)
)
.to.emit(tokenVault, "EtherSent")
.withArgs(owner.address, destChainId, depositValue, testSignal)
})

it("succeeds with 0 processingFee", async () => {
const { owner, tokenVault } = await deployTokenVaultFixture()

const processingFee = 0
const depositValue = 1000
const destChainId = 167001

const testSignal =
"0x3fd54831f488a22b28398de0c567a3b064b937f54f81739ae9bd545967f3abab"

await expect(
tokenVault.sendEther(
destChainId,
owner.address,
10000,
processingFee,
owner.address,
"",
{
value: depositValue,
}
)
)
.to.emit(tokenVault, "EtherSent")
.withArgs(owner.address, destChainId, depositValue, testSignal)
})
})
})