Skip to content

Commit

Permalink
rpcv03 starknet_getClass (#293)
Browse files Browse the repository at this point in the history
* rpcv03 starknet_getClass

* Fix some typos

* fix comments

---------

Co-authored-by: Carmen Cabrera <kr1000a@gmail.com>
  • Loading branch information
rianhughes and cicr99 committed Sep 1, 2023
1 parent da74b63 commit 9d9bb9a
Show file tree
Hide file tree
Showing 20 changed files with 11,533 additions and 86 deletions.
4 changes: 2 additions & 2 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ type account interface {
Nonce(ctx context.Context) (*big.Int, error)
EstimateFee(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.FeeEstimate, error)
Execute(ctx context.Context, calls []types.FunctionCall, details types.ExecuteDetails) (*types.AddInvokeTransactionOutput, error)
Declare(ctx context.Context, classHash string, contract rpc.ContractClass, details types.ExecuteDetails) (types.AddDeclareResponse, error)
Declare(ctx context.Context, classHash string, contract rpc.DeprecatedContractClass, details types.ExecuteDetails) (types.AddDeclareResponse, error)
Deploy(ctx context.Context, classHash string, details types.ExecuteDetails) (*types.AddDeployResponse, error)
}

Expand Down Expand Up @@ -530,7 +530,7 @@ func (account *Account) Execute(ctx context.Context, calls []types.FunctionCall,
return nil, ErrUnsupportedAccount
}

func (account *Account) Declare(ctx context.Context, classHash string, contract rpc.ContractClass, details types.ExecuteDetails) (types.AddDeclareResponse, error) {
func (account *Account) Declare(ctx context.Context, classHash string, contract rpc.DeprecatedContractClass, details types.ExecuteDetails) (types.AddDeclareResponse, error) {
switch account.provider {
case ProviderRPC:
panic("unsupported")
Expand Down
6 changes: 3 additions & 3 deletions contracts/contracts.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type GatewayProvider gateway.GatewayProvider

func (p *GatewayProvider) declareAndWaitWithWallet(ctx context.Context, compiledClass []byte) (*DeclareOutput, error) {
provider := gateway.GatewayProvider(*p)
class := rpc.ContractClass{}
class := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(compiledClass, &class); err != nil {
return nil, err
}
Expand All @@ -54,7 +54,7 @@ func (p *GatewayProvider) declareAndWaitWithWallet(ctx context.Context, compiled
// TODO: remove compiledClass from the interface
func (p *GatewayProvider) deployAccountAndWaitNoWallet(ctx context.Context, classHash *felt.Felt, compiledClass []byte, salt string, inputs []string) (*DeployOutput, error) {
provider := gateway.GatewayProvider(*p)
class := rpc.ContractClass{}
class := rpc.DeprecatedContractClass{}

if err := json.Unmarshal(compiledClass, &class); err != nil {
return nil, err
Expand Down Expand Up @@ -99,7 +99,7 @@ func (p *GatewayProvider) deployAccountAndWaitNoWallet(ctx context.Context, clas
// DEPLOY_ACCOUNT for an account.
func (p *GatewayProvider) deployAndWaitNoWallet(ctx context.Context, compiledClass []byte, salt string, inputs []string) (*DeployOutput, error) {
provider := gateway.GatewayProvider(*p)
class := rpc.ContractClass{}
class := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(compiledClass, &class); err != nil {
return nil, err
}
Expand Down
8 changes: 4 additions & 4 deletions contracts/contractsv02.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ type RPCProvider rpc.Provider

func (p *RPCProvider) declareAndWaitWithWallet(ctx context.Context, compiledClass []byte) (*DeclareOutput, error) {
provider := rpc.Provider(*p)
class := rpc.ContractClass{}
class := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(compiledClass, &class); err != nil {
return nil, err
}
Expand Down Expand Up @@ -60,7 +60,7 @@ func (p *RPCProvider) declareAndWaitWithWallet(ctx context.Context, compiledClas

func (p *RPCProvider) deployAccountAndWaitNoWallet(ctx context.Context, classHash *felt.Felt, compiledClass []byte, salt string, inputs []string) (*DeployOutput, error) {
provider := rpc.Provider(*p)
class := rpc.ContractClass{}
class := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(compiledClass, &class); err != nil {
return nil, err
}
Expand Down Expand Up @@ -109,7 +109,7 @@ func (p *RPCProvider) deployAccountAndWaitNoWallet(ctx context.Context, classHas

func (p *RPCProvider) deployAndWaitWithWallet(ctx context.Context, compiledClass []byte, salt string, inputs []string) (*DeployOutput, error) {
provider := rpc.Provider(*p)
class := rpc.ContractClass{}
class := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(compiledClass, &class); err != nil {
return nil, err
}
Expand All @@ -131,7 +131,7 @@ func (p *RPCProvider) deployAndWaitWithWallet(ctx context.Context, compiledClass
ContractAddressSalt: saltFelt,
ConstructorCalldata: inputsFelt,
},
ContractClass: class,
DeprecatedContractClass: class,
})
fmt.Println("b")
if err != nil {
Expand Down
8 changes: 4 additions & 4 deletions gateway/class.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
)

// TODO: returns DeprecatedContractClass | SierraContractClass
func (sg *Gateway) ClassByHash(ctx context.Context, hash string) (*rpc.ContractClass, error) {
func (sg *Gateway) ClassByHash(ctx context.Context, hash string) (*rpc.DeprecatedContractClass, error) {
req, err := sg.newRequest(ctx, http.MethodGet, "/get_class_by_hash", nil)
if err != nil {
return nil, err
Expand All @@ -20,7 +20,7 @@ func (sg *Gateway) ClassByHash(ctx context.Context, hash string) (*rpc.ContractC
"classHash": []string{hash},
})

var resp rpc.ContractClass
var resp rpc.DeprecatedContractClass
return &resp, sg.do(req, &resp)
}

Expand All @@ -41,10 +41,10 @@ func (sg *Gateway) ClassHashAt(ctx context.Context, address string) (*felt.Felt,
return resp, nil
}

func (sg *Gateway) Class(context.Context, string) (*rpc.ContractClass, error) {
func (sg *Gateway) Class(context.Context, string) (*rpc.DeprecatedContractClass, error) {
panic("not implemented")
}

func (sg *Gateway) ClassAt(context.Context, string) (*rpc.ContractClass, error) {
func (sg *Gateway) ClassAt(context.Context, string) (*rpc.DeprecatedContractClass, error) {
panic("not implemented")
}
6 changes: 3 additions & 3 deletions gateway/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
type Bytecode []string

type Code struct {
Bytecode Bytecode `json:"bytecode"`
Bytecode Bytecode `json:"bytecode"`
Abi *rpc.ABI `json:"abi"`
}

Expand Down Expand Up @@ -100,14 +100,14 @@ func (sg *Gateway) CodeAt(ctx context.Context, contract string, blockNumber *big
return &resp, sg.do(req, &resp)
}

func (sg *Gateway) FullContract(ctx context.Context, contract string) (*rpc.ContractClass, error) {
func (sg *Gateway) FullContract(ctx context.Context, contract string) (*rpc.DeprecatedContractClass, error) {
req, err := sg.newRequest(ctx, http.MethodGet, "/get_full_contract", nil)
if err != nil {
return nil, err
}

appendQueryValues(req, url.Values{"contractAddress": []string{contract}})

var resp rpc.ContractClass
var resp rpc.DeprecatedContractClass
return &resp, sg.do(req, &resp)
}
2 changes: 1 addition & 1 deletion gateway/gateway_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func setupDevnet(ctx context.Context) error {
return fmt.Errorf("could not connect to devnet: %v", err)
}

contract := rpc.ContractClass{}
contract := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(counterCompiled, &contract); err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions gateway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ func (p *GatewayProvider) TransactionByHash(ctx context.Context, hash string) (*
return &t.Transaction, nil
}

func (p *GatewayProvider) Class(ctx context.Context, classHash string) (*rpc.ContractClass, error) {
func (p *GatewayProvider) Class(ctx context.Context, classHash string) (*rpc.DeprecatedContractClass, error) {
panic("not implemented")
}

func (p *GatewayProvider) ClassHashAt(ctx context.Context, contractAddress string) (*felt.Felt, error) {
panic("not implemented")
}

func (p *GatewayProvider) ClassAt(ctx context.Context, contractAddress string) (*rpc.ContractClass, error) {
func (p *GatewayProvider) ClassAt(ctx context.Context, contractAddress string) (*rpc.DeprecatedContractClass, error) {
panic("not implemented")
}
18 changes: 9 additions & 9 deletions gateway/starknet.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func (sg *Gateway) Invoke(ctx context.Context, invoke types.FunctionInvoke) (*ty
/*
'add_transaction' wrapper for compressing and deploying a compiled StarkNet contract
*/
func (sg *Gateway) Deploy(ctx context.Context, contract rpc.ContractClass, deployRequest rpc.DeployAccountTxn) (resp types.AddDeployResponse, err error) {
func (sg *Gateway) Deploy(ctx context.Context, contract rpc.DepcreatedContractClass, deployRequest rpc.DeployAccountTxn) (resp types.AddDeployResponse, err error) {
panic("deploy transaction has been removed, use account.Deploy() instead")
}

Expand Down Expand Up @@ -197,7 +197,7 @@ func (sg *Gateway) DeployAccount(ctx context.Context, deployAccountRequest types
/*
'add_transaction' wrapper for compressing and declaring a contract class
*/
func (sg *Gateway) Declare(ctx context.Context, contract rpc.ContractClass, declareRequest DeclareRequest) (resp types.AddDeclareResponse, err error) {
func (sg *Gateway) Declare(ctx context.Context, contract rpc.DepcreatedContractClass, declareRequest DeclareRequest) (resp types.AddDeclareResponse, err error) {
declareRequest.Type = DECLARE

req, err := sg.newRequest(ctx, http.MethodPost, "/add_transaction", declareRequest)
Expand All @@ -220,13 +220,13 @@ func (sg *Gateway) Declare(ctx context.Context, contract rpc.ContractClass, decl
// }

type DeclareRequest struct {
Type string `json:"type"`
SenderAddress *felt.Felt `json:"sender_address"`
Version string `json:"version"`
MaxFee string `json:"max_fee"`
Nonce string `json:"nonce"`
Signature []string `json:"signature"`
ContractClass rpc.ContractClass `json:"contract_class"`
Type string `json:"type"`
SenderAddress *felt.Felt `json:"sender_address"`
Version string `json:"version"`
MaxFee string `json:"max_fee"`
Nonce string `json:"nonce"`
Signature []string `json:"signature"`
ContractClass rpc.DepcreatedContractClass `json:"contract_class"`
}

func (sg *Gateway) StateUpdate(ctx context.Context, opts *BlockOptions) (*StateUpdate, error) {
Expand Down
6 changes: 3 additions & 3 deletions gateway/starknet_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func TestDeclare(t *testing.T) {

for _, env := range testSet {
gw := testConfig.client
accountClass := rpc.ContractClass{}
accountClass := rpc.DeprecatedContractClass{}
err := json.Unmarshal(accountCompiled, &accountClass)
if err != nil {
t.Fatalf("could not parse contract: %v\n", err)
Expand Down Expand Up @@ -73,7 +73,7 @@ func TestDeployCounterContract(t *testing.T) {

gw := testConfig.client

counterClass := rpc.ContractClass{}
counterClass := rpc.DeprecatedContractClass{}
err := json.Unmarshal(counterCompiled, &counterClass)
if err != nil {
t.Fatalf("could not parse contract: %v\n", err)
Expand Down Expand Up @@ -148,7 +148,7 @@ func TestDeployAccountContract(t *testing.T) {
for _, test := range testSet {
gw := testConfig.client
// Step 1: deploy the a class
accountClass := rpc.ContractClass{}
accountClass := rpc.DeprecatedContractClass{}
err := json.Unmarshal(accountCompiled, &accountClass)
if err != nil {
t.Fatalf("could not parse account: %v\n", err)
Expand Down
2 changes: 1 addition & 1 deletion plugins/xsessions/accounts_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ var counterCompiled = artifacts.CounterCompiled
func TestCounter_DeployContract(t *testing.T) {
provider := beforeEachRPC(t)

counterClass := rpc.ContractClass{}
counterClass := rpc.DeprecatedContractClass{}
inputs := []string{}

if err := json.Unmarshal(counterCompiled, &counterClass); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions plugins/xsessions/plugin_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
func RegisterClass(t *testing.T, pluginCompiled []byte) string {
provider := beforeEachRPC(t)

yeasayerClass := rpc.ContractClass{}
yeasayerClass := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(pluginCompiled, &yeasayerClass); err != nil {
t.Fatal(err)
}
Expand Down Expand Up @@ -61,7 +61,7 @@ func RegisterClass(t *testing.T, pluginCompiled []byte) string {
// DeployContract
func DeployContract(t *testing.T, contractCompiled []byte, inputs []string) string {
provider := beforeEachRPC(t)
contractClass := rpc.ContractClass{}
contractClass := rpc.DeprecatedContractClass{}

if err := json.Unmarshal(contractCompiled, &contractClass); err != nil {
t.Fatal(err)
Expand Down
2 changes: 1 addition & 1 deletion providers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ var (
)

func InstallCounterContract(provider *gateway.GatewayProvider) (string, error) {
class := rpc.ContractClass{}
class := rpc.DeprecatedContractClass{}
if err := json.Unmarshal(artifacts.CounterCompiled, &class); err != nil {
return "", err
}
Expand Down
38 changes: 32 additions & 6 deletions rpc/contract.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package rpc

import (
"context"
"encoding/json"
"errors"
"fmt"

Expand All @@ -10,8 +11,8 @@ import (
)

// Class gets the contract class definition associated with the given hash.
func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash string) (*ContractClass, error) {
var rawClass ContractClass
func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash string) (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):
Expand All @@ -21,12 +22,14 @@ func (provider *Provider) Class(ctx context.Context, blockID BlockID, classHash
}
return nil, err
}
return &rawClass, nil

return typecastClassOutput(&rawClass)

}

// ClassAt get the contract class definition at the given address.
func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contractAddress *felt.Felt) (*ContractClass, error) {
var rawClass ContractClass
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):
Expand All @@ -36,7 +39,30 @@ func (provider *Provider) ClassAt(ctx context.Context, blockID BlockID, contract
}
return nil, err
}
return &rawClass, nil
return typecastClassOutput(&rawClass)
}

func typecastClassOutput(rawClass *map[string]any) (ClassOutput, error) {
rawClassByte, err := json.Marshal(rawClass)
if err != nil {
return nil, err
}

// if contract_class_version exists, then it's a ContractClass type
if _, exists := (*rawClass)["contract_class_version"]; exists {
var contractClass ContractClass
err = json.Unmarshal(rawClassByte, &contractClass)
if err != nil {
return nil, err
}
return &contractClass, nil
}
var depContractClass DeprecatedContractClass
err = json.Unmarshal(rawClassByte, &depContractClass)
if err != nil {
return nil, err
}
return &depContractClass, nil
}

// ClassHashAt gets the contract class hash for the contract deployed at the given address.
Expand Down
Loading

0 comments on commit 9d9bb9a

Please sign in to comment.