diff --git a/account/account.go b/account/account.go index 763e28a9..c4436158 100644 --- a/account/account.go +++ b/account/account.go @@ -41,7 +41,6 @@ type AccountInterface interface { } var _ AccountInterface = &Account{} -var _ rpc.RpcProvider = &Account{} type Account struct { provider rpc.RpcProvider @@ -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. diff --git a/account/account_test.go b/account/account_test.go index b9af5ad9..91097805 100644 --- a/account/account_test.go +++ b/account/account_test.go @@ -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 @@ -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) @@ -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") } @@ -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") } @@ -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") } @@ -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") diff --git a/examples/deployAccount/main.go b/examples/deployAccount/main.go index e135825f..38a6d6e2 100644 --- a/examples/deployAccount/main.go +++ b/examples/deployAccount/main.go @@ -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) diff --git a/examples/deployContractUDC/main.go b/examples/deployContractUDC/main.go index 31ddc0c5..90f66e85 100644 --- a/examples/deployContractUDC/main.go +++ b/examples/deployContractUDC/main.go @@ -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) } diff --git a/examples/simpleInvoke/main.go b/examples/simpleInvoke/main.go index 2ed8ca89..09be957e 100644 --- a/examples/simpleInvoke/main.go +++ b/examples/simpleInvoke/main.go @@ -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) } diff --git a/rpc/types_transaction_response.go b/rpc/types_transaction_response.go index 3e94c07d..cf4fd33d 100644 --- a/rpc/types_transaction_response.go +++ b/rpc/types_transaction_response.go @@ -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"` +}