-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathGateway.sol
More file actions
652 lines (546 loc) · 24.7 KB
/
Gateway.sol
File metadata and controls
652 lines (546 loc) · 24.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.23;
import {MerkleProof} from "openzeppelin/utils/cryptography/MerkleProof.sol";
import {Verification} from "./Verification.sol";
import {Assets} from "./Assets.sol";
import {AgentExecutor} from "./AgentExecutor.sol";
import {Agent} from "./Agent.sol";
import {
Channel,
ChannelID,
InboundMessage,
OperatingMode,
ParaID,
Command,
MultiAddress,
Ticket,
Costs,
OriginKind,
Weight
} from "./Types.sol";
import {Upgrade} from "./Upgrade.sol";
import {IGateway} from "./interfaces/IGateway.sol";
import {IInitializable} from "./interfaces/IInitializable.sol";
import {IUpgradable} from "./interfaces/IUpgradable.sol";
import {ERC1967} from "./utils/ERC1967.sol";
import {Address} from "./utils/Address.sol";
import {SafeNativeTransfer} from "./utils/SafeTransfer.sol";
import {Call} from "./utils/Call.sol";
import {Math} from "./utils/Math.sol";
import {ScaleCodec} from "./utils/ScaleCodec.sol";
import {SubstrateTypes} from "./SubstrateTypes.sol";
import {
UpgradeParams,
CreateAgentParams,
AgentExecuteParams,
CreateChannelParams,
UpdateChannelParams,
SetOperatingModeParams,
TransferNativeFromAgentParams,
SetTokenTransferFeesParams,
SetPricingParametersParams
} from "./Params.sol";
import {CoreStorage} from "./storage/CoreStorage.sol";
import {PricingStorage} from "./storage/PricingStorage.sol";
import {AssetsStorage} from "./storage/AssetsStorage.sol";
import {UD60x18, ud60x18, convert} from "prb/math/src/UD60x18.sol";
contract Gateway is IGateway, IInitializable, IUpgradable {
using Address for address;
using SafeNativeTransfer for address payable;
address public immutable AGENT_EXECUTOR;
// Verification state
address public immutable BEEFY_CLIENT;
// BridgeHub
ParaID internal immutable BRIDGE_HUB_PARA_ID;
bytes4 internal immutable BRIDGE_HUB_PARA_ID_ENCODED;
bytes32 internal immutable BRIDGE_HUB_AGENT_ID;
// ChannelIDs
ChannelID internal constant PRIMARY_GOVERNANCE_CHANNEL_ID = ChannelID.wrap(bytes32(uint256(1)));
ChannelID internal constant SECONDARY_GOVERNANCE_CHANNEL_ID = ChannelID.wrap(bytes32(uint256(2)));
// Gas used for:
// 1. Mapping a command id to an implementation function
// 2. Calling implementation function
uint256 DISPATCH_OVERHEAD_GAS = 10_000;
// The maximum fee that can be sent to a destination parachain to pay for execution (DOT).
// Has two functions:
// * Reduces the ability of users to perform arbitrage using a favourable exchange rate
// * Prevents users from mistakenly providing too much fees, which would drain AssetHub's
// sovereign account here on Ethereum.
uint128 internal immutable MAX_DESTINATION_FEE;
uint8 internal immutable FOREIGN_TOKEN_DECIMALS;
error InvalidProof();
error InvalidNonce();
error NotEnoughGas();
error FeePaymentToLow();
error Unauthorized();
error Disabled();
error AgentAlreadyCreated();
error AgentDoesNotExist();
error ChannelAlreadyCreated();
error ChannelDoesNotExist();
error InvalidChannelUpdate();
error AgentExecutionFailed(bytes returndata);
error InvalidAgentExecutionPayload();
error InvalidConstructorParams();
error AlreadyInitialized();
error InvalidTransact();
// Message handlers can only be dispatched by the gateway itself
modifier onlySelf() {
if (msg.sender != address(this)) {
revert Unauthorized();
}
_;
}
constructor(
address beefyClient,
address agentExecutor,
ParaID bridgeHubParaID,
bytes32 bridgeHubAgentID,
uint8 foreignTokenDecimals,
uint128 maxDestinationFee
) {
if (bridgeHubParaID == ParaID.wrap(0) || bridgeHubAgentID == 0) {
revert InvalidConstructorParams();
}
BEEFY_CLIENT = beefyClient;
AGENT_EXECUTOR = agentExecutor;
BRIDGE_HUB_PARA_ID_ENCODED = ScaleCodec.encodeU32(uint32(ParaID.unwrap(bridgeHubParaID)));
BRIDGE_HUB_PARA_ID = bridgeHubParaID;
BRIDGE_HUB_AGENT_ID = bridgeHubAgentID;
FOREIGN_TOKEN_DECIMALS = foreignTokenDecimals;
MAX_DESTINATION_FEE = maxDestinationFee;
}
/// @dev Submit a message from Polkadot for verification and dispatch
/// @param message A message produced by the OutboundQueue pallet on BridgeHub
/// @param leafProof A message proof used to verify that the message is in the merkle tree committed by the OutboundQueue pallet
/// @param headerProof A proof that the commitment is included in parachain header that was finalized by BEEFY.
function submitV1(
InboundMessage calldata message,
bytes32[] calldata leafProof,
Verification.Proof calldata headerProof
) external {
uint256 startGas = gasleft();
Channel storage channel = _ensureChannel(message.channelID);
// Ensure this message is not being replayed
if (message.nonce != channel.inboundNonce + 1) {
revert InvalidNonce();
}
// Increment nonce for origin.
// This also prevents the re-entrancy case in which a malicious party tries to re-enter by calling `submitInbound`
// again with the same (message, leafProof, headerProof) arguments.
channel.inboundNonce++;
// Produce the commitment (message root) by applying the leaf proof to the message leaf
bytes32 leafHash = keccak256(abi.encode(message));
bytes32 commitment = MerkleProof.processProof(leafProof, leafHash);
// Verify that the commitment is included in a parachain header finalized by BEEFY.
if (!_verifyCommitment(commitment, headerProof)) {
revert InvalidProof();
}
// Make sure relayers provide enough gas so that inner message dispatch
// does not run out of gas.
uint256 maxDispatchGas = message.maxDispatchGas;
if (gasleft() < maxDispatchGas + DISPATCH_OVERHEAD_GAS) {
revert NotEnoughGas();
}
bool success = true;
// Dispatch message to a handler
if (message.command == Command.AgentExecute) {
try Gateway(this).agentExecute{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.CreateAgent) {
try Gateway(this).createAgent{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.CreateChannel) {
try Gateway(this).createChannel{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.UpdateChannel) {
try Gateway(this).updateChannel{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.SetOperatingMode) {
try Gateway(this).setOperatingMode{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.TransferNativeFromAgent) {
try Gateway(this).transferNativeFromAgent{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.Upgrade) {
try Gateway(this).upgrade{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.SetTokenTransferFees) {
try Gateway(this).setTokenTransferFees{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
} else if (message.command == Command.SetPricingParameters) {
try Gateway(this).setPricingParameters{gas: maxDispatchGas}(message.params) {}
catch {
success = false;
}
}
// Calculate a gas refund, capped to protect against huge spikes in `tx.gasprice`
// that could drain funds unnecessarily. During these spikes, relayers should back off.
uint256 gasUsed = _transactionBaseGas() + (startGas - gasleft());
uint256 refund = gasUsed * Math.min(tx.gasprice, message.maxFeePerGas);
// Add the reward to the refund amount. If the sum is more than the funds available
// in the channel agent, then reduce the total amount
uint256 amount = Math.min(refund + message.reward, address(channel.agent).balance);
// Do the payment if there funds available in the agent
if (amount > _dustThreshold()) {
_transferNativeFromAgent(channel.agent, payable(msg.sender), amount);
}
emit IGateway.InboundMessageDispatched(message.channelID, message.nonce, message.id, success);
}
/**
* Getters
*/
function operatingMode() external view returns (OperatingMode) {
return CoreStorage.layout().mode;
}
function channelOperatingModeOf(ChannelID channelID) external view returns (OperatingMode) {
Channel storage ch = _ensureChannel(channelID);
return ch.mode;
}
function channelNoncesOf(ChannelID channelID) external view returns (uint64, uint64) {
Channel storage ch = _ensureChannel(channelID);
return (ch.inboundNonce, ch.outboundNonce);
}
function agentOf(bytes32 agentID) external view returns (address) {
return _ensureAgent(agentID);
}
function pricingParameters() external view returns (UD60x18, uint128) {
PricingStorage.Layout storage pricing = PricingStorage.layout();
return (pricing.exchangeRate, pricing.deliveryCost);
}
function implementation() public view returns (address) {
return ERC1967.load();
}
/**
* Handlers
*/
// Execute code within an agent
function agentExecute(bytes calldata data) external onlySelf {
AgentExecuteParams memory params = abi.decode(data, (AgentExecuteParams));
address agent = _ensureAgent(params.agentID);
if (params.payload.length == 0) {
revert InvalidAgentExecutionPayload();
}
bytes memory call = abi.encodeCall(AgentExecutor.execute, params.payload);
(bool success, bytes memory returndata) = Agent(payable(agent)).invoke(AGENT_EXECUTOR, call);
if (!success) {
revert AgentExecutionFailed(returndata);
}
}
/// @dev Create an agent for a consensus system on Polkadot
function createAgent(bytes calldata data) external onlySelf {
CoreStorage.Layout storage $ = CoreStorage.layout();
CreateAgentParams memory params = abi.decode(data, (CreateAgentParams));
// Ensure we don't overwrite an existing agent
if (address($.agents[params.agentID]) != address(0)) {
revert AgentAlreadyCreated();
}
address payable agent = payable(new Agent(params.agentID));
$.agents[params.agentID] = agent;
emit AgentCreated(params.agentID, agent);
}
/// @dev Create a messaging channel for a Polkadot parachain
function createChannel(bytes calldata data) external onlySelf {
CoreStorage.Layout storage $ = CoreStorage.layout();
CreateChannelParams memory params = abi.decode(data, (CreateChannelParams));
// Ensure that specified agent actually exists
address agent = _ensureAgent(params.agentID);
// Ensure channel has not already been created
Channel storage ch = $.channels[params.channelID];
if (address(ch.agent) != address(0)) {
revert ChannelAlreadyCreated();
}
ch.mode = params.mode;
ch.agent = agent;
ch.inboundNonce = 0;
ch.outboundNonce = 0;
emit ChannelCreated(params.channelID);
}
/// @dev Update the configuration for a channel
function updateChannel(bytes calldata data) external onlySelf {
UpdateChannelParams memory params = abi.decode(data, (UpdateChannelParams));
Channel storage ch = _ensureChannel(params.channelID);
// Extra sanity checks when updating the primary governance channel, which should never be halted.
if (params.channelID == PRIMARY_GOVERNANCE_CHANNEL_ID && (params.mode != OperatingMode.Normal)) {
revert InvalidChannelUpdate();
}
ch.mode = params.mode;
emit ChannelUpdated(params.channelID);
}
/// @dev Perform an upgrade of the gateway
function upgrade(bytes calldata data) external onlySelf {
UpgradeParams memory params = abi.decode(data, (UpgradeParams));
Upgrade.upgrade(params.impl, params.implCodeHash, params.initParams);
}
// @dev Set the operating mode of the gateway
function setOperatingMode(bytes calldata data) external onlySelf {
CoreStorage.Layout storage $ = CoreStorage.layout();
SetOperatingModeParams memory params = abi.decode(data, (SetOperatingModeParams));
$.mode = params.mode;
emit OperatingModeChanged(params.mode);
}
// @dev Transfer funds from an agent to a recipient account
function transferNativeFromAgent(bytes calldata data) external onlySelf {
TransferNativeFromAgentParams memory params = abi.decode(data, (TransferNativeFromAgentParams));
address agent = _ensureAgent(params.agentID);
_transferNativeFromAgent(agent, payable(params.recipient), params.amount);
emit AgentFundsWithdrawn(params.agentID, params.recipient, params.amount);
}
// @dev Set token fees of the gateway
function setTokenTransferFees(bytes calldata data) external onlySelf {
AssetsStorage.Layout storage $ = AssetsStorage.layout();
SetTokenTransferFeesParams memory params = abi.decode(data, (SetTokenTransferFeesParams));
$.assetHubCreateAssetFee = params.assetHubCreateAssetFee;
$.assetHubReserveTransferFee = params.assetHubReserveTransferFee;
$.registerTokenFee = params.registerTokenFee;
emit TokenTransferFeesChanged();
}
// @dev Set pricing params of the gateway
function setPricingParameters(bytes calldata data) external onlySelf {
PricingStorage.Layout storage pricing = PricingStorage.layout();
SetPricingParametersParams memory params = abi.decode(data, (SetPricingParametersParams));
pricing.exchangeRate = params.exchangeRate;
pricing.deliveryCost = params.deliveryCost;
pricing.multiplier = params.multiplier;
emit PricingParametersChanged();
}
/**
* Assets
*/
function isTokenRegistered(address token) external view returns (bool) {
return Assets.isTokenRegistered(token);
}
// Total fee for registering a token
function quoteRegisterTokenFee() external view returns (uint256) {
return _calculateFee(Assets.registerTokenCosts());
}
// Register an Ethereum-native token in the gateway and on AssetHub
function registerToken(address token) external payable {
_submitOutbound(Assets.registerToken(token));
}
// Total fee for sending a token
function quoteSendTokenFee(address token, ParaID destinationChain, uint128 destinationFee)
external
view
returns (uint256)
{
return _calculateFee(Assets.sendTokenCosts(token, destinationChain, destinationFee, MAX_DESTINATION_FEE));
}
// Transfer ERC20 tokens to a Polkadot parachain
function sendToken(
address token,
ParaID destinationChain,
MultiAddress calldata destinationAddress,
uint128 destinationFee,
uint128 amount
) external payable {
_submitOutbound(
Assets.sendToken(
token, msg.sender, destinationChain, destinationAddress, destinationFee, MAX_DESTINATION_FEE, amount
)
);
}
/**
* Internal functions
*/
// Best-effort attempt at estimating the base gas use of `submitInbound` transaction, outside the block of
// code that is metered.
// This includes:
// * Cost paid for every transaction: 21000 gas
// * Cost of calldata: Zero byte = 4 gas, Non-zero byte = 16 gas
// * Cost of code inside submitInitial that is not metered: 14_698
//
// The major cost of calldata are the merkle proofs, which should dominate anything else (including the message payload)
// Since the merkle proofs are hashes, they are much more likely to be composed of more non-zero bytes than zero bytes.
//
// Reference: Ethereum Yellow Paper
function _transactionBaseGas() internal pure returns (uint256) {
return 21_000 + 14_698 + (msg.data.length * 16);
}
// Verify that a message commitment is considered finalized by our BEEFY light client.
function _verifyCommitment(bytes32 commitment, Verification.Proof calldata proof)
internal
view
virtual
returns (bool)
{
return Verification.verifyCommitment(BEEFY_CLIENT, BRIDGE_HUB_PARA_ID_ENCODED, commitment, proof);
}
// Convert foreign currency to native currency (ROC/KSM/DOT -> ETH)
function _convertToNative(UD60x18 exchangeRate, UD60x18 multiplier, UD60x18 amount)
internal
view
returns (uint256)
{
UD60x18 ethDecimals = convert(1e18);
UD60x18 foreignDecimals = convert(10).pow(convert(uint256(FOREIGN_TOKEN_DECIMALS)));
UD60x18 nativeAmount = multiplier.mul(amount).mul(exchangeRate).div(foreignDecimals).mul(ethDecimals);
return convert(nativeAmount);
}
// Calculate the fee for accepting an outbound message
function _calculateFee(Costs memory costs) internal view returns (uint256) {
PricingStorage.Layout storage pricing = PricingStorage.layout();
UD60x18 amount = convert(pricing.deliveryCost + costs.foreign);
return costs.native + _convertToNative(pricing.exchangeRate, pricing.multiplier, amount);
}
// Submit an outbound message to Polkadot, after taking fees
function _submitOutbound(Ticket memory ticket) internal {
ChannelID channelID = ticket.dest.into();
Channel storage channel = _ensureChannel(channelID);
// Ensure outbound messaging is allowed
_ensureOutboundMessagingEnabled(channel);
uint256 fee = _calculateFee(ticket.costs);
// Ensure the user has enough funds for this message to be accepted
if (msg.value < fee) {
revert FeePaymentToLow();
}
channel.outboundNonce = channel.outboundNonce + 1;
// Deposit total fee into agent's contract
payable(channel.agent).safeNativeTransfer(fee);
// Reimburse excess fee payment
if (msg.value > fee) {
payable(msg.sender).safeNativeTransfer(msg.value - fee);
}
// Generate a unique ID for this message
bytes32 messageID = keccak256(abi.encodePacked(channelID, channel.outboundNonce));
emit IGateway.OutboundMessageAccepted(channelID, channel.outboundNonce, messageID, ticket.payload);
}
/// @dev Outbound message can be disabled globally or on a per-channel basis.
function _ensureOutboundMessagingEnabled(Channel storage ch) internal view {
CoreStorage.Layout storage $ = CoreStorage.layout();
if ($.mode != OperatingMode.Normal || ch.mode != OperatingMode.Normal) {
revert Disabled();
}
}
/// @dev Ensure that the specified parachain has a channel allocated
function _ensureChannel(ChannelID channelID) internal view returns (Channel storage ch) {
ch = CoreStorage.layout().channels[channelID];
// A channel always has an agent specified.
if (ch.agent == address(0)) {
revert ChannelDoesNotExist();
}
}
/// @dev Ensure that the specified agentID has a corresponding contract
function _ensureAgent(bytes32 agentID) internal view returns (address agent) {
agent = CoreStorage.layout().agents[agentID];
if (agent == address(0)) {
revert AgentDoesNotExist();
}
}
/// @dev Invoke some code within an agent
function _invokeOnAgent(address agent, bytes memory data) internal returns (bytes memory) {
(bool success, bytes memory returndata) = (Agent(payable(agent)).invoke(AGENT_EXECUTOR, data));
return Call.verifyResult(success, returndata);
}
/// @dev Transfer ether from an agent
function _transferNativeFromAgent(address agent, address payable recipient, uint256 amount) internal {
bytes memory call = abi.encodeCall(AgentExecutor.transferNative, (recipient, amount));
_invokeOnAgent(agent, call);
}
/// @dev Define the dust threshold as the minimum cost to transfer ether between accounts
function _dustThreshold() internal view returns (uint256) {
return 21000 * tx.gasprice;
}
/**
* Upgrades
*/
// Initial configuration for bridge
struct Config {
OperatingMode mode;
/// @dev The fee charged to users for submitting outbound messages (DOT)
uint128 deliveryCost;
/// @dev The ETH/DOT exchange rate
UD60x18 exchangeRate;
ParaID assetHubParaID;
bytes32 assetHubAgentID;
/// @dev The extra fee charged for registering tokens (DOT)
uint128 assetHubCreateAssetFee;
/// @dev The extra fee charged for sending tokens (DOT)
uint128 assetHubReserveTransferFee;
/// @dev extra fee to discourage spamming
uint256 registerTokenFee;
/// @dev Fee multiplier
UD60x18 multiplier;
}
/// @dev Initialize storage in the gateway
/// NOTE: This is not externally accessible as this function selector is overshadowed in the proxy
function initialize(bytes calldata data) external virtual {
// Prevent initialization of storage in implementation contract
if (ERC1967.load() == address(0)) {
revert Unauthorized();
}
CoreStorage.Layout storage core = CoreStorage.layout();
Config memory config = abi.decode(data, (Config));
core.mode = config.mode;
// Initialize agent for BridgeHub
address bridgeHubAgent = address(new Agent(BRIDGE_HUB_AGENT_ID));
core.agents[BRIDGE_HUB_AGENT_ID] = bridgeHubAgent;
// Initialize channel for primary governance track
core.channels[PRIMARY_GOVERNANCE_CHANNEL_ID] =
Channel({mode: OperatingMode.Normal, agent: bridgeHubAgent, inboundNonce: 0, outboundNonce: 0});
// Initialize channel for secondary governance track
core.channels[SECONDARY_GOVERNANCE_CHANNEL_ID] =
Channel({mode: OperatingMode.Normal, agent: bridgeHubAgent, inboundNonce: 0, outboundNonce: 0});
// Initialize agent for for AssetHub
address assetHubAgent = address(new Agent(config.assetHubAgentID));
core.agents[config.assetHubAgentID] = assetHubAgent;
// Initialize channel for AssetHub
core.channels[config.assetHubParaID.into()] =
Channel({mode: OperatingMode.Normal, agent: assetHubAgent, inboundNonce: 0, outboundNonce: 0});
// Initialize pricing storage
PricingStorage.Layout storage pricing = PricingStorage.layout();
pricing.exchangeRate = config.exchangeRate;
pricing.deliveryCost = config.deliveryCost;
pricing.multiplier = config.multiplier;
// Initialize assets storage
AssetsStorage.Layout storage assets = AssetsStorage.layout();
assets.assetHubParaID = config.assetHubParaID;
assets.assetHubAgent = assetHubAgent;
assets.registerTokenFee = config.registerTokenFee;
assets.assetHubCreateAssetFee = config.assetHubCreateAssetFee;
assets.assetHubReserveTransferFee = config.assetHubReserveTransferFee;
}
// Calculate cost for transact
function _calculateTransactCost() internal pure returns (Costs memory costs) {
return Costs({native: 0, foreign: 0});
}
/// @inheritdoc IGateway
function sendCall(
ParaID destinationChain,
OriginKind originKind,
uint128 destinationFee,
Weight calldata weightAtMost,
bytes calldata call
) external payable {
if (call.length == 0 || destinationFee == 0 || weightAtMost.refTime == 0 || weightAtMost.proofSize == 0) {
revert InvalidTransact();
}
bytes memory payload =
SubstrateTypes.Transact(msg.sender, originKind.encode(), destinationFee, weightAtMost, call);
Costs memory costs = _calculateTransactCost();
Ticket memory ticket = Ticket({dest: destinationChain, costs: costs, payload: payload});
_submitOutbound(ticket);
}
/// @inheritdoc IGateway
function quoteSendCallFee() external view returns (uint256) {
Costs memory costs = _calculateTransactCost();
return _calculateFee(costs);
}
}