Skip to content

Commit

Permalink
Rpcv03 broadcast declare (#295)
Browse files Browse the repository at this point in the history
* rpcv03 starknet_getClass

* rpcv03_Broadcast_Declare

* typo

* fix test

* typo

* small fix in TODO comment

---------

Co-authored-by: Carmen Cabrera <kr1000a@gmail.com>
  • Loading branch information
rianhughes and cicr99 committed Sep 5, 2023
1 parent 80a6f98 commit af79655
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
2 changes: 1 addition & 1 deletion contracts/contractsv02.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func (p *RPCProvider) declareAndWaitWithWallet(ctx context.Context, compiledClas
if err != nil {
return nil, err
}
tx, err := provider.AddDeclareTransaction(ctx, rpc.BroadcastedDeclareTransaction{
tx, err := provider.AddDeclareTransaction(ctx, rpc.BroadcastedDeclareTransactionV1{
BroadcastedTxnCommonProperties: rpc.BroadcastedTxnCommonProperties{
Type: "DECLARE",
MaxFee: new(felt.Felt).SetUint64(10000),
Expand Down
6 changes: 3 additions & 3 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.DepcreatedContractClass, deployRequest rpc.DeployAccountTxn) (resp types.AddDeployResponse, err error) {
func (sg *Gateway) Deploy(ctx context.Context, contract rpc.DeprecatedContractClass, 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.DepcreatedContractClass, declareRequest DeclareRequest) (resp types.AddDeclareResponse, err error) {
func (sg *Gateway) Declare(ctx context.Context, contract rpc.DeprecatedContractClass, declareRequest DeclareRequest) (resp types.AddDeclareResponse, err error) {
declareRequest.Type = DECLARE

req, err := sg.newRequest(ctx, http.MethodPost, "/add_transaction", declareRequest)
Expand Down Expand Up @@ -226,7 +226,7 @@ type DeclareRequest struct {
MaxFee string `json:"max_fee"`
Nonce string `json:"nonce"`
Signature []string `json:"signature"`
ContractClass rpc.DepcreatedContractClass `json:"contract_class"`
ContractClass rpc.DeprecatedContractClass `json:"contract_class"`
}

func (sg *Gateway) StateUpdate(ctx context.Context, opts *BlockOptions) (*StateUpdate, error) {
Expand Down
36 changes: 33 additions & 3 deletions rpc/types_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ type DeployAccountTransactionProperties struct {
ConstructorCalldata []*felt.Felt `json:"constructor_calldata"`
}

// DeployTxn The structure of a deploy transaction. Note that this transaction type is deprecated and will no longer be supported in future versions
// DeployAccountTxn The structure of a deployAccount transaction.
type DeployAccountTxn struct {
CommonTransaction
DeployAccountTransactionProperties
Expand Down Expand Up @@ -278,13 +278,42 @@ func (b BroadcastedInvokeV1Transaction) MarshalJSON() ([]byte, error) {
return json.Marshal(output)
}

type BroadcastedDeclareTransaction struct {
type BroadcastedDeclareTransaction interface{}

var _ BroadcastedDeclareTransaction = BroadcastedDeclareTransactionV1{}
var _ BroadcastedDeclareTransaction = BroadcastedDeclareTransactionV2{}

type BroadcastedDeclareTransactionV1 struct {
BroadcastedTxnCommonProperties
ContractClass DeprecatedContractClass `json:"contract_class"`
SenderAddress *felt.Felt `json:"sender_address"`
}

func (b BroadcastedDeclareTransaction) MarshalJSON() ([]byte, error) {
func (b BroadcastedDeclareTransactionV1) MarshalJSON() ([]byte, error) {
output := map[string]interface{}{}
output["type"] = "DECLARE"
if b.MaxFee != nil {
output["max_fee"] = fmt.Sprintf("0x%x", b.MaxFee)
}
if b.Nonce != nil {
output["nonce"] = fmt.Sprintf("0x%x", b.Nonce)
}
output["version"] = b.Version
signature := b.Signature
output["signature"] = signature
output["sender_address"] = b.SenderAddress.String()
output["contract_class"] = b.ContractClass
return json.Marshal(output)
}

type BroadcastedDeclareTransactionV2 struct {
BroadcastedTxnCommonProperties
ContractClass ContractClass `json:"contract_class"`
SenderAddress *felt.Felt `json:"sender_address"`
CompiledClassHash *felt.Felt `json:"compiled_class_hash"`
}

func (b BroadcastedDeclareTransactionV2) MarshalJSON() ([]byte, error) {
output := map[string]interface{}{}
output["type"] = "DECLARE"
if b.MaxFee != nil {
Expand All @@ -298,6 +327,7 @@ func (b BroadcastedDeclareTransaction) MarshalJSON() ([]byte, error) {
output["signature"] = signature
output["sender_address"] = b.SenderAddress.String()
output["contract_class"] = b.ContractClass
output["compiled_class_hash"] = b.CompiledClassHash
return json.Marshal(output)
}

Expand Down
4 changes: 2 additions & 2 deletions rpc/write_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ func TestDeclareTransaction(t *testing.T) {
t.Fatal("should be able to read file", err)
}

var declareTx BroadcastedDeclareTransaction
var declareTx BroadcastedDeclareTransactionV1
err = json.Unmarshal(declareTxJSON, &declareTx)
require.Nil(t, err, "Error unmarshalling decalreTx")

spy := NewSpy(testConfig.provider.c)
testConfig.provider.c = spy

// To do: test transaction against client that supports RPC method (currently Sequencer uses
// "sierra_program" instead of "program" in BroadcastedDeclareTransaction
// "sierra_program" instead of "program" in BroadcastedDeclareTransactionV2)
dec, err := testConfig.provider.AddDeclareTransaction(context.Background(), declareTx)
if err != nil {
require.Equal(t, err.Error(), test.ExpectedError)
Expand Down

0 comments on commit af79655

Please sign in to comment.