Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor to use use unwrapErr() throughout the codebase #346

Merged
merged 13 commits into from
Oct 19, 2023
11 changes: 2 additions & 9 deletions rpc/call.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
67 changes: 14 additions & 53 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
13 changes: 2 additions & 11 deletions rpc/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,8 @@ import (
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
}
10 changes: 3 additions & 7 deletions rpc/transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,9 @@ func (provider *Provider) TransactionByHash(ctx context.Context, hash *felt.Felt
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 Down