Skip to content

Commit

Permalink
Tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
firelizzard18 committed Jul 16, 2024
1 parent 6777ee7 commit 119f6c0
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 33 deletions.
17 changes: 16 additions & 1 deletion internal/api/ethereum/service.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package ethimpl

import (
Expand Down Expand Up @@ -28,6 +34,7 @@ func (s *Service) EthChainId(ctx context.Context) (*ethrpc.Number, error) {
}

func (s *Service) EthBlockNumber(ctx context.Context) (*ethrpc.Number, error) {
// TODO: Is this the right number?
ns, err := s.Network.NetworkStatus(ctx, api.NetworkStatusOptions{Partition: protocol.Directory})
if err != nil {
return nil, err
Expand All @@ -41,7 +48,9 @@ func (s *Service) EthGasPrice(ctx context.Context) (*ethrpc.Number, error) {
return nil, err
}
// Instead of the ACME precision, we have to use 18, because Ethereum
// wallets require native tokens to have a precision of 18
// wallets require native tokens to have a precision of 18.
//
// TODO: Verify this is the right result.
v := big.NewInt(1e18 / protocol.CreditPrecision)
v.Div(v, big.NewInt(int64(ns.Oracle.Price)))
return (*ethrpc.Number)(v), nil
Expand Down Expand Up @@ -77,5 +86,11 @@ func (s *Service) EthGetBlockByNumber(ctx context.Context, block string, expand
}

func (s *Service) AccTypedData(_ context.Context, txn *protocol.Transaction, sig protocol.Signature) (*encoding.EIP712Call, error) {
if txn == nil {
return nil, errors.BadRequest.WithFormat("missing transaction")
}
if sig == nil {
return nil, errors.BadRequest.WithFormat("missing signature")
}
return protocol.NewEIP712Call(txn, sig)
}
6 changes: 6 additions & 0 deletions pkg/api/ethereum/generate.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package ethrpc

//go:generate go run gitlab.com/accumulatenetwork/core/schema/cmd/generate schema schema.yml -w schema_gen.go
Expand Down
59 changes: 30 additions & 29 deletions pkg/api/ethereum/jsonrpc.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package ethrpc

import (
Expand All @@ -21,6 +27,8 @@ type JSONRPCHandler struct {
Service Service
}

var _ Service = (*JSONRPCClient)(nil)

func NewClient(endpoint string) Service {
return &JSONRPCClient{Endpoint: accumulate.ResolveWellKnownEndpoint(endpoint, "eth")}
}
Expand Down Expand Up @@ -62,7 +70,7 @@ func (h *JSONRPCHandler) eth_gasPrice(ctx context.Context, _ json.RawMessage) (a
}

func (c *JSONRPCClient) EthGetBalance(ctx context.Context, addr Address, block string) (*Number, error) {
return clientCall[*Number](c, ctx, "eth_getBalance", []any{})
return clientCall[*Number](c, ctx, "eth_getBalance", []any{addr, block})
}

func (h *JSONRPCHandler) eth_getBalance(ctx context.Context, raw json.RawMessage) (any, error) {
Expand All @@ -78,32 +86,13 @@ func (h *JSONRPCHandler) eth_getBlockByNumber(ctx context.Context, raw json.RawM
}

func (c *JSONRPCClient) AccTypedData(ctx context.Context, txn *protocol.Transaction, sig protocol.Signature) (*encoding.EIP712Call, error) {
return clientCall[*encoding.EIP712Call](c, ctx, "acc_typedData", map[string]any{
"transaction": txn,
"signature": sig,
})
return clientCall[*encoding.EIP712Call](c, ctx, "acc_typedData", []any{txn, sig})
}

func (h *JSONRPCHandler) acc_typedData(ctx context.Context, raw json.RawMessage) (any, error) {
var params struct {
Transaction *protocol.Transaction `json:"transaction"`
Signature encoding.JsonUnmarshalWith[protocol.Signature] `json:"signature"`
}
params.Signature.Func = protocol.UnmarshalSignatureJSON

err := json.Unmarshal(raw, &params)
if err != nil {
return nil, jsonrpc.InvalidParams.With(err, err)
}

if params.Transaction == nil {
return nil, jsonrpc.InvalidParams.With("missing transaction", nil)
}
if params.Signature.Value == nil {
return nil, jsonrpc.InvalidParams.With("missing signature", nil)
}

return h.Service.AccTypedData(ctx, params.Transaction, params.Signature.Value)
return handlerCall2(ctx, raw, func(ctx context.Context, txn *protocol.Transaction, sig *signatureWrapper) (any, error) {
return h.Service.AccTypedData(ctx, txn, sig.V)
})
}

func clientCall[V any](c *JSONRPCClient, ctx context.Context, method string, params any) (V, error) {
Expand All @@ -113,17 +102,15 @@ func clientCall[V any](c *JSONRPCClient, ctx context.Context, method string, par
}

func handlerCall2[V1, V2, R any](ctx context.Context, raw json.RawMessage, fn func(context.Context, V1, V2) (R, error)) (any, error) {
v1, v2, err := decode2[V1, V2](raw)
var v1 V1
var v2 V2
err := decodeN(raw, &v1, &v2)
if err != nil {
return nil, err
}
return fn(ctx, v1, v2)
}

func decode2[V1, V2 any](raw json.RawMessage) (v1 V1, v2 V2, err error) {
return v1, v2, decodeN(raw, &v1, &v2)
}

func decodeN(raw json.RawMessage, v ...any) error {
var params []json.RawMessage
err := json.Unmarshal(raw, &params)
Expand Down Expand Up @@ -173,3 +160,17 @@ func (h *JSONRPCHandler) ServeJSONRPC(ctx context.Context, method string, params
Message: fmt.Sprintf("%q is not a supported method", method),
}
}

type signatureWrapper struct {
V protocol.Signature
}

func (s *signatureWrapper) MarshalJSON() ([]byte, error) {
return json.Marshal(s.V)
}

func (s *signatureWrapper) UnmarshalJSON(b []byte) error {
var err error
s.V, err = protocol.UnmarshalSignatureJSON(b)
return err
}
3 changes: 0 additions & 3 deletions pkg/api/ethereum/schema.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@ $generate:
methods:
json: true

encode:
keepEmpty: true

import:
json: encoding/json

Expand Down
6 changes: 6 additions & 0 deletions pkg/api/ethereum/services.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package ethrpc

import (
Expand Down
6 changes: 6 additions & 0 deletions pkg/api/ethereum/types.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// Copyright 2024 The Accumulate Authors
//
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.

package ethrpc

import (
Expand Down

0 comments on commit 119f6c0

Please sign in to comment.