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

Improve solidity code for PNA #1275

Merged

Conversation

vgeddes
Copy link
Collaborator

@vgeddes vgeddes commented Aug 25, 2024

Various improvements and cleanups

Changes:

  • More consistent and descriptive naming style
  • Simplified token registry
  • AgentExecutor.sol: Remove deprecated execute method
  • Foreign tokens are now owned by the gateway rather than the assethub agent. This simplifies code without sacrificing any security & robustness
  • Moved more asset-specific code to Assets.sol

FIXES: SNO-1133

@vgeddes vgeddes requested a review from yrong August 25, 2024 16:10
@acatangiu
Copy link

Foreign tokens are now owned by the gateway rather than the assethub agent.

What about already registered foreign assets? Are they still using the agent as admin account? Or do you plan to switch the registered admin on AH for them (need to check if that is possible)?

@yrong
Copy link
Contributor

yrong commented Aug 26, 2024

Cool, much simple!

Btw: It seems we want to support multi-hop for Ethereum->AH->Para direction. So is the fee token on Para still be relay token? If so will destination chain accept that? Can we always assume it's reserve-backed transfers beween AH and Para(more discussion in https://snowfork.slack.com/archives/G01BGMGLAC9/p1724456977726639thread_ts=1724432403.659139&cid=G01BGMGLAC9)?

My concern here is seems not easy to find a common pattern to support multi-hop which applies to all parachains.

address public immutable OWNER;

uint8 public immutable decimals;
TokenLib.Token token;
Copy link
Contributor

@yrong yrong Aug 26, 2024

Choose a reason for hiding this comment

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

Would suggest to move the token slots to the very end after decimals.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Moved fields around a bit


Token(token).burn(sender, amount);

ticket.dest = destinationChain;
Copy link
Contributor

Choose a reason for hiding this comment

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

Is the dest here supposed to be assetHubParaID?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah good catch. I've reimplemented _sendForeignToken to mirror _sendNativeToken. So that PNA assets can be sent to assethub or a further hop from assethub.

Comment on lines 209 to 221
if (destinationAddress.isAddress32()) {
// The receiver has a 32-byte account ID
ticket.payload = SubstrateTypes.SendForeignTokenToAddress32(
foreignID, destinationChain, destinationAddress.asAddress32(), destinationChainFee, amount
);
} else if (destinationAddress.isAddress20()) {
// The receiver has a 20-byte account ID
ticket.payload = SubstrateTypes.SendForeignTokenToAddress20(
foreignID, destinationChain, destinationAddress.asAddress20(), destinationChainFee, amount
);
} else {
revert Unsupported();
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Need to double check the execution cost on AH(i.e. assetHubReserveTransferFee) is included.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

For now I'm assuming assetHubReserveTransferFee (which is configured to have considerable buffer) will cover costs for xcm execution on AH for both ENA and PNA flows.

We should probably rename the field to something like assetHubXcmFee

@vgeddes
Copy link
Collaborator Author

vgeddes commented Aug 26, 2024

Foreign tokens are now owned by the gateway rather than the assethub agent.

What about already registered foreign assets? Are they still using the agent as admin account? Or do you plan to switch the registered admin on AH for them (need to check if that is possible)?

This is an Ethereum-specific addition that does not involve AH. Currently there are only native tokens (aka Ethereum-native ERC20s) registered in the Gateway contract, and on AH too.

This change gives our gateway contract the ability to be aware of foreign assets on AH for example the Hydra or Kilt token in the ForeignAssets pallets.

So it does affect the code paths that involve ethereum-native assets such as WETH or WBTC.

@acatangiu
Copy link

Foreign tokens are now owned by the gateway rather than the assethub agent.

What about already registered foreign assets? Are they still using the agent as admin account? Or do you plan to switch the registered admin on AH for them (need to check if that is possible)?

This is an Ethereum-specific addition that does not involve AH. Currently there are only native tokens (aka Ethereum-native ERC20s) registered in the Gateway contract, and on AH too.

This change gives our gateway contract the ability to be aware of foreign assets on AH for example the Hydra or Kilt token in the ForeignAssets pallets.

So it does affect the code paths that involve ethereum-native assets such as WETH or WBTC.

I see. Sounds good! Thanks for the explanation 👍🏻

@vgeddes
Copy link
Collaborator Author

vgeddes commented Aug 27, 2024

Cool, much simple!

Btw: It seems we want to support multi-hop for Ethereum->AH->Para direction. So is the fee token on Para still be relay token? If so will destination chain accept that? Can we always assume it's reserve-backed transfers beween AH and Para(more discussion in https://snowfork.slack.com/archives/G01BGMGLAC9/p1724456977726639thread_ts=1724432403.659139&cid=G01BGMGLAC9)?

My concern here is seems not easy to find a common pattern to support multi-hop which applies to all parachains.

One idea I had for the Ethereum->AH->Parachain flow regarding fees on the final destination parachain.

We already take DOT from AH's SA on BH to pay for XCM execution on AH. So we could go on step further and on AH we swap some of that DOT for the native token of the destination parachain.

@yrong
Copy link
Contributor

yrong commented Aug 28, 2024

go on step further and on AH we swap some of that DOT for the native token of the destination parachain.

Sounds good. Can we make the fee asset of destination Para configurable when register token here?

So for chains does allow AH-DOT as fee asset, swap is not required.

Updates: Seems swap heavily depends on these PRs which are not ready yet, also not exist in our forked repo.

So I would prefer to make things easy just use AH-DOT as fee for PNA and move the swap improvement to a seperate PR in future when it's included in https://github.com/polkadot-fellows/runtimes, I would assume it applies also to the legacy Ethereum native asset like WETH.

Comment on lines 250 to 252
ticket.payload = SubstrateTypes.SendTokenToAssetHubAddress32(
token, destinationAddress.asAddress32(), $.assetHubReserveTransferFee, amount
);
Copy link
Contributor

@yrong yrong Aug 28, 2024

Choose a reason for hiding this comment

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

Copy link
Contributor

Choose a reason for hiding this comment

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

Fixed in 57fa08c

@yrong yrong merged commit 84bb52c into ron/polkadot-assets-on-ethereum Aug 28, 2024
1 check passed
@yrong yrong deleted the vincent/polkadot-assets-on-ethereum branch August 28, 2024 11:24
yrong added a commit that referenced this pull request Sep 2, 2024
* Asset from polkadot

* Remove params unnecessary

* Experiment: return calldata from Agent instead of call Gateway directly

* To store registries also on agent

* Send polkadot tokens back

* Cleanup for not exceed the size limit

* Add smoke test for register polkadot token

* Transfer relay token to Ethereum

* Reuse IGateway.sendToken for polkadot native asset

* More cleanup

* Fix encode substrate types

* Add smoke test send relay token back

* Rename to sendForeignToken

* Improve ERC20.sol

* Revert the change double register the token

* Migration for AssetsStorage

* Rename to _sendForeignTokenCosts

* Use storage for less gas

* Rename to tokenAddressOf

* Rename as _burnToken

* _ensureAgent in Gateway

* Remove unused

* Clean up smoke test

* Terminate register foreign token in Gateway

* Mint foreign token without the callback

* Burn token without the callback

* constructor ERC20 with the agent as owner

* Move handler to Assets.sol reduce contract size

* Refactoring to move transferToken to Assets

* Remove TokenMinted&TokenBurned event

* Move Command.RegisterToken to top level

* Fix smoke tests

* Move the check before interactions

* Polish

* Introduce ERC20Lib.sol

* Remove unused

* More test

* Remove AgentExecuteCommand

* More contract tests

* Update contract address

* Add AgentExecuteCommand back for compatibility

* Cleanup

* Add rfc

* Update polkadot-native-assets.md

* Update rfc

* Update rfc with fee section

* Fix _calculateFee for polkadot native token

* Improve solidity code for PNA  (#1275)

* Improve PNA implementation

* More improvents to ERC20 token

* Fix implementation of Assets._sendForeignToken

* Fix build payload for foreign token

---------

Co-authored-by: ron <yrong1997@gmail.com>

* Remove storage migration

* Smoke test for register relay token

* Fix fee estimation

* Fix binding

* Remove outdated doc

* Fork upgrade test with sanity checks

* Fix test

---------

Co-authored-by: Vincent Geddes <117534+vgeddes@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants