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

Bridge v3 #7

Draft
wants to merge 8 commits into
base: bridge-v2
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 48 additions & 20 deletions Bridge/Bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import "./Ownable.sol";
import "./ERC20Burnable.sol";
import "./SafeERC20.sol";
import "./EthTokenReciever.sol";
import "./IUSDT.sol";

/**
* Provides functionality of the HASHI bridge
Expand Down Expand Up @@ -43,6 +44,8 @@ contract Bridge is EthTokenReciever {
/** EVM netowrk ID */
bytes32 public immutable _networkId;

IUSDT public immutable _addressUSDT;

event Withdrawal(bytes32 txHash);
event Deposit(
bytes32 destination,
Expand All @@ -68,6 +71,7 @@ contract Bridge is EthTokenReciever {
address[] memory erc20Addresses,
address addressVAL,
address addressXOR,
address addressUSDT,
bytes32 networkId
) {
require(
Expand Down Expand Up @@ -97,6 +101,7 @@ contract Bridge is EthTokenReciever {
for (uint256 i; i < erc20TokensCount; i++) {
acceptedEthTokens[erc20Addresses[i]] = true;
}
_addressUSDT = IUSDT(addressUSDT);
}

modifier shouldBeInitialized() {
Expand Down Expand Up @@ -167,7 +172,8 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(
abi.encode(
"addToken",
address(this),
newToken,
ticker,
Expand Down Expand Up @@ -205,7 +211,7 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(
abi.encode(
"prepareMigration",
address(this),
salt,
Expand Down Expand Up @@ -245,7 +251,8 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Transition from encode to encodePacked is not that necessary but might be taken as it may only affect the order of the params for the statement we sign but not the signature itself
Just make sure that it works fine when passing params to recoverAddress, now it looks okay, but it will not be superfluous to double check it

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's needed to prevent signature collision

abi.encode(
"migrate",
address(this),
newContractAddress,
salt,
Expand Down Expand Up @@ -273,6 +280,10 @@ contract Bridge is EthTokenReciever {
token.balanceOf(address(this))
);
}
_addressUSDT.transfer(
newContractAddress,
_addressUSDT.balanceOf(address(this))
);
EthTokenReciever(newContractAddress).receivePayment{
value: address(this).balance
}();
Expand Down Expand Up @@ -306,7 +317,8 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(
abi.encode(
"addAsset",
address(this),
name,
symbol,
Expand Down Expand Up @@ -381,13 +393,23 @@ contract Bridge is EthTokenReciever {
acceptedEthTokens[tokenAddress],
"The Token is not accepted for transfer to sidechain"
);
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(msg.sender, address(this), amount);
uint256 balanceAfter = token.balanceOf(address(this));
require(
balanceAfter - balanceBefore >= amount,
"Not enough tokens transferred"
);
if (tokenAddress == address(_addressUSDT)) {
uint256 balanceBefore = _addressUSDT.balanceOf(address(this));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

balanceOf in usdt and other ERC20 are the same, so i guess there is no need to split using if

_addressUSDT.transferFrom(msg.sender, address(this), amount);
uint256 balanceAfter = _addressUSDT.balanceOf(address(this));
require(
balanceAfter - balanceBefore >= amount,
"Not enough tokens transferred"
);
} else {
uint256 balanceBefore = token.balanceOf(address(this));
token.safeTransferFrom(msg.sender, address(this), amount);
uint256 balanceAfter = token.balanceOf(address(this));
require(
balanceAfter - balanceBefore >= amount,
"Not enough tokens transferred"
);
}
}
emit Deposit(to, amount, tokenAddress, sidechainAssetId);
}
Expand All @@ -412,9 +434,9 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(
address(this),
abi.encode(
"addPeer",
address(this),
newPeerAddress,
txHash,
_networkId
Expand Down Expand Up @@ -453,9 +475,9 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(
address(this),
abi.encode(
"removePeer",
address(this),
peerAddress,
txHash,
_networkId
Expand Down Expand Up @@ -499,7 +521,8 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(
abi.encode(
"transfer",
address(this),
tokenAddress,
amount,
Expand All @@ -521,9 +544,13 @@ contract Bridge is EthTokenReciever {
// untrusted transfer, relies on provided cryptographic proof
to.transfer(amount);
} else {
IERC20 coin = IERC20(tokenAddress);
// untrusted call, relies on provided cryptographic proof
coin.safeTransfer(to, amount);
if (tokenAddress == address(_addressUSDT)) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

have you tested safeERC20 with USDT token? it's supposed to work correctly

_addressUSDT.transfer(to, amount);
} else {
IERC20 coin = IERC20(tokenAddress);
// untrusted call, relies on provided cryptographic proof
coin.safeTransfer(to, amount);
}
}
emit Withdrawal(txHash);
}
Expand Down Expand Up @@ -557,7 +584,8 @@ contract Bridge is EthTokenReciever {
require(
checkSignatures(
keccak256(
abi.encodePacked(
abi.encode(
"transferOwned",
address(this),
sidechainAssetId,
amount,
Expand Down
32 changes: 32 additions & 0 deletions Bridge/IUSDT.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// SPDX-License-Identifier: Apache 2.0

pragma solidity =0.8.17;

// Taken from https://etherscan.io/address/0xdac17f958d2ee523a2206206994597c13d831ec7#code

/**
* @title ERC20Basic
* @dev Simpler version of ERC20 interface
* @dev see https://github.com/ethereum/EIPs/issues/20
*/
interface IUSDT {
function totalSupply() external returns (uint);

function balanceOf(address who) external returns (uint);

function transfer(address to, uint value) external;

event Transfer(address indexed from, address indexed to, uint value);

function allowance(address owner, address spender) external returns (uint);

function transferFrom(
address from,
address to,
uint value
) external;

function approve(address spender, uint value) external;

event Approval(address indexed owner, address indexed spender, uint value);
}
1 change: 1 addition & 0 deletions deploy-data/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ interface SidechainAsset {
interface DeployConfig {
xorAddress: string;
valAddress: string;
usdtAddress: string;
erc20Addresses: string[];
sidechainAssets: SidechainAsset[];
peers: string[];
Expand Down
1 change: 1 addition & 0 deletions deploy-data/ganache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DeployConfig from "./config";
const config: DeployConfig = {
"xorAddress": "0x40fd72257597aa14c7231a7b1aaa29fce868f677",
"valAddress": "0xe88f8313e61a97cec1871ee37fbbe2a8bf3ed1e4",
usdtAddress: undefined,
"sidechainAssets": [
{
"address": "0x519C1001D550C0a1DaE7d1fC220f7d14c2A521BB",
Expand Down
1 change: 1 addition & 0 deletions deploy-data/geth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import DeployConfig from "./config";
const config: DeployConfig = {
xorAddress: "0x0",
valAddress: "0x0",
usdtAddress: undefined,
erc20Addresses: [
"0x0"
],
Expand Down
1 change: 1 addition & 0 deletions deploy-data/mainnet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DeployConfig from "./config";
const config: DeployConfig = {
"xorAddress": "0x40fd72257597aa14c7231a7b1aaa29fce868f677",
"valAddress": "0xe88f8313e61a97cec1871ee37fbbe2a8bf3ed1e4",
"usdtAddress": "0xdAC17F958D2ee523a2206206994597C13D831ec7",
"sidechainAssets": [
{
"address": "0x519C1001D550C0a1DaE7d1fC220f7d14c2A521BB",
Expand Down
1 change: 1 addition & 0 deletions deploy-data/rinkeby.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DeployConfig from "./config";
const config: DeployConfig = {
"xorAddress": "0x83ba842e5e26a4eda2466891221187aabbc33692",
"valAddress": "0x7fcb82ab5a4762f0f18287ece64d4ec74b6071c0",
usdtAddress: undefined,
"sidechainAssets": [
{
"address": "0x4c8886E4B8d4bb146cbC03B851307FDDf869fe8B",
Expand Down
1 change: 1 addition & 0 deletions deploy-data/sepolia.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import DeployConfig from "./config";
const config: DeployConfig = {
"xorAddress": "0x9826ecfcd937c4518e1c42b3703c7cb908b61197",
"valAddress": "0x88ee18defc56d78417b0d331e794ef75799ca6d1",
usdtAddress: undefined,
"sidechainAssets": [
{
"address": "0x37F93FD8A3c606B3a8cceB84e8613238707137ee",
Expand Down
19 changes: 19 additions & 0 deletions deploy-data/sepolia2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@

import DeployConfig from "./config";

const config: DeployConfig = {
"xorAddress": "0x9826ecfcd937c4518e1c42b3703c7cb908b61197",
"valAddress": "0x88ee18defc56d78417b0d331e794ef75799ca6d1",
usdtAddress: undefined,
"sidechainAssets": [
],
"erc20Addresses": [
],
"peers": [
"0x981bccf6d8593dc49c22cf744a59afa5a2b23118",
"0x90dca3c91c10e802093318123ad8bb8809061765",
"0x457d710255184dbf63c019ab50f65743c6cb072f"
]
};

export default config;
22 changes: 19 additions & 3 deletions deploy/01-bridge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import { default as gethConfig } from "../deploy-data/geth"
import { default as rinkebyConfig } from "../deploy-data/rinkeby"
import { default as mainnetConfig } from "../deploy-data/mainnet"
import { default as sepoliaConfig } from "../deploy-data/sepolia"
import { default as sepolia2Config } from "../deploy-data/sepolia2"


module.exports = async ({
deployments,
getUnnamedAccounts,
network
network,
run
}: HardhatRuntimeEnvironment) => {
let [deployer] = await getUnnamedAccounts();

Expand All @@ -28,10 +30,14 @@ module.exports = async ({
case "sepolia":
config = sepoliaConfig;
break;
case "sepolia2":
config = sepolia2Config;
break;
case "geth":
config = gethConfig;
break;
default:
console.log("No config for network", network.name);
config = ganacheConfig;
break;
}
Expand All @@ -40,15 +46,25 @@ module.exports = async ({

let tokenAddresses = config.sidechainAssets.map((val) => val.address);
let assetIds = config.sidechainAssets.map((val) => val.asset_id);
await deployments.deploy("Bridge", {
let bridge = await deployments.deploy("Bridge", {
from: deployer,
log: true,
autoMine: true,
args: [
config.peers,
tokenAddresses, assetIds, config.erc20Addresses,
config.valAddress, config.xorAddress,
config.valAddress, config.xorAddress, config.usdtAddress,
'0x0000000000000000000000000000000000000000000000000000000000000000'
]
});

await run("verify:verify", {
address: bridge.address,
constructorArguments: [
config.peers,
tokenAddresses, assetIds, config.erc20Addresses,
config.valAddress, config.xorAddress,
'0x0000000000000000000000000000000000000000000000000000000000000000'
],
});
};
31 changes: 30 additions & 1 deletion hardhat.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { resolve } from "path";
import "solidity-coverage"

import * as gen_config from "./tasks/gen-config";
import * as send from "./tasks/send";

dotenv({ path: resolve(__dirname, ".env") });

Expand Down Expand Up @@ -57,6 +58,11 @@ const config: HardhatUserConfig = {
url: 'https://rpc.sepolia.org',
accounts: [sepoliaPrivateKey],
},
sepolia2: {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why? they are the same (sepolia and sepolia2)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To have deployment data for test and stage environment

chainId: 11155111,
url: 'https://rpc.sepolia.org',
accounts: [sepoliaPrivateKey],
},
mainnet: {
chainId: 1,
url: `https://mainnet.infura.io/v3/${infuraKey}`,
Expand Down Expand Up @@ -87,7 +93,25 @@ const config: HardhatUserConfig = {
timeout: 60000
},
etherscan: {
apiKey: etherscanKey
apiKey: etherscanKey,
customChains: [
{
network: "sepolia",

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 exactly the same chains

chainId: 11155111,
urls: {
apiURL: "https://api-sepolia.etherscan.io/api",
browserURL: "https://sepolia.etherscan.io"
}
},
{
network: "sepolia",
chainId: 11155111,
urls: {
apiURL: "https://api-sepolia.etherscan.io/api",
browserURL: "https://sepolia.etherscan.io"
}
}
]
}
};

Expand All @@ -97,4 +121,9 @@ task("gen-config", "Generate config file for given network")
.addParam("peers")
.addParam("output").setAction(gen_config.main);

task("send", "Generate config file for given network")
.addParam("to")
.addParam("amount")
.addParam("token", "Token address", undefined, undefined, true)
.setAction(send.main);
export default config;
Loading