Skip to content

Commit

Permalink
Merge pull request #48 from m-Peter/update-to-latest-additions
Browse files Browse the repository at this point in the history
Update to latest additions in flow-go and cadence
  • Loading branch information
m-Peter authored Feb 13, 2024
2 parents 6382a16 + db202a5 commit 31a2a66
Show file tree
Hide file tree
Showing 11 changed files with 2,720 additions and 94 deletions.
6 changes: 3 additions & 3 deletions api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,12 +225,12 @@ func (s *BlockChainAPI) GetBalance(
return nil, err
}

balance, ok := value.(cadence.UFix64)
balance, ok := value.(cadence.UInt)
if !ok {
return nil, fmt.Errorf("script doesn't return UFix64 as it should")
return nil, fmt.Errorf("script doesn't return UInt as it should")
}

return (*hexutil.Big)(big.NewInt(int64(balance))), nil
return (*hexutil.Big)(balance.Value), nil
}

// eth_getCode (returns the code for the given address)
Expand Down
41 changes: 20 additions & 21 deletions api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,7 @@ func TestBlockChainAPI(t *testing.T) {
mockFlowClient := new(mocks.MockAccessClient)
blockchainAPI = api.NewBlockChainAPI(config, store, mockFlowClient)

result, err := cadence.NewUFix64("1500.0")
require.NoError(t, err)
result := cadence.NewUInt(150000000000)
mockFlowClient.On("ExecuteScriptAtLatestBlock", mock.Anything, mock.Anything, mock.Anything).Return(result, nil)

key, _ := crypto.GenerateKey()
Expand Down Expand Up @@ -1003,7 +1002,7 @@ func TestBlockChainAPI(t *testing.T) {
}
returnValue := cadence.NewArray(
toBytes,
).WithType(cadence.NewVariableSizedArrayType(cadence.TheUInt8Type))
).WithType(cadence.NewVariableSizedArrayType(cadence.UInt8Type))
mockFlowClient.On("ExecuteScriptAtLatestBlock", mock.Anything, mock.Anything, mock.Anything).Return(returnValue, nil)

returnedValue, err := blockchainAPI.Call(
Expand Down Expand Up @@ -1152,15 +1151,15 @@ func transactionExecutedEvent(
stdlib.FlowLocation{},
"evm.TransactionExecuted",
[]cadence.Field{
cadence.NewField("blockHeight", cadence.UInt64Type{}),
cadence.NewField("transactionHash", cadence.StringType{}),
cadence.NewField("transaction", cadence.StringType{}),
cadence.NewField("failed", cadence.BoolType{}),
cadence.NewField("transactionType", cadence.UInt8Type{}),
cadence.NewField("gasConsumed", cadence.UInt64Type{}),
cadence.NewField("deployedContractAddress", cadence.StringType{}),
cadence.NewField("returnedValue", cadence.StringType{}),
cadence.NewField("logs", cadence.StringType{}),
cadence.NewField("blockHeight", cadence.UInt64Type),
cadence.NewField("transactionHash", cadence.StringType),
cadence.NewField("transaction", cadence.StringType),
cadence.NewField("failed", cadence.BoolType),
cadence.NewField("transactionType", cadence.UInt8Type),
cadence.NewField("gasConsumed", cadence.UInt64Type),
cadence.NewField("deployedContractAddress", cadence.StringType),
cadence.NewField("returnedValue", cadence.StringType),
cadence.NewField("logs", cadence.StringType),
},
nil,
),
Expand All @@ -1181,7 +1180,7 @@ func transactionExecutedEvent(
func blockExecutedEvent(
blockHeight uint64,
blockHash string,
totalSupply uint64,
totalSupply int64,
parentBlockHash string,
receiptRoot string,
transactionHashes []string,
Expand All @@ -1196,26 +1195,26 @@ func blockExecutedEvent(
stdlib.FlowLocation{},
"evm.BlockExecuted",
[]cadence.Field{
cadence.NewField("blockHeight", cadence.UInt64Type{}),
cadence.NewField("blockHash", cadence.StringType{}),
cadence.NewField("totalSupply", cadence.UInt64Type{}),
cadence.NewField("parentBlockHash", cadence.StringType{}),
cadence.NewField("receiptRoot", cadence.StringType{}),
cadence.NewField("blockHeight", cadence.UInt64Type),
cadence.NewField("blockHash", cadence.StringType),
cadence.NewField("totalSupply", cadence.IntType),
cadence.NewField("parentBlockHash", cadence.StringType),
cadence.NewField("receiptRoot", cadence.StringType),
cadence.NewField(
"transactionHashes",
cadence.NewVariableSizedArrayType(cadence.StringType{}),
cadence.NewVariableSizedArrayType(cadence.StringType),
),
},
nil,
),
Fields: []cadence.Value{
cadence.NewUInt64(blockHeight),
cadence.String(blockHash),
cadence.NewUInt64(totalSupply),
cadence.NewIntFromBig(big.NewInt(totalSupply)),
cadence.String(parentBlockHash),
cadence.String(receiptRoot),
cadence.NewArray(hashes).WithType(
cadence.NewVariableSizedArrayType(cadence.StringType{}),
cadence.NewVariableSizedArrayType(cadence.StringType),
),
},
}
Expand Down
2 changes: 1 addition & 1 deletion api/cadence/scripts/bridged_account_call.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ fun main(data: [UInt8], contractAddress: [UInt8; 20]): [UInt8] {
to: EVM.EVMAddress(bytes: contractAddress),
data: data,
gasLimit: 300000, // TODO(m-Peter): Maybe also pass this as script argument
value: EVM.Balance(flow: 0.0)
value: EVM.Balance(attoflow: 0)
)

return evmResult
Expand Down
4 changes: 2 additions & 2 deletions api/cadence/scripts/evm_address_balance.cdc
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import EVM from 0xf8d6e0586b0a20c7

access(all)
fun main(addressBytes: [UInt8; 20]): UFix64 {
fun main(addressBytes: [UInt8; 20]): UInt {
let address = EVM.EVMAddress(bytes: addressBytes)

return address.balance().flow
return address.balance().inAttoFLOW()
}
4 changes: 2 additions & 2 deletions api/cadence/transactions/create_bridged_account.cdc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ transaction(amount: UFix64) {
let auth: auth(Storage) &Account

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

Expand All @@ -20,7 +20,7 @@ transaction(amount: UFix64) {
log(account.address())
account.deposit(from: <-self.sentVault)

log(account.balance().flow)
log(account.balance())
self.auth.storage.save<@EVM.BridgedAccount>(<-account, to: StoragePath(identifier: "evm")!)
}
}
5 changes: 2 additions & 3 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func TestServerJSONRPCOveHTTPHandler(t *testing.T) {
}
returnValue := cadence.NewArray(
toBytes,
).WithType(cadence.NewVariableSizedArrayType(cadence.TheUInt8Type))
).WithType(cadence.NewVariableSizedArrayType(cadence.UInt8Type))
mockFlowClient.On(
"ExecuteScriptAtLatestBlock",
mock.Anything,
Expand Down Expand Up @@ -257,8 +257,7 @@ func TestServerJSONRPCOveHTTPHandler(t *testing.T) {
request := `{"jsonrpc":"2.0","id":1,"method":"eth_getBalance","params":["0x407d73d8a49eeb85d32cf465507dd71d507100c1","latest"]}`
expectedResponse := `{"jsonrpc":"2.0","id":1,"result":"0x22ecb25c00"}`

result, err := cadence.NewUFix64("1500.0")
require.NoError(t, err)
result := cadence.NewUInt(150000000000)
mockFlowClient.On(
"ExecuteScriptAtLatestBlock",
mock.Anything,
Expand Down
4 changes: 2 additions & 2 deletions api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
"github.com/onflow/cadence"
)

var byteArrayType = cadence.NewVariableSizedArrayType(cadence.TheUInt8Type)
var byteArrayType = cadence.NewVariableSizedArrayType(cadence.UInt8Type)

var evmAddressType = cadence.NewConstantSizedArrayType(
common.AddressLength,
cadence.TheUInt8Type,
cadence.UInt8Type,
)

func GethTxFromBytes(input hexutil.Bytes) (*types.Transaction, error) {
Expand Down
24 changes: 16 additions & 8 deletions cmd/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/onflow/flow-evm-gateway/storage"
"github.com/onflow/flow-go-sdk"
"github.com/onflow/flow-go-sdk/access/grpc"
"github.com/onflow/flow-go/fvm/evm/types"
"github.com/rs/zerolog"
)

Expand All @@ -25,11 +26,12 @@ const (
coinbaseAddr = "0xf02c1c8e6114b1dbe8937a39260b5b0a374432bb"
)

// TODO(m-Peter): These should be updates to the EVM location,
// as soon as it gets merged.
var blockExecutedType = (types.EVMLocation{}).TypeID(nil, string(types.EventTypeBlockExecuted))
var txExecutedType = (types.EVMLocation{}).TypeID(nil, string(types.EventTypeTransactionExecuted))

var evmEventTypes = []string{
"flow.evm.BlockExecuted",
"flow.evm.TransactionExecuted",
string(blockExecutedType),
string(txExecutedType),
}

func main() {
Expand Down Expand Up @@ -141,13 +143,19 @@ func runIndexer(ctx context.Context, store *storage.Store, logger zerolog.Logger

for _, event := range response.Events {
logger.Info().Msgf(" %s", event.Value)
if event.Type == "flow.evm.TransactionExecuted" {
store.StoreTransaction(ctx, event.Value)
if event.Type == "evm.TransactionExecuted" {
err := store.StoreTransaction(ctx, event.Value)
if err != nil {
logger.Error().Msgf("got error when storing tx: %s", err)
}
store.UpdateAccountNonce(ctx, event.Value)
store.StoreLog(ctx, event.Value)
}
if event.Type == "flow.evm.BlockExecuted" {
store.StoreBlock(ctx, event.Value)
if event.Type == "evm.BlockExecuted" {
err := store.StoreBlock(ctx, event.Value)
if err != nil {
logger.Error().Msgf("got error when storing block: %s", err)
}
}
}

Expand Down
Loading

0 comments on commit 31a2a66

Please sign in to comment.