From d9d48f8e6db51ddbb5de3f1eedb38db5966cb980 Mon Sep 17 00:00:00 2001 From: ramtinms Date: Thu, 25 Jan 2024 22:16:52 -0800 Subject: [PATCH 1/6] update balance to store Int instead of UFix64 --- fvm/evm/evm_test.go | 6 +- fvm/evm/stdlib/contract.cdc | 53 ++++++++++----- fvm/evm/stdlib/contract.go | 116 ++++++++++++++++++++++++++------ fvm/evm/stdlib/contract_test.go | 23 +++---- 4 files changed, 149 insertions(+), 49 deletions(-) diff --git a/fvm/evm/evm_test.go b/fvm/evm/evm_test.go index acd0a3c8289..9beaf848eb5 100644 --- a/fvm/evm/evm_test.go +++ b/fvm/evm/evm_test.go @@ -196,7 +196,9 @@ func TestBridgedAccountWithdraw(t *testing.T) { let bridgedAccount <- EVM.createBridgedAccount() bridgedAccount.deposit(from: <-vault) - let vault2 <- bridgedAccount.withdraw(balance: EVM.Balance(flow: 1.23)) + let bal = EVM.Balance(0) + bal.setFLOW(flow: 1.23) + let vault2 <- bridgedAccount.withdraw(balance: bal) let balance = vault2.balance destroy bridgedAccount destroy vault2 @@ -258,7 +260,7 @@ func TestBridgedAccountDeploy(t *testing.T) { let address = bridgedAccount.deploy( code: [], gasLimit: 53000, - value: EVM.Balance(flow: 1.23) + value: EVM.Balance(attoflow: 1230000000000000000) ) destroy bridgedAccount return address.bytes diff --git a/fvm/evm/stdlib/contract.cdc b/fvm/evm/stdlib/contract.cdc index 3151542131b..9030a3c9fcc 100644 --- a/fvm/evm/stdlib/contract.cdc +++ b/fvm/evm/stdlib/contract.cdc @@ -22,28 +22,47 @@ contract EVM { let balance = InternalEVM.balance( address: self.bytes ) - - return Balance(flow: balance) + return Balance(attoflow: balance) } } access(all) struct Balance { - /// The balance in FLOW + /// The balance in atto-FLOW + /// Atto-FLOW is the smallest denomination of FLOW (1e10^-18 FLOW) + /// that is used to store account balances inside EVM + /// similar to the way WEI is used to store ETH divisible to 18 decimal places. + access(all) + var attoflow: Int + + /// Constructs a new balance + access(all) + init(attoflow: Int) { + self.attoflow = attoflow + } + + /// Sets the balance by a UFix64 (8 decimal points), the format + /// that is used in Cadence to store FLOW tokens. access(all) - let flow: UFix64 + fun setFLOW(flow: UFix64){ + self.attoflow = InternalEVM.castToAttoFLOW(balance: flow) + } - /// Constructs a new balance, given the balance in FLOW - init(flow: UFix64) { - self.flow = flow + /// Casts the balance to a UFix64 (rounding down) + /// Warning! casting a balance to a UFix64 which supports a lower level of precision + /// (8 decimal points in compare to 18) might result in rounding down error. + /// Use the toAttoFlow function if you care need more accuracy. + access(all) + fun inFLOW(): UFix64 { + return InternalEVM.castToFLOW(balance: self.attoflow) } - // TODO: - // /// Returns the balance in terms of atto-FLOW. - // /// Atto-FLOW is the smallest denomination of FLOW inside EVM - // access(all) - // fun toAttoFlow(): UInt64 + /// Returns the balance in Atto-FLOW + access(all) + fun inAttoFLOW(): Int { + return self.attoflow + } } access(all) @@ -79,11 +98,15 @@ contract EVM { } /// Withdraws the balance from the bridged 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 + /// rounding error, this function would fail. access(all) fun withdraw(balance: Balance): @FlowToken.Vault { let vault <- InternalEVM.withdraw( from: self.addressBytes, - amount: balance.flow + amount: balance.attoflow ) as! @FlowToken.Vault return <-vault } @@ -100,7 +123,7 @@ contract EVM { from: self.addressBytes, code: code, gasLimit: gasLimit, - value: value.flow + value: value.attoflow ) return EVMAddress(bytes: addressBytes) } @@ -119,7 +142,7 @@ contract EVM { to: to.bytes, data: data, gasLimit: gasLimit, - value: value.flow + value: value.attoflow ) } } diff --git a/fvm/evm/stdlib/contract.go b/fvm/evm/stdlib/contract.go index bd19dab6a80..f99814003a2 100644 --- a/fvm/evm/stdlib/contract.go +++ b/fvm/evm/stdlib/contract.go @@ -1019,7 +1019,7 @@ var internalEVMTypeCallFunctionType = &sema.FunctionType{ }, { Label: "value", - TypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.ByteArrayType), @@ -1120,12 +1120,12 @@ func newInternalEVMTypeCallFunction( // Get balance - balanceValue, ok := invocation.Arguments[4].(interpreter.UFix64Value) + balanceValue, ok := invocation.Arguments[4].(interpreter.IntValue) if !ok { panic(errors.NewUnreachableError()) } - balance := types.NewBalanceFromUFix64(cadence.UFix64(balanceValue)) + balance := types.NewBalance(balanceValue.BigInt) // Call const isAuthorized = true @@ -1242,7 +1242,7 @@ var internalEVMTypeBalanceFunctionType = &sema.FunctionType{ TypeAnnotation: sema.NewTypeAnnotation(evmAddressBytesType), }, }, - ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), + ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.IntType), } // newInternalEVMTypeBalanceFunction returns the Flow balance of the account @@ -1270,12 +1270,7 @@ func newInternalEVMTypeBalanceFunction( const isAuthorized = false account := handler.AccountByAddress(address, isAuthorized) - // TODO: return roundoff flag or handle it - ufix, _, err := types.ConvertBalanceToUFix64(account.Balance()) - if err != nil { - panic(err) - } - return interpreter.UFix64Value(ufix) + return interpreter.IntValue{BigInt: account.Balance()} }, ) } @@ -1290,7 +1285,7 @@ var internalEVMTypeWithdrawFunctionType = &sema.FunctionType{ }, { Label: "amount", - TypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.AnyResourceType), @@ -1321,12 +1316,12 @@ func newInternalEVMTypeWithdrawFunction( // Get amount - amountValue, ok := invocation.Arguments[1].(interpreter.UFix64Value) + amountValue, ok := invocation.Arguments[1].(interpreter.IntValue) if !ok { panic(errors.NewUnreachableError()) } - amount := types.NewBalanceFromUFix64(cadence.UFix64(amountValue)) + amount := types.NewBalance(amountValue.BigInt) // Withdraw @@ -1334,11 +1329,13 @@ func newInternalEVMTypeWithdrawFunction( account := handler.AccountByAddress(fromAddress, isAuthorized) vault := account.Withdraw(amount) - // TODO: return rounded off flag or handle it ? - ufix, _, err := types.ConvertBalanceToUFix64(vault.Balance()) + ufix, roundedOff, err := types.ConvertBalanceToUFix64(vault.Balance()) if err != nil { panic(err) } + if roundedOff { + panic(types.ErrWithdrawBalanceRounding) + } // TODO: improve: maybe call actual constructor return interpreter.NewCompositeValue( @@ -1379,7 +1376,7 @@ var internalEVMTypeDeployFunctionType = &sema.FunctionType{ }, { Label: "value", - TypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation(evmAddressBytesType), @@ -1431,12 +1428,12 @@ func newInternalEVMTypeDeployFunction( // Get value - amountValue, ok := invocation.Arguments[3].(interpreter.UFix64Value) + amountValue, ok := invocation.Arguments[3].(interpreter.IntValue) if !ok { panic(errors.NewUnreachableError()) } - amount := types.NewBalanceFromUFix64(cadence.UFix64(amountValue)) + amount := types.NewBalance(amountValue.BigInt) // Deploy @@ -1449,6 +1446,71 @@ func newInternalEVMTypeDeployFunction( ) } +const internalEVMTypeCastToAttoFLOWFunctionName = "castToAttoFLOW" + +var internalEVMTypeCastToAttoFLOWFunctionType = &sema.FunctionType{ + Parameters: []sema.Parameter{ + { + Label: "balance", + TypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), + }, + }, + ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.IntType), +} + +func newInternalEVMTypeCastToAttoFLOWFunction( + gauge common.MemoryGauge, + handler types.ContractHandler, +) *interpreter.HostFunctionValue { + return interpreter.NewHostFunctionValue( + gauge, + internalEVMTypeCallFunctionType, + func(invocation interpreter.Invocation) interpreter.Value { + balanceValue, ok := invocation.Arguments[0].(interpreter.UFix64Value) + if !ok { + panic(errors.NewUnreachableError()) + } + balance := types.NewBalanceFromUFix64(cadence.UFix64(balanceValue)) + return interpreter.IntValue{BigInt: balance} + }, + ) +} + +const internalEVMTypeCastToFLOWFunctionName = "castToFLOW" + +var internalEVMTypeCastToFLOWFunctionType = &sema.FunctionType{ + Parameters: []sema.Parameter{ + { + Label: "balance", + TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + }, + }, + ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), +} + +func newInternalEVMTypeCastToFLOWFunction( + gauge common.MemoryGauge, + handler types.ContractHandler, +) *interpreter.HostFunctionValue { + return interpreter.NewHostFunctionValue( + gauge, + internalEVMTypeCallFunctionType, + func(invocation interpreter.Invocation) interpreter.Value { + balanceValue, ok := invocation.Arguments[0].(interpreter.IntValue) + if !ok { + panic(errors.NewUnreachableError()) + } + balance := types.NewBalance(balanceValue.BigInt) + // ignoring the rounding error and let user handle it + v, _, err := types.ConvertBalanceToUFix64(balance) + if err != nil { + panic(err) + } + return interpreter.UFix64Value(v) + }, + ) +} + func NewInternalEVMContractValue( gauge common.MemoryGauge, handler types.ContractHandler, @@ -1469,6 +1531,8 @@ func NewInternalEVMContractValue( internalEVMTypeBalanceFunctionName: newInternalEVMTypeBalanceFunction(gauge, handler), internalEVMTypeEncodeABIFunctionName: newInternalEVMTypeEncodeABIFunction(gauge, location), internalEVMTypeDecodeABIFunctionName: newInternalEVMTypeDecodeABIFunction(gauge, location), + internalEVMTypeCastToAttoFLOWFunctionName: newInternalEVMTypeCastToAttoFLOWFunction(gauge, handler), + internalEVMTypeCastToFLOWFunctionName: newInternalEVMTypeCastToFLOWFunction(gauge, handler), }, nil, nil, @@ -1521,6 +1585,18 @@ var InternalEVMContractType = func() *sema.CompositeType { internalEVMTypeDeployFunctionType, "", ), + sema.NewUnmeteredPublicFunctionMember( + ty, + internalEVMTypeCastToAttoFLOWFunctionName, + internalEVMTypeCastToAttoFLOWFunctionType, + "", + ), + sema.NewUnmeteredPublicFunctionMember( + ty, + internalEVMTypeCastToFLOWFunctionName, + internalEVMTypeCastToFLOWFunctionType, + "", + ), sema.NewUnmeteredPublicFunctionMember( ty, internalEVMTypeBalanceFunctionName, @@ -1600,8 +1676,8 @@ func NewBalanceCadenceType(address common.Address) *cadence.StructType { "EVM.Balance", []cadence.Field{ { - Identifier: "flow", - Type: cadence.UFix64Type{}, + Identifier: "attoflow", + Type: cadence.IntType{}, }, }, nil, diff --git a/fvm/evm/stdlib/contract_test.go b/fvm/evm/stdlib/contract_test.go index 7ad8cc64adb..56e727b9af5 100644 --- a/fvm/evm/stdlib/contract_test.go +++ b/fvm/evm/stdlib/contract_test.go @@ -2644,8 +2644,8 @@ func TestBalanceConstructionAndReturn(t *testing.T) { import EVM from 0x1 access(all) - fun main(_ flow: UFix64): EVM.Balance { - return EVM.Balance(flow: flow) + fun main(_ attoflow: Int): EVM.Balance { + return EVM.Balance(attoflow: attoflow) } `) @@ -2692,8 +2692,7 @@ func TestBalanceConstructionAndReturn(t *testing.T) { // Run script - flowValue, err := cadence.NewUFix64FromParts(1, 23000000) - require.NoError(t, err) + flowValue := cadence.NewInt(1230000000000000000) result, err := rt.ExecuteScript( runtime.Script{ @@ -2986,13 +2985,15 @@ func TestBridgedAccountCall(t *testing.T) { access(all) fun main(): [UInt8] { let bridgedAccount <- EVM.createBridgedAccount() + let bal = EVM.Balance(0) + bal.setFLOW(flow: 1.23) let response = bridgedAccount.call( to: EVM.EVMAddress( bytes: [2, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0] ), data: [4, 5, 6], gasLimit: 9999, - value: EVM.Balance(flow: 1.23) + value: bal ) destroy bridgedAccount return response @@ -3237,7 +3238,7 @@ func TestBridgedAccountWithdraw(t *testing.T) { let bridgedAccount <- EVM.createBridgedAccount() bridgedAccount.deposit(from: <-vault) - let vault2 <- bridgedAccount.withdraw(balance: EVM.Balance(flow: 1.23)) + let vault2 <- bridgedAccount.withdraw(balance: EVM.Balance(attoflow: 1230000000000000000)) let balance = vault2.balance destroy bridgedAccount destroy vault2 @@ -3353,7 +3354,7 @@ func TestBridgedAccountDeploy(t *testing.T) { let address = bridgedAccount.deploy( code: [4, 5, 6], gasLimit: 9999, - value: EVM.Balance(flow: 1.23) + value: EVM.Balance(flow: 1230000000000000000) ) destroy bridgedAccount return address.bytes @@ -3442,13 +3443,11 @@ func TestEVMAccountBalance(t *testing.T) { contractsAddress := flow.BytesToAddress([]byte{0x1}) - expectedBalanceValue, err := cadence.NewUFix64FromParts(1, 1337000) + expectedBalanceValue := cadence.NewInt(1013370000000000000) expectedBalance := cadence. NewStruct([]cadence.Value{expectedBalanceValue}). WithType(stdlib.NewBalanceCadenceType(common.Address(contractsAddress))) - require.NoError(t, err) - handler := &testContractHandler{ flowTokenAddress: common.Address(contractsAddress), accountByAddress: func(fromAddress types.Address, isAuthorized bool) types.Account { @@ -3458,7 +3457,7 @@ func TestEVMAccountBalance(t *testing.T) { return &testFlowAccount{ address: fromAddress, balance: func() types.Balance { - return types.NewBalanceFromUFix64(expectedBalanceValue) + return types.NewBalance(expectedBalanceValue.Value) }, } }, @@ -3537,7 +3536,7 @@ func TestEVMAccountBalance(t *testing.T) { require.NoError(t, err) require.NoError(t, err) - require.Equal(t, expectedBalance, actual) + require.Equal(t, expectedBalance.ToGoValue(), actual.ToGoValue()) } func TestEVMAccountBalanceForABIOnlyContract(t *testing.T) { From f210e1f0d027a66688c014589065ca281bdde349 Mon Sep 17 00:00:00 2001 From: ramtinms Date: Thu, 25 Jan 2024 22:19:47 -0800 Subject: [PATCH 2/6] clean up --- fvm/evm/evm_test.go | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/fvm/evm/evm_test.go b/fvm/evm/evm_test.go index 9beaf848eb5..da05d183e8d 100644 --- a/fvm/evm/evm_test.go +++ b/fvm/evm/evm_test.go @@ -11,7 +11,6 @@ import ( "github.com/onflow/flow-go/fvm" "github.com/onflow/flow-go/fvm/evm/stdlib" - "github.com/onflow/flow-go/fvm/evm/testutils" . "github.com/onflow/flow-go/fvm/evm/testutils" "github.com/onflow/flow-go/fvm/storage/snapshot" "github.com/onflow/flow-go/fvm/systemcontracts" @@ -24,7 +23,7 @@ func TestEVMRun(t *testing.T) { t.Parallel() t.Run("testing EVM.run (happy case)", func(t *testing.T) { - RunWithTestBackend(t, func(backend *testutils.TestBackend) { + RunWithTestBackend(t, func(backend *TestBackend) { RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) { tc := GetStorageTestContract(t) RunWithDeployedContract(t, tc, backend, rootAddr, func(testContract *TestContract) { @@ -115,7 +114,7 @@ func TestEVMAddressDeposit(t *testing.T) { t.Parallel() - RunWithTestBackend(t, func(backend *testutils.TestBackend) { + RunWithTestBackend(t, func(backend *TestBackend) { RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) { tc := GetStorageTestContract(t) RunWithDeployedContract(t, tc, backend, rootAddr, func(testContract *TestContract) { @@ -170,7 +169,7 @@ func TestBridgedAccountWithdraw(t *testing.T) { t.Parallel() - RunWithTestBackend(t, func(backend *testutils.TestBackend) { + RunWithTestBackend(t, func(backend *TestBackend) { RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) { tc := GetStorageTestContract(t) RunWithDeployedContract(t, tc, backend, rootAddr, func(testContract *TestContract) { @@ -231,7 +230,7 @@ func TestBridgedAccountWithdraw(t *testing.T) { func TestBridgedAccountDeploy(t *testing.T) { t.Parallel() - RunWithTestBackend(t, func(backend *testutils.TestBackend) { + RunWithTestBackend(t, func(backend *TestBackend) { RunWithTestFlowEVMRootAddress(t, backend, func(rootAddr flow.Address) { tc := GetStorageTestContract(t) RunWithDeployedContract(t, tc, backend, rootAddr, func(testContract *TestContract) { From f000c8dafe7c2475fafbc3d749201551c152dfa3 Mon Sep 17 00:00:00 2001 From: ramtinms Date: Fri, 26 Jan 2024 01:44:07 -0800 Subject: [PATCH 3/6] fix typos --- fvm/evm/stdlib/contract.cdc | 2 +- fvm/evm/types/balance.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/fvm/evm/stdlib/contract.cdc b/fvm/evm/stdlib/contract.cdc index 9030a3c9fcc..5e5decc1470 100644 --- a/fvm/evm/stdlib/contract.cdc +++ b/fvm/evm/stdlib/contract.cdc @@ -30,7 +30,7 @@ contract EVM { struct Balance { /// The balance in atto-FLOW - /// Atto-FLOW is the smallest denomination of FLOW (1e10^-18 FLOW) + /// Atto-FLOW is the smallest denomination of FLOW (1e18 FLOW) /// that is used to store account balances inside EVM /// similar to the way WEI is used to store ETH divisible to 18 decimal places. access(all) diff --git a/fvm/evm/types/balance.go b/fvm/evm/types/balance.go index 1de293ae851..a586ef71170 100644 --- a/fvm/evm/types/balance.go +++ b/fvm/evm/types/balance.go @@ -21,10 +21,10 @@ var ( ) // Balance represents the balance of an address -// in the evm environment (Flow EVM), balances are kept in attoflow (1e10^-18 flow); +// in the evm environment (Flow EVM), balances are kept in attoflow (1e-18 flow); // the smallest denomination of FLOW token (similar to how Wei is used to store Eth) // But A Cadence FLOW Vault uses a Cadence.UFix64 to store values in Flow, which means -// 1e18^-8 is the smallest value that can be stored on the vault. +// 1e-8 is the smallest value that can be stored on the vault. // The balance here considers the highest precision (attoflow) but utility // function has been provided for conversion from/to UFix64 to prevent accidental // conversion errors and dealing with rounding errors. From 0243de5ceae2f54e57a98aa703904de23efdd21d Mon Sep 17 00:00:00 2001 From: ramtinms Date: Fri, 26 Jan 2024 11:15:07 -0800 Subject: [PATCH 4/6] fix test --- fvm/fvm_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fvm/fvm_test.go b/fvm/fvm_test.go index 6fb975b7007..d4ba4a5c4a4 100644 --- a/fvm/fvm_test.go +++ b/fvm/fvm_test.go @@ -3085,7 +3085,7 @@ func TestEVM(t *testing.T) { import EVM from %s pub fun main() { - let bal = EVM.Balance(flow: 1.0); + let bal = EVM.Balance(attoflow: 1000000000000000000); let acc <- EVM.createBridgedAccount(); // withdraw insufficient balance destroy acc.withdraw(balance: bal); From 1d407047dd7371fba76a238f362cec7384941c59 Mon Sep 17 00:00:00 2001 From: ramtinms Date: Fri, 26 Jan 2024 15:45:02 -0800 Subject: [PATCH 5/6] update to UInt --- fvm/evm/stdlib/contract.cdc | 6 +++--- fvm/evm/stdlib/contract.go | 26 +++++++++++++------------- fvm/evm/stdlib/contract_test.go | 14 +++++++------- 3 files changed, 23 insertions(+), 23 deletions(-) diff --git a/fvm/evm/stdlib/contract.cdc b/fvm/evm/stdlib/contract.cdc index 5e5decc1470..8dfec9eadb3 100644 --- a/fvm/evm/stdlib/contract.cdc +++ b/fvm/evm/stdlib/contract.cdc @@ -34,11 +34,11 @@ contract EVM { /// that is used to store account balances inside EVM /// similar to the way WEI is used to store ETH divisible to 18 decimal places. access(all) - var attoflow: Int + var attoflow: UInt /// Constructs a new balance access(all) - init(attoflow: Int) { + init(attoflow: UInt) { self.attoflow = attoflow } @@ -60,7 +60,7 @@ contract EVM { /// Returns the balance in Atto-FLOW access(all) - fun inAttoFLOW(): Int { + fun inAttoFLOW(): UInt { return self.attoflow } } diff --git a/fvm/evm/stdlib/contract.go b/fvm/evm/stdlib/contract.go index f99814003a2..bd07f358b8f 100644 --- a/fvm/evm/stdlib/contract.go +++ b/fvm/evm/stdlib/contract.go @@ -1019,7 +1019,7 @@ var internalEVMTypeCallFunctionType = &sema.FunctionType{ }, { Label: "value", - TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + TypeAnnotation: sema.NewTypeAnnotation(sema.UIntType), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.ByteArrayType), @@ -1120,7 +1120,7 @@ func newInternalEVMTypeCallFunction( // Get balance - balanceValue, ok := invocation.Arguments[4].(interpreter.IntValue) + balanceValue, ok := invocation.Arguments[4].(interpreter.UIntValue) if !ok { panic(errors.NewUnreachableError()) } @@ -1242,7 +1242,7 @@ var internalEVMTypeBalanceFunctionType = &sema.FunctionType{ TypeAnnotation: sema.NewTypeAnnotation(evmAddressBytesType), }, }, - ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.UIntType), } // newInternalEVMTypeBalanceFunction returns the Flow balance of the account @@ -1270,7 +1270,7 @@ func newInternalEVMTypeBalanceFunction( const isAuthorized = false account := handler.AccountByAddress(address, isAuthorized) - return interpreter.IntValue{BigInt: account.Balance()} + return interpreter.UIntValue{BigInt: account.Balance()} }, ) } @@ -1285,7 +1285,7 @@ var internalEVMTypeWithdrawFunctionType = &sema.FunctionType{ }, { Label: "amount", - TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + TypeAnnotation: sema.NewTypeAnnotation(sema.UIntType), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.AnyResourceType), @@ -1316,7 +1316,7 @@ func newInternalEVMTypeWithdrawFunction( // Get amount - amountValue, ok := invocation.Arguments[1].(interpreter.IntValue) + amountValue, ok := invocation.Arguments[1].(interpreter.UIntValue) if !ok { panic(errors.NewUnreachableError()) } @@ -1376,7 +1376,7 @@ var internalEVMTypeDeployFunctionType = &sema.FunctionType{ }, { Label: "value", - TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + TypeAnnotation: sema.NewTypeAnnotation(sema.UIntType), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation(evmAddressBytesType), @@ -1428,7 +1428,7 @@ func newInternalEVMTypeDeployFunction( // Get value - amountValue, ok := invocation.Arguments[3].(interpreter.IntValue) + amountValue, ok := invocation.Arguments[3].(interpreter.UIntValue) if !ok { panic(errors.NewUnreachableError()) } @@ -1455,7 +1455,7 @@ var internalEVMTypeCastToAttoFLOWFunctionType = &sema.FunctionType{ TypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), }, }, - ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.UIntType), } func newInternalEVMTypeCastToAttoFLOWFunction( @@ -1471,7 +1471,7 @@ func newInternalEVMTypeCastToAttoFLOWFunction( panic(errors.NewUnreachableError()) } balance := types.NewBalanceFromUFix64(cadence.UFix64(balanceValue)) - return interpreter.IntValue{BigInt: balance} + return interpreter.UIntValue{BigInt: balance} }, ) } @@ -1482,7 +1482,7 @@ var internalEVMTypeCastToFLOWFunctionType = &sema.FunctionType{ Parameters: []sema.Parameter{ { Label: "balance", - TypeAnnotation: sema.NewTypeAnnotation(sema.IntType), + TypeAnnotation: sema.NewTypeAnnotation(sema.UIntType), }, }, ReturnTypeAnnotation: sema.NewTypeAnnotation(sema.UFix64Type), @@ -1496,7 +1496,7 @@ func newInternalEVMTypeCastToFLOWFunction( gauge, internalEVMTypeCallFunctionType, func(invocation interpreter.Invocation) interpreter.Value { - balanceValue, ok := invocation.Arguments[0].(interpreter.IntValue) + balanceValue, ok := invocation.Arguments[0].(interpreter.UIntValue) if !ok { panic(errors.NewUnreachableError()) } @@ -1677,7 +1677,7 @@ func NewBalanceCadenceType(address common.Address) *cadence.StructType { []cadence.Field{ { Identifier: "attoflow", - Type: cadence.IntType{}, + Type: cadence.UIntType{}, }, }, nil, diff --git a/fvm/evm/stdlib/contract_test.go b/fvm/evm/stdlib/contract_test.go index 56e727b9af5..c93468f6d08 100644 --- a/fvm/evm/stdlib/contract_test.go +++ b/fvm/evm/stdlib/contract_test.go @@ -1581,9 +1581,9 @@ func TestEVMEncodeDecodeABIErrors(t *testing.T) { access(all) struct Token { access(all) let id: Int - access(all) var balance: Int + access(all) var balance: UInt - init(id: Int, balance: Int) { + init(id: Int, balance: UInt) { self.id = id self.balance = balance } @@ -2113,9 +2113,9 @@ func TestEVMEncodeDecodeABIErrors(t *testing.T) { access(all) struct Token { access(all) let id: Int - access(all) var balance: Int + access(all) var balance: UInt - init(id: Int, balance: Int) { + init(id: Int, balance: UInt) { self.id = id self.balance = balance } @@ -2644,7 +2644,7 @@ func TestBalanceConstructionAndReturn(t *testing.T) { import EVM from 0x1 access(all) - fun main(_ attoflow: Int): EVM.Balance { + fun main(_ attoflow: UInt): EVM.Balance { return EVM.Balance(attoflow: attoflow) } `) @@ -2692,7 +2692,7 @@ func TestBalanceConstructionAndReturn(t *testing.T) { // Run script - flowValue := cadence.NewInt(1230000000000000000) + flowValue := cadence.NewUInt(1230000000000000000) result, err := rt.ExecuteScript( runtime.Script{ @@ -3443,7 +3443,7 @@ func TestEVMAccountBalance(t *testing.T) { contractsAddress := flow.BytesToAddress([]byte{0x1}) - expectedBalanceValue := cadence.NewInt(1013370000000000000) + expectedBalanceValue := cadence.NewUInt(1013370000000000000) expectedBalance := cadence. NewStruct([]cadence.Value{expectedBalanceValue}). WithType(stdlib.NewBalanceCadenceType(common.Address(contractsAddress))) From a3241294f3696046d335d2625b3316a9df13057c Mon Sep 17 00:00:00 2001 From: ramtinms Date: Thu, 1 Feb 2024 09:34:22 -0800 Subject: [PATCH 6/6] clean up --- fvm/evm/evm_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fvm/evm/evm_test.go b/fvm/evm/evm_test.go index 4a6dd013ae6..e1c1a4c1c7a 100644 --- a/fvm/evm/evm_test.go +++ b/fvm/evm/evm_test.go @@ -198,8 +198,8 @@ func TestBridgedAccountWithdraw(t *testing.T) { let bridgedAccount <- EVM.createBridgedAccount() bridgedAccount.deposit(from: <-vault) - let bal = EVM.Balance(0) - bal.setFLOW(flow: 1.23) + let bal = EVM.Balance(0) + bal.setFLOW(flow: 1.23) let vault2 <- bridgedAccount.withdraw(balance: bal) let balance = vault2.balance destroy bridgedAccount