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 38 commits into
base: v2
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
e0b2371
v2 initial commit
vgeddes Oct 2, 2024
a7ad609
latest changes
vgeddes Oct 7, 2024
1be9e5c
Flesh out dispatch logic for inbound messages
vgeddes Oct 8, 2024
e7fecaf
Add reward address
vgeddes Oct 10, 2024
08715b0
make code compile
vgeddes Oct 17, 2024
111f738
Split initializer impl into a library to reduce contract size
vgeddes Oct 17, 2024
b0867e8
Major refactor
vgeddes Oct 18, 2024
ee37e3c
Update tests
vgeddes Oct 18, 2024
6829f38
add scripts back
vgeddes Oct 19, 2024
8c878ce
Update scripts
vgeddes Oct 19, 2024
0b25864
Merge branch 'v2' into vincent/v2
vgeddes Oct 19, 2024
c35743d
Finish outbound messaging
vgeddes Oct 19, 2024
149c3e4
Implement token registration for V2
vgeddes Oct 20, 2024
02ac263
Make functions payable
vgeddes Oct 21, 2024
c7332c3
improve docs
vgeddes Oct 21, 2024
c0bab8a
comments
vgeddes Oct 24, 2024
eae219e
review feedback
vgeddes Oct 27, 2024
318b7dc
cleanups
vgeddes Oct 27, 2024
e73125d
Add initial tests for V2
vgeddes Oct 27, 2024
fa9b8a7
Make `rewardAddress` an indexed event parameter
vgeddes Oct 27, 2024
00016a7
Clean up interfaces
vgeddes Nov 3, 2024
5817542
Make WETH address configurable
vgeddes Nov 4, 2024
4c3b2af
Autowrap ether
vgeddes Nov 4, 2024
98b7d51
auto-unwrap ether
vgeddes Nov 10, 2024
553b0b9
Add view keyword
yrong Nov 11, 2024
3a7f56c
improve
vgeddes Dec 7, 2024
dabecbf
improve
vgeddes Dec 7, 2024
8a1e688
allow unlocking native ether
vgeddes Dec 7, 2024
75ad3f5
improve token registration flows
vgeddes Dec 7, 2024
6bac207
improve docs
vgeddes Dec 7, 2024
7b28a11
improve
vgeddes Dec 7, 2024
7378735
improve
vgeddes Dec 7, 2024
4e17456
nit
vgeddes Dec 7, 2024
fc48f45
Update contracts/src/v1/Calls.sol
vgeddes Dec 10, 2024
e35cd91
Apply suggestions from code review
vgeddes Dec 10, 2024
55e6cab
Fix unlock WETH
yrong Dec 8, 2024
005da17
Suppress Error (6243): The "tload" instruction
yrong Dec 11, 2024
d77abee
Merge recent changes
yrong Dec 11, 2024
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
10 changes: 9 additions & 1 deletion contracts/foundry.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[profile.default]
solc_version = "0.8.25"
solc_version = "0.8.28"
optimizer = true
optimizer_runs = 20000
via_ir = false
Expand All @@ -13,10 +13,18 @@ fs_permissions = [
ignored_error_codes = [
# DeployLocal.sol is never deployed
5574,
# tstore
2394,
]

evm_version = 'Cancun'

[profile.production]
via_ir = true

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

[fmt]
number_underscore = "thousands"
line_length = 99
2 changes: 1 addition & 1 deletion contracts/scripts/Deploy.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {Script} from "forge-std/Script.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
Expand Down
4 changes: 2 additions & 2 deletions contracts/scripts/DeployBeefyClient.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";
Expand All @@ -18,7 +18,7 @@ contract DeployBeefyClient is Script {
function readConfig() internal pure returns (Config memory config) {
// Checkpoint generated using the script `./beefy-checkpoint.js` script in Polkadot-JS.
config = Config({
startBlock: 21087413,
startBlock: 21_087_413,
current: BeefyClient.ValidatorSet({
id: 644,
length: 297,
Expand Down
45 changes: 19 additions & 26 deletions contracts/scripts/DeployLocal.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";

import {IGateway} from "../src/interfaces/IGateway.sol";
import {IGatewayV2} from "../src/v2/IGateway.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
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,15 @@ 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"));

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,30 +69,32 @@ contract DeployLocal is Script {
defaultOperatingMode = OperatingMode.Normal;
}

Gateway.Config memory config = Gateway.Config({
// Deploy WETH for testing
address weth = address(new WETH9());

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,
weth: weth
Comment on lines +83 to +86
Copy link
Contributor

Choose a reason for hiding this comment

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

Make weth first item.

Suggested change
rescueOperator: address(0),
foreignTokenDecimals: foreignTokenDecimals,
maxDestinationFee: maxDestinationFee,
weth: weth
rescueOperator: address(0),
weth: weth,
foreignTokenDecimals: foreignTokenDecimals,
maxDestinationFee: maxDestinationFee

Commit:
e1a8578

});

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

// Deploy WETH for testing
new WETH9();

// Fund the sovereign account for the BridgeHub parachain. Used to reward relayers
// 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 =
IGatewayV2(address(gateway)).agentOf(Constants.BRIDGE_HUB_AGENT_ID);
address assetHubAgent = IGatewayV2(address(gateway)).agentOf(Constants.ASSET_HUB_AGENT_ID);

payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);
Expand Down
17 changes: 2 additions & 15 deletions contracts/scripts/DeployLocalGatewayLogic.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {AgentExecutor} from "../src/AgentExecutor.sol";
import {Gateway} from "../src//Gateway.sol";
Expand All @@ -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
9 changes: 4 additions & 5 deletions contracts/scripts/FundAgent.sol
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";

import {IGateway} from "../src/interfaces/IGateway.sol";
import {IGatewayV2} from "../src/v2/IGateway.sol";
import {GatewayProxy} from "../src/GatewayProxy.sol";
import {Gateway} from "../src/Gateway.sol";
import {Agent} from "../src/Agent.sol";
Expand All @@ -32,8 +31,8 @@ contract FundAgent is Script {
bytes32 bridgeHubAgentID = vm.envBytes32("BRIDGE_HUB_AGENT_ID");
bytes32 assetHubAgentID = vm.envBytes32("ASSET_HUB_AGENT_ID");

address bridgeHubAgent = IGateway(gatewayAddress).agentOf(bridgeHubAgentID);
address assetHubAgent = IGateway(gatewayAddress).agentOf(assetHubAgentID);
address bridgeHubAgent = IGatewayV2(gatewayAddress).agentOf(bridgeHubAgentID);
address assetHubAgent = IGatewayV2(gatewayAddress).agentOf(assetHubAgentID);

payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);
Expand Down
34 changes: 14 additions & 20 deletions contracts/scripts/UpgradeShell.sol
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
import {BeefyClient} from "../src/BeefyClient.sol";

import {IGateway} from "../src/interfaces/IGateway.sol";
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 +36,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 +45,18 @@ 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),
weth: 0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2
})
});
}
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
48 changes: 19 additions & 29 deletions contracts/scripts/westend/UpgradeShell.sol
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

import {WETH9} from "canonical-weth/WETH9.sol";
import {Script} from "forge-std/Script.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {UD60x18, ud60x18} from "prb/math/src/UD60x18.sol";

import {BeefyClient} from "../../src/BeefyClient.sol";
import {IGateway} from "../../src/interfaces/IGateway.sol";
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 +25,25 @@ 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,
weth: 0x7b79995e5f793A07Bc00c21412e50Ecae098E7f9
})
});
}
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
2 changes: 1 addition & 1 deletion contracts/src/Agent.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.25;
pragma solidity 0.8.28;

/// @title An agent contract that acts on behalf of a consensus system on Polkadot
/// @dev Instances of this contract act as an agents for arbitrary consensus systems on Polkadot. These consensus systems
Expand Down
Loading