Skip to content

Commit

Permalink
add fee set & calculation tests
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Apr 15, 2024
1 parent 0d29f8b commit 23729c6
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 0 deletions.
12 changes: 12 additions & 0 deletions cadence/scripts/bridge/calculate_bridge_fee.cdc
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import "FlowEVMBridgeUtils"

/// Returns the calculated fee based on the number of bytes used to escrow an asset plus the base fee.
///
/// @param bytes: The number of bytes used to escrow an asset.
///
/// @return The calculated fee to be paid in FlowToken
///
access(all)
fun main(bytes used: UFix64): UFix64 {
return FlowEVMBridgeUtils.calculateBridgeFee(bytes: used)
}
71 changes: 71 additions & 0 deletions cadence/tests/flow_evm_bridge_tests.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import BlockchainHelpers

import "FungibleToken"
import "NonFungibleToken"
import "FlowStorageFees"

import "test_helpers.cdc"

Expand Down Expand Up @@ -33,6 +34,9 @@ access(all) let erc721URI = "URI"
// ERC20 values
access(all) let erc20MintAmount: UInt256 = 100_000_000_000_000_000_000

access(all) let expectedOnboardFee = 1.0
access(all) let expectedBaseFee = 0.001

access(all)
fun setup() {
// Deploy supporting util contracts
Expand Down Expand Up @@ -365,6 +369,73 @@ fun testMintERC20Succeeds() {
Test.assertEqual(erc20MintAmount, aliceBalance)
}

access(all)
fun testUpdateBridgeFeesSucceeds() {
let bytesUsed: UInt64 = 1024
let expectedFinalFee = FlowStorageFees.storageCapacityToFlow(
FlowStorageFees.convertUInt64StorageBytesToUFix64Megabytes(bytesUsed)
) + expectedBaseFee

// Validate the initialized values are set to 0.0
var actualOnboardFeeResult = executeScript(
"../scripts/config/get_onboard_fee.cdc",
[]
)
Test.expect(actualOnboardFeeResult, Test.beSucceeded())
var actualBaseFeeResult = executeScript(
"../scripts/config/get_base_fee.cdc",
[]
)
Test.expect(actualBaseFeeResult, Test.beSucceeded())

Test.assertEqual(0.0, actualOnboardFeeResult.returnValue as! UFix64? ?? panic("Problem getting onboard fee"))
Test.assertEqual(0.0, actualBaseFeeResult.returnValue as! UFix64? ?? panic("Problem getting base fee"))

var actualCalculatedResult = executeScript(
"../scripts/bridge/calculate_bridge_fee.cdc",
[bytesUsed]
)
Test.expect(actualCalculatedResult, Test.beSucceeded())
Test.assertEqual(0.0, actualCalculatedResult.returnValue as! UFix64? ?? panic("Problem getting calculated fee"))

// Set the fees to new values
let updateOnboardFeeResult = executeTransaction(
"../transactions/bridge/admin/update_onboard_fee.cdc",
[expectedOnboardFee],
bridgeAccount
)
Test.expect(updateOnboardFeeResult, Test.beSucceeded())
let updateBaseFeeResult = executeTransaction(
"../transactions/bridge/admin/update_base_fee.cdc",
[expectedBaseFee],
bridgeAccount
)
Test.expect(updateBaseFeeResult, Test.beSucceeded())

// Validate the values have been updated
actualOnboardFeeResult = executeScript(
"../scripts/config/get_onboard_fee.cdc",
[]
)
Test.expect(actualOnboardFeeResult, Test.beSucceeded())
actualBaseFeeResult = executeScript(
"../scripts/config/get_base_fee.cdc",
[]
)
Test.expect(actualBaseFeeResult, Test.beSucceeded())

Test.assertEqual(expectedOnboardFee, actualOnboardFeeResult.returnValue as! UFix64? ?? panic("Problem getting onboard fee"))
Test.assertEqual(expectedBaseFee, actualBaseFeeResult.returnValue as! UFix64? ?? panic("Problem getting base fee"))

actualCalculatedResult = executeScript(
"../scripts/bridge/calculate_bridge_fee.cdc",
[bytesUsed]
)
Test.expect(actualCalculatedResult, Test.beSucceeded())
Test.assertEqual(expectedFinalFee, actualCalculatedResult.returnValue as! UFix64? ?? panic("Problem getting calculated fee"))

}

/* --- ONBOARDING - Test asset onboarding to the bridge --- */

access(all)
Expand Down

0 comments on commit 23729c6

Please sign in to comment.