Skip to content

Commit

Permalink
Remove prefunding mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yrong committed Mar 19, 2024
1 parent 72f6d3e commit 68a567f
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 79 deletions.
28 changes: 8 additions & 20 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ import {
Ticket,
Costs,
OriginKind,
Weight,
TransactFeeMode
Weight
} from "./Types.sol";
import {IGateway} from "./interfaces/IGateway.sol";
import {IInitializable} from "./interfaces/IInitializable.sol";
Expand Down Expand Up @@ -629,39 +628,28 @@ contract Gateway is IGateway, IInitializable {
}

// Calculate cost for transact
function _calculateTransactCost(TransactFeeMode feeMode, uint128 destinationFee)
internal
pure
returns (Costs memory costs)
{
if (feeMode == TransactFeeMode.OnEthereum) {
costs = Costs({native: 0, foreign: destinationFee});
} else if (feeMode == TransactFeeMode.OnSubstrate) {
costs = Costs({native: 0, foreign: 0});
}
return costs;
function _calculateTransactCost(uint128 destinationFee) internal pure returns (Costs memory costs) {
return Costs({native: 0, foreign: destinationFee});
}

/// @inheritdoc IGateway
function sendCall(
ParaID destinationChain,
OriginKind originKind,
TransactFeeMode feeMode,
uint128 destinationFee,
Weight calldata weightAtMost,
bytes calldata call
) external payable {
bytes memory payload = SubstrateTypes.Transact(
msg.sender, originKind.encode(), feeMode.encodeFeeMode(), destinationFee, weightAtMost, call
);
Costs memory costs = _calculateTransactCost(feeMode, destinationFee);
bytes memory payload =
SubstrateTypes.Transact(msg.sender, originKind.encode(), destinationFee, weightAtMost, call);
Costs memory costs = _calculateTransactCost(destinationFee);
Ticket memory ticket = Ticket({dest: destinationChain, costs: costs, payload: payload});
_submitOutbound(ticket);
}

/// @inheritdoc IGateway
function quoteSendCallFee(TransactFeeMode feeMode, uint128 destinationFee) external view returns (uint256) {
Costs memory costs = _calculateTransactCost(feeMode, destinationFee);
function quoteSendCallFee(uint128 destinationFee) external view returns (uint256) {
Costs memory costs = _calculateTransactCost(destinationFee);
return _calculateFee(costs);
}
}
14 changes: 5 additions & 9 deletions contracts/src/SubstrateTypes.sol
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,17 @@ library SubstrateTypes {
}

// Arbitrary transact
function Transact(
address sender,
bytes1 originKind,
bytes1 feeMode,
uint128 fee,
Weight memory weight,
bytes memory call
) internal view returns (bytes memory) {
function Transact(address sender, bytes1 originKind, uint128 fee, Weight memory weight, bytes memory call)
internal
view
returns (bytes memory)
{
return bytes.concat(
bytes1(0x00),
ScaleCodec.encodeU64(uint64(block.chainid)),
bytes1(0x02),
SubstrateTypes.H160(sender),
originKind,
feeMode,
ScaleCodec.encodeU128(fee),
ScaleCodec.encodeCompactU64(weight.refTime),
ScaleCodec.encodeCompactU64(weight.proofSize),
Expand Down
19 changes: 0 additions & 19 deletions contracts/src/Types.sol
Original file line number Diff line number Diff line change
Expand Up @@ -131,22 +131,3 @@ function encode(OriginKind kind) pure returns (bytes1 result) {
}
return result;
}

/// @dev FeeMode
enum TransactFeeMode {
/// @dev Transact Fee paid full on Ethereum side upfront
OnEthereum,
/// @dev Transact Fee paid on destination chain by prefunded sender
OnSubstrate
}

using {encodeFeeMode} for TransactFeeMode global;

function encodeFeeMode(TransactFeeMode kind) pure returns (bytes1 result) {
if (kind == TransactFeeMode.OnEthereum) {
result = 0x00;
} else if (kind == TransactFeeMode.OnSubstrate) {
result = 0x01;
}
return result;
}
14 changes: 2 additions & 12 deletions contracts/src/interfaces/IGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,7 @@
// SPDX-FileCopyrightText: 2023 Snowfork <[email protected]>
pragma solidity 0.8.23;

import {
OperatingMode,
InboundMessage,
ParaID,
ChannelID,
MultiAddress,
OriginKind,
Weight,
TransactFeeMode
} from "../Types.sol";
import {OperatingMode, InboundMessage, ParaID, ChannelID, MultiAddress, OriginKind, Weight} from "../Types.sol";
import {Verification} from "../Verification.sol";
import {UD60x18} from "prb/math/src/UD60x18.sol";

Expand Down Expand Up @@ -119,12 +110,11 @@ interface IGateway {
function sendCall(
ParaID destinationChain,
OriginKind originKind,
TransactFeeMode feeMode,
uint128 destinationFee,
Weight calldata weightAtMost,
bytes calldata call
) external payable;

/// @dev Quote a fee in Ether for transact
function quoteSendCallFee(TransactFeeMode feeMode, uint128 destinationFee) external view returns (uint256);
function quoteSendCallFee(uint128 destinationFee) external view returns (uint256);
}
25 changes: 10 additions & 15 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@ import {
multiAddressFromBytes32,
multiAddressFromBytes20,
Weight,
OriginKind,
TransactFeeMode
OriginKind
} from "../src/Types.sol";

import {WETH9} from "canonical-weth/WETH9.sol";
Expand Down Expand Up @@ -920,44 +919,40 @@ contract GatewayTest is Test {
* Transact
*/
function testTransactFromXcmOrigin() public {
bytes memory payload = SubstrateTypes.Transact(account1, 0x03, 0x01, 1, Weight(1, 1), bytes("0x1"));
bytes memory payload = SubstrateTypes.Transact(account1, 0x03, 1, Weight(1, 1), bytes("0x1"));
console.logBytes(payload);
vm.expectEmit(true, false, false, true);
emit IGateway.OutboundMessageAccepted(penpalParaID.into(), 1, messageID, payload);
hoax(address(account1));
IGateway(address(gateway)).sendCall{value: 1 ether}(
penpalParaID, OriginKind.Xcm, TransactFeeMode.OnSubstrate, 1, Weight(1, 1), bytes("0x1")
);
IGateway(address(gateway)).sendCall{value: 1 ether}(penpalParaID, OriginKind.Xcm, 1, Weight(1, 1), bytes("0x1"));
}

function testTransactFromSovereignAccount() public {
bytes memory payload = SubstrateTypes.Transact(account1, 0x01, 0x00, 1, Weight(1, 1), bytes("0x1"));
bytes memory payload = SubstrateTypes.Transact(account1, 0x01, 1, Weight(1, 1), bytes("0x1"));
console.logBytes(payload);
vm.expectEmit(true, false, false, true);
emit IGateway.OutboundMessageAccepted(penpalParaID.into(), 1, messageID, payload);
hoax(address(account1));
IGateway(address(gateway)).sendCall{value: 1 ether}(
penpalParaID, OriginKind.SovereignAccount, TransactFeeMode.OnEthereum, 1, Weight(1, 1), bytes("0x1")
penpalParaID, OriginKind.SovereignAccount, 1, Weight(1, 1), bytes("0x1")
);
}

function testTransactFromSovereignAccountWithFee() public {
bytes memory payload = SubstrateTypes.Transact(account1, 0x01, 0x01, 1, Weight(1, 1), bytes("0x1"));
bytes memory payload = SubstrateTypes.Transact(account1, 0x01, 1, Weight(1, 1), bytes("0x1"));
console.logBytes(payload);
uint256 fee = IGateway(address(gateway)).quoteSendCallFee(TransactFeeMode.OnSubstrate, 1);
assertEq(fee, 2500000000000000);
uint256 fee = IGateway(address(gateway)).quoteSendCallFee(1);
assertEq(fee, 2500000000250000);
vm.expectEmit(true, false, false, true);
emit IGateway.OutboundMessageAccepted(penpalParaID.into(), 1, messageID, payload);
hoax(address(account1));
IGateway(address(gateway)).sendCall{value: fee}(
penpalParaID, OriginKind.SovereignAccount, TransactFeeMode.OnSubstrate, 1, Weight(1, 1), bytes("0x1")
penpalParaID, OriginKind.SovereignAccount, 1, Weight(1, 1), bytes("0x1")
);
}

function testQuoteSendCallFee() public {
uint256 fee = IGateway(address(gateway)).quoteSendCallFee(TransactFeeMode.OnSubstrate, 1);
assertEq(fee, 2500000000000000);
fee = IGateway(address(gateway)).quoteSendCallFee(TransactFeeMode.OnEthereum, 1);
uint256 fee = IGateway(address(gateway)).quoteSendCallFee(1);
assertEq(fee, 2500000000250000);
}
}
6 changes: 2 additions & 4 deletions contracts/test/mocks/GatewayUpgradeMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@ import {
ChannelID,
MultiAddress,
OriginKind,
Weight,
TransactFeeMode
Weight
} from "../../src/Types.sol";
import {IGateway} from "../../src/interfaces/IGateway.sol";
import {IInitializable} from "../../src/interfaces/IInitializable.sol";
Expand Down Expand Up @@ -76,13 +75,12 @@ contract GatewayUpgradeMock is IGateway, IInitializable {
function sendCall(
ParaID destinationChain,
OriginKind originKind,
TransactFeeMode feeMode,
uint128 destinationFee,
Weight calldata weightAtMost,
bytes calldata call
) external payable {}

function quoteSendCallFee(TransactFeeMode, uint128) external pure returns (uint256) {
function quoteSendCallFee(uint128) external pure returns (uint256) {
return 1;
}
}

0 comments on commit 68a567f

Please sign in to comment.