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

Transact from sub to eth #1145

Open
wants to merge 25 commits into
base: bridge-next-gen
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 18 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
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
3 changes: 3 additions & 0 deletions contracts/scripts/DeployLocal.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import {ChannelID, ParaID, OperatingMode} from "../src/Types.sol";
import {SafeNativeTransfer} from "../src/utils/SafeTransfer.sol";
import {stdJson} from "forge-std/StdJson.sol";
import {UD60x18, ud60x18} from "prb/math/src/UD60x18.sol";
import {HelloWorld} from "../test/mocks/HelloWorld.sol";

contract DeployLocal is Script {
using SafeNativeTransfer for address payable;
Expand Down Expand Up @@ -105,6 +106,8 @@ contract DeployLocal is Script {
payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);

// Deploy HelloWorld for testing transact
new HelloWorld();
// Deploy MockGatewayV2 for testing
new MockGatewayV2();

Expand Down
3 changes: 3 additions & 0 deletions contracts/scripts/FundAgent.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,15 @@ contract FundAgent is Script {

bytes32 bridgeHubAgentID = vm.envBytes32("BRIDGE_HUB_AGENT_ID");
bytes32 assetHubAgentID = vm.envBytes32("ASSET_HUB_AGENT_ID");
bytes32 penpalAgentID = vm.envBytes32("PENPAL_AGENT_ID");

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

payable(bridgeHubAgent).safeNativeTransfer(initialDeposit);
payable(assetHubAgent).safeNativeTransfer(initialDeposit);
payable(penpalAgent).safeNativeTransfer(initialDeposit);

vm.stopBroadcast();
}
Expand Down
7 changes: 7 additions & 0 deletions contracts/src/AgentExecutor.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import {SubstrateTypes} from "./SubstrateTypes.sol";

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

/// @title Code which will run within an `Agent` using `delegatecall`.
/// @dev This is a singleton contract, meaning that all agents will execute the same code.
contract AgentExecutor {
using SafeTokenTransfer for IERC20;
using SafeNativeTransfer for address payable;
using Call for address;

/// @dev Execute a message which originated from the Polkadot side of the bridge. In other terms,
/// the `data` parameter is constructed by the BridgeHub parachain.
Expand All @@ -36,4 +38,9 @@ contract AgentExecutor {
function _transferToken(address token, address recipient, uint128 amount) internal {
IERC20(token).safeTransfer(recipient, amount);
}

/// @dev Call a contract at the given address, with provided bytes as payload.
function executeCall(address target, bytes memory payload, uint64 dynamicGas) external returns (bool) {
return target.safeCall(dynamicGas, 0, payload);
}
}
11 changes: 6 additions & 5 deletions contracts/src/Assets.sol
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,12 @@ library Assets {
IERC20(token).safeTransferFrom(sender, agent, amount);
}

function sendTokenCosts(address token, ParaID destinationChain, uint128 destinationChainFee, uint128 maxDestinationChainFee)
external
view
returns (Costs memory costs)
{
function sendTokenCosts(
address token,
ParaID destinationChain,
uint128 destinationChainFee,
uint128 maxDestinationChainFee
) external view returns (Costs memory costs) {
AssetsStorage.Layout storage $ = AssetsStorage.layout();
TokenInfo storage info = $.tokenRegistry[token];
if (!info.isRegistered) {
Expand Down
30 changes: 27 additions & 3 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@ import {
Command,
MultiAddress,
Ticket,
Costs
Costs,
AgentExecuteCommand
} from "./Types.sol";
import {Upgrade} from "./Upgrade.sol";
import {IGateway} from "./interfaces/IGateway.sol";
Expand All @@ -39,7 +40,8 @@ import {
SetOperatingModeParams,
TransferNativeFromAgentParams,
SetTokenTransferFeesParams,
SetPricingParametersParams
SetPricingParametersParams,
TransactCallParams
} from "./Params.sol";

import {CoreStorage} from "./storage/CoreStorage.sol";
Expand Down Expand Up @@ -94,6 +96,7 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
error AgentExecutionFailed(bytes returndata);
error InvalidAgentExecutionPayload();
error InvalidConstructorParams();
error AgentTransactCallFailed();

// Message handlers can only be dispatched by the gateway itself
modifier onlySelf() {
Expand Down Expand Up @@ -211,6 +214,11 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
catch {
success = false;
}
} else if (message.command == Command.Transact) {
try Gateway(this).transact{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
}

// Calculate a gas refund, capped to protect against huge spikes in `tx.gasprice`
Expand Down Expand Up @@ -381,6 +389,20 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
emit PricingParametersChanged();
}

// @dev Transact
function transact(bytes calldata data) external onlySelf {
TransactCallParams memory params = abi.decode(data, (TransactCallParams));
address agent = _ensureAgent(params.agentID);
bytes memory call =
abi.encodeCall(AgentExecutor.executeCall, (params.target, params.payload, params.dynamicGas));
(, bytes memory result) = Agent(payable(agent)).invoke(AGENT_EXECUTOR, call);
(bool success) = abi.decode(result, (bool));
if (!success) {
revert AgentTransactCallFailed();
}
emit TransactExecuted(params.agentID, params.target, params.payload);
Copy link
Collaborator

Choose a reason for hiding this comment

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

params.payload could be very large, cheaper to hash it when emitting event

Suggested change
emit TransactExecuted(params.agentID, params.target, params.payload);
emit TransactExecuted(params.agentID, params.target, keccak256(params.payload));

Copy link
Contributor Author

Choose a reason for hiding this comment

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

}

/**
* Assets
*/
Expand Down Expand Up @@ -416,7 +438,9 @@ contract Gateway is IGateway, IInitializable, IUpgradable {
uint128 amount
) external payable {
_submitOutbound(
Assets.sendToken(token, msg.sender, destinationChain, destinationAddress, destinationFee, MAX_DESTINATION_FEE, amount)
Assets.sendToken(
token, msg.sender, destinationChain, destinationAddress, destinationFee, MAX_DESTINATION_FEE, amount
)
);
}

Expand Down
12 changes: 12 additions & 0 deletions contracts/src/Params.sol
Original file line number Diff line number Diff line change
Expand Up @@ -82,3 +82,15 @@ struct SetPricingParametersParams {
/// @dev Fee multiplier
UD60x18 multiplier;
}

// Payload for TransactCall
struct TransactCallParams {
/// @dev The agent ID of the consensus system
bytes32 agentID;
/// @dev The target contract
address target;
/// @dev Payload of the call
bytes payload;
/// @dev Max gas cost of the call
uint64 dynamicGas;
Copy link
Collaborator

Choose a reason for hiding this comment

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

The message packet already has a maxDispatchGas, which is the upper limit a transact can consume. So I am not sure what role this dynamicGas plays.

The gas estimator on BridgeHub should add a buffer to maxDispatchGas to cover the cost of dispatching via the agent.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, it's the same thing. Just rename it for less confusion.
27c14e9

}
3 changes: 2 additions & 1 deletion contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ enum Command {
SetOperatingMode,
TransferNativeFromAgent,
SetTokenTransferFees,
SetPricingParameters
SetPricingParameters,
Transact
}

enum AgentExecuteCommand {
Expand Down
2 changes: 2 additions & 0 deletions contracts/src/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ interface IGateway {
// Emitted when funds are withdrawn from an agent
event AgentFundsWithdrawn(bytes32 indexed agentID, address indexed recipient, uint256 amount);

event TransactExecuted(bytes32 indexed agentID, address indexed target, bytes payload);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
event TransactExecuted(bytes32 indexed agentID, address indexed target, bytes payload);
event Transacted(bytes32 indexed agentID, address indexed target, bytes32 payloadHash);

Copy link
Contributor Author

Choose a reason for hiding this comment

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


/**
* Getters
*/
Expand Down
Loading
Loading