From 99b0088bc321b545abb5e903251d990ad929b74e Mon Sep 17 00:00:00 2001 From: Timepunk <45543880+0xTimepunk@users.noreply.github.com> Date: Tue, 23 Jan 2024 15:20:45 +0000 Subject: [PATCH] feat: add estimateAckCostDefaultNativeSource --- .github/workflows/CI.yml | 2 +- src/interfaces/IPaymentHelper.sol | 15 ++++++++ src/payments/PaymentHelper.sol | 22 +++++++++--- .../extensions/CoreStateRegistry.t.sol | 34 +++++++++++++++++-- 4 files changed, 65 insertions(+), 8 deletions(-) diff --git a/.github/workflows/CI.yml b/.github/workflows/CI.yml index 80ea4ca55..5044789a8 100644 --- a/.github/workflows/CI.yml +++ b/.github/workflows/CI.yml @@ -19,7 +19,7 @@ env: AVALANCHE_RPC_URL: ${{ secrets.AVALANCHE_RPC_URL }} FANTOM_RPC_URL: ${{ secrets.FANTOM_RPC_URL }} TENDERLY_ACCESS_KEY: ${{ secrets.TENDERLY_ACCESS_KEY }} - TENDERLY_PROJECT_SLUG: "superform-v1-final" # your project slug + TENDERLY_PROJECT_SLUG: "v1" # your project slug TENDERLY_ACCOUNT_ID: "superform" # your username or organization name FOUNDRY_EXPORTS_OVERWRITE_LATEST: "true" diff --git a/src/interfaces/IPaymentHelper.sol b/src/interfaces/IPaymentHelper.sol index 1eb8106d8..732e5accd 100644 --- a/src/interfaces/IPaymentHelper.sol +++ b/src/interfaces/IPaymentHelper.sol @@ -195,6 +195,21 @@ interface IPaymentHelper { external view returns (uint256 totalFees); + + /// @dev helps estimate the acknowledgement costs for amb processing without relying on payloadId (using max values) + /// with source native amounts + /// @param multi is the flag indicating if the payload is multi or single + /// @param ackAmbIds is the list of ambIds to be used for acknowledgement + /// @param srcChainId is the source chain identifier + /// @return totalFees is the total fees to be paid in native tokens + function estimateAckCostDefaultNativeSource( + bool multi, + uint8[] memory ackAmbIds, + uint64 srcChainId + ) + external + view + returns (uint256 totalFees); ////////////////////////////////////////////////////////////// // EXTERNAL WRITE FUNCTIONS // ////////////////////////////////////////////////////////////// diff --git a/src/payments/PaymentHelper.sol b/src/payments/PaymentHelper.sol index 0109f451c..5e15f6e1e 100644 --- a/src/payments/PaymentHelper.sol +++ b/src/payments/PaymentHelper.sol @@ -225,8 +225,7 @@ contract PaymentHelper is IPaymentHelper { /// @dev step 7: estimate execution costs in destination including sending acknowledgement to source /// @dev ensure that acknowledgement costs from dst to src are not double counted - v.totalDstGas += - xChain ? _estimateDstExecutionCost(isDeposit_, req_.dstChainIds[i], v.superformIdsLen) : 0; + v.totalDstGas += xChain ? _estimateDstExecutionCost(isDeposit_, req_.dstChainIds[i], v.superformIdsLen) : 0; /// @dev step 8: convert all dst gas estimates to src chain estimate (withdraw / deposit) dstAmount += _convertToNativeFee(req_.dstChainIds[i], v.totalDstGas); @@ -290,8 +289,7 @@ contract PaymentHelper is IPaymentHelper { } /// @dev step 7: estimate execution costs in destination including sending acknowledgement to source - totalDstGas += xChain - ? _estimateDstExecutionCost(isDeposit_, req_.dstChainIds[i], 1) : 0; + totalDstGas += xChain ? _estimateDstExecutionCost(isDeposit_, req_.dstChainIds[i], 1) : 0; /// @dev step 8: convert all dst gas estimates to src chain estimate dstAmount += _convertToNativeFee(req_.dstChainIds[i], totalDstGas); @@ -547,7 +545,7 @@ contract PaymentHelper is IPaymentHelper { uint8[] memory ackAmbIds, uint64 srcChainId ) - external + public view override returns (uint256 totalFees) @@ -568,6 +566,20 @@ contract PaymentHelper is IPaymentHelper { return _estimateAMBFees(ackAmbIds, srcChainId, abi.encode(AMBMessage(type(uint256).max, payloadBody))); } + /// @inheritdoc IPaymentHelper + function estimateAckCostDefaultNativeSource( + bool multi, + uint8[] memory ackAmbIds, + uint64 srcChainId + ) + external + view + override + returns (uint256) + { + return _convertToNativeFee(srcChainId, estimateAckCostDefault(multi, ackAmbIds, srcChainId)); + } + ////////////////////////////////////////////////////////////// // EXTERNAL WRITE FUNCTIONS // ////////////////////////////////////////////////////////////// diff --git a/test/unit/crosschain-data/extensions/CoreStateRegistry.t.sol b/test/unit/crosschain-data/extensions/CoreStateRegistry.t.sol index 0b4611c11..39af9451f 100644 --- a/test/unit/crosschain-data/extensions/CoreStateRegistry.t.sol +++ b/test/unit/crosschain-data/extensions/CoreStateRegistry.t.sol @@ -673,7 +673,7 @@ contract CoreStateRegistryTest is ProtocolActions { vm.clearMockedCalls(); } - function test_ackGasCost_paymentHelperComparison() public { + function test_ackGasCost_single_paymentHelperComparison() public { uint8[] memory ambIds_ = new uint8[](2); ambIds_[0] = 1; ambIds_[1] = 2; @@ -690,7 +690,37 @@ contract CoreStateRegistryTest is ProtocolActions { console.log("defaultEstimate: %s", defaultEstimate); console.log("realEstimate: %s", realEstimate); - assertGe(realEstimate, defaultEstimate); + assertEq(realEstimate, defaultEstimate); + + uint256 defaultEstimateNativeSrc = + PaymentHelper(getContract(AVAX, "PaymentHelper")).estimateAckCostDefaultNativeSource(false, ambIds_, ETH); + + console.log("defaultEstimateNativeSrc: %s", defaultEstimateNativeSrc); + } + + function test_ackGasCost_multi_paymentHelperComparison() public { + uint8[] memory ambIds_ = new uint8[](2); + ambIds_[0] = 1; + ambIds_[1] = 2; + + _successfulMultiDeposit(ambIds_); + + vm.selectFork(FORKS[AVAX]); + + uint256 defaultEstimate = + PaymentHelper(getContract(AVAX, "PaymentHelper")).estimateAckCostDefault(true, ambIds_, ETH); + + uint256 realEstimate = PaymentHelper(getContract(AVAX, "PaymentHelper")).estimateAckCost(1); + + console.log("defaultEstimate: %s", defaultEstimate); + console.log("realEstimate: %s", realEstimate); + + assertLe(realEstimate, defaultEstimate); + + uint256 defaultEstimateNativeSrc = + PaymentHelper(getContract(AVAX, "PaymentHelper")).estimateAckCostDefaultNativeSource(true, ambIds_, ETH); + + console.log("defaultEstimateNativeSrc: %s", defaultEstimateNativeSrc); } /*///////////////////////////////////////////////////////////////