Skip to content

Commit

Permalink
TokenBridge: Add ability to collect transaction fees for the network
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Aug 12, 2022
1 parent da84f6e commit 91b3964
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 12 deletions.
11 changes: 10 additions & 1 deletion contracts/bridge/TokenBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ contract TokenBridge is ManagerAccessControl {
bytes32 tokenId;
uint256 timeLock;
uint256 amount;
uint256 txFee;
address traderAddress;
address withdrawAddress;
bytes32 secretLock;
Expand Down Expand Up @@ -139,17 +140,22 @@ contract TokenBridge is ManagerAccessControl {
}

/// @notice Open the deposit lock box
/// @notice Declared payable to receive the fee as a native token
function openDeposit(
bytes32 _tokenId,
bytes32 _boxID,
uint256 _amount,
address _withdrawAddress,
bytes32 _secretLock
) public onlyInvalidDepositBoxes(_boxID) onlyRegisteredToken(_tokenId) {
) public payable onlyInvalidDepositBoxes(_boxID) onlyRegisteredToken(_tokenId) {
// Check if the exchange is activated.
require(active, "E004");
require(_withdrawAddress != address(0), "E003");

// Check if the fee has been sent.
// If it is not an appropriate fee, the exchange will not take place.
require(msg.value > 1000000000, "E003");

ERC20 token = tokens[_tokenId].token;

require(_amount <= token.allowance(msg.sender, address(this)), "E003");
Expand All @@ -160,6 +166,7 @@ contract TokenBridge is ManagerAccessControl {
tokenId: _tokenId,
timeLock: depositTimeLock,
amount: _amount,
txFee: msg.value,
traderAddress: msg.sender,
withdrawAddress: _withdrawAddress,
secretLock: _secretLock,
Expand Down Expand Up @@ -203,6 +210,7 @@ contract TokenBridge is ManagerAccessControl {
bytes32 tokenId,
uint256 timeLock,
uint256 amount,
uint256 txFee,
address traderAddress,
address withdrawAddress,
bytes32 secretLock,
Expand All @@ -216,6 +224,7 @@ contract TokenBridge is ManagerAccessControl {
box.tokenId,
box.timeLock,
box.amount,
box.txFee,
box.traderAddress,
box.withdrawAddress,
box.secretLock,
Expand Down
44 changes: 33 additions & 11 deletions test/bridge/TokenBridgeSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ describe("Test Swap of TokenBridge", () => {
const liquidity_amount = Amount.make(1000000, decimal);
const swap_amount = Amount.make(10000, decimal);
const time_lock = 60 * 60 * 24;
let txFee = Amount.make(1, 18);

let token_id_ethnet: string;
let token_id_biznet: string;
Expand Down Expand Up @@ -91,6 +92,9 @@ describe("Test Swap of TokenBridge", () => {
let old_bridge_ethnet_balance: BigNumber;
let old_bridge_biznet_balance: BigNumber;

// transaction fee (ETH)
txFee = Amount.make(0.01, 18);

before("Distribute the fund", async () => {
await token_ethnet.connect(admin_signer).transfer(user_eth.address, swap_amount.value);
});
Expand All @@ -115,7 +119,9 @@ describe("Test Swap of TokenBridge", () => {
expect(
await bridge_ethnet
.connect(user_eth_signer)
.openDeposit(token_id_ethnet, lock_box_id, swap_amount.value, user_biz.address, lock)
.openDeposit(token_id_ethnet, lock_box_id, swap_amount.value, user_biz.address, lock, {
value: txFee.value,
})
).to.emit(bridge_ethnet, "OpenDeposit");
});

Expand All @@ -124,9 +130,10 @@ describe("Test Swap of TokenBridge", () => {
assert.strictEqual(result[0].toString(), "1");
assert.strictEqual(result[1].toString(), token_id_ethnet);
assert.strictEqual(result[3].toString(), swap_amount.toString());
assert.strictEqual(result[4].toString(), user_eth.address);
assert.strictEqual(result[5].toString(), user_biz.address);
assert.strictEqual(result[6].toString(), lock);
assert.strictEqual(result[4].toString(), txFee.toString());
assert.strictEqual(result[5].toString(), user_eth.address);
assert.strictEqual(result[6].toString(), user_biz.address);
assert.strictEqual(result[7].toString(), lock);
});

it("Open the lock box in BizNet by Manager", async () => {
Expand Down Expand Up @@ -197,6 +204,9 @@ describe("Test Swap of TokenBridge", () => {
let old_bridge_ethnet_balance: BigNumber;
let old_bridge_biznet_balance: BigNumber;

// transaction fee (BOA)
txFee = Amount.make(300, 18);

before("Distribute the fund", async () => {
// await token_biznet.connect(admin_signer).transfer(user.address, swap_amount_token);
});
Expand All @@ -221,7 +231,9 @@ describe("Test Swap of TokenBridge", () => {
expect(
await bridge_biznet
.connect(user_biz_signer)
.openDeposit(token_id_biznet, lock_box_id, swap_amount.value, user_eth.address, lock)
.openDeposit(token_id_biznet, lock_box_id, swap_amount.value, user_eth.address, lock, {
value: txFee.value,
})
).to.emit(bridge_biznet, "OpenDeposit");
});

Expand All @@ -230,9 +242,10 @@ describe("Test Swap of TokenBridge", () => {
assert.strictEqual(result[0].toString(), "1");
assert.strictEqual(result[1].toString(), token_id_biznet);
assert.strictEqual(result[3].toString(), swap_amount.toString());
assert.strictEqual(result[4].toString(), user_biz.address);
assert.strictEqual(result[5].toString(), user_eth.address);
assert.strictEqual(result[6].toString(), lock);
assert.strictEqual(result[4].toString(), txFee.toString());
assert.strictEqual(result[5].toString(), user_biz.address);
assert.strictEqual(result[6].toString(), user_eth.address);
assert.strictEqual(result[7].toString(), lock);
});

it("Open the lock box in EthNet by Manager", async () => {
Expand Down Expand Up @@ -289,6 +302,9 @@ describe("Test Swap of TokenBridge", () => {
});

context("Test the stop function of the swap", async () => {
// transaction fee
txFee = Amount.make(300, 18);

it("Create key by User", async () => {
const key_buffer = ContractUtils.createKey();
const lock_buffer = ContractUtils.sha256(key_buffer);
Expand All @@ -307,7 +323,9 @@ describe("Test Swap of TokenBridge", () => {
await expect(
bridge_ethnet
.connect(user_eth_signer)
.openDeposit(token_id_ethnet, lock_box_id, swap_amount.value, user_biz.address, lock)
.openDeposit(token_id_ethnet, lock_box_id, swap_amount.value, user_biz.address, lock, {
value: txFee.value,
})
).to.be.reverted;
});

Expand All @@ -326,7 +344,9 @@ describe("Test Swap of TokenBridge", () => {
await expect(
bridge_biznet
.connect(user_biz_signer)
.openDeposit(token_id_biznet, lock_box_id, swap_amount.value, user_eth.address, lock)
.openDeposit(token_id_biznet, lock_box_id, swap_amount.value, user_eth.address, lock, {
value: txFee.value,
})
).to.be.reverted;
});

Expand Down Expand Up @@ -354,7 +374,9 @@ describe("Test Swap of TokenBridge", () => {
await token_ethnet.connect(user_eth_signer).approve(bridge_ethnet.address, swap_amount.value);
await bridge_ethnet
.connect(user_eth_signer)
.openDeposit(token_id_ethnet, lockBox_expiry, swap_amount.value, user_biz.address, lock);
.openDeposit(token_id_ethnet, lockBox_expiry, swap_amount.value, user_biz.address, lock, {
value: txFee.value,
});
});

it("No Expiry", async () => {
Expand Down

0 comments on commit 91b3964

Please sign in to comment.