Skip to content

Commit

Permalink
Merge PR #592 to main (#647)
Browse files Browse the repository at this point in the history
* 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]>
  • Loading branch information
3 people authored Dec 11, 2024
1 parent d3c2187 commit 1c399f4
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 39 deletions.
56 changes: 26 additions & 30 deletions account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type AccountInterface interface {
}

var _ AccountInterface = &Account{}
var _ rpc.RpcProvider = &Account{}

type Account struct {
provider rpc.RpcProvider
Expand Down Expand Up @@ -513,40 +512,37 @@ func (account *Account) WaitForTransactionReceipt(ctx context.Context, transacti
}
}

// AddInvokeTransaction generates an invoke transaction and adds it to the account's provider.
// SendTransaction can send Invoke, Declare, and Deploy transactions. It provides a unified way to send different transactions.
//
// Parameters:
// - ctx: the context.Context object for the transaction.
// - invokeTx: the invoke transaction to be added.
// - txn: the Broadcast Transaction to be sent.
// Returns:
// - *rpc.AddInvokeTransactionResponse: The response for the AddInvokeTransactionResponse
// - *rpc.TransactionResponse: the transaction response.
// - error: an error if any.
func (account *Account) AddInvokeTransaction(ctx context.Context, invokeTx rpc.BroadcastInvokeTxnType) (*rpc.AddInvokeTransactionResponse, error) {
return account.provider.AddInvokeTransaction(ctx, invokeTx)
}

// AddDeclareTransaction adds a declare transaction to the account.
//
// Parameters:
// - ctx: The context.Context for the request.
// - declareTransaction: The input for adding a declare transaction.
// Returns:
// - *rpc.AddDeclareTransactionResponse: The response for adding a declare transaction
// - error: an error, if any
func (account *Account) AddDeclareTransaction(ctx context.Context, declareTransaction rpc.BroadcastDeclareTxnType) (*rpc.AddDeclareTransactionResponse, error) {
return account.provider.AddDeclareTransaction(ctx, declareTransaction)
}

// AddDeployAccountTransaction adds a deploy account transaction to the account.
//
// Parameters:
// - ctx: The context.Context object for the function.
// - deployAccountTransaction: The rpc.DeployAccountTxn object representing the deploy account transaction.
// Returns:
// - *rpc.AddDeployAccountTransactionResponse: a pointer to rpc.AddDeployAccountTransactionResponse
// - error: an error if any
func (account *Account) AddDeployAccountTransaction(ctx context.Context, deployAccountTransaction rpc.BroadcastAddDeployTxnType) (*rpc.AddDeployAccountTransactionResponse, error) {
return account.provider.AddDeployAccountTransaction(ctx, deployAccountTransaction)
func (account *Account) SendTransaction(ctx context.Context, txn rpc.BroadcastTxn) (*rpc.TransactionResponse, error) {
switch tx := txn.(type) {
case rpc.BroadcastInvokeTxnType:
resp, err := account.provider.AddInvokeTransaction(ctx, tx)
if err != nil {
return nil, err
}
return &rpc.TransactionResponse{TransactionHash: resp.TransactionHash}, nil
case rpc.BroadcastDeclareTxnType:
resp, err := account.provider.AddDeclareTransaction(ctx, tx)
if err != nil {
return nil, err
}
return &rpc.TransactionResponse{TransactionHash: resp.TransactionHash, ClassHash: resp.ClassHash}, nil
case rpc.BroadcastAddDeployTxnType:
resp, err := account.provider.AddDeployAccountTransaction(ctx, tx)
if err != nil {
return nil, err
}
return &rpc.TransactionResponse{TransactionHash: resp.TransactionHash, ContractAddress: resp.ContractAddress}, nil
default:
return nil, errors.New("unsupported transaction type")
}
}

// BlockHashAndNumber returns the block hash and number for the account.
Expand Down
12 changes: 6 additions & 6 deletions account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ func TestSignMOCK(t *testing.T) {
// Returns:
//
// none
func TestAddInvoke(t *testing.T) {
func TestSendInvokeTxn(t *testing.T) {

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

resp, err := acnt.AddInvokeTransaction(context.Background(), test.InvokeTx)
resp, err := acnt.SendTransaction(context.Background(), test.InvokeTx)
if err != nil {
require.Equal(t, test.ExpectedErr.Error(), err.Error(), "AddInvokeTransaction returned an unexpected error")
require.Nil(t, resp)
Expand All @@ -552,7 +552,7 @@ func TestAddInvoke(t *testing.T) {
// Returns:
//
// none
func TestAddDeployAccountDevnet(t *testing.T) {
func TestSendDeployAccountDevnet(t *testing.T) {
if testEnv != "devnet" {
t.Skip("Skipping test as it requires a devnet environment")
}
Expand Down Expand Up @@ -595,7 +595,7 @@ func TestAddDeployAccountDevnet(t *testing.T) {
_, err = devnet.Mint(precomputedAddress, new(big.Int).SetUint64(10000000000000000000))
require.NoError(t, err)

resp, err := acnt.AddDeployAccountTransaction(context.Background(), rpc.BroadcastDeployAccountTxn{DeployAccountTxn: tx})
resp, err := acnt.SendTransaction(context.Background(), rpc.BroadcastDeployAccountTxn{DeployAccountTxn: tx})
require.Nil(t, err, "AddDeployAccountTransaction gave an Error")
require.NotNil(t, resp, "AddDeployAccountTransaction resp not nil")
}
Expand Down Expand Up @@ -1106,7 +1106,7 @@ func TestWaitForTransactionReceipt(t *testing.T) {
// Returns:
//
// none
func TestAddDeclareTxn(t *testing.T) {
func TestSendDeclareTxn(t *testing.T) {
if testEnv != "testnet" {
t.Skip("Skipping test as it requires a testnet environment")
}
Expand Down Expand Up @@ -1174,7 +1174,7 @@ func TestAddDeclareTxn(t *testing.T) {
ContractClass: class,
}

resp, err := acnt.AddDeclareTransaction(context.Background(), broadcastTx)
resp, err := acnt.SendTransaction(context.Background(), broadcastTx)

if err != nil {
require.Equal(t, rpc.ErrDuplicateTx.Error(), err.Error(), "AddDeclareTransaction error not what expected")
Expand Down
2 changes: 1 addition & 1 deletion examples/deployAccount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func main() {
fmt.Scan(&input)

// Send transaction to the network
resp, err := accnt.AddDeployAccountTransaction(context.Background(), tx)
resp, err := accnt.SendTransaction(context.Background(), tx)
if err != nil {
fmt.Println("Error returned from AddDeployAccountTransaction: ")
setup.PanicRPC(err)
Expand Down
2 changes: 1 addition & 1 deletion examples/deployContractUDC/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func main() {
}

// After the signing we finally call the AddInvokeTransaction in order to invoke the contract function
resp, err := accnt.AddInvokeTransaction(context.Background(), InvokeTx)
resp, err := accnt.SendTransaction(context.Background(), InvokeTx)
if err != nil {
setup.PanicRPC(err)
}
Expand Down
2 changes: 1 addition & 1 deletion examples/simpleInvoke/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func main() {
}

// After the signing we finally call the AddInvokeTransaction in order to invoke the contract function
resp, err := accnt.AddInvokeTransaction(context.Background(), InvokeTx)
resp, err := accnt.SendTransaction(context.Background(), InvokeTx)
if err != nil {
setup.PanicRPC(err)
}
Expand Down
6 changes: 6 additions & 0 deletions rpc/types_transaction_response.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,9 @@ type AddDeployAccountTransactionResponse struct {
type AddInvokeTransactionResponse struct {
TransactionHash *felt.Felt `json:"transaction_hash"`
}

type TransactionResponse struct {
TransactionHash *felt.Felt `json:"transaction_hash"`
ClassHash *felt.Felt `json:"class_hash,omitempty"`
ContractAddress *felt.Felt `json:"contract_address,omitempty"`
}

0 comments on commit 1c399f4

Please sign in to comment.