Skip to content

Commit

Permalink
Improve to use encrypted environment variables
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelKim20 committed Aug 12, 2024
1 parent e3920db commit 3b3fdc3
Show file tree
Hide file tree
Showing 32 changed files with 185 additions and 271 deletions.
10 changes: 0 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,15 +1,5 @@
# DMS Bridge

Forked from Repository https://github.com/gnosis/MultiSigWallet

The following features have been changed
- Change the version of the smart contract to 0.8.2.
- Change to hardhat instead of truffle

The following features have been added
- Implement a feature that provides the address of the owner's multi-sig wallet contracts
- The interface and the class were separated

## Install NodeJS

https://nodejs.org/en/download
Expand Down
1 change: 1 addition & 0 deletions packages/contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ web_modules/
.yarn-integrity

# dotenv environment variable files
*.env
.env
.env.development.local
.env.test.local
Expand Down
42 changes: 1 addition & 41 deletions packages/contracts/README.md
Original file line number Diff line number Diff line change
@@ -1,41 +1 @@
# Multi-Signature Wallet

---

Forked from Repository https://github.com/gnosis/MultiSigWallet

The following features have been changed

- Change the version of the smart contract to 0.8.2.
- Change to hardhat instead of truffle

The following features have been added

- Implement a feature that provides the address of the owner's multi-sig wallet contracts
- The interface and the class were separated

## Install NodeJS

https://nodejs.org/en/download

## Install yarn

```shell
npm install -g yarn
```

## Install Project

```shell
git clone https://github.com/bosagora/MultiSigWallet.git
cd MultiSigWallet
yarn install
```

## Test Project

```shell
cd packages/contracts
cp -r env/.env.sample env/.env
yarn run test
```
# acc-bridge-contracts-v2
56 changes: 40 additions & 16 deletions packages/contracts/contracts/bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
_;
}

function initialize(address _validatorAddress, address _feeAccount) external initializer {
function initialize(address _validatorAddress, address _protocolFeeAccount) external initializer {
__UUPSUpgradeable_init();
__Ownable_init_unchained();

feeAccount = _feeAccount;
protocolFeeAccount = _protocolFeeAccount;
validatorContract = IBridgeValidator(_validatorAddress);
}

Expand All @@ -78,7 +78,7 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
address(0x0),
TokenStatus.Registered,
true,
5e18
BridgeLib.NATIVE_DEFAULT_PROTOCOL_FEE
);
emit TokenRegistered(_tokenId, _tokenAddress);
} else {
Expand All @@ -87,7 +87,13 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
bytes32 tokenId = BridgeLib.getTokenId(token.name(), token.symbol());
require(tokenId == _tokenId);

tokenInfos[_tokenId] = TokenInfo(token, _tokenAddress, TokenStatus.Registered, false, 5e18);
tokenInfos[_tokenId] = TokenInfo(
token,
_tokenAddress,
TokenStatus.Registered,
false,
BridgeLib.TOKEN_DEFAULT_PROTOCOL_FEE
);
emit TokenRegistered(_tokenId, _tokenAddress);
}
}
Expand All @@ -104,12 +110,14 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
require(tokenInfos[_tokenId].status == TokenStatus.Registered, "1713");

if (tokenInfos[_tokenId].native) {
require(msg.value > tokenInfos[_tokenId].fee, "1031");

DepositData memory data = DepositData({ tokenId: _tokenId, account: msg.sender, amount: msg.value });
deposits[_depositId] = data;
emit BridgeDeposited(_tokenId, _depositId, data.account, data.amount, 0);
} else {
require(_amount % 1 gwei == 0, "1030");
require(_amount > tokenInfos[_tokenId].fee * 2, "1031");
require(_amount > tokenInfos[_tokenId].fee, "1031");

BIP20DelegatedTransfer token = tokenInfos[_tokenId].token;
if (token.delegatedTransfer(_account, address(this), _amount, _expiry, _signature)) {
Expand All @@ -130,6 +138,7 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
require(tokenInfos[_tokenId].status == TokenStatus.Registered, "1713");

if (tokenInfos[_tokenId].native) {
require(_amount > tokenInfos[_tokenId].fee, "1031");
if (withdraws[_withdrawId].account == address(0x0)) {
WithdrawData memory data = WithdrawData({
tokenId: _tokenId,
Expand All @@ -149,14 +158,14 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
uint256 withdrawalAmount = _amount - tokenInfos[_tokenId].fee;
if (address(this).balance >= withdraws[_withdrawId].amount) {
payable(_account).transfer(withdrawalAmount);
payable(feeAccount).transfer(tokenInfos[_tokenId].fee);
payable(protocolFeeAccount).transfer(tokenInfos[_tokenId].fee);
withdraws[_withdrawId].executed = true;
emit BridgeWithdrawn(_tokenId, _withdrawId, _account, withdrawalAmount, 0);
}
}
} else {
require(_amount % 1 gwei == 0, "1030");
require(_amount > tokenInfos[_tokenId].fee * 2, "1031");
require(_amount > tokenInfos[_tokenId].fee, "1031");
BIP20DelegatedTransfer token = tokenInfos[_tokenId].token;
if (withdraws[_withdrawId].account == address(0x0)) {
WithdrawData memory data = WithdrawData({
Expand All @@ -177,7 +186,7 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
uint256 withdrawalAmount = _amount - tokenInfos[_tokenId].fee;
if (token.balanceOf(address(this)) >= withdraws[_withdrawId].amount) {
token.transfer(_account, withdrawalAmount);
token.transfer(feeAccount, tokenInfos[_tokenId].fee);
token.transfer(protocolFeeAccount, tokenInfos[_tokenId].fee);
withdraws[_withdrawId].executed = true;
emit BridgeWithdrawn(_tokenId, _withdrawId, _account, withdrawalAmount, 0);
}
Expand All @@ -197,15 +206,15 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
if (tokenInfos[tokenId].native) {
if (address(this).balance >= withdraws[_withdrawId].amount) {
payable(withdraws[_withdrawId].account).transfer(withdrawalAmount);
payable(feeAccount).transfer(tokenInfos[tokenId].fee);
payable(protocolFeeAccount).transfer(tokenInfos[tokenId].fee);
withdraws[_withdrawId].executed = true;
emit BridgeWithdrawn(tokenId, _withdrawId, withdraws[_withdrawId].account, withdrawalAmount, 0);
}
} else {
BIP20DelegatedTransfer token = tokenInfos[tokenId].token;
if (token.balanceOf(address(this)) >= withdraws[_withdrawId].amount) {
token.transfer(withdraws[_withdrawId].account, withdrawalAmount);
token.transfer(feeAccount, tokenInfos[tokenId].fee);
token.transfer(protocolFeeAccount, tokenInfos[tokenId].fee);
withdraws[_withdrawId].executed = true;
emit BridgeWithdrawn(tokenId, _withdrawId, withdraws[_withdrawId].account, withdrawalAmount, 0);
}
Expand Down Expand Up @@ -244,16 +253,31 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
return withdraws[_withdrawId];
}

function getFee(bytes32 _tokenId) external view override returns (uint256) {
function getProtocolFee(bytes32 _tokenId) external view override returns (uint256) {
return tokenInfos[_tokenId].fee;
}

function changeFee(bytes32 _tokenId, uint256 _fee) external override {
function changeProtocolFee(bytes32 _tokenId, uint256 _fee) external override {
if (_tokenId == bytes32(0x00)) {
require(_fee <= BridgeLib.NATIVE_MAX_PROTOCOL_FEE, "1714");
} else {
require(_fee <= BridgeLib.TOKEN_MAX_PROTOCOL_FEE, "1714");
}
require(tokenInfos[_tokenId].status == TokenStatus.Registered, "1713");
require(_msgSender() == owner(), "1050");
tokenInfos[_tokenId].fee = _fee;
}

function getProtocolFeeAccount() external view returns (address) {
return protocolFeeAccount;
}

function changeProtocolFeeAccount(address _protocolFeeAccount) external {
require(_msgSender() == protocolFeeAccount, "1050");

protocolFeeAccount = _protocolFeeAccount;
}

/// @notice 브리지를 위한 전체 유동성 자금을 조회합니다.
function getTotalLiquidity(bytes32 _tokenId) external view override returns (uint256) {
require(tokenInfos[_tokenId].status == TokenStatus.Registered, "1713");
Expand Down Expand Up @@ -295,16 +319,16 @@ contract Bridge is BridgeStorage, Initializable, OwnableUpgradeable, UUPSUpgrade
require(tokenInfos[_tokenId].status == TokenStatus.Registered, "1713");

if (tokenInfos[_tokenId].native) {
require(liquidity[_tokenId][_msgSender()] > _amount, "1514");
require(address(this).balance > _amount, "1511");
require(liquidity[_tokenId][_msgSender()] >= _amount, "1514");
require(address(this).balance >= _amount, "1511");

payable(_msgSender()).transfer(_amount);
liquidity[_tokenId][_msgSender()] -= _amount;
emit WithdrawnLiquidity(_tokenId, _msgSender(), _amount, liquidity[_tokenId][_msgSender()]);
} else {
BIP20DelegatedTransfer token = tokenInfos[_tokenId].token;
require(liquidity[_tokenId][_msgSender()] > _amount, "1514");
require(token.balanceOf(address(this)) > _amount, "1511");
require(liquidity[_tokenId][_msgSender()] >= _amount, "1514");
require(token.balanceOf(address(this)) >= _amount, "1511");
require(_amount % 1 gwei == 0, "1030");

token.transfer(_msgSender(), _amount);
Expand Down
2 changes: 1 addition & 1 deletion packages/contracts/contracts/bridge/BridgeStorage.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ contract BridgeStorage {
mapping(bytes32 => IBridge.WithdrawData) internal withdraws;
mapping(bytes32 => mapping(address => bool)) internal confirmations;
mapping(bytes32 => mapping(address => uint256)) internal liquidity;
address internal feeAccount;
address internal protocolFeeAccount;

IBridgeValidator internal validatorContract;
}
4 changes: 2 additions & 2 deletions packages/contracts/contracts/interfaces/IBridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ interface IBridge {

function getWithdrawInfo(bytes32 _withdrawId) external view returns (WithdrawData memory);

function getFee(bytes32 _tokenId) external view returns (uint256);
function getProtocolFee(bytes32 _tokenId) external view returns (uint256);

function changeFee(bytes32 _tokenId, uint256 _fee) external;
function changeProtocolFee(bytes32 _tokenId, uint256 _fee) external;

function getTotalLiquidity(bytes32 _tokenId) external view returns (uint256);
}
5 changes: 5 additions & 0 deletions packages/contracts/contracts/lib/BridgeLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
pragma solidity ^0.8.2;

library BridgeLib {
uint256 public constant TOKEN_MAX_PROTOCOL_FEE = 5e18;
uint256 public constant TOKEN_DEFAULT_PROTOCOL_FEE = 1e17;
uint256 public constant NATIVE_MAX_PROTOCOL_FEE = 5e18;
uint256 public constant NATIVE_DEFAULT_PROTOCOL_FEE = 1e17;

function zeroGWEI(uint256 value) internal pure returns (uint256) {
return (value / 1 gwei) * 1 gwei;
}
Expand Down
5 changes: 4 additions & 1 deletion packages/contracts/contracts/token/TestLYT.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ contract TestLYT is BIP20DelegatedTransfer {
/*
* Public functions
*/
constructor(address owner_) BIP20DelegatedTransfer("Loyalty Coin", "LYT") {
constructor(
address owner_,
address feeAccount_
) BIP20DelegatedTransfer("Loyalty Coin", "LYT", owner_, feeAccount_) {
_mint(owner_, 1e10 * 1e18);
}

Expand Down
4 changes: 2 additions & 2 deletions packages/contracts/env/.env.sample
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
MAIN_NET_URL=https://mainnet.bosagora.org
TEST_NET_URL=https://testnet.bosagora.org

# 0x6F8CF905906dDe9E440F0DF5B26146cf1f195F12
DEPLOYER=0x0d451ab5bd459f59fda0cf8018cfbfa4df5c454c5c25a844b70a5afcaf246c98
WALLET_ENV=wallet/.development.wallet.env.enc
WALLET_SECRET=95TZDgJcYX38QGUwyTbX

REPORT_GAS=true
71 changes: 32 additions & 39 deletions packages/contracts/hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,29 +8,46 @@ import "solidity-docgen";

import * as dotenv from "dotenv";
import { Wallet } from "ethers";
import { HardhatAccount } from "./src/HardhatAccount";

dotenv.config({ path: "env/.env" });

// tslint:disable-next-line:no-var-requires
const secureEnv = require("secure-env");
import extend from "extend";

import { HardhatAccount } from "./src/HardhatAccount";

function getAccounts() {
if (HardhatAccount.keys.length !== 0) return HardhatAccount.keys;
console.log(`Wallet file name: ${process.env.WALLET_ENV}`);
process.env = extend(
true,
process.env,
secureEnv({ path: process.env.WALLET_ENV, secret: process.env.WALLET_SECRET })
);

const accounts: string[] = [];
const reg_bytes64: RegExp = /^(0x)[0-9a-f]{64}$/i;
if (
process.env.DEPLOYER !== undefined &&
process.env.DEPLOYER.trim() !== "" &&
reg_bytes64.test(process.env.DEPLOYER)
process.env.DEPLOYER_SIDE_CHAIN !== undefined &&
process.env.DEPLOYER_SIDE_CHAIN.trim() !== "" &&
reg_bytes64.test(process.env.DEPLOYER_SIDE_CHAIN)
) {
accounts.push(process.env.DEPLOYER);
accounts.push(process.env.DEPLOYER_SIDE_CHAIN);
} else {
process.env.DEPLOYER = Wallet.createRandom().privateKey;
accounts.push(process.env.DEPLOYER);
process.env.DEPLOYER_SIDE_CHAIN = Wallet.createRandom().privateKey;
accounts.push(process.env.DEPLOYER_SIDE_CHAIN);
}

if (process.env.FEE !== undefined && process.env.FEE.trim() !== "" && reg_bytes64.test(process.env.FEE)) {
accounts.push(process.env.FEE);
if (
process.env.PROTOCOL_FEE !== undefined &&
process.env.PROTOCOL_FEE.trim() !== "" &&
reg_bytes64.test(process.env.PROTOCOL_FEE)
) {
accounts.push(process.env.PROTOCOL_FEE);
} else {
process.env.FEE = Wallet.createRandom().privateKey;
accounts.push(process.env.FEE);
process.env.PROTOCOL_FEE = Wallet.createRandom().privateKey;
accounts.push(process.env.PROTOCOL_FEE);
}

if (
Expand Down Expand Up @@ -99,39 +116,15 @@ function getAccounts() {
accounts.push(process.env.BRIDGE_VALIDATOR3);
}

if (
process.env.BRIDGE_VALIDATOR4 !== undefined &&
process.env.BRIDGE_VALIDATOR4.trim() !== "" &&
reg_bytes64.test(process.env.BRIDGE_VALIDATOR4)
) {
accounts.push(process.env.BRIDGE_VALIDATOR4);
} else {
process.env.BRIDGE_VALIDATOR4 = Wallet.createRandom().privateKey;
accounts.push(process.env.BRIDGE_VALIDATOR4);
}

if (
process.env.BRIDGE_VALIDATOR5 !== undefined &&
process.env.BRIDGE_VALIDATOR5.trim() !== "" &&
reg_bytes64.test(process.env.BRIDGE_VALIDATOR5)
) {
accounts.push(process.env.BRIDGE_VALIDATOR5);
} else {
process.env.BRIDGE_VALIDATOR5 = Wallet.createRandom().privateKey;
accounts.push(process.env.BRIDGE_VALIDATOR5);
}

while (accounts.length < 100) {
while (accounts.length < 70) {
accounts.push(Wallet.createRandom().privateKey);
}

if (HardhatAccount.keys.length === 0) {
for (const account of accounts) {
HardhatAccount.keys.push(account);
}
for (const account of accounts) {
HardhatAccount.keys.push(account);
}

return accounts;
return HardhatAccount.keys;
}

function getTestAccounts() {
Expand Down
Loading

0 comments on commit 3b3fdc3

Please sign in to comment.