-
Notifications
You must be signed in to change notification settings - Fork 107
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
SNO-613: Arbitrary transact from Ethereum to Polkadot #925
Changes from 19 commits
d58565b
66e0486
1bcb9b7
99b5267
8ddb34a
547dfc8
1c14ab4
3d59a44
152ce33
f11f5d9
d36cd35
a9508a7
48bcb82
abae23c
d00cd8b
f0cd660
919f318
64c48f8
8f3e52b
e3fab42
b2f4388
aa61938
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,6 +19,7 @@ import {ScaleCodec} from "./utils/ScaleCodec.sol"; | |
|
||
import {CoreStorage} from "./storage/CoreStorage.sol"; | ||
import {AssetsStorage} from "./storage/AssetsStorage.sol"; | ||
import {SubstrateTypes} from "./SubstrateTypes.sol"; | ||
|
||
contract Gateway is IGateway, IInitializable { | ||
using Address for address; | ||
|
@@ -29,6 +30,14 @@ contract Gateway is IGateway, IInitializable { | |
uint256 internal immutable DISPATCH_GAS; | ||
address internal immutable AGENT_EXECUTOR; | ||
|
||
// Todo: Could be a dynamic registry map set by destination chain | ||
// Default params for arbitrary transact | ||
// should be big enough to cover most of the transact cost in destination chain | ||
// could be somehow overestimated since the surplus will be refunded | ||
uint256 internal constant DEFAULT_EXTRA_FEE = 100_000_000_000_000; | ||
uint64 internal constant DEFAULT_REF_TIME = 1_000_000_000; | ||
uint64 internal constant DEFAULT_PROOF_SIZE = 100_000; | ||
|
||
// Verification state | ||
address internal immutable BEEFY_CLIENT; | ||
|
||
|
@@ -40,7 +49,15 @@ contract Gateway is IGateway, IInitializable { | |
// AssetHub | ||
ParaID internal immutable ASSET_HUB_PARA_ID; | ||
bytes32 internal immutable ASSET_HUB_AGENT_ID; | ||
|
||
// Template | ||
ParaID internal immutable TEMPLATE_PARA_ID; | ||
bytes32 internal immutable TEMPLATE_AGENT_ID; | ||
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. If I understand correctly, the template parachain is used for "testing" and illustrating how the arbitrary transact will work, right? So should "testing" code like this form part of the gateway contract? 🤔 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. Exactly! Previously I did that in Btw, seems you commented on outdated code. |
||
|
||
// Call index of create token in assethub runtime | ||
bytes2 internal immutable CREATE_TOKEN_CALL_ID; | ||
// Salt | ||
bytes32 internal immutable CREATE2_SALT; | ||
|
||
error InvalidProof(); | ||
error InvalidNonce(); | ||
|
@@ -79,7 +96,10 @@ contract Gateway is IGateway, IInitializable { | |
bytes32 bridgeHubHubAgentID, | ||
ParaID assetHubParaID, | ||
bytes32 assetHubHubAgentID, | ||
bytes2 createTokenCallID | ||
ParaID templateParaID, | ||
bytes32 templateAgentID, | ||
bytes2 createTokenCallID, | ||
bytes32 salt | ||
) { | ||
BEEFY_CLIENT = beefyClient; | ||
AGENT_EXECUTOR = agentExecutor; | ||
|
@@ -89,7 +109,10 @@ contract Gateway is IGateway, IInitializable { | |
BRIDGE_HUB_AGENT_ID = bridgeHubHubAgentID; | ||
ASSET_HUB_PARA_ID = assetHubParaID; | ||
ASSET_HUB_AGENT_ID = assetHubHubAgentID; | ||
TEMPLATE_PARA_ID = templateParaID; | ||
TEMPLATE_AGENT_ID = templateAgentID; | ||
CREATE_TOKEN_CALL_ID = createTokenCallID; | ||
CREATE2_SALT = salt; | ||
} | ||
|
||
/// @dev Submit a message from Polkadot for verification and dispatch | ||
|
@@ -535,7 +558,7 @@ contract Gateway is IGateway, IInitializable { | |
$.defaultReward = defaultReward; | ||
|
||
// Initialize an agent & channel for BridgeHub | ||
address bridgeHubAgent = address(new Agent(BRIDGE_HUB_AGENT_ID)); | ||
address bridgeHubAgent = address(new Agent{salt:CREATE2_SALT}(BRIDGE_HUB_AGENT_ID)); | ||
$.agents[BRIDGE_HUB_AGENT_ID] = bridgeHubAgent; | ||
$.channels[BRIDGE_HUB_PARA_ID] = Channel({ | ||
mode: OperatingMode.Normal, | ||
|
@@ -547,7 +570,7 @@ contract Gateway is IGateway, IInitializable { | |
}); | ||
|
||
// Initialize an agent & channel for AssetHub | ||
address assetHubAgent = address(new Agent(ASSET_HUB_AGENT_ID)); | ||
address assetHubAgent = address(new Agent{salt:CREATE2_SALT}(ASSET_HUB_AGENT_ID)); | ||
$.agents[ASSET_HUB_AGENT_ID] = assetHubAgent; | ||
$.channels[ASSET_HUB_PARA_ID] = Channel({ | ||
mode: OperatingMode.Normal, | ||
|
@@ -558,6 +581,75 @@ contract Gateway is IGateway, IInitializable { | |
reward: defaultReward | ||
}); | ||
|
||
// Initialize an agent & channel for Template | ||
address templateAgent = address(new Agent{salt:CREATE2_SALT}(TEMPLATE_AGENT_ID)); | ||
$.agents[TEMPLATE_AGENT_ID] = templateAgent; | ||
$.channels[TEMPLATE_PARA_ID] = Channel({ | ||
mode: OperatingMode.Normal, | ||
agent: templateAgent, | ||
inboundNonce: 0, | ||
outboundNonce: 0, | ||
fee: defaultFee, | ||
reward: defaultReward | ||
}); | ||
// Todo: Should be configurable/upgradable include a on-chain price oracle SWAP_RATE from https://coincodex.com/convert/ethereum/polkadot/ | ||
Assets.initialize(registerTokenFee, sendTokenFee); | ||
} | ||
|
||
/** | ||
* Transacts | ||
*/ | ||
|
||
/// @inheritdoc IGateway | ||
function transactThroughSovereign(ParaID destinationChain, bytes calldata payload) external payable { | ||
Channel storage channel = _ensureChannel(destinationChain); | ||
bytes memory message_payload = SubstrateTypes.Transact( | ||
address(channel.agent), bytes1(0x03), payload, DEFAULT_EXTRA_FEE, DEFAULT_REF_TIME, DEFAULT_PROOF_SIZE | ||
); | ||
_submitOutbound(destinationChain, message_payload, DEFAULT_EXTRA_FEE); | ||
} | ||
|
||
/// @inheritdoc IGateway | ||
function transactThroughSovereign( | ||
ParaID destinationChain, | ||
bytes1 originKind, | ||
bytes calldata payload, | ||
uint256 extraFee, | ||
uint64 refTime, | ||
uint64 proofSize | ||
) external payable { | ||
Channel storage channel = _ensureChannel(destinationChain); | ||
bytes memory message_payload = | ||
SubstrateTypes.Transact(address(channel.agent), originKind, payload, extraFee, refTime, proofSize); | ||
_submitOutbound(destinationChain, message_payload, extraFee); | ||
} | ||
|
||
/// @inheritdoc IGateway | ||
function transactThroughSigned(ParaID destinationChain, bytes calldata payload) external payable { | ||
bytes memory message_payload = SubstrateTypes.Transact( | ||
msg.sender, bytes1(0x01), payload, DEFAULT_EXTRA_FEE, DEFAULT_REF_TIME, DEFAULT_PROOF_SIZE | ||
); | ||
_submitOutbound(destinationChain, message_payload, 0); | ||
} | ||
|
||
/// @inheritdoc IGateway | ||
function transactThroughSigned( | ||
ParaID destinationChain, | ||
bytes calldata payload, | ||
uint256 extraFee, | ||
uint64 refTime, | ||
uint64 proofSize | ||
) external payable { | ||
bytes memory message_payload = | ||
SubstrateTypes.Transact(msg.sender, bytes1(0x01), payload, extraFee, refTime, proofSize); | ||
_submitOutbound(destinationChain, message_payload, 0); | ||
} | ||
|
||
/// Todo: transactThroughSigned and pay extraFee with foreign asset in dest chain which depends on https://github.com/Snowfork/snowbridge/pull/927 | ||
function transactThroughSignedPayExtraFeeWithForeignAsset( | ||
ParaID destinationChain, | ||
bytes calldata payload, | ||
uint32 fee_asset_id, | ||
uint256 fee_amount | ||
) external payable {} | ||
} |
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 if I understand this right, sendToken is a different kind of operation than an arbitrary contract call. So why is extraFee being sent here too, if it is not an arbitrary transact call being done 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.
Actually it's some improvement for https://linear.app/snowfork/issue/SNO-582 for transfer, but relevant to the Transact change here(i.e. extra fee will consistently be used to cover the cost of XCM dispatch) so I link it as subtask as https://linear.app/snowfork/issue/SNO-613
@vgeddes Could you help to confirm if that's also your intention?