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

Erc20 XCM bridge #2090

Merged
merged 46 commits into from Feb 20, 2023
Merged

Erc20 XCM bridge #2090

merged 46 commits into from Feb 20, 2023

Conversation

librelois
Copy link
Collaborator

@librelois librelois commented Feb 10, 2023

What does it do?

Add ability to transfer any ERC20 token through xcm directly.

The implementation is mainly about adding a new TransactAsset implementation that allow the xcm executor to handle erc20 assets.

Breaking changes

  • xtokens precompiles: currencyId will always resolve to erc20 asset as a default instead of throwing revert error "Cannot convert into currency id".
  • XCM fees: XCM instructions TransferAsset, TransferAssetReserveAsset and WithdrawAsset will cost 2_000_000_000 weights per erc20 asset instead of 200_000 if they not contains any erc20 asset.
  • Arbitrary local xcm execution: instructions InitiateReserveWithdraw and InitiateTeleport are now forbidden if they can contains erc20 assets.

Security considerations for erc20 contracts

If the implementation of the transfer function returns true when the transfer has not really been done, it allows to duplicate tokens. Indeed, in the case of sending tokens to another chain, they must be transferred to the sovereign account of the destination chain, if the contract returns true, the xcm protocol will consider that the tokens have been transferred to the sovereign account of the destination chain and will send the tokens on the destination chain.

We consider that it is the responsibility of each erc20 contract to handle error cases correctly, and to return true only if the transfer really went well.

What important points reviewers should know?

Is there something left for follow-up PRs?

What alternative implementations were considered?

Are there relevant PRs or issues in other repositories (Substrate, Polkadot, Frontier, Cumulus)?

What value does it bring to the blockchain users?

TODO

  • add more tests

@librelois librelois added B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes D9-needsaudit👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Feb 10, 2023
@librelois librelois added the breaking Needs to be mentioned in breaking changes label Feb 10, 2023
@librelois librelois marked this pull request as ready for review February 10, 2023 15:34
librelois and others added 5 commits February 10, 2023 19:51
Co-authored-by: Amar Singh <asinghchrony@protonmail.com>
Conflicts:
	Cargo.lock
	runtime/common/Cargo.toml
	runtime/moonbase/Cargo.toml
	runtime/moonbase/src/lib.rs
impl pallet_xcm::Config for Runtime {
type RuntimeEvent = RuntimeEvent;
type SendXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmRouter = XcmRouter;
type ExecuteXcmOrigin = EnsureXcmOrigin<RuntimeOrigin, LocalOriginToLocation>;
type XcmExecuteFilter = Everything;
type XcmExecuteFilter = XcmExecuteFilter;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are we changing this?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I added more documentation about this filter, I took the opportunity to improve it and move it to the erc20 xcm pallet.

/// some XCM instructions if they could manipulate erc20 assets.
/// If your runtime allows arbitrary xcm messages to be executed locally you should use this
// wrapper.
pub struct XcmExecuteFilterWrapper<Runtime, FallbackFilter>(
Copy link
Collaborator

Choose a reason for hiding this comment

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

I dont mind leaving this filter, but I dont think it adds much value TBH. InitiateReserveWithdraw will instruct our sovereign account in another chain to withdraw the ERC20, but our sovereign account in another chain will likely not contain these (unless someone else has put it there on purpose).

Copy link
Collaborator

Choose a reason for hiding this comment

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

In other words the message will fail. This should be no security risk, if something we are telling users not to try this because their message will fail. And I am not a big fan of the latter...

Copy link
Collaborator

@girazoki girazoki left a comment

Choose a reason for hiding this comment

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

Good job @librelois, I just have a comment on executeFilter that I dont think it adds much value, but other than that looks good!

@librelois librelois merged commit 77c2928 into master Feb 20, 2023
@librelois librelois deleted the elois-erc20-xcm-bridge branch February 20, 2023 09:42
@notlesh notlesh added D1-audited👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D9-needsaudit👮 PR contains changes to fund-managing logic that should be properly reviewed and externally audited labels Apr 26, 2023
imstar15 pushed a commit to OAK-Foundation/moonbeam that referenced this pull request May 16, 2023
* prepare evm runner for erc20 bridge

* upgrade frontier

* catch ethereum execution informations

* add pallet erc20-xcm-bridge

* add bridge supply counter

* impl internal_transfer_asset for Erc20XcmBridge

* add xcm holding extension "erc20s origins"

* inject precompile handle in substrate calls triggered by precompiles

* rename rust module evm_runner

* fix: xtokens should be able to perform evm subcall

* fix: adress without know prefix should resolve into erc20 currency

* add ts test "transfer ERC20 token throught xcm with xtokens precompile"

* Reserve pallet id 109 for future integration in moonriver/moonbeam

* fmt

* environmental/std

* editorconfig

* rm unreachable!

* toml-sort

* clean some unused code

* forbid-evm-reentrancy

* impl an AssetTrap that filter out erc20 assets

* Update runtime/moonbase/src/xcm_config.rs

Co-authored-by: Amar Singh <asinghchrony@protonmail.com>

* ref: replace xcm holding ext by asset multilocation ext

* Merge branch 'master' into elois-erc20-xcm-bridge

* update frontier

* fix compilation warning

* erc20_transfer: opt call data allocation

* Revert "replace xcm holding ext by asset multilocation ext"

* remove duplicated code

* add rust unit tests for Erc20Matcher

* configurable erc20 transfer gas limit

* get evm config from pallet evm config

* remove reserved pallet id for moonriver/moonbeam

* perform erc20 transfer in a db transaction in all cases

* charge weigh for xcm erc20 transfers according to configured gas limit

* filter some xcm instructions for arbitrary local execution

* apply suggestion

* 80000 gas limit is enough for almost all erc20 implementations

* more doc

* optimize  is_erc20_asset and use it on trap

* each withdrawal of erc20 tokens must be deposited at once

* improve XcmExecuteFilter impl and move it in pallet erc20 xcm

* call inner filter

---------

Co-authored-by: Alan Sapede <alan@purestake.com>
Co-authored-by: Amar Singh <asinghchrony@protonmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
B7-runtimenoteworthy Changes should be noted in any runtime-upgrade release notes breaking Needs to be mentioned in breaking changes D1-audited👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants