Skip to content

Commit

Permalink
Update rfc doc
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Apr 29, 2024
1 parent 606e867 commit 539c180
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions rfc/transact_from_substrate_to_ethereum.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,21 @@ This RFC proposes the feature to call transact from Substrate to Ethereum throug

We use penpal for the integration, basically it works as follows:

On penpal end user call the custom extrinsic [transact_to_ethereum](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/cumulus/parachains/runtimes/testing/penpal/src/pallets/transact_helper.rs#L70), the parameters of the extrinsic are:
On penpal end user call the custom extrinsic [transact_to_ethereum](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/cumulus/parachains/runtimes/testing/penpal/src/pallets/transact_helper.rs#L97), the parameters of the extrinsic are:

- `agent_id` is the custom agent supposed to execute the transact
- `target` is the contract address
- `call` is abi-encoded call data
- `fee` is the execution cost for the call
- `gas_limit` is the max gas limit for the call

It requires penpal to extend the `XcmRouter` to route xcm destination to Ethereum through our bridge, i.e. a [SovereignPaidRemoteExporter](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs#L376) for the reference.

It requires penpal to extend the `XcmRouter` to route xcm with destination to Ethereum through our bridge on BH, [config](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs#L376) for the reference.
Worth to note that since the `fee` as dynamic input parameter of the extrinsic, the [fee config in BridgeTable](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs#L441) should only cover the execution cost on BridgeHub, but not including the cost on Ethereum.

Worth to note that the [fee config in BridgeTable](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/cumulus/parachains/runtimes/testing/penpal/src/xcm_config.rs#L451) should only cover the execution cost on BridgeHub, but not including the cost on Ethereum which is the `fee` as input parameter of the extrinsic.
Then [charge fees](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/cumulus/parachains/runtimes/testing/penpal/src/pallets/transact_helper.rs#L128-L134) from the sender which includes both the two portions(i.e. the execution cost on BridgeHub and on Ethereum).

Then we [charge fees](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/cumulus/parachains/runtimes/testing/penpal/src/pallets/transact_helper.rs#L104) from the sender which includes both the execution cost on BridgeHub and cost on Ethereum.

Finally we [deliver](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/cumulus/parachains/runtimes/testing/penpal/src/pallets/transact_helper.rs#L107) the xcm to BH.
Finally [deliver](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/cumulus/parachains/runtimes/testing/penpal/src/pallets/transact_helper.rs#L107) the xcm to BH.

The xcm sent to BH as following:

Expand All @@ -48,11 +48,15 @@ instructions: [

So the top-level `WithdrawAsset` will withdraw relay token from sovereign account of penpal as fee to pay for the execution cost on BridgeHub.

What we really care about is the internal xcm in `ExportMessage` with [the convert logic in outbound-router](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/bridges/snowbridge/primitives/router/src/outbound/mod.rs#L203) it will be converted into a simple `AgentExecuteCommand` which will be relayed and finally executed on Ethereum.
What we really care about is the internal xcm in `ExportMessage`, the internal `call` of the `Transact` is actually the encoded [TransactInfo](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/bridges/snowbridge/primitives/core/src/outbound.rs#L444).

With [the convert logic in outbound-router](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/bridges/snowbridge/primitives/router/src/outbound/mod.rs#L207) it will be converted into a simple `Command` which will be relayed and finally executed on Ethereum.

Worth to note that the sovereign of Penpal only pays(DOT) for the [delivery(local fee) portion](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/bridges/snowbridge/primitives/router/src/outbound/mod.rs#L111-L117).

There is a E2E test [transact_from_penpal_to_ethereum](https://github.com/Snowfork/polkadot-sdk/blob/55377cd94b5ef543f1dca2cfd8bcfdd90998dcd4/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/snowbridge.rs#L566) for demonstration.
There is a E2E test [transact_from_penpal_to_ethereum](https://github.com/Snowfork/polkadot-sdk/blob/6cc2175ff5063178260cfa5e243aa981d32a3622/cumulus/parachains/integration-tests/emulated/tests/bridges/bridge-hub-rococo/src/tests/snowbridge.rs#L567) for demonstration.

On Ethereum side based on the `AgentExecuteCommand` the Agent will [execute the call](https://github.com/Snowfork/snowbridge/blob/0a8dc1e425d495a1bfa217cea6dde520260519ec/contracts/src/AgentExecutor.sol#L48) to finish the whole flow.
On Ethereum side based on the `Command` the specified agent will [execute the call](https://github.com/Snowfork/snowbridge/blob/606e867b7badc6d356c8f4b56e6b81ee0eb27811/contracts/src/Gateway.sol#L393).

## Fee flow

Expand All @@ -61,7 +65,7 @@ On Ethereum side based on the `AgentExecuteCommand` the Agent will [execute the

Sequence|Where|Who|What
-|-|-|-
1|Parachain|User|pays(DOT, Native) to node to execute custom extrinsic; pays (DOT) to Treasury for both delivery cost on BH and execution cost on Ethereum as part of custom extrinsic.
2|Bridge Hub|Parachain|pays(DOT) to Treasury Account for delivery(local fee), check Remote fee passed as expected.
1|Parachain|User|pays(DOT, Native) to node to execute custom extrinsic; pays (DOT) to Treasury for both delivery cost on BH and execution cost on Ethereum.
2|Bridge Hub|Parachain|pays(DOT) to Treasury Account for delivery(local portion), only check remote fee passed as expected without charging
3|Gateway|Relayer|pays(ETH) to validate and execute message.
4|Gateway|Parachain Agent|pays(ETH) to relayer for delivery(reward+refund) and execution.

0 comments on commit 539c180

Please sign in to comment.