Skip to content

Commit 1c399f4

Browse files
thiagodeevPsychoPunkSagerianhughes
authored
Merge PR #592 to main (#647)
* fix::> introduced comman AddTransaction function in account package * fix::> replace all the instances of Add<Type>Transaction in example/test with AddTransaction * fix::> func name and doc changes made * fix::> account_test.go changed * fix::> examples error solved * fix::> Removed type assertions, created generic TransactionResponse struct, and transaction type converter * fix::> removed unnecessary lines * fix:> made required changes * fix::> removed unnecessary comments * fix::> renamed test function --------- Co-authored-by: Abhinav Prakash <[email protected]> Co-authored-by: Rian Hughes <[email protected]>
1 parent d3c2187 commit 1c399f4

File tree

6 files changed

+41
-39
lines changed

6 files changed

+41
-39
lines changed

Diff for: account/account.go

+26-30
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type AccountInterface interface {
4141
}
4242

4343
var _ AccountInterface = &Account{}
44-
var _ rpc.RpcProvider = &Account{}
4544

4645
type Account struct {
4746
provider rpc.RpcProvider
@@ -513,40 +512,37 @@ func (account *Account) WaitForTransactionReceipt(ctx context.Context, transacti
513512
}
514513
}
515514

516-
// AddInvokeTransaction generates an invoke transaction and adds it to the account's provider.
515+
// SendTransaction can send Invoke, Declare, and Deploy transactions. It provides a unified way to send different transactions.
517516
//
518517
// Parameters:
519518
// - ctx: the context.Context object for the transaction.
520-
// - invokeTx: the invoke transaction to be added.
519+
// - txn: the Broadcast Transaction to be sent.
521520
// Returns:
522-
// - *rpc.AddInvokeTransactionResponse: The response for the AddInvokeTransactionResponse
521+
// - *rpc.TransactionResponse: the transaction response.
523522
// - error: an error if any.
524-
func (account *Account) AddInvokeTransaction(ctx context.Context, invokeTx rpc.BroadcastInvokeTxnType) (*rpc.AddInvokeTransactionResponse, error) {
525-
return account.provider.AddInvokeTransaction(ctx, invokeTx)
526-
}
527-
528-
// AddDeclareTransaction adds a declare transaction to the account.
529-
//
530-
// Parameters:
531-
// - ctx: The context.Context for the request.
532-
// - declareTransaction: The input for adding a declare transaction.
533-
// Returns:
534-
// - *rpc.AddDeclareTransactionResponse: The response for adding a declare transaction
535-
// - error: an error, if any
536-
func (account *Account) AddDeclareTransaction(ctx context.Context, declareTransaction rpc.BroadcastDeclareTxnType) (*rpc.AddDeclareTransactionResponse, error) {
537-
return account.provider.AddDeclareTransaction(ctx, declareTransaction)
538-
}
539-
540-
// AddDeployAccountTransaction adds a deploy account transaction to the account.
541-
//
542-
// Parameters:
543-
// - ctx: The context.Context object for the function.
544-
// - deployAccountTransaction: The rpc.DeployAccountTxn object representing the deploy account transaction.
545-
// Returns:
546-
// - *rpc.AddDeployAccountTransactionResponse: a pointer to rpc.AddDeployAccountTransactionResponse
547-
// - error: an error if any
548-
func (account *Account) AddDeployAccountTransaction(ctx context.Context, deployAccountTransaction rpc.BroadcastAddDeployTxnType) (*rpc.AddDeployAccountTransactionResponse, error) {
549-
return account.provider.AddDeployAccountTransaction(ctx, deployAccountTransaction)
523+
func (account *Account) SendTransaction(ctx context.Context, txn rpc.BroadcastTxn) (*rpc.TransactionResponse, error) {
524+
switch tx := txn.(type) {
525+
case rpc.BroadcastInvokeTxnType:
526+
resp, err := account.provider.AddInvokeTransaction(ctx, tx)
527+
if err != nil {
528+
return nil, err
529+
}
530+
return &rpc.TransactionResponse{TransactionHash: resp.TransactionHash}, nil
531+
case rpc.BroadcastDeclareTxnType:
532+
resp, err := account.provider.AddDeclareTransaction(ctx, tx)
533+
if err != nil {
534+
return nil, err
535+
}
536+
return &rpc.TransactionResponse{TransactionHash: resp.TransactionHash, ClassHash: resp.ClassHash}, nil
537+
case rpc.BroadcastAddDeployTxnType:
538+
resp, err := account.provider.AddDeployAccountTransaction(ctx, tx)
539+
if err != nil {
540+
return nil, err
541+
}
542+
return &rpc.TransactionResponse{TransactionHash: resp.TransactionHash, ContractAddress: resp.ContractAddress}, nil
543+
default:
544+
return nil, errors.New("unsupported transaction type")
545+
}
550546
}
551547

552548
// BlockHashAndNumber returns the block hash and number for the account.

Diff for: account/account_test.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ func TestSignMOCK(t *testing.T) {
436436
// Returns:
437437
//
438438
// none
439-
func TestAddInvoke(t *testing.T) {
439+
func TestSendInvokeTxn(t *testing.T) {
440440

441441
type testSetType struct {
442442
ExpectedErr error
@@ -526,7 +526,7 @@ func TestAddInvoke(t *testing.T) {
526526
err = acnt.SignInvokeTransaction(context.Background(), &test.InvokeTx.InvokeTxnV1)
527527
require.NoError(t, err)
528528

529-
resp, err := acnt.AddInvokeTransaction(context.Background(), test.InvokeTx)
529+
resp, err := acnt.SendTransaction(context.Background(), test.InvokeTx)
530530
if err != nil {
531531
require.Equal(t, test.ExpectedErr.Error(), err.Error(), "AddInvokeTransaction returned an unexpected error")
532532
require.Nil(t, resp)
@@ -552,7 +552,7 @@ func TestAddInvoke(t *testing.T) {
552552
// Returns:
553553
//
554554
// none
555-
func TestAddDeployAccountDevnet(t *testing.T) {
555+
func TestSendDeployAccountDevnet(t *testing.T) {
556556
if testEnv != "devnet" {
557557
t.Skip("Skipping test as it requires a devnet environment")
558558
}
@@ -595,7 +595,7 @@ func TestAddDeployAccountDevnet(t *testing.T) {
595595
_, err = devnet.Mint(precomputedAddress, new(big.Int).SetUint64(10000000000000000000))
596596
require.NoError(t, err)
597597

598-
resp, err := acnt.AddDeployAccountTransaction(context.Background(), rpc.BroadcastDeployAccountTxn{DeployAccountTxn: tx})
598+
resp, err := acnt.SendTransaction(context.Background(), rpc.BroadcastDeployAccountTxn{DeployAccountTxn: tx})
599599
require.Nil(t, err, "AddDeployAccountTransaction gave an Error")
600600
require.NotNil(t, resp, "AddDeployAccountTransaction resp not nil")
601601
}
@@ -1106,7 +1106,7 @@ func TestWaitForTransactionReceipt(t *testing.T) {
11061106
// Returns:
11071107
//
11081108
// none
1109-
func TestAddDeclareTxn(t *testing.T) {
1109+
func TestSendDeclareTxn(t *testing.T) {
11101110
if testEnv != "testnet" {
11111111
t.Skip("Skipping test as it requires a testnet environment")
11121112
}
@@ -1174,7 +1174,7 @@ func TestAddDeclareTxn(t *testing.T) {
11741174
ContractClass: class,
11751175
}
11761176

1177-
resp, err := acnt.AddDeclareTransaction(context.Background(), broadcastTx)
1177+
resp, err := acnt.SendTransaction(context.Background(), broadcastTx)
11781178

11791179
if err != nil {
11801180
require.Equal(t, rpc.ErrDuplicateTx.Error(), err.Error(), "AddDeclareTransaction error not what expected")

Diff for: examples/deployAccount/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func main() {
120120
fmt.Scan(&input)
121121

122122
// Send transaction to the network
123-
resp, err := accnt.AddDeployAccountTransaction(context.Background(), tx)
123+
resp, err := accnt.SendTransaction(context.Background(), tx)
124124
if err != nil {
125125
fmt.Println("Error returned from AddDeployAccountTransaction: ")
126126
setup.PanicRPC(err)

Diff for: examples/deployContractUDC/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ func main() {
128128
}
129129

130130
// After the signing we finally call the AddInvokeTransaction in order to invoke the contract function
131-
resp, err := accnt.AddInvokeTransaction(context.Background(), InvokeTx)
131+
resp, err := accnt.SendTransaction(context.Background(), InvokeTx)
132132
if err != nil {
133133
setup.PanicRPC(err)
134134
}

Diff for: examples/simpleInvoke/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ func main() {
120120
}
121121

122122
// After the signing we finally call the AddInvokeTransaction in order to invoke the contract function
123-
resp, err := accnt.AddInvokeTransaction(context.Background(), InvokeTx)
123+
resp, err := accnt.SendTransaction(context.Background(), InvokeTx)
124124
if err != nil {
125125
setup.PanicRPC(err)
126126
}

Diff for: rpc/types_transaction_response.go

+6
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,9 @@ type AddDeployAccountTransactionResponse struct {
1818
type AddInvokeTransactionResponse struct {
1919
TransactionHash *felt.Felt `json:"transaction_hash"`
2020
}
21+
22+
type TransactionResponse struct {
23+
TransactionHash *felt.Felt `json:"transaction_hash"`
24+
ClassHash *felt.Felt `json:"class_hash,omitempty"`
25+
ContractAddress *felt.Felt `json:"contract_address,omitempty"`
26+
}

0 commit comments

Comments
 (0)