diff --git a/account_balance_query.go b/account_balance_query.go index 365bf51f8..9043443a7 100644 --- a/account_balance_query.go +++ b/account_balance_query.go @@ -2,11 +2,17 @@ package hedera import "github.com/hashgraph/hedera-sdk-go/proto" +// AccountBalanceQuery gets the balance of a CryptoCurrency account. This returns only the balance, so it is a smaller +// and faster reply than AccountInfoQuery, which returns the balance plus additional information. type AccountBalanceQuery struct { QueryBuilder pb *proto.CryptoGetAccountBalanceQuery } +// NewAccountBalanceQuery creates an AccountBalanceQuery builder which can be used to construct and execute +// an AccountBalanceQuery. +// It is recommended that you use this for creating new instances of an AccountBalanceQuery +// instead of manually creating an instance of the struct. func NewAccountBalanceQuery() *AccountBalanceQuery { pb := &proto.CryptoGetAccountBalanceQuery{Header: &proto.QueryHeader{}} @@ -16,11 +22,31 @@ func NewAccountBalanceQuery() *AccountBalanceQuery { return &AccountBalanceQuery{inner, pb} } +// SetAccountID sets the AccountID for which you wish to query the balance. +// +// Note: you can only query an Account or Contract but not both -- if a Contract ID or Account ID has already been set, +// it will be overwritten by this method. func (builder *AccountBalanceQuery) SetAccountID(id AccountID) *AccountBalanceQuery { - builder.pb.AccountID = id.toProto() + builder.pb.BalanceSource = &proto.CryptoGetAccountBalanceQuery_AccountID{ + AccountID: id.toProto(), + } + + return builder +} + +// SetContractID sets the ContractID for which you wish to query the balance. +// +// Note: you can only query an Account or Contract but not both -- if a Contract ID or Account ID has already been set, +// it will be overwritten by this method. +func (builder *AccountBalanceQuery) SetContractID(id ContractID) *AccountBalanceQuery { + builder.pb.BalanceSource = &proto.CryptoGetAccountBalanceQuery_ContractID{ + ContractID: id.toProto(), + } + return builder } +// Execute executes the AccountBalanceQuery using the provided client func (builder *AccountBalanceQuery) Execute(client *Client) (Hbar, error) { resp, err := builder.execute(client) if err != nil { diff --git a/account_balance_query_test.go b/account_balance_query_test.go index 36c2b7f63..e5319cdeb 100644 --- a/account_balance_query_test.go +++ b/account_balance_query_test.go @@ -16,3 +16,14 @@ func TestNewAccountBalanceQuery(t *testing.T) { cupaloy.SnapshotT(t, query.pb.String()) } + +func TestNewAccountBalanceQuery_ForContract(t *testing.T) { + mockTransaction, err := newMockTransaction() + assert.NoError(t, err) + + query := NewAccountBalanceQuery(). + SetContractID(ContractID{Contract: 3}). + SetQueryPaymentTransaction(mockTransaction) + + cupaloy.SnapshotT(t, query.pb.String()) +} diff --git a/proto/CryptoGetAccountBalance.pb.go b/proto/CryptoGetAccountBalance.pb.go index 64b26e0e8..9c19db9ed 100644 --- a/proto/CryptoGetAccountBalance.pb.go +++ b/proto/CryptoGetAccountBalance.pb.go @@ -26,8 +26,11 @@ type CryptoGetAccountBalanceQuery struct { sizeCache protoimpl.SizeCache unknownFields protoimpl.UnknownFields - Header *QueryHeader `protobuf:"bytes,1,opt,name=header,proto3" json:"header,omitempty"` // Standard info sent from client to node, including the signed payment, and what kind of response is requested (cost, state proof, both, or neither). - AccountID *AccountID `protobuf:"bytes,2,opt,name=accountID,proto3" json:"accountID,omitempty"` // The account ID for which information is requested + Header *QueryHeader `protobuf:"bytes,1,opt,name=header,proto3" json:"header,omitempty"` // Standard info sent from client to node, including the signed payment, and what kind of response is requested (cost, state proof, both, or neither). + // Types that are assignable to BalanceSource: + // *CryptoGetAccountBalanceQuery_AccountID + // *CryptoGetAccountBalanceQuery_ContractID + BalanceSource isCryptoGetAccountBalanceQuery_BalanceSource `protobuf_oneof:"balanceSource"` } func (x *CryptoGetAccountBalanceQuery) Reset() { @@ -69,13 +72,43 @@ func (x *CryptoGetAccountBalanceQuery) GetHeader() *QueryHeader { return nil } +func (m *CryptoGetAccountBalanceQuery) GetBalanceSource() isCryptoGetAccountBalanceQuery_BalanceSource { + if m != nil { + return m.BalanceSource + } + return nil +} + func (x *CryptoGetAccountBalanceQuery) GetAccountID() *AccountID { - if x != nil { + if x, ok := x.GetBalanceSource().(*CryptoGetAccountBalanceQuery_AccountID); ok { return x.AccountID } return nil } +func (x *CryptoGetAccountBalanceQuery) GetContractID() *ContractID { + if x, ok := x.GetBalanceSource().(*CryptoGetAccountBalanceQuery_ContractID); ok { + return x.ContractID + } + return nil +} + +type isCryptoGetAccountBalanceQuery_BalanceSource interface { + isCryptoGetAccountBalanceQuery_BalanceSource() +} + +type CryptoGetAccountBalanceQuery_AccountID struct { + AccountID *AccountID `protobuf:"bytes,2,opt,name=accountID,proto3,oneof"` // The account ID for which information is requested +} + +type CryptoGetAccountBalanceQuery_ContractID struct { + ContractID *ContractID `protobuf:"bytes,3,opt,name=contractID,proto3,oneof"` // The account ID for which information is requested +} + +func (*CryptoGetAccountBalanceQuery_AccountID) isCryptoGetAccountBalanceQuery_BalanceSource() {} + +func (*CryptoGetAccountBalanceQuery_ContractID) isCryptoGetAccountBalanceQuery_BalanceSource() {} + // Response when the client sends the node CryptoGetAccountBalanceQuery type CryptoGetAccountBalanceResponse struct { state protoimpl.MessageState @@ -150,30 +183,34 @@ var file_proto_CryptoGetAccountBalance_proto_rawDesc = []byte{ 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x17, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x51, 0x75, 0x65, 0x72, 0x79, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x1a, 0x1a, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2f, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x48, 0x65, 0x61, - 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0x7a, 0x0a, 0x1c, 0x43, 0x72, 0x79, - 0x70, 0x74, 0x6f, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x42, 0x61, 0x6c, - 0x61, 0x6e, 0x63, 0x65, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x2a, 0x0a, 0x06, 0x68, 0x65, 0x61, - 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x06, 0x68, - 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x2e, 0x0a, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, - 0x49, 0x44, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x2e, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x44, 0x52, 0x09, 0x61, 0x63, 0x63, 0x6f, - 0x75, 0x6e, 0x74, 0x49, 0x44, 0x22, 0x9a, 0x01, 0x0a, 0x1f, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, - 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x42, 0x61, 0x6c, 0x61, 0x6e, 0x63, - 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x2d, 0x0a, 0x06, 0x68, 0x65, 0x61, - 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, - 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, - 0x52, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x2e, 0x0a, 0x09, 0x61, 0x63, 0x63, 0x6f, - 0x75, 0x6e, 0x74, 0x49, 0x44, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x44, 0x52, 0x09, 0x61, - 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x44, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x61, 0x6c, 0x61, - 0x6e, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, 0x28, 0x04, 0x52, 0x07, 0x62, 0x61, 0x6c, 0x61, 0x6e, - 0x63, 0x65, 0x42, 0x48, 0x0a, 0x1a, 0x63, 0x6f, 0x6d, 0x2e, 0x68, 0x65, 0x64, 0x65, 0x72, 0x61, - 0x2e, 0x68, 0x61, 0x73, 0x68, 0x67, 0x72, 0x61, 0x70, 0x68, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, - 0x50, 0x01, 0x5a, 0x28, 0x67, 0x69, 0x74, 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, - 0x61, 0x73, 0x68, 0x67, 0x72, 0x61, 0x70, 0x68, 0x2f, 0x68, 0x65, 0x64, 0x65, 0x72, 0x61, 0x2d, - 0x73, 0x64, 0x6b, 0x2d, 0x67, 0x6f, 0x2f, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x64, 0x65, 0x72, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x22, 0xc2, 0x01, 0x0a, 0x1c, 0x43, 0x72, + 0x79, 0x70, 0x74, 0x6f, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x42, 0x61, + 0x6c, 0x61, 0x6e, 0x63, 0x65, 0x51, 0x75, 0x65, 0x72, 0x79, 0x12, 0x2a, 0x0a, 0x06, 0x68, 0x65, + 0x61, 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x12, 0x2e, 0x70, 0x72, 0x6f, + 0x74, 0x6f, 0x2e, 0x51, 0x75, 0x65, 0x72, 0x79, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x06, + 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x12, 0x30, 0x0a, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, + 0x74, 0x49, 0x44, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x2e, 0x41, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x44, 0x48, 0x00, 0x52, 0x09, 0x61, + 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x44, 0x12, 0x33, 0x0a, 0x0a, 0x63, 0x6f, 0x6e, 0x74, + 0x72, 0x61, 0x63, 0x74, 0x49, 0x44, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x11, 0x2e, 0x70, + 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x43, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x49, 0x44, 0x48, + 0x00, 0x52, 0x0a, 0x63, 0x6f, 0x6e, 0x74, 0x72, 0x61, 0x63, 0x74, 0x49, 0x44, 0x42, 0x0f, 0x0a, + 0x0d, 0x62, 0x61, 0x6c, 0x61, 0x6e, 0x63, 0x65, 0x53, 0x6f, 0x75, 0x72, 0x63, 0x65, 0x22, 0x9a, + 0x01, 0x0a, 0x1f, 0x43, 0x72, 0x79, 0x70, 0x74, 0x6f, 0x47, 0x65, 0x74, 0x41, 0x63, 0x63, 0x6f, + 0x75, 0x6e, 0x74, 0x42, 0x61, 0x6c, 0x61, 0x6e, 0x63, 0x65, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, + 0x73, 0x65, 0x12, 0x2d, 0x0a, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, + 0x28, 0x0b, 0x32, 0x15, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x48, 0x65, 0x61, 0x64, 0x65, 0x72, 0x52, 0x06, 0x68, 0x65, 0x61, 0x64, 0x65, + 0x72, 0x12, 0x2e, 0x0a, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x44, 0x18, 0x02, + 0x20, 0x01, 0x28, 0x0b, 0x32, 0x10, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x2e, 0x41, 0x63, 0x63, + 0x6f, 0x75, 0x6e, 0x74, 0x49, 0x44, 0x52, 0x09, 0x61, 0x63, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x49, + 0x44, 0x12, 0x18, 0x0a, 0x07, 0x62, 0x61, 0x6c, 0x61, 0x6e, 0x63, 0x65, 0x18, 0x03, 0x20, 0x01, + 0x28, 0x04, 0x52, 0x07, 0x62, 0x61, 0x6c, 0x61, 0x6e, 0x63, 0x65, 0x42, 0x48, 0x0a, 0x1a, 0x63, + 0x6f, 0x6d, 0x2e, 0x68, 0x65, 0x64, 0x65, 0x72, 0x61, 0x2e, 0x68, 0x61, 0x73, 0x68, 0x67, 0x72, + 0x61, 0x70, 0x68, 0x2e, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x50, 0x01, 0x5a, 0x28, 0x67, 0x69, 0x74, + 0x68, 0x75, 0x62, 0x2e, 0x63, 0x6f, 0x6d, 0x2f, 0x68, 0x61, 0x73, 0x68, 0x67, 0x72, 0x61, 0x70, + 0x68, 0x2f, 0x68, 0x65, 0x64, 0x65, 0x72, 0x61, 0x2d, 0x73, 0x64, 0x6b, 0x2d, 0x67, 0x6f, 0x2f, + 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, 0x6f, 0x33, } var ( @@ -194,18 +231,20 @@ var file_proto_CryptoGetAccountBalance_proto_goTypes = []interface{}{ (*CryptoGetAccountBalanceResponse)(nil), // 1: proto.CryptoGetAccountBalanceResponse (*QueryHeader)(nil), // 2: proto.QueryHeader (*AccountID)(nil), // 3: proto.AccountID - (*ResponseHeader)(nil), // 4: proto.ResponseHeader + (*ContractID)(nil), // 4: proto.ContractID + (*ResponseHeader)(nil), // 5: proto.ResponseHeader } var file_proto_CryptoGetAccountBalance_proto_depIdxs = []int32{ 2, // 0: proto.CryptoGetAccountBalanceQuery.header:type_name -> proto.QueryHeader 3, // 1: proto.CryptoGetAccountBalanceQuery.accountID:type_name -> proto.AccountID - 4, // 2: proto.CryptoGetAccountBalanceResponse.header:type_name -> proto.ResponseHeader - 3, // 3: proto.CryptoGetAccountBalanceResponse.accountID:type_name -> proto.AccountID - 4, // [4:4] is the sub-list for method output_type - 4, // [4:4] is the sub-list for method input_type - 4, // [4:4] is the sub-list for extension type_name - 4, // [4:4] is the sub-list for extension extendee - 0, // [0:4] is the sub-list for field type_name + 4, // 2: proto.CryptoGetAccountBalanceQuery.contractID:type_name -> proto.ContractID + 5, // 3: proto.CryptoGetAccountBalanceResponse.header:type_name -> proto.ResponseHeader + 3, // 4: proto.CryptoGetAccountBalanceResponse.accountID:type_name -> proto.AccountID + 5, // [5:5] is the sub-list for method output_type + 5, // [5:5] is the sub-list for method input_type + 5, // [5:5] is the sub-list for extension type_name + 5, // [5:5] is the sub-list for extension extendee + 0, // [0:5] is the sub-list for field type_name } func init() { file_proto_CryptoGetAccountBalance_proto_init() } @@ -242,6 +281,10 @@ func file_proto_CryptoGetAccountBalance_proto_init() { } } } + file_proto_CryptoGetAccountBalance_proto_msgTypes[0].OneofWrappers = []interface{}{ + (*CryptoGetAccountBalanceQuery_AccountID)(nil), + (*CryptoGetAccountBalanceQuery_ContractID)(nil), + } type x struct{} out := protoimpl.TypeBuilder{ File: protoimpl.DescBuilder{ diff --git a/proto/CryptoGetAccountBalance.proto b/proto/CryptoGetAccountBalance.proto index 258985058..7e1af77d5 100644 --- a/proto/CryptoGetAccountBalance.proto +++ b/proto/CryptoGetAccountBalance.proto @@ -14,7 +14,10 @@ option go_package = "github.com/hashgraph/hedera-sdk-go/proto"; /* Get the balance of a cryptocurrency account. This returns only the balance, so it is a smaller and faster reply than CryptoGetInfo, which returns the balance plus additional information. */ message CryptoGetAccountBalanceQuery { QueryHeader header = 1; // Standard info sent from client to node, including the signed payment, and what kind of response is requested (cost, state proof, both, or neither). - AccountID accountID = 2; // The account ID for which information is requested + oneof balanceSource { + AccountID accountID = 2; // The account ID for which information is requested + ContractID contractID = 3; // The account ID for which information is requested + } } /* Response when the client sends the node CryptoGetAccountBalanceQuery */ @@ -23,5 +26,3 @@ message CryptoGetAccountBalanceResponse { AccountID accountID = 2; // The account ID that is being described (this is useful with state proofs, for proving to a third party) uint64 balance = 3; // The current balance, in tinybars } - -