Skip to content

Commit

Permalink
update Handler interfaces and WFLOWTokenHandler impl
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Dec 3, 2024
1 parent 5924724 commit 6ddfaaf
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 33 deletions.
49 changes: 25 additions & 24 deletions cadence/contracts/bridge/FlowEVMBridgeHandlers.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,14 @@ access(all) contract FlowEVMBridgeHandlers {
}
}

/**
TODO:
- Configure handler
-
- Update association between FLOW & WFLOW
- Remove any pre-conditions on FLOW in bridging functionality
*/

/// Facilitates moving Flow between Cadence and EVM as WFLOW. Since WFLOW is an artifact of the EVM ecosystem,
/// wrapping the native token as an ERC20, it does not have a place in Cadence's fungible token ecosystem.
/// Given the native interface on EVM.CadenceOwnedAccount and EVM.EVMAddress to move FLOW between Cadence and EVM,
Expand Down Expand Up @@ -243,11 +251,6 @@ access(all) contract FlowEVMBridgeHandlers {
tokens: @{FungibleToken.Vault},
to: EVM.EVMAddress
) {
pre {
tokens.getType() == Type<@FlowToken.Vault>():
"Invalid token type=".concat(tokens.getType().identifier)
.concat(" - WFLOWTokenHandler only handles FlowToken Vault")
}
let flowVault <- tokens as! @FlowToken.Vault
let wflowAddress = self.getTargetEVMAddress()!

Expand Down Expand Up @@ -276,14 +279,14 @@ access(all) contract FlowEVMBridgeHandlers {
// Cover underflow
assert(
postBalance > preBalance,
message: "Balance decremented after wrapping FLOW"
message: "Escrowed WFLOW balance did not increment after wrapping FLOW"
)
// Confirm bridge COA's WFLOW balance has incremented by the expected amount
assert(
postBalance - preBalance == uintAmount,
message: "Balance after wrapping FLOW does not match requested amount - expected="
message: "Balance after wrapping FLOW does not match requested amount - expected "
.concat((preBalance + uintAmount).toString())
.concat(" actual=")
.concat(" but got ")
.concat((postBalance - preBalance).toString())
)

Expand Down Expand Up @@ -315,7 +318,11 @@ access(all) contract FlowEVMBridgeHandlers {
amount,
erc20Address: wflowAddress
)
assert(ufixAmount > 0.0, message: "Amount to bridge must be greater than 0")
assert(
ufixAmount > 0.0,
message: "Requested UInt256 amount ".concat(amount.toString()).concat(" converted to 0.0 ")
.concat(" - try bridging a larger amount to avoid UFix64 precision loss during conversion")
)

// Transfers WFLOW to bridge COA as escrow
FlowEVMBridgeUtils.mustEscrowERC20(
Expand Down Expand Up @@ -344,7 +351,7 @@ access(all) contract FlowEVMBridgeHandlers {
// Cover underflow
assert(
postBalance > preBalance,
message: "Balance did not increment after unwrapping WFLOW amount=".concat(amount.toString())
message: "Escrowed FLOW Balance did not increment after unwrapping WFLOW"
)
// Confirm bridge COA's FLOW balance has incremented by the expected amount
assert(
Expand All @@ -355,7 +362,7 @@ access(all) contract FlowEVMBridgeHandlers {
.concat((postBalance - preBalance).toString())
)

// Withdraw FLOW from bridge COA
// Withdraw escrowed FLOW from bridge COA
let withdrawBalance = EVM.Balance(attoflow: UInt(amount))
assert(
UInt256(withdrawBalance.attoflow) == amount,
Expand All @@ -367,9 +374,9 @@ access(all) contract FlowEVMBridgeHandlers {
let flowVault <- coa.withdraw(balance: withdrawBalance)
assert(
flowVault.balance == ufixAmount,
message: "Vault balance does not match requested amount - expected="
message: "Resulting Vault balance does not match requested amount - expected "
.concat(ufixAmount.toString())
.concat(" actual=")
.concat(" but returned ")
.concat(flowVault.balance.toString())
)
return <-flowVault
Expand All @@ -380,13 +387,15 @@ access(all) contract FlowEVMBridgeHandlers {
/// Sets the target type for the handler
access(FlowEVMBridgeHandlerInterfaces.Admin)
fun setTargetType(_ type: Type) {
panic("WFLOWTokenHandler has targetType set at initialization")
panic("WFLOWTokenHandler has targetType set to "
.concat(self.targetType.identifier).concat(" at initialization"))
}

/// Sets the target EVM address for the handler
access(FlowEVMBridgeHandlerInterfaces.Admin)
fun setTargetEVMAddress(_ address: EVM.EVMAddress) {
panic("WFLOWTokenHandler has EVMAddress set at initialization")
panic("WFLOWTokenHandler has EVMAddress set to "
.concat(self.targetEVMAddress.toString()).concat(" at initialization"))
}

/// Sets the target type for the handler
Expand All @@ -400,14 +409,6 @@ access(all) contract FlowEVMBridgeHandlers {
fun enableBridging() {
self.enabled = true
}

/* --- Internal --- */

/// Returns an entitled reference to the encapsulated minter resource
access(self)
view fun borrowMinter(): auth(FlowEVMBridgeHandlerInterfaces.Mint) &{FlowEVMBridgeHandlerInterfaces.TokenMinter}? {
return nil
}
}

/// This resource enables the configuration of Handlers. These Handlers are stored in FlowEVMBridgeConfig from which
Expand All @@ -417,7 +418,7 @@ access(all) contract FlowEVMBridgeHandlers {
/// Creates a new Handler and adds it to the bridge configuration
///
/// @param handlerType: The type of handler to create as defined in this contract
/// @param targetType: The type of the asset the handler will handle.
/// @param targetType: The type of the asset the handler will handle
/// @param targetEVMAddress: The EVM contract address the handler will handle, can be nil if still unknown
/// @param expectedMinterType: The Type of the expected minter to be set for the created TokenHandler
///
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ access(all) contract FlowEVMBridgeHandlerInterfaces {
access(Admin) fun enableBridging() {
pre {
self.getTargetType() != nil && self.getTargetEVMAddress() != nil:
"Cannot enable before setting bridge targets"
!self.isEnabled(): "Handler already enabled"
"Cannot enable before setting bridge target Type and EVM Address"
!self.isEnabled(): "Handler has already been enabled"
}
post {
self.isEnabled(): "Problem enabling Handler"
Expand All @@ -122,11 +122,16 @@ access(all) contract FlowEVMBridgeHandlerInterfaces {
/// Mints the specified amount of tokens
access(Mint) fun mint(amount: UFix64): @{FungibleToken.Vault} {
pre {
amount > 0.0: "Amount must be greater than 0"
amount > 0.0: "Attempting to mint 0.0 - Amount minted must be greater than 0"
}
post {
result.getType() == self.getMintedType(): "Invalid Vault type returned"
result.balance == amount: "Minted amount does not match requested amount"
result.getType() == self.getMintedType():
"TokenMinter ".concat(self.getType().identifier).concat(" with uuid ").concat(self.uuid.toString())
.concat(" expected to mint ").concat(self.getMintedType().identifier)
.concat(" but returned ").concat(result.getType().identifier)
result.balance == amount:
"Minted amount ".concat(result.balance.toString())
.concat(" does not match requested amount ").concat(amount.toString())
}
}
}
Expand All @@ -141,8 +146,15 @@ access(all) contract FlowEVMBridgeHandlerInterfaces {
to: EVM.EVMAddress
) {
pre {
self.isEnabled(): "Handler is not yet enabled"
tokens.getType() == self.getTargetType(): "Invalid Vault type"
self.isEnabled():
"TokenHandler ".concat(self.getType().identifier).concat(" with uuid ")
.concat(self.uuid.toString()).concat(" is not yet enabled")
tokens.getType() == self.getTargetType():
"TokenHandler ".concat(self.getType().identifier).concat(" with uuid ").concat(self.uuid.toString())
.concat(" expects ").concat(self.getTargetType()?.identifier ?? "nil")
.concat(" but received ").concat(tokens.getType().identifier)
tokens.balance > 0.0:
"Attempting to bridge 0.0 tokens - zero amounts are unsupported"
}
}
/// Fulfills a request to bridge tokens from the EVM side to the Cadence side
Expand All @@ -153,10 +165,16 @@ access(all) contract FlowEVMBridgeHandlerInterfaces {
protectedTransferCall: fun (): EVM.Result
): @{FungibleToken.Vault} {
pre {
self.isEnabled(): "Handler is not yet enabled"
self.isEnabled():
"TokenHandler ".concat(self.getType().identifier).concat(" with uuid ")
.concat(self.uuid.toString()).concat(" is not yet enabled")
amount > UInt256(0): "Attempting to bridge 0 tokens from EVM - zero amounts are unsupported"
}
post {
result.getType() == self.getTargetType(): "Invalid Vault type returned"
result.getType() == self.getTargetType():
"TokenHandler ".concat(self.getType().identifier).concat(" with uuid ").concat(self.uuid.toString())
.concat(" expected to return ").concat(self.getTargetType()?.identifier ?? "nil")
.concat(" but returned ").concat(result.getType().identifier)
}
}
}
Expand Down

0 comments on commit 6ddfaaf

Please sign in to comment.