erigon-pulse/cmd/rpcdaemon22/commands/starknet_call.go
ledgerwatch 8e3ac8a21c
Erigon2 upgrade 2 prototype (#4341)
* Erigon2 upgrade 2 prototype

* Latest erigon-lib

* Fixes

* Fix print

* Fix maxSpan

* Reduce maxSpan

* Remove duplicate joins

* TxNum

* Fix resuming

* first draft of history22

* Introduce historical reads

* Update to erigon-lib

* Update erigon-lib

* Update erigon-lib

* Fixes and tracing for checkChangeSets

* More trace

* Print account details

* fix getHeader

* Update to erigon-lib main

* Add tracer indices and event log indices

* Fix calltracer

* Fix calltracer

* Duplicate rpcdaemon into rpcdaemon22

* Fix tests

* Fix tests

* Fix tests

* Update to latest erigon-lib

Co-authored-by: Alexey Sharp <alexeysharp@Alexeys-iMac.local>
Co-authored-by: Alex Sharp <alexsharp@Alexs-MacBook-Pro.local>
2022-06-10 16:18:43 +01:00

97 lines
2.3 KiB
Go

package commands
import (
"context"
"github.com/ledgerwatch/erigon-lib/gointerfaces/starknet"
"github.com/ledgerwatch/erigon/common"
"github.com/ledgerwatch/erigon/rpc"
"google.golang.org/protobuf/types/known/anypb"
"google.golang.org/protobuf/types/known/wrapperspb"
"reflect"
"strings"
)
type StarknetGrpcCallArgs struct {
Inputs string
Address string
Function string
Code string
BlockHash string
BlockNumber int64
Network string
}
type StarknetCallRequest struct {
ContractAddress common.Address32
EntryPointSelector string
CallData []string
}
func (s StarknetGrpcCallArgs) ToMapAny() (result map[string]*anypb.Any) {
result = make(map[string]*anypb.Any)
v := reflect.ValueOf(s)
typeOfS := v.Type()
for i := 0; i < v.NumField(); i++ {
fieldName := strings.ToLower(typeOfS.Field(i).Name)
switch v.Field(i).Kind() {
case reflect.Int64:
result[fieldName], _ = anypb.New(wrapperspb.Int64(v.Field(i).Interface().(int64)))
default:
result[fieldName], _ = anypb.New(wrapperspb.String(v.Field(i).Interface().(string)))
}
}
return result
}
// Call implements starknet_call.
func (api *StarknetImpl) Call(ctx context.Context, request StarknetCallRequest, blockNrOrHash rpc.BlockNumberOrHash) ([]string, error) {
tx, err := api.db.BeginRo(ctx)
if err != nil {
return nil, err
}
defer tx.Rollback()
code, err := api.GetCode(ctx, request.ContractAddress.ToCommonAddress(), blockNrOrHash)
if err != nil {
return nil, err
}
requestParams := &StarknetGrpcCallArgs{
Inputs: strings.Join(request.CallData, ","),
Address: request.ContractAddress.String(),
Function: request.EntryPointSelector,
Code: code.String(),
}
if blockNrOrHash.BlockHash != nil {
requestParams.BlockHash = blockNrOrHash.BlockHash.String()
}
if blockNrOrHash.BlockNumber != nil {
requestParams.BlockNumber = blockNrOrHash.BlockNumber.Int64()
}
requestParamsMap := requestParams.ToMapAny()
grpcRequest := &starknet.CallRequest{
Method: "starknet_call",
Params: requestParamsMap,
}
response, err := api.client.Call(ctx, grpcRequest)
if err != nil {
return nil, err
}
var result []string
for _, v := range response.Result {
s := wrapperspb.String("")
v.UnmarshalTo(s)
result = append(result, s.GetValue())
}
return result, nil
}