Skip to content

Commit

Permalink
Merge pull request #5381 from onflow/ramtin/5380-rename-bridge-account
Browse files Browse the repository at this point in the history
  • Loading branch information
turbolent authored Feb 17, 2024
2 parents b579900 + de4a549 commit 6976a4d
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 122 deletions.
38 changes: 19 additions & 19 deletions fvm/evm/evm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,9 +108,9 @@ func TestEVMAddressDeposit(t *testing.T) {
let vault <- minter.mintTokens(amount: 1.23)
destroy minter
let bridgedAccount <- EVM.createBridgedAccount()
bridgedAccount.deposit(from: <-vault)
destroy bridgedAccount
let cadenceOwnedAccount <- EVM.createCadenceOwnedAccount()
cadenceOwnedAccount.deposit(from: <-vault)
destroy cadenceOwnedAccount
}
`,
sc.EVMContract.Address.HexWithPrefix(),
Expand Down Expand Up @@ -158,14 +158,14 @@ func TestCOAWithdraw(t *testing.T) {
let vault <- minter.mintTokens(amount: 2.34)
destroy minter
let bridgedAccount <- EVM.createBridgedAccount()
bridgedAccount.deposit(from: <-vault)
let cadenceOwnedAccount <- EVM.createCadenceOwnedAccount()
cadenceOwnedAccount.deposit(from: <-vault)
let bal = EVM.Balance(0)
bal.setFLOW(flow: 1.23)
let vault2 <- bridgedAccount.withdraw(balance: bal)
let vault2 <- cadenceOwnedAccount.withdraw(balance: bal)
let balance = vault2.balance
destroy bridgedAccount
destroy cadenceOwnedAccount
destroy vault2
return balance
Expand All @@ -190,7 +190,7 @@ func TestCOAWithdraw(t *testing.T) {
})
}

func TestBridgedAccountDeploy(t *testing.T) {
func TestCadenceOwnedAccountDeploy(t *testing.T) {
t.Parallel()
chain := flow.Emulator.Chain()
sc := systemcontracts.SystemContractsForChain(chain.ChainID())
Expand All @@ -215,15 +215,15 @@ func TestBridgedAccountDeploy(t *testing.T) {
let vault <- minter.mintTokens(amount: 2.34)
destroy minter
let bridgedAccount <- EVM.createBridgedAccount()
bridgedAccount.deposit(from: <-vault)
let cadenceOwnedAccount <- EVM.createCadenceOwnedAccount()
cadenceOwnedAccount.deposit(from: <-vault)
let address = bridgedAccount.deploy(
let address = cadenceOwnedAccount.deploy(
code: [],
gasLimit: 53000,
value: EVM.Balance(attoflow: 1230000000000000000)
)
destroy bridgedAccount
destroy cadenceOwnedAccount
return address.bytes
}
`,
Expand Down Expand Up @@ -330,11 +330,11 @@ func TestCadenceArch(t *testing.T) {
transaction {
prepare(account: AuthAccount) {
let bridgedAccount1 <- EVM.createBridgedAccount()
account.save<@EVM.BridgedAccount>(<-bridgedAccount1,
to: /storage/bridgedAccount)
account.link<&EVM.BridgedAccount{EVM.Addressable}>(/public/bridgedAccount,
target: /storage/bridgedAccount)
let cadenceOwnedAccount1 <- EVM.createCadenceOwnedAccount()
account.save<@EVM.CadenceOwnedAccount>(<-cadenceOwnedAccount1,
to: /storage/coa)
account.link<&EVM.CadenceOwnedAccount{EVM.Addressable}>(/public/coa,
target: /storage/coa)
}
}
`,
Expand All @@ -351,7 +351,7 @@ func TestCadenceArch(t *testing.T) {
require.NoError(t, output.Err)
snapshot = snapshot.Append(es)

// 3rd event is the bridged account created event
// 3rd event is the cadence owned account created event
coaAddress, err := types.COAAddressFromFlowEvent(sc.EVMContract.Address, output.Events[2])
require.NoError(t, err)

Expand All @@ -366,7 +366,7 @@ func TestCadenceArch(t *testing.T) {
proof := types.COAOwnershipProof{
KeyIndices: []uint64{0},
Address: types.FlowAddress(account),
CapabilityPath: "bridgedAccount",
CapabilityPath: "coa",
Signatures: []types.Signature{types.Signature(sig)},
}

Expand Down
28 changes: 14 additions & 14 deletions fvm/evm/stdlib/contract.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import "FlowToken"
access(all)
contract EVM {

pub event BridgedAccountCreated(addressBytes: [UInt8; 20])
pub event CadenceOwnedAccountCreated(addressBytes: [UInt8; 20])

/// EVMAddress is an EVM-compatible address
access(all)
Expand Down Expand Up @@ -76,7 +76,7 @@ contract EVM {
}

access(all)
resource BridgedAccount: Addressable {
resource CadenceOwnedAccount: Addressable {

access(self)
var addressBytes: [UInt8; 20]
Expand All @@ -85,7 +85,7 @@ contract EVM {
// address is initially set to zero
// but updated through initAddress later
// we have to do this since we need resource id (uuid)
// to calculate the EVM address for this bridge account
// to calculate the EVM address for this cadence owned account
self.addressBytes = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}

Expand All @@ -99,20 +99,20 @@ contract EVM {
self.addressBytes = addressBytes
}

/// The EVM address of the bridged account
/// The EVM address of the cadence owned account
access(all)
fun address(): EVMAddress {
// Always create a new EVMAddress instance
return EVMAddress(bytes: self.addressBytes)
}

/// Get balance of the bridged account
/// Get balance of the cadence owned account
access(all)
fun balance(): Balance {
return self.address().balance()
}

/// Deposits the given vault into the bridged account's balance
/// Deposits the given vault into the cadence owned account's balance
access(all)
fun deposit(from: @FlowToken.Vault) {
InternalEVM.deposit(
Expand All @@ -121,7 +121,7 @@ contract EVM {
)
}

/// Withdraws the balance from the bridged account's balance
/// Withdraws the balance from the cadence owned account's balance
/// Note that amounts smaller than 10nF (10e-8) can't be withdrawn
/// given that Flow Token Vaults use UFix64s to store balances.
/// If the given balance conversion to UFix64 results in
Expand Down Expand Up @@ -171,13 +171,13 @@ contract EVM {
}
}

/// Creates a new bridged account
/// Creates a new cadence owned account
access(all)
fun createBridgedAccount(): @BridgedAccount {
let acc <-create BridgedAccount()
let addr = InternalEVM.createBridgedAccount(uuid: acc.uuid)
fun createCadenceOwnedAccount(): @CadenceOwnedAccount {
let acc <-create CadenceOwnedAccount()
let addr = InternalEVM.createCadenceOwnedAccount(uuid: acc.uuid)
acc.initAddress(addressBytes: addr)
emit BridgedAccountCreated(addressBytes: addr)
emit CadenceOwnedAccountCreated(addressBytes: addr)
return <-acc
}

Expand Down Expand Up @@ -281,8 +281,8 @@ contract EVM {
assert(isValid, message: "signatures not valid")

let coaRef = acc.getCapability(path)
.borrow<&EVM.BridgedAccount{EVM.Addressable}>()
?? panic("could not borrow bridge account's address")
.borrow<&EVM.CadenceOwnedAccount{EVM.Addressable}>()
?? panic("could not borrow coa resource addressable capability")

// verify evm address matching
var i = 0
Expand Down
34 changes: 17 additions & 17 deletions fvm/evm/stdlib/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -1137,9 +1137,9 @@ func newInternalEVMTypeCallFunction(
)
}

const internalEVMTypeCreateBridgedAccountFunctionName = "createBridgedAccount"
const internalEVMTypeCreateCadenceOwnedAccountFunctionName = "createCadenceOwnedAccount"

var internalEVMTypeCreateBridgedAccountFunctionType = &sema.FunctionType{
var internalEVMTypeCreateCadenceOwnedAccountFunctionType = &sema.FunctionType{
Parameters: []sema.Parameter{
{
Label: "uuid",
Expand All @@ -1149,13 +1149,13 @@ var internalEVMTypeCreateBridgedAccountFunctionType = &sema.FunctionType{
ReturnTypeAnnotation: sema.NewTypeAnnotation(evmAddressBytesType),
}

func newInternalEVMTypeCreateBridgedAccountFunction(
func newInternalEVMTypeCreateCadenceOwnedAccountFunction(
gauge common.MemoryGauge,
handler types.ContractHandler,
) *interpreter.HostFunctionValue {
return interpreter.NewHostFunctionValue(
gauge,
internalEVMTypeCreateBridgedAccountFunctionType,
internalEVMTypeCreateCadenceOwnedAccountFunctionType,
func(invocation interpreter.Invocation) interpreter.Value {
inter := invocation.Interpreter
uuid, ok := invocation.Arguments[0].(interpreter.UInt64Value)
Expand Down Expand Up @@ -1532,17 +1532,17 @@ func NewInternalEVMContractValue(
internalEVMContractStaticType,
InternalEVMContractType.Fields,
map[string]interpreter.Value{
internalEVMTypeRunFunctionName: newInternalEVMTypeRunFunction(gauge, handler),
internalEVMTypeCreateBridgedAccountFunctionName: newInternalEVMTypeCreateBridgedAccountFunction(gauge, handler),
internalEVMTypeCallFunctionName: newInternalEVMTypeCallFunction(gauge, handler),
internalEVMTypeDepositFunctionName: newInternalEVMTypeDepositFunction(gauge, handler),
internalEVMTypeWithdrawFunctionName: newInternalEVMTypeWithdrawFunction(gauge, handler),
internalEVMTypeDeployFunctionName: newInternalEVMTypeDeployFunction(gauge, handler),
internalEVMTypeBalanceFunctionName: newInternalEVMTypeBalanceFunction(gauge, handler),
internalEVMTypeEncodeABIFunctionName: newInternalEVMTypeEncodeABIFunction(gauge, location),
internalEVMTypeDecodeABIFunctionName: newInternalEVMTypeDecodeABIFunction(gauge, location),
internalEVMTypeCastToAttoFLOWFunctionName: newInternalEVMTypeCastToAttoFLOWFunction(gauge, handler),
internalEVMTypeCastToFLOWFunctionName: newInternalEVMTypeCastToFLOWFunction(gauge, handler),
internalEVMTypeRunFunctionName: newInternalEVMTypeRunFunction(gauge, handler),
internalEVMTypeCreateCadenceOwnedAccountFunctionName: newInternalEVMTypeCreateCadenceOwnedAccountFunction(gauge, handler),
internalEVMTypeCallFunctionName: newInternalEVMTypeCallFunction(gauge, handler),
internalEVMTypeDepositFunctionName: newInternalEVMTypeDepositFunction(gauge, handler),
internalEVMTypeWithdrawFunctionName: newInternalEVMTypeWithdrawFunction(gauge, handler),
internalEVMTypeDeployFunctionName: newInternalEVMTypeDeployFunction(gauge, handler),
internalEVMTypeBalanceFunctionName: newInternalEVMTypeBalanceFunction(gauge, handler),
internalEVMTypeEncodeABIFunctionName: newInternalEVMTypeEncodeABIFunction(gauge, location),
internalEVMTypeDecodeABIFunctionName: newInternalEVMTypeDecodeABIFunction(gauge, location),
internalEVMTypeCastToAttoFLOWFunctionName: newInternalEVMTypeCastToAttoFLOWFunction(gauge, handler),
internalEVMTypeCastToFLOWFunctionName: newInternalEVMTypeCastToFLOWFunction(gauge, handler),
},
nil,
nil,
Expand All @@ -1567,8 +1567,8 @@ var InternalEVMContractType = func() *sema.CompositeType {
),
sema.NewUnmeteredPublicFunctionMember(
ty,
internalEVMTypeCreateBridgedAccountFunctionName,
internalEVMTypeCreateBridgedAccountFunctionType,
internalEVMTypeCreateCadenceOwnedAccountFunctionName,
internalEVMTypeCreateCadenceOwnedAccountFunctionType,
"",
),
sema.NewUnmeteredPublicFunctionMember(
Expand Down
Loading

0 comments on commit 6976a4d

Please sign in to comment.