-
Notifications
You must be signed in to change notification settings - Fork 147
Execute multiple contract calls atomically with optional sweep-on-failure #1702
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
Open
yrong
wants to merge
4
commits into
main
Choose a base branch
from
ron/multi-contract-calls
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -25,7 +25,7 @@ import { | |
| IGatewayV1, | ||
| IGatewayV2 | ||
| } from "./Types.sol"; | ||
| import {Network} from "./v2/Types.sol"; | ||
| import {Network, CallContractsParams} from "./v2/Types.sol"; | ||
| import {Upgrade} from "./Upgrade.sol"; | ||
| import {IInitializable} from "./interfaces/IInitializable.sol"; | ||
| import {IUpgradable} from "./interfaces/IUpgradable.sol"; | ||
|
|
@@ -500,6 +500,12 @@ contract Gateway is IGatewayBase, IGatewayV1, IGatewayV2, IInitializable, IUpgra | |
| catch { | ||
| success = false; | ||
| emit IGatewayV2.CommandFailed(nonce, i); | ||
| if (command.kind == CommandKind.CallContracts) { | ||
| try this.trySweepOnFailure(origin, command.payload) {} | ||
| catch { | ||
| emit IGatewayV2.SweepAfterCallContractsFailed(nonce, i); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| return (false, success); | ||
|
|
@@ -523,11 +529,25 @@ contract Gateway is IGatewayBase, IGatewayV1, IGatewayV2, IInitializable, IUpgra | |
| HandlersV2.mintForeignToken(command.payload); | ||
| } else if (command.kind == CommandKind.CallContract) { | ||
| HandlersV2.callContract(origin, AGENT_EXECUTOR, command.payload); | ||
| } else if (command.kind == CommandKind.CallContracts) { | ||
| HandlersV2.callContracts(origin, AGENT_EXECUTOR, command.payload); | ||
| } else { | ||
| revert IGatewayV2.InvalidCommand(); | ||
| } | ||
| } | ||
|
|
||
| /// @dev Decode CallContractsParams and run sweep when configured; used in CallContracts catch block. | ||
| /// Caller wraps in try-catch so malformed payloads or sweep failures don't revert dispatch. | ||
| function trySweepOnFailure(bytes32 origin, bytes calldata payload) | ||
| external | ||
| onlySelf | ||
| { | ||
| CallContractsParams memory params = abi.decode(payload, (CallContractsParams)); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Params are decoded here for a second time, can probably send |
||
| if (params.sweepRecipient != address(0) && params.tokensToSweep.length > 0) { | ||
| HandlersV2.sweepAfterCallContracts(origin, AGENT_EXECUTOR, payload); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Upgrades | ||
| */ | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can unrelated assets be swept unintentionally here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Typically, the sweep token list is constructed by the SDK and kept consistent with the transfer token list.
By the way, the sweep flow is optional and can be skipped by providing an empty address.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I like this PR in theory. But I would like to build this at a higher level. So here we are mimicking the Asset Claimer behaviour by using (maybe abusing) Transact and the fact that we can implement anything via arbitrary contract call, rather than sticking close to the XCM functionality and build it into our protocol directly.
For instance we should probably stick closer to the XCM spec, and by default always sweep assets on any failure. By default we should sweep assets to the locations Agent, however if
SetHints{Claimer}is set then then we use that account. The asset list should be filled in by the on-chain code from the XCM ground truth instead of expecting the sdk to build it correctly offchain. Also if a user has an agent, JIT create the Agent if it does not exist. Jit creating an agent and paying more gas, but having funds recoverable is probably better than failing and having funds trapped in limbo.If a user didnt specify any claimer and the funds got sent to the agent, we could use ClaimAsset instruction to get them back for instance.