Skip to content

Commit

Permalink
feat: add estimateAckCostDefaultNativeSource
Browse files Browse the repository at this point in the history
  • Loading branch information
0xTimepunk committed Jan 23, 2024
1 parent ef3b8ff commit 99b0088
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
15 changes: 15 additions & 0 deletions src/interfaces/IPaymentHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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 //
//////////////////////////////////////////////////////////////
Expand Down
22 changes: 17 additions & 5 deletions src/payments/PaymentHelper.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -547,7 +545,7 @@ contract PaymentHelper is IPaymentHelper {
uint8[] memory ackAmbIds,
uint64 srcChainId
)
external
public
view
override
returns (uint256 totalFees)
Expand All @@ -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 //
//////////////////////////////////////////////////////////////
Expand Down
34 changes: 32 additions & 2 deletions test/unit/crosschain-data/extensions/CoreStateRegistry.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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);
}

/*///////////////////////////////////////////////////////////////
Expand Down

0 comments on commit 99b0088

Please sign in to comment.