Skip to content

Commit

Permalink
TokenBridge: Add active functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Aug 12, 2022
1 parent d6f2d19 commit da84f6e
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
11 changes: 11 additions & 0 deletions contracts/bridge/TokenBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,12 @@ contract TokenBridge is ManagerAccessControl {

uint256 public depositTimeLock;
uint256 public withdrawTimeLock;
bool public active;

constructor(uint256 _timeLock) {
depositTimeLock = _timeLock * 2;
withdrawTimeLock = _timeLock;
active = true;
}

/// @notice Register the ERC20 token
Expand Down Expand Up @@ -79,6 +81,13 @@ contract TokenBridge is ManagerAccessControl {
emit ChangeTimeLock(depositTimeLock);
}

event ChangeActive(bool);

function setActive(bool _value) public onlyManager {
active = _value;
emit ChangeActive(_value);
}

enum States {
INVALID,
OPEN,
Expand Down Expand Up @@ -137,6 +146,8 @@ contract TokenBridge is ManagerAccessControl {
address _withdrawAddress,
bytes32 _secretLock
) public onlyInvalidDepositBoxes(_boxID) onlyRegisteredToken(_tokenId) {
// Check if the exchange is activated.
require(active, "E004");
require(_withdrawAddress != address(0), "E003");

ERC20 token = tokens[_tokenId].token;
Expand Down
48 changes: 48 additions & 0 deletions test/bridge/TokenBridgeSwap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,54 @@ describe("Test Swap of TokenBridge", () => {
});
});

context("Test the stop function of the swap", async () => {
it("Create key by User", async () => {
const key_buffer = ContractUtils.createKey();
const lock_buffer = ContractUtils.sha256(key_buffer);
key = ContractUtils.BufferToString(key_buffer);
lock = ContractUtils.BufferToString(lock_buffer);
lock_box_id = ContractUtils.BufferToString(ContractUtils.createLockBoxID());
});

it("Inactive swap in EthNet", async () => {
await bridge_ethnet.connect(manager_signer).setActive(false);
assert.strictEqual(await bridge_ethnet.connect(manager_signer).active(), false);
});

it("Open the lock box in EthNet", async () => {
await token_ethnet.connect(user_eth_signer).approve(bridge_ethnet.address, swap_amount.value);
await expect(
bridge_ethnet
.connect(user_eth_signer)
.openDeposit(token_id_ethnet, lock_box_id, swap_amount.value, user_biz.address, lock)
).to.be.reverted;
});

it("Active swap in EthNet", async () => {
await bridge_ethnet.connect(manager_signer).setActive(true);
assert.strictEqual(await bridge_ethnet.connect(manager_signer).active(), true);
});

it("Inactive swap in BizNet", async () => {
await bridge_biznet.connect(manager_signer).setActive(false);
assert.strictEqual(await bridge_biznet.connect(manager_signer).active(), false);
});

it("Open the lock box in BizNet", async () => {
await token_biznet.connect(user_biz_signer).approve(bridge_biznet.address, swap_amount.value);
await expect(
bridge_biznet
.connect(user_biz_signer)
.openDeposit(token_id_biznet, lock_box_id, swap_amount.value, user_eth.address, lock)
).to.be.reverted;
});

it("Active swap in BizNet", async () => {
await bridge_biznet.connect(manager_signer).setActive(true);
assert.strictEqual(await bridge_biznet.connect(manager_signer).active(), true);
});
});

context("Expiry Deposit Lock Box", async () => {
const lockBox_expiry = ContractUtils.BufferToString(ContractUtils.createLockBoxID());

Expand Down

0 comments on commit da84f6e

Please sign in to comment.