Skip to content

Commit

Permalink
Make multiplier non-linear
Browse files Browse the repository at this point in the history
  • Loading branch information
vgeddes committed Mar 21, 2024
1 parent 1ae5ef5 commit c5d8db3
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
18 changes: 8 additions & 10 deletions contracts/src/Gateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -463,24 +463,22 @@ contract Gateway is IGateway, IInitializable {
}

// Convert foreign currency to native currency (ROC/KSM/DOT -> ETH)
function _convertToNative(UD60x18 exchangeRate, UD60x18 amount) internal view returns (uint256) {
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 = amount.mul(exchangeRate).div(foreignDecimals).mul(ethDecimals);
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();

// The exchange rate and amount are both multiplied by `multiplier` to add a safety margin
// * `exchangeRate` is subject to real-world fluctuations
// * `amount` includes XCM execution fees in Polkadot that are also subject to fluctuations
UD60x18 exchangeRate = pricing.multiplier.mul(pricing.exchangeRate);
UD60x18 amount = pricing.multiplier.mul(convert(pricing.deliveryCost + costs.foreign));

return costs.native + _convertToNative(exchangeRate, amount);
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
Expand Down
8 changes: 4 additions & 4 deletions contracts/test/Gateway.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ contract GatewayTest is Test {

testSetPricingParameters();
uint256 fee = IGateway(address(gateway)).quoteRegisterTokenFee();
assertEq(fee, 40000000000000003);
assertEq(fee, 20000000000000001);

testCreateAgent();
assertNotEq(GatewayMock(address(gateway)).agentOf(agentID), address(0));
Expand All @@ -541,7 +541,7 @@ contract GatewayTest is Test {

// Verify that storage was not overwritten
fee = IGateway(address(gateway)).quoteRegisterTokenFee();
assertEq(fee, 40000000000000003);
assertEq(fee, 20000000000000001);
assertNotEq(GatewayMock(address(gateway)).agentOf(agentID), address(0));
}

Expand Down Expand Up @@ -878,7 +878,7 @@ contract GatewayTest is Test {
function testSetPricingParameters() public {
uint256 fee = IGateway(address(gateway)).quoteRegisterTokenFee();
assertEq(fee, 5000000000000000);
// Double both the exchangeRate and multiplier. Should lead to an 8x fee increase
// Double both the exchangeRate and multiplier. Should lead to an 4x fee increase
GatewayMock(address(gateway)).setPricingParametersPublic(
abi.encode(
SetPricingParametersParams({
Expand All @@ -890,7 +890,7 @@ contract GatewayTest is Test {
);
// Should expect 4x fee increase
fee = IGateway(address(gateway)).quoteRegisterTokenFee();
assertEq(fee, 40000000000000003);
assertEq(fee, 20000000000000001);
}

function testSendTokenToForeignDestWithInvalidFee() public {
Expand Down

0 comments on commit c5d8db3

Please sign in to comment.