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

[EVM] DRAFT - Replace EVM.BridgeRouter interface with implementation #5821

Closed
wants to merge 2 commits into from
Closed
Changes from all commits
Commits
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
35 changes: 26 additions & 9 deletions fvm/evm/stdlib/contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -684,25 +684,42 @@ contract EVM {
): @FungibleToken.Vault
}

/// Interface which captures a Capability to the bridge Accessor, saving it within the BridgeRouter resource
access(all)
resource interface BridgeRouter {
/// BridgeRouter captures a BridgeAccessor Capability and routes bridge calls from COAs to the BridgeAccessor
access(all) resource BridgeRouter {
/// Capability to the BridgeAccessor resource, initialized to nil
access(self) var bridgeAccessorCap: Capability<&{BridgeAccessor}>?

init() {
self.bridgeAccessorCap = nil
}

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(all) view fun borrowBridgeAccessor(): &{BridgeAccessor}
/// Returns a Bridge entitled reference to the underlying BridgeAccessor resource
access(all) view fun borrowBridgeAccessor(): &{BridgeAccessor} {
let cap = self.bridgeAccessorCap ?? panic("BridgeAccessor Capabaility is not yet set")
return cap.borrow() ?? panic("Problem retrieving BridgeAccessor reference")
}

/// Sets the BridgeAccessor Capability in the BridgeRouter
access(all) fun setBridgeAccessor(_ accessor: Capability<&{BridgeAccessor}>) {
/// Sets the BridgeAccessor Capability
access(all) fun setBridgeAccessor(_ accessorCap: Capability<&{BridgeAccessor}>) {
pre {
accessor.check(): "Invalid BridgeAccessor Capability provided"
accessorCap.check(): "Invalid BridgeAccessor Capability provided"
}
self.bridgeAccessorCap = accessorCap
}
}

/// Initializes the BridgeRouter resource. Can only be executed if there is not already a BridgeRouter resource.
access(all) fun initBridgeRouter() {
pre {
self.account.type(at: /storage/evmBridgeRouter) == nil: "BridgeRouter has already been initialized"
}
self.account.save(<-create BridgeRouter(), to: /storage/evmBridgeRouter)
}

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(self)
view fun borrowBridgeAccessor(): &{BridgeAccessor} {
return self.account.borrow<&{BridgeRouter}>(from: /storage/evmBridgeRouter)
return self.account.borrow<&BridgeRouter>(from: /storage/evmBridgeRouter)
?.borrowBridgeAccessor()
?? panic("Could not borrow reference to the EVM bridge")
}
Expand Down
Loading