Skip to content

Commit

Permalink
Merge branch 'main' into rpcv05-getTxnStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
cicr99 committed Oct 20, 2023
2 parents c04fb71 + 13df8a0 commit 93466d9
Show file tree
Hide file tree
Showing 13 changed files with 106 additions and 119 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ go run main.go
| `starknet_addDeployAccountTransaction` | :heavy_check_mark: |
| `starknet_traceTransaction` | :heavy_check_mark: |
| `starknet_simulateTransaction` | :heavy_check_mark: |
| `starknet_specVersion` | :heavy_check_mark: |
| `starknet_traceBlockTransactions` | :heavy_check_mark: |

### Run Tests
Expand Down
3 changes: 3 additions & 0 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,9 @@ func (account *Account) StorageAt(ctx context.Context, contractAddress *felt.Fel
func (account *Account) StateUpdate(ctx context.Context, blockID rpc.BlockID) (*rpc.StateUpdateOutput, error) {
return account.provider.StateUpdate(ctx, blockID)
}
func (account *Account) SpecVersion(ctx context.Context) (string, error) {
return account.provider.SpecVersion(ctx)
}
func (account *Account) Syncing(ctx context.Context) (*rpc.SyncStatus, error) {
return account.provider.Syncing(ctx)
}
Expand Down
15 changes: 15 additions & 0 deletions mocks/mock_rpc_provider.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 7 additions & 19 deletions rpc/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,8 @@ func (provider *Provider) BlockNumber(ctx context.Context) (uint64, error) {
// BlockHashAndNumber gets block information given the block number or its hash.
func (provider *Provider) BlockHashAndNumber(ctx context.Context) (*BlockHashAndNumberOutput, error) {
var block BlockHashAndNumberOutput
if err := do(ctx, provider.c, "starknet_blockHashAndNumber", &block); err != nil {
if errors.Is(err, errNotFound) {
return nil, ErrNoBlocks
}
return nil, err
if err := do(ctx, provider.c, "starknet_blockHashAndNumber", &block); err != nil {
return nil, tryUnwrapToRPCErr(err, ErrNoBlocks )
}
return &block, nil
}
Expand All @@ -52,11 +49,8 @@ func WithBlockTag(tag string) BlockID {
// BlockWithTxHashes gets block information given the block id.
func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID) (interface{}, error) {
var result BlockTxHashes
if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil {
if errors.Is(err, errNotFound) {
return nil, ErrBlockNotFound
}
return nil, err
if err := do(ctx, provider.c, "starknet_getBlockWithTxHashes", &result, blockID); err != nil {
return nil, tryUnwrapToRPCErr(err,ErrBlockNotFound )
}

// if header.Hash == nil it's a pending block
Expand All @@ -75,11 +69,8 @@ func (provider *Provider) BlockWithTxHashes(ctx context.Context, blockID BlockID
// StateUpdate gets the information about the result of executing the requested block.
func (provider *Provider) StateUpdate(ctx context.Context, blockID BlockID) (*StateUpdateOutput, error) {
var state StateUpdateOutput
if err := do(ctx, provider.c, "starknet_getStateUpdate", &state, blockID); err != nil {
if errors.Is(err, errNotFound) {
return nil, ErrBlockNotFound
}
return nil, err
if err := do(ctx, provider.c, "starknet_getStateUpdate", &state, blockID); err != nil {
return nil,tryUnwrapToRPCErr(err,ErrBlockNotFound )
}
return &state, nil
}
Expand All @@ -100,10 +91,7 @@ func (provider *Provider) BlockTransactionCount(ctx context.Context, blockID Blo
func (provider *Provider) BlockWithTxs(ctx context.Context, blockID BlockID) (interface{}, error) {
var result Block
if err := do(ctx, provider.c, "starknet_getBlockWithTxs", &result, blockID); err != nil {
if errors.Is(err, errNotFound) {
return nil, ErrBlockNotFound
}
return nil, err
return nil, tryUnwrapToRPCErr(err,ErrBlockNotFound )
}
// if header.Hash == nil it's a pending block
if result.BlockHeader.BlockHash == nil {
Expand Down
13 changes: 3 additions & 10 deletions rpc/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package rpc

import (
"context"
"errors"


"github.com/NethermindEth/juno/core/felt"
)
Expand All @@ -15,15 +15,8 @@ func (provider *Provider) Call(ctx context.Context, request FunctionCall, blockI
}
var result []*felt.Felt
if err := do(ctx, provider.c, "starknet_call", &result, request, blockID); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return nil, ErrContractNotFound
case errors.Is(err, ErrContractError):
return nil, ErrContractError
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrContractNotFound, ErrContractError, ErrBlockNotFound)
}
return result, nil
}
69 changes: 15 additions & 54 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package rpc
import (
"context"
"encoding/json"
"errors"

"fmt"

"github.com/NethermindEth/juno/core/felt"
Expand All @@ -14,13 +14,8 @@ import (
func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash *felt.Felt) (ClassOutput, error) {
var rawClass map[string]any
if err := do(ctx, provider.c, "starknet_getClass", &rawClass, blockID, classHash); err != nil {
switch {
case errors.Is(err, ErrClassHashNotFound):
return nil, ErrClassHashNotFound
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrClassHashNotFound, ErrBlockNotFound)
}

return typecastClassOutput(&rawClass)
Expand All @@ -31,13 +26,8 @@ func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash
func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (ClassOutput, error) {
var rawClass map[string]any
if err := do(ctx, provider.c, "starknet_getClassAt", &rawClass, blockID, contractAddress); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return nil, ErrContractNotFound
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrContractNotFound, ErrBlockNotFound)
}
return typecastClassOutput(&rawClass)
}
Expand Down Expand Up @@ -69,13 +59,8 @@ func typecastClassOutput(rawClass *map[string]any) (ClassOutput, error) {
func (provider *Provider) ClassHashAt(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*felt.Felt, error) {
var result *felt.Felt
if err := do(ctx, provider.c, "starknet_getClassHashAt", &result, blockID, contractAddress); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return nil, ErrContractNotFound
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrContractNotFound, ErrBlockNotFound)
}
return result, nil
}
Expand All @@ -85,13 +70,8 @@ func (provider *Provider) StorageAt(ctx context.Context, contractAddress *felt.F
var value string
hashKey := fmt.Sprintf("0x%x", utils.GetSelectorFromName(key))
if err := do(ctx, provider.c, "starknet_getStorageAt", &value, contractAddress, hashKey, blockID); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return "", ErrContractNotFound
case errors.Is(err, ErrBlockNotFound):
return "", ErrBlockNotFound
}
return "", err

return "", tryUnwrapToRPCErr(err, ErrContractNotFound, ErrBlockNotFound)
}
return value, nil
}
Expand All @@ -100,13 +80,8 @@ func (provider *Provider) StorageAt(ctx context.Context, contractAddress *felt.F
func (provider *Provider) Nonce(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*string, error) {
nonce := ""
if err := do(ctx, provider.c, "starknet_getNonce", &nonce, blockID, contractAddress); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return nil, ErrContractNotFound
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrContractNotFound, ErrBlockNotFound)
}
return &nonce, nil
}
Expand All @@ -115,15 +90,8 @@ func (provider *Provider) Nonce(ctx context.Context, blockID BlockID, contractAd
func (provider *Provider) EstimateFee(ctx context.Context, requests []EstimateFeeInput, blockID BlockID) ([]FeeEstimate, error) {
var raw []FeeEstimate
if err := do(ctx, provider.c, "starknet_estimateFee", &raw, requests, blockID); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return nil, ErrContractNotFound
case errors.Is(err, ErrContractError):
return nil, ErrContractError
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrContractNotFound,ErrContractError, ErrBlockNotFound)
}
return raw, nil
}
Expand All @@ -132,15 +100,8 @@ func (provider *Provider) EstimateFee(ctx context.Context, requests []EstimateFe
func (provider *Provider) EstimateMessageFee(ctx context.Context, msg MsgFromL1, blockID BlockID) (*FeeEstimate, error) {
var raw FeeEstimate
if err := do(ctx, provider.c, "starknet_estimateMessageFee", &raw, msg, blockID); err != nil {
switch {
case errors.Is(err, ErrContractNotFound):
return nil, ErrContractNotFound
case errors.Is(err, ErrContractError):
return nil, ErrContractError
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrContractNotFound,ErrContractError, ErrBlockNotFound)
}
return &raw, nil
}
15 changes: 3 additions & 12 deletions rpc/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,15 @@ package rpc

import (
"context"
"errors"

)

// Events returns all events matching the given filter
func (provider *Provider) Events(ctx context.Context, input EventsInput) (*EventChunk, error) {
var result EventChunk
if err := do(ctx, provider.c, "starknet_getEvents", &result, input); err != nil {
switch {
case errors.Is(err, ErrPageSizeTooBig):
return nil, ErrPageSizeTooBig
case errors.Is(err, ErrInvalidContinuationToken):
return nil, ErrInvalidContinuationToken
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
case errors.Is(err, ErrTooManyKeysInFilter):
return nil, ErrTooManyKeysInFilter
}
return nil, err

return nil, tryUnwrapToRPCErr(err, ErrPageSizeTooBig , ErrInvalidContinuationToken , ErrBlockNotFound ,ErrTooManyKeysInFilter)
}
return &result, nil
}
1 change: 1 addition & 0 deletions rpc/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ type RpcProvider interface {
SimulateTransactions(ctx context.Context, blockID BlockID, txns []Transaction, simulationFlags []SimulationFlag) ([]SimulatedTransaction, error)
StateUpdate(ctx context.Context, blockID BlockID) (*StateUpdateOutput, error)
StorageAt(ctx context.Context, contractAddress *felt.Felt, key string, blockID BlockID) (string, error)
SpecVersion(ctx context.Context) (string, error)
Syncing(ctx context.Context) (*SyncStatus, error)
TraceBlockTransactions(ctx context.Context, blockHash *felt.Felt) ([]Trace, error)
TransactionByBlockIdAndIndex(ctx context.Context, blockID BlockID, index uint64) (Transaction, error)
Expand Down
22 changes: 6 additions & 16 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,18 @@ func (provider *Provider) TransactionByHash(ctx context.Context, hash *felt.Felt
// todo: update to return a custom Transaction type, then use adapt function
var tx TXN
if err := do(ctx, provider.c, "starknet_getTransactionByHash", &tx, hash); err != nil {
if errors.Is(err, ErrHashNotFound) {
return nil, ErrHashNotFound
}
return nil, err
}
return nil, tryUnwrapToRPCErr(err,ErrHashNotFound)
}
return adaptTransaction(tx)
}

// TransactionByBlockIdAndIndex Get the details of the transaction given by the identified block and index in that block. If no transaction is found, null is returned.
func (provider *Provider) TransactionByBlockIdAndIndex(ctx context.Context, blockID BlockID, index uint64) (Transaction, error) {
var tx TXN
if err := do(ctx, provider.c, "starknet_getTransactionByBlockIdAndIndex", &tx, blockID, index); err != nil {
switch {
case errors.Is(err, ErrInvalidTxnIndex):
return nil, ErrInvalidTxnIndex
case errors.Is(err, ErrBlockNotFound):
return nil, ErrBlockNotFound
}
return nil, err

return nil,tryUnwrapToRPCErr(err, ErrInvalidTxnIndex ,ErrBlockNotFound)

}
return adaptTransaction(tx)
}
Expand All @@ -90,10 +83,7 @@ func (provider *Provider) TransactionReceipt(ctx context.Context, transactionHas
var receipt UnknownTransactionReceipt
err := do(ctx, provider.c, "starknet_getTransactionReceipt", &receipt, transactionHash)
if err != nil {
if errors.Is(err, ErrHashNotFound) {
return nil, ErrHashNotFound
}
return nil, err
return nil, tryUnwrapToRPCErr(err,ErrHashNotFound)
}
return receipt.TransactionReceipt, nil
}
Expand Down
12 changes: 12 additions & 0 deletions rpc/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package rpc

import "context"

// SpecVersion returns the version of the Starknet JSON-RPC specification being used
// Parameters: None
// Returns: String of the Starknet JSON-RPC specification
func (provider *Provider) SpecVersion(ctx context.Context) (string, error) {
var result string
err := do(ctx, provider.c, "starknet_specVersion", &result)
return result, err
}
32 changes: 32 additions & 0 deletions rpc/version_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package rpc

import (
"context"
"testing"

"github.com/test-go/testify/require"
)

// TestSpecVersion tests starknet_specVersion
func TestSpecVersion(t *testing.T) {

testConfig := beforeEach(t)

type testSetType struct {
ExpectedResp string
}
testSet := map[string][]testSetType{
"devnet": {},
"mainnet": {},
"mock": {},
"testnet": {{
ExpectedResp: "0.5.0",
}},
}[testEnv]

for _, test := range testSet {
resp, err := testConfig.provider.SpecVersion(context.Background())
require.NoError(t, err)
require.Equal(t, test.ExpectedResp, resp)
}
}
6 changes: 3 additions & 3 deletions typed/typed.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,11 +105,11 @@ func NewTypedData(types map[string]TypeDef, pType string, dom Domain) (td TypedD
return td, nil
}

// (ref: https://github.com/0xs34n/starknet.js/blob/767021a203ac0b9cdb282eb6d63b33bfd7614858/src/utils/typedData/index.ts#L166)
// (ref: https://github.com/starknet-io/starknet.js/blob/d7bfc37ede85448e0a55ee4efe65200ff2c45f91/src/utils/typedData.ts#L249)
func (td TypedData) GetMessageHash(account *big.Int, msg TypedMessage, sc curve.StarkCurve) (hash *big.Int, err error) {
elements := []*big.Int{utils.UTF8StrToBig("Starknet Message")}
elements := []*big.Int{utils.UTF8StrToBig("StarkNet Message")}

domEnc, err := td.GetTypedMessageHash("StarknetDomain", td.Domain, sc)
domEnc, err := td.GetTypedMessageHash("StarkNetDomain", td.Domain, sc)
if err != nil {
return hash, fmt.Errorf("could not hash domain: %w", err)
}
Expand Down
Loading

0 comments on commit 93466d9

Please sign in to comment.