Skip to content

Commit

Permalink
rpcv03_Broadcast_Declare
Browse files Browse the repository at this point in the history
  • Loading branch information
rianhughes committed Aug 22, 2023
1 parent c049339 commit fa907b1
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 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
34 changes: 32 additions & 2 deletions rpc/types_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -295,13 +295,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 DepcreatedContractClass `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 @@ -315,6 +344,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 BroadcastedDeclareTransactionV1
dec, err := testConfig.provider.AddDeclareTransaction(context.Background(), declareTx)
if err != nil {
require.Equal(t, err.Error(), test.ExpectedError)
Expand Down

0 comments on commit fa907b1

Please sign in to comment.