Skip to content

Commit

Permalink
add updated EVM contract methods
Browse files Browse the repository at this point in the history
  • Loading branch information
sisyphusSmiling committed Apr 12, 2024
1 parent 2519c65 commit d877f20
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
49 changes: 36 additions & 13 deletions cadence/contracts/standards/EVM.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ contract EVM {
access(all)
event CadenceOwnedAccountCreated(addressBytes: [UInt8; 20])

/// FLOWTokensDeposited is emitted when FLOW tokens is bridged
/// into the EVM environment. Note that this event is not emitted
/// for transfer of flow tokens between two EVM addresses.
access(all)
event FLOWTokensDeposited(addressBytes: [UInt8; 20], amount: UFix64)

/// FLOWTokensWithdrawn is emitted when FLOW tokens are bridged
/// out of the EVM environment. Note that this event is not emitted
/// for transfer of flow tokens between two EVM addresses.
access(all)
event FLOWTokensWithdrawn(addressBytes: [UInt8; 20], amount: UFix64)

/// EVMAddress is an EVM-compatible address
access(all)
struct EVMAddress {
Expand Down Expand Up @@ -66,10 +78,15 @@ contract EVM {
/// Deposits the given vault into the EVM account with the given address
access(all)
fun deposit(from: @FlowToken.Vault) {
let amount = from.balance
if amount == 0.0 {
panic("calling deposit function with an empty vault is not allowed")
}
InternalEVM.deposit(
from: <-from,
to: self.bytes
)
emit FLOWTokensDeposited(addressBytes: self.bytes, amount: amount)
}
}

Expand Down Expand Up @@ -110,6 +127,12 @@ contract EVM {
view fun inAttoFLOW(): UInt {
return self.attoflow
}

/// Returns true if the balance is zero
access(all)
fun isZero(): Bool {
return self.attoflow == 0
}
}

/// reports the status of evm execution.
Expand Down Expand Up @@ -220,10 +243,7 @@ contract EVM {
/// Deposits the given vault into the cadence owned account's balance
access(all)
fun deposit(from: @FlowToken.Vault) {
InternalEVM.deposit(
from: <-from,
to: self.addressBytes
)
self.address().deposit(from: <-from)
}

/// The EVM address of the cadence owned account behind an entitlement, acting as proof of access
Expand All @@ -239,10 +259,14 @@ contract EVM {
/// rounding error, this function would fail.
access(Owner | Withdraw)
fun withdraw(balance: Balance): @FlowToken.Vault {
if balance.isZero() {
panic("calling withdraw function with zero balance is not allowed")
}
let vault <- InternalEVM.withdraw(
from: self.addressBytes,
amount: balance.attoflow
) as! @FlowToken.Vault
emit FLOWTokensWithdrawn(addressBytes: self.addressBytes, amount: balance.inFLOW())
return <-vault
}

Expand Down Expand Up @@ -484,7 +508,6 @@ contract EVM {
}

let coaRef = acc.capabilities.borrow<&EVM.CadenceOwnedAccount>(path)

if coaRef == nil {
return ValidationResult(
isValid: false,
Expand All @@ -509,14 +532,6 @@ contract EVM {
)
}

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(self)
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")
}

/// Interface for a resource which acts as an entrypoint to the VM bridge
access(all)
resource interface BridgeAccessor {
Expand Down Expand Up @@ -563,4 +578,12 @@ contract EVM {
/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(Bridge) view fun borrowBridgeAccessor(): auth(Bridge) &{BridgeAccessor}
}

/// Returns a reference to the BridgeAccessor designated for internal bridge requests
access(self)
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")
}
}
Loading

0 comments on commit d877f20

Please sign in to comment.