Skip to content

Commit

Permalink
Merge pull request #500 from onflow/improve-coa-creation-transaction
Browse files Browse the repository at this point in the history
Improve COA creation transaction to gracefully handle existing COA in storage
  • Loading branch information
sideninja authored Sep 2, 2024
2 parents a0590c4 + 57f2aae commit e74a62d
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 11 deletions.
25 changes: 15 additions & 10 deletions services/requester/cadence/create_coa.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -3,24 +3,29 @@ import FungibleToken
import FlowToken

transaction(amount: UFix64) {
let sentVault: @FlowToken.Vault
let auth: auth(Storage) &Account

prepare(signer: auth(Storage) &Account) {
let vaultRef = signer.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(
from: /storage/flowTokenVault
) ?? panic("Could not borrow reference to the owner's Vault!")

self.sentVault <- vaultRef.withdraw(amount: amount) as! @FlowToken.Vault
self.auth = signer
}

execute {
// If the COA is already created & saved, there's nothing to do, just return.
if let coa = self.auth.storage.borrow<&EVM.CadenceOwnedAccount>(from: /storage/evm) {
return
}

let vaultRef = self.auth.storage.borrow<auth(FungibleToken.Withdraw) &FlowToken.Vault>(
from: /storage/flowTokenVault
) ?? panic("Could not borrow reference to the owner's Vault!")
let vault <- vaultRef.withdraw(amount: amount) as! @FlowToken.Vault

let account <- EVM.createCadenceOwnedAccount()
log(account.address())
account.deposit(from: <-self.sentVault)
account.deposit(from: <-vault)

log(account.balance())
self.auth.storage.save<@EVM.CadenceOwnedAccount>(<-account, to: StoragePath(identifier: "evm")!)
self.auth.storage.save<@EVM.CadenceOwnedAccount>(
<-account,
to: /storage/evm
)
}
}
3 changes: 2 additions & 1 deletion services/requester/requester.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,17 +181,18 @@ func NewEVM(

// create COA on the account
if config.CreateCOAResource {
// we ignore errors for now since creation of already existing COA resource will fail, which is fine for now
tx, err := evm.buildTransaction(
context.Background(),
evm.replaceAddresses(createCOAScript),
cadence.UFix64(coaFundingBalance),
)
if err != nil {
logger.Warn().Err(err).Msg("COA resource auto-creation failure")
return nil, fmt.Errorf("COA resource auto-creation failure: %w", err)
}
if err := evm.client.SendTransaction(context.Background(), *tx); err != nil {
logger.Warn().Err(err).Msg("failed to send COA resource auto-creation transaction")
return nil, fmt.Errorf("failed to send COA resource auto-creation transaction: %w", err)
}
}

Expand Down

0 comments on commit e74a62d

Please sign in to comment.