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

Contracts V2 #1300

Open
wants to merge 23 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 20 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
2 changes: 1 addition & 1 deletion contracts/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ coverage/
out/
cache/
broadcast/
types/
/types/
tsconfig.tsbuildinfo
lcov.info
tenderly.yaml
Expand Down
4 changes: 4 additions & 0 deletions contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,7 @@ via_ir = true

[profile.production.etherscan]
mainnet = { key = "${ETHERSCAN_API_KEY}" }

[fmt]
number_underscore = "thousands"
line_length = 89
46 changes: 23 additions & 23 deletions contracts/scripts/DeployLocal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ import {Gateway} from "../src/Gateway.sol";
import {MockGatewayV2} from "../test/mocks/MockGatewayV2.sol";
import {Agent} from "../src/Agent.sol";
import {AgentExecutor} from "../src/AgentExecutor.sol";
import {Constants} from "../src/Constants.sol";
import {ChannelID, ParaID, OperatingMode} from "../src/Types.sol";
import {Initializer} from "../src/Initializer.sol";
import {SafeNativeTransfer} from "../src/utils/SafeTransfer.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {UD60x18, ud60x18} from "prb/math/src/UD60x18.sol";
Expand Down Expand Up @@ -49,26 +51,21 @@ contract DeployLocal is Script {
uint256 randaoCommitDelay = vm.envUint("RANDAO_COMMIT_DELAY");
uint256 randaoCommitExpiration = vm.envUint("RANDAO_COMMIT_EXP");
uint256 minimumSignatures = vm.envUint("MINIMUM_REQUIRED_SIGNATURES");
BeefyClient beefyClient =
new BeefyClient(randaoCommitDelay, randaoCommitExpiration, minimumSignatures, startBlock, current, next);

ParaID bridgeHubParaID = ParaID.wrap(uint32(vm.envUint("BRIDGE_HUB_PARAID")));
bytes32 bridgeHubAgentID = vm.envBytes32("BRIDGE_HUB_AGENT_ID");
ParaID assetHubParaID = ParaID.wrap(uint32(vm.envUint("ASSET_HUB_PARAID")));
bytes32 assetHubAgentID = vm.envBytes32("ASSET_HUB_AGENT_ID");
BeefyClient beefyClient = new BeefyClient(
randaoCommitDelay,
randaoCommitExpiration,
minimumSignatures,
startBlock,
current,
next
);

uint8 foreignTokenDecimals = uint8(vm.envUint("FOREIGN_TOKEN_DECIMALS"));
uint128 maxDestinationFee = uint128(vm.envUint("RESERVE_TRANSFER_MAX_DESTINATION_FEE"));
uint128 maxDestinationFee =
uint128(vm.envUint("RESERVE_TRANSFER_MAX_DESTINATION_FEE"));

AgentExecutor executor = new AgentExecutor();
Gateway gatewayLogic = new Gateway(
address(beefyClient),
address(executor),
bridgeHubParaID,
bridgeHubAgentID,
foreignTokenDecimals,
maxDestinationFee
);
Gateway gatewayLogic = new Gateway(address(beefyClient), address(executor));

bool rejectOutboundMessages = vm.envBool("REJECT_OUTBOUND_MESSAGES");
OperatingMode defaultOperatingMode;
Expand All @@ -78,20 +75,21 @@ contract DeployLocal is Script {
defaultOperatingMode = OperatingMode.Normal;
}

Gateway.Config memory config = Gateway.Config({
Initializer.Config memory config = Initializer.Config({
mode: defaultOperatingMode,
deliveryCost: uint128(vm.envUint("DELIVERY_COST")),
registerTokenFee: uint128(vm.envUint("REGISTER_TOKEN_FEE")),
assetHubParaID: assetHubParaID,
assetHubAgentID: assetHubAgentID,
assetHubCreateAssetFee: uint128(vm.envUint("CREATE_ASSET_FEE")),
assetHubReserveTransferFee: uint128(vm.envUint("RESERVE_TRANSFER_FEE")),
exchangeRate: ud60x18(vm.envUint("EXCHANGE_RATE")),
multiplier: ud60x18(vm.envUint("FEE_MULTIPLIER")),
rescueOperator: address(0)
rescueOperator: address(0),
foreignTokenDecimals: foreignTokenDecimals,
maxDestinationFee: maxDestinationFee
});

GatewayProxy gateway = new GatewayProxy(address(gatewayLogic), abi.encode(config));
GatewayProxy gateway =
new GatewayProxy(address(gatewayLogic), abi.encode(config));

// Deploy WETH for testing
new WETH9();
Expand All @@ -100,8 +98,10 @@ contract DeployLocal is Script {
// of messages originating from BridgeHub
uint256 initialDeposit = vm.envUint("BRIDGE_HUB_INITIAL_DEPOSIT");

address bridgeHubAgent = IGateway(address(gateway)).agentOf(bridgeHubAgentID);
address assetHubAgent = IGateway(address(gateway)).agentOf(assetHubAgentID);
address bridgeHubAgent =
IGateway(address(gateway)).agentOf(Constants.BRIDGE_HUB_AGENT_ID);
address assetHubAgent =
IGateway(address(gateway)).agentOf(Constants.ASSET_HUB_AGENT_ID);

payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);
Expand Down
15 changes: 1 addition & 14 deletions contracts/scripts/DeployLocalGatewayLogic.sol
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,9 @@ contract DeployLocalGatewayLogic is Script {

address beefyClient = vm.envAddress("BEEFY_CLIENT_CONTRACT_ADDRESS");

ParaID bridgeHubParaID = ParaID.wrap(uint32(vm.envUint("BRIDGE_HUB_PARAID")));
bytes32 bridgeHubAgentID = vm.envBytes32("BRIDGE_HUB_AGENT_ID");

uint8 foreignTokenDecimals = uint8(vm.envUint("FOREIGN_TOKEN_DECIMALS"));
uint128 maxDestinationFee = uint128(vm.envUint("RESERVE_TRANSFER_MAX_DESTINATION_FEE"));

AgentExecutor executor = new AgentExecutor();

new Gateway(
address(beefyClient),
address(executor),
bridgeHubParaID,
bridgeHubAgentID,
foreignTokenDecimals,
maxDestinationFee
);
new Gateway(address(beefyClient), address(executor));

vm.stopBroadcast();
}
Expand Down
30 changes: 12 additions & 18 deletions contracts/scripts/UpgradeShell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {IShell} from "../src/interfaces/IShell.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
import {Gateway} from "../src/Gateway.sol";
import {MockGatewayV2} from "../test/mocks/MockGatewayV2.sol";
import {Initializer} from "../src/Initializer.sol";
import {Agent} from "../src/Agent.sol";
import {AgentExecutor} from "../src/AgentExecutor.sol";
import {ChannelID, ParaID, OperatingMode} from "../src/Types.sol";
Expand All @@ -36,9 +37,7 @@ contract UpgradeShell is Script {
address beefyClient;
ParaID bridgeHubParaID;
bytes32 bridgeHubAgentID;
uint8 foreignTokenDecimals;
uint128 maxDestinationFee;
Gateway.Config initializerParams;
Initializer.Config initializerParams;
}

function readConfig() internal pure returns (Config memory config) {
Expand All @@ -47,19 +46,17 @@ contract UpgradeShell is Script {
beefyClient: 0x6eD05bAa904df3DE117EcFa638d4CB84e1B8A00C,
bridgeHubParaID: ParaID.wrap(1002),
bridgeHubAgentID: 0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314,
foreignTokenDecimals: 10,
maxDestinationFee: dot(2),
initializerParams: Gateway.Config({
initializerParams: Initializer.Config({
mode: OperatingMode.Normal,
deliveryCost: mDot(100), // 0.1 DOT
registerTokenFee: 0.002 ether,
assetHubParaID: ParaID.wrap(1000),
assetHubAgentID: 0x81c5ab2571199e3188135178f3c2c8e2d268be1313d029b30f534fa579b69b79,
assetHubCreateAssetFee: mDot(100), // 0.1 DOT
assetHubReserveTransferFee: mDot(100), // 0.1 DOT
exchangeRate: ud60x18(0.0024e18),
multiplier: ud60x18(1.33e18),
rescueOperator: 0x4B8a782D4F03ffcB7CE1e95C5cfe5BFCb2C8e967
rescueOperator: 0x4B8a782D4F03ffcB7CE1e95C5cfe5BFCb2C8e967,
Copy link
Contributor

Choose a reason for hiding this comment

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

Probably don't need this anymore, since we have already dropped the operator on mainnet?

foreignTokenDecimals: 10,
maxDestinationFee: dot(2)
})
});
}
Expand All @@ -73,18 +70,15 @@ contract UpgradeShell is Script {
AgentExecutor executor = new AgentExecutor();

// Gateway implementation
Gateway gatewayLogic = new Gateway(
config.beefyClient,
address(executor),
config.bridgeHubParaID,
config.bridgeHubAgentID,
config.foreignTokenDecimals,
config.maxDestinationFee
);
Gateway gatewayLogic = new Gateway(config.beefyClient, address(executor));

IShell shell = IShell(config.gatewayProxy);

shell.upgrade(address(gatewayLogic), address(gatewayLogic).codehash, abi.encode(config.initializerParams));
shell.upgrade(
address(gatewayLogic),
address(gatewayLogic).codehash,
abi.encode(config.initializerParams)
);

vm.stopBroadcast();
}
Expand Down
44 changes: 17 additions & 27 deletions contracts/scripts/westend/UpgradeShell.sol
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {IShell} from "../../src/interfaces/IShell.sol";
import {GatewayProxy} from "../../src/GatewayProxy.sol";
import {Gateway} from "../../src/Gateway.sol";
import {MockGatewayV2} from "../../test/mocks/MockGatewayV2.sol";
import {Initializer} from "../../src/Initializer.sol";
import {Agent} from "../../src/Agent.sol";
import {AgentExecutor} from "../../src/AgentExecutor.sol";
import {ChannelID, ParaID, OperatingMode} from "../../src/Types.sol";
Expand All @@ -25,32 +26,24 @@ contract UpgradeShell is Script {
struct Config {
address gatewayProxy;
address beefyClient;
ParaID bridgeHubParaID;
bytes32 bridgeHubAgentID;
uint8 foreignTokenDecimals;
uint128 maxDestinationFee;
Gateway.Config initializerParams;
Initializer.Config initializerParams;
}

function readConfig() internal pure returns (Config memory config) {
config = Config({
gatewayProxy: 0x9Ed8b47Bc3417e3BD0507ADC06E56e2Fa360A4E9,
beefyClient: 0x6DFaD3D73A28c48E4F4c616ECda80885b415283a,
bridgeHubParaID: ParaID.wrap(1002),
bridgeHubAgentID: 0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314,
foreignTokenDecimals: 12,
maxDestinationFee: 2000000000000,
initializerParams: Gateway.Config({
initializerParams: Initializer.Config({
mode: OperatingMode.Normal,
deliveryCost: 200000000000, // 0.2 Wnd
deliveryCost: 200_000_000_000, // 0.2 Wnd
registerTokenFee: 0.002 ether,
assetHubParaID: ParaID.wrap(1000),
assetHubAgentID: 0x81c5ab2571199e3188135178f3c2c8e2d268be1313d029b30f534fa579b69b79,
assetHubCreateAssetFee: 200000000000, // 0.2 Wnd
assetHubReserveTransferFee: 200000000000, // 0.2 Wnd
exchangeRate: ud60x18(2400000000000000),
multiplier: ud60x18(1330000000000000000),
rescueOperator: 0x302F0B71B8aD3CF6dD90aDb668E49b2168d652fd
assetHubCreateAssetFee: 200_000_000_000, // 0.2 Wnd
assetHubReserveTransferFee: 200_000_000_000, // 0.2 Wnd
exchangeRate: ud60x18(2_400_000_000_000_000),
multiplier: ud60x18(1_330_000_000_000_000_000),
rescueOperator: 0x302F0B71B8aD3CF6dD90aDb668E49b2168d652fd,
foreignTokenDecimals: 12,
maxDestinationFee: 2_000_000_000_000
})
});
}
Expand All @@ -64,18 +57,15 @@ contract UpgradeShell is Script {
AgentExecutor executor = new AgentExecutor();

// Gateway implementation
Gateway gatewayLogic = new Gateway(
config.beefyClient,
address(executor),
config.bridgeHubParaID,
config.bridgeHubAgentID,
config.foreignTokenDecimals,
config.maxDestinationFee
);
Gateway gatewayLogic = new Gateway(config.beefyClient, address(executor));

IShell shell = IShell(config.gatewayProxy);

shell.upgrade(address(gatewayLogic), address(gatewayLogic).codehash, abi.encode(config.initializerParams));
shell.upgrade(
address(gatewayLogic),
address(gatewayLogic).codehash,
abi.encode(config.initializerParams)
);

vm.stopBroadcast();
}
Expand Down
12 changes: 11 additions & 1 deletion contracts/src/AgentExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;

import {AgentExecuteCommand, ParaID} from "./Types.sol";
import {ParaID} from "./Types.sol";
import {SubstrateTypes} from "./SubstrateTypes.sol";

import {IERC20} from "./interfaces/IERC20.sol";
import {SafeTokenTransfer, SafeNativeTransfer} from "./utils/SafeTransfer.sol";
import {Call} from "./utils/Call.sol";
import {Gateway} from "./Gateway.sol";

/// @title Code which will run within an `Agent` using `delegatecall`.
Expand All @@ -27,6 +28,15 @@ contract AgentExecutor {
_transferToken(token, recipient, amount);
}

function callContract(address target, bytes memory data) external {
bool success = Call.safeCall(target, data);
if (!success) {
revert();
}
}

function deposit() external payable {}

/// @dev Transfer ERC20 to `recipient`. Only callable via `execute`.
function _transferToken(address token, address recipient, uint128 amount) internal {
IERC20(token).safeTransfer(recipient, amount);
Expand Down
21 changes: 21 additions & 0 deletions contracts/src/Constants.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;

import {ParaID, ChannelID} from "./v1/Types.sol";

library Constants {
ParaID constant ASSET_HUB_PARA_ID = ParaID.wrap(1000);
bytes32 constant ASSET_HUB_AGENT_ID =
0x81c5ab2571199e3188135178f3c2c8e2d268be1313d029b30f534fa579b69b79;
alistair-singh marked this conversation as resolved.
Show resolved Hide resolved

ParaID constant BRIDGE_HUB_PARA_ID = ParaID.wrap(1002);
Comment on lines +8 to +12
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice! Seems like these para IDs are the same on all the environments now anyway.

bytes32 constant BRIDGE_HUB_AGENT_ID =
0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314;

// ChannelIDs
ChannelID internal constant PRIMARY_GOVERNANCE_CHANNEL_ID =
ChannelID.wrap(bytes32(uint256(1)));
ChannelID internal constant SECONDARY_GOVERNANCE_CHANNEL_ID =
ChannelID.wrap(bytes32(uint256(2)));
}
Loading
Loading