Skip to content

Commit

Permalink
update EVM.BridgeRouter, add BridgeAccessorUpdated event & refactor i…
Browse files Browse the repository at this point in the history
…mplementations
  • Loading branch information
sisyphusSmiling committed Apr 17, 2024
1 parent a820914 commit 2300a65
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 15 deletions.
14 changes: 5 additions & 9 deletions cadence/contracts/bridge/FlowEVMBridgeAccessor.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -160,21 +160,17 @@ contract FlowEVMBridgeAccessor {
self.bridgeAccessorCap = nil
}

/// Sets the BridgeAccessor capability on the BridgeRouter
///
access(EVM.Bridge) fun setBridgeAccessorCap(_ cap: Capability<auth(EVM.Bridge) &{EVM.BridgeAccessor}>) {
pre {
cap.check(): "BridgeAccessor capability already set"
}
self.bridgeAccessorCap = cap
}

/// Returns an EVM.Bridge entitled reference to the underlying BridgeAccessor resource
///
access(EVM.Bridge) view fun borrowBridgeAccessor(): auth(EVM.Bridge) &{EVM.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(EVM.Bridge) fun setBridgeAccessor(_ accessorCap: Capability<auth(EVM.Bridge) &{EVM.BridgeAccessor}>) {
self.bridgeAccessorCap = accessorCap
}
}

init(publishToEVMAccount: Address) {
Expand Down
28 changes: 28 additions & 0 deletions cadence/contracts/standards/EVM.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ contract EVM {
access(all)
event FLOWTokensWithdrawn(addressBytes: [UInt8; 20], amount: UFix64)

/// BridgeAccessorUpdated is emitted when the BridgeAccessor Capability
/// is updated in the stored BridgeRouter along with identifying
/// information about both.
access(all)
event BridgeAccessorUpdated(
routerType: Type,
routerUUID: UInt64,
routerAddress: Address,
accessorType: Type,
accessorUUID: UInt64,
accessorAddress: Address
)

/// EVMAddress is an EVM-compatible address
access(all)
struct EVMAddress {
Expand Down Expand Up @@ -602,6 +615,21 @@ contract EVM {

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(Bridge) view fun borrowBridgeAccessor(): auth(Bridge) &{BridgeAccessor}

/// Sets the BridgeAccessor Capability in the BridgeRouter
access(Bridge) fun setBridgeAccessor(_ accessor: Capability<auth(Bridge) &{BridgeAccessor}>) {
pre {
accessor.check(): "Invalid BridgeAccessor Capability provided"
emit BridgeAccessorUpdated(
routerType: self.getType(),
routerUUID: self.uuid,
routerAddress: self.owner?.address ?? panic("Router must have an owner to be identified"),
accessorType: accessor.borrow().getType(),
accessorUUID: accessor.borrow()!.uuid,
accessorAddress: accessor.address
)
}
}
}

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
Expand Down
2 changes: 1 addition & 1 deletion cadence/tests/test_helpers.cdc

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,21 @@ import "FlowEVMBridgeAccessor"
///
transaction(name: String, provider: Address) {

prepare(signer: auth(Inbox, SaveValue) &Account) {
prepare(signer: auth(BorrowValue, ClaimInboxCapability, SaveValue) &Account) {
// Claim the BridgeAccessor Capability
let accessorCap = signer.inbox.claim<auth(EVM.Bridge) &FlowEVMBridgeAccessor.BridgeAccessor>(name, provider: provider)
?? panic("BridgeAccessor Capability not found")

// Ensure the Capability is valid
assert(accessorCap.check() == true, message: "Invalid BridgeAccessor Capability")

// Create a Router to store the Capability and set the BridgeAccessor Capability in the Router
// Create a Router to store the Capability and save the Router in storage
let router <- accessorCap.borrow()!.createBridgeRouter()
router.setBridgeAccessorCap(accessorCap)

// Save the Router in storage
signer.storage.save(<-router, to: /storage/evmBridgeRouter)

// Borrow the router from storage and set the BridgeAccessor Capability
let routerRef = signer.storage.borrow<auth(EVM.Bridge) &{EVM.BridgeRouter}>(from: /storage/evmBridgeRouter)
?? panic("BridgeRouter not found in storage")
routerRef.setBridgeAccessor(accessorCap)
}
}

0 comments on commit 2300a65

Please sign in to comment.