Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add wflow handler #137

Merged
merged 20 commits into from
Dec 21, 2024
Merged
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
20 commits
Select commit Hold shift + click to select a range
6b0507d
add WFLOWTokenHandler definition
sisyphusSmiling Sep 18, 2024
a8dbbe8
update FlowEVMBridgeConfig init block
sisyphusSmiling Sep 18, 2024
1da5a87
remove checks on FlowToken Vault type from bridge
sisyphusSmiling Sep 18, 2024
a506223
fix FlowEVMBridgeUtils.borrowCOA()
sisyphusSmiling Sep 18, 2024
53ce62f
add initial WFLOW handler tests
sisyphusSmiling Sep 19, 2024
0de2049
Merge branch 'main' into add-wflow-handler
sisyphusSmiling Sep 19, 2024
940d174
remove previewnet from flow.json
sisyphusSmiling Oct 22, 2024
5924724
Merge branch 'main' into add-wflow-handler
sisyphusSmiling Dec 2, 2024
6ddfaaf
update Handler interfaces and WFLOWTokenHandler impl
sisyphusSmiling Dec 3, 2024
8c3241c
fix WFLOWHandler WFLOW withdrawal call
sisyphusSmiling Dec 3, 2024
7fb155c
update error messages
sisyphusSmiling Dec 4, 2024
9708f10
remove redundant test case
sisyphusSmiling Dec 6, 2024
80afa22
update README
sisyphusSmiling Dec 6, 2024
769a6a1
add TokenHandler disabling path & tests
sisyphusSmiling Dec 18, 2024
215b6e1
remove unused test lines
sisyphusSmiling Dec 18, 2024
3a958b9
Merge branch 'main' into add-wflow-handler
sisyphusSmiling Dec 20, 2024
027f676
add ability to remove FlowToken.Vault association to FlowEVMBridgeConfig
sisyphusSmiling Dec 20, 2024
4f9050d
fix Config.getEVMAddressAssociated which failed for handled tokens
sisyphusSmiling Dec 20, 2024
33ba50e
remove unused import
sisyphusSmiling Dec 20, 2024
8545872
fix type:evm association when setting TokenHandler evm address
sisyphusSmiling Dec 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,17 @@ side, it must escrow on the other as the native VM contract is owned by an exter

With fungible tokens in particular, there may be some cases where the Cadence contract is not deployed to the bridge
account, but the bridge still follows a mint/burn pattern in Cadence. These cases are handled via
[`TokenHandler`](./cadence/contracts/bridge/interfaces/FlowEVMBridgeHandlerInterfaces.cdc) implementations. Also know
that moving $FLOW to EVM is built into the `EVMAddress` object so any requests bridging $FLOW to EVM will simply
leverage this interface; however, moving $FLOW from EVM to Cadence must be done through the COA resource.
[`TokenHandler`](./cadence/contracts/bridge/interfaces/FlowEVMBridgeHandlerInterfaces.cdc) implementations.

Also know that moving $FLOW to EVM is built into the `EVMAddress` object via `EVMAddress.deposit(from: @FlowToken.Vault)`.
Conversely, moving $FLOW from EVM is facilitated via the `CadenceOwnedAccount.withdraw(balance: Balance): @FlowToken.Vault`
method. Given these existing interfaces, the bridge instead handles $FLOW as corresponding fungible token
implementations - `FungibleToken.Vault` in Cadence & ERC20 in EVM. Therefore, the bridge wraps $FLOW en route to EVM
(depositing WFLOW to the recipient) and unwraps WFLOW when bridging when moving from EVM. In short, the cross-VM
association for $FLOW as far as the bridge is concerned is `@FlowToken.Vault` <> WFLOW
(`0xd3bF53DAC106A0290B0483EcBC89d40FcC961f3e` on
[Testnet](https://evm-testnet.flowscan.io/address/0xd3bF53DAC106A0290B0483EcBC89d40FcC961f3e) &
[Mainnet](https://evm.flowscan.io/address/0xd3bF53DAC106A0290B0483EcBC89d40FcC961f3e)).

Below are transactions relevant to bridging fungible tokens:
- [`bridge_tokens_to_evm.cdc`](./cadence/transactions/bridge/tokens/bridge_tokens_to_evm.cdc)
Expand Down
8 changes: 0 additions & 8 deletions cadence/contracts/bridge/FlowEVMBridge.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,6 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
fun onboardByType(_ type: Type, feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}) {
pre {
!FlowEVMBridgeConfig.isPaused(): "Bridge operations are currently paused"
type != Type<@FlowToken.Vault>():
"$FLOW cannot be bridged via the VM bridge - use the CadenceOwnedAccount interface"
self.typeRequiresOnboarding(type) == true: "Onboarding is not needed for this type"
FlowEVMBridgeUtils.typeAllowsBridging(type):
"This type is not supported as defined by the project's development team"
Expand Down Expand Up @@ -353,11 +351,6 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
/* Handle $FLOW requests via EVM interface & return */
//
let vaultType = vault.getType()
if vaultType == Type<@FlowToken.Vault>() {
let flowVault <-vault as! @FlowToken.Vault
to.deposit(from: <-flowVault)
return
}

// Gather the vault balance before acting on the resource
let vaultBalance = vault.balance
Expand Down Expand Up @@ -446,7 +439,6 @@ contract FlowEVMBridge : IFlowEVMNFTBridge, IFlowEVMTokenBridge {
pre {
!FlowEVMBridgeConfig.isPaused(): "Bridge operations are currently paused"
!type.isSubtype(of: Type<@{NonFungibleToken.Collection}>()): "Mixed asset types are not yet supported"
!type.isInstance(Type<@FlowToken.Vault>()): "Must use the CadenceOwnedAccount interface to bridge $FLOW from EVM"
self.typeRequiresOnboarding(type) == false: "FungibleToken must first be onboarded"
FlowEVMBridgeConfig.isTypePaused(type) == false: "Bridging is currently paused for this token"
}
Expand Down
34 changes: 23 additions & 11 deletions cadence/contracts/bridge/FlowEVMBridgeConfig.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ contract FlowEVMBridgeConfig {
access(FlowEVMBridgeHandlerInterfaces.Admin)
fun enableHandler(targetType: Type) {
let handler = FlowEVMBridgeConfig.borrowTokenHandlerAdmin(targetType)
?? panic("No handler found for target Type")
?? panic("No handler found for target Type ".concat(targetType.identifier))
handler.enableBridging()

let targetEVMAddressHex = handler.getTargetEVMAddress()?.toString()
Expand All @@ -465,6 +465,26 @@ contract FlowEVMBridgeConfig {
isEnabled: handler.isEnabled()
)
}

/// Disables the TokenHandler for the given Type. If a TokenHandler does not exist for the given Type, the
/// operation reverts.
///
/// @param targetType: Cadence type indexing the relevant TokenHandler
///
/// @emits HandlerConfigured with the target Type, target EVM address, and whether the handler is enabled
///
access(FlowEVMBridgeHandlerInterfaces.Admin)
fun disableHandler(targetType: Type) {
let handler = FlowEVMBridgeConfig.borrowTokenHandlerAdmin(targetType)
?? panic("No handler found for target Type".concat(targetType.identifier))
handler.disableBridging()

emit HandlerConfigured(
targetType: handler.getTargetType()!.identifier,
targetEVMAddress: handler.getTargetEVMAddress()?.toString(),
isEnabled: handler.isEnabled()
)
}
}

init() {
Expand All @@ -474,16 +494,8 @@ contract FlowEVMBridgeConfig {
self.gasLimit = 15_000_000
self.paused = true

// Although $FLOW does not have ERC20 address, we associate the the Vault with the EVM address from which
// EVM transfers originate
// See FLIP #223 - https://github.com/onflow/flips/pull/225
let flowOriginationAddress = EVM.EVMAddress(
bytes: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0]
)
let flowVaultType = Type<@FlowToken.Vault>()
let flowOriginationAddressHex = flowOriginationAddress.toString()
self.registeredTypes = { flowVaultType: TypeEVMAssociation(associated: flowOriginationAddress) }
self.evmAddressHexToType = { flowOriginationAddressHex: flowVaultType }
self.registeredTypes = {}
self.evmAddressHexToType = {}

self.typeToTokenHandlers <- {}

Expand Down
Loading
Loading