Skip to content

Commit

Permalink
update EVM contract with 1.0 bridging interface
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Apr 18, 2024
1 parent f3b72b2 commit f617988
Showing 1 changed file with 39 additions and 30 deletions.
69 changes: 39 additions & 30 deletions fvm/evm/stdlib/contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ contract EVM {
access(all) entitlement Call
access(all) entitlement Deploy
access(all) entitlement Owner
access(all) entitlement Bridge

access(all)
event CadenceOwnedAccountCreated(addressBytes: [UInt8; 20])
Expand Down Expand Up @@ -321,22 +322,22 @@ contract EVM {
/// the bridge request
access(all)
fun depositNFT(
nft: @NonFungibleToken.NFT,
feeProvider: &{FungibleToken.Provider}
nft: @{NonFungibleToken.NFT},
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
) {
EVM.borrowBridgeAccessor().depositNFT(nft: <-nft, to: self.address(), feeProvider: feeProvider)
}

/// Bridges the given NFT from the EVM environment, requiring a Provider from which to withdraw a fee to fulfill
/// the bridge request. Note: the caller should own the requested NFT in EVM
access(all)
access(Owner | Bridge)
fun withdrawNFT(
type: Type,
id: UInt256,
feeProvider: &{FungibleToken.Provider}
): @NonFungibleToken.NFT {
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
): @{NonFungibleToken.NFT} {
return <- EVM.borrowBridgeAccessor().withdrawNFT(
caller: &self as &CadenceOwnedAccount,
caller: &self as auth(Call) &CadenceOwnedAccount,
type: type,
id: id,
feeProvider: feeProvider
Expand All @@ -347,23 +348,23 @@ contract EVM {
/// the bridge request
access(all)
fun depositTokens(
vault: @FungibleToken.Vault,
feeProvider: &{FungibleToken.Provider}
vault: @{FungibleToken.Vault},
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
) {
EVM.borrowBridgeAccessor().depositTokens(vault: <-vault, to: self.address(), feeProvider: feeProvider)
}

/// Bridges the given fungible tokens from the EVM environment, requiring a Provider from which to withdraw a
/// fee to fulfill the bridge request. Note: the caller should own the requested tokens & sufficient balance of
/// requested tokens in EVM
access(all)
access(Owner | Bridge)
fun withdrawTokens(
type: Type,
amount: UInt256,
feeProvider: &{FungibleToken.Provider}
): @FungibleToken.Vault {
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
): @{FungibleToken.Vault} {
return <- EVM.borrowBridgeAccessor().withdrawTokens(
caller: &self as &CadenceOwnedAccount,
caller: &self as auth(Call) &CadenceOwnedAccount,
type: type,
amount: amount,
feeProvider: feeProvider
Expand Down Expand Up @@ -574,59 +575,67 @@ contract EVM {
resource interface BridgeAccessor {

/// Endpoint enabling the bridging of an NFT to EVM
access(all)
access(Bridge)
fun depositNFT(
nft: @NonFungibleToken.NFT,
nft: @{NonFungibleToken.NFT},
to: EVMAddress,
feeProvider: &{FungibleToken.Provider}
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
)

/// Endpoint enabling the bridging of an NFT from EVM
access(all)
access(Bridge)
fun withdrawNFT(
caller: &CadenceOwnedAccount,
caller: auth(Call) &CadenceOwnedAccount,
type: Type,
id: UInt256,
feeProvider: &{FungibleToken.Provider}
): @NonFungibleToken.NFT
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
): @{NonFungibleToken.NFT}

/// Endpoint enabling the bridging of a fungible token vault to EVM
access(all)
access(Bridge)
fun depositTokens(
vault: @FungibleToken.Vault,
vault: @{FungibleToken.Vault},
to: EVMAddress,
feeProvider: &{FungibleToken.Provider}
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
)

/// Endpoint enabling the bridging of fungible tokens from EVM
access(all)
access(Bridge)
fun withdrawTokens(
caller: &CadenceOwnedAccount,
caller: auth(Call) &CadenceOwnedAccount,
type: Type,
amount: UInt256,
feeProvider: &{FungibleToken.Provider}
): @FungibleToken.Vault
feeProvider: auth(FungibleToken.Withdraw) &{FungibleToken.Provider}
): @{FungibleToken.Vault}
}

/// Interface which captures a Capability to the bridge Accessor, saving it within the BridgeRouter resource
access(all)
resource interface BridgeRouter {

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

/// Sets the BridgeAccessor Capability in the BridgeRouter
access(all) fun setBridgeAccessor(_ accessor: Capability<&{BridgeAccessor}>) {
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
access(self)
view fun borrowBridgeAccessor(): &{BridgeAccessor} {
return self.account.borrow<&{BridgeRouter}>(from: /storage/evmBridgeRouter)
view fun borrowBridgeAccessor(): auth(Bridge) &{BridgeAccessor} {
return self.account.storage.borrow<auth(Bridge) &{BridgeRouter}>(from: /storage/evmBridgeRouter)
?.borrowBridgeAccessor()
?? panic("Could not borrow reference to the EVM bridge")
}
Expand Down

0 comments on commit f617988

Please sign in to comment.