Skip to content

Commit

Permalink
Use Hbar in all the relevant places and fix minor issues
Browse files Browse the repository at this point in the history
  • Loading branch information
mehcode committed Jan 18, 2020
1 parent 87a9b94 commit 063feec
Show file tree
Hide file tree
Showing 30 changed files with 182 additions and 279 deletions.
43 changes: 12 additions & 31 deletions account_create_transaction.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package hedera

import (
"github.com/hashgraph/hedera-sdk-go/proto"
"math"
"time"

"github.com/hashgraph/hedera-sdk-go/proto"
)

type AccountCreateTransaction struct {
Expand All @@ -23,19 +23,19 @@ func NewAccountCreateTransaction() AccountCreateTransaction {
// Default to maximum values for record thresholds. Without this records would be
// auto-created whenever a send or receive transaction takes place for this new account.
// This should be an explicit ask.
builder.SetReceiveRecordThreshold(uint64(math.MaxInt64))
builder.SetSendRecordThreshold(uint64(math.MaxInt64))
builder.SetReceiveRecordThreshold(MaxHbar)
builder.SetSendRecordThreshold(MaxHbar)

return builder
}

func (builder AccountCreateTransaction) SetKey(publicKey Ed25519PublicKey) AccountCreateTransaction {
func (builder AccountCreateTransaction) SetKey(publicKey PublicKey) AccountCreateTransaction {
builder.pb.Key = publicKey.toProto()
return builder
}

func (builder AccountCreateTransaction) SetInitialBalance(tinyBars uint64) AccountCreateTransaction {
builder.pb.InitialBalance = tinyBars
func (builder AccountCreateTransaction) SetInitialBalance(initialBalance Hbar) AccountCreateTransaction {
builder.pb.InitialBalance = uint64(initialBalance.AsTinybar())
return builder
}

Expand All @@ -44,13 +44,13 @@ func (builder AccountCreateTransaction) SetAutoRenewPeriod(autoRenewPeriod time.
return builder
}

func (builder AccountCreateTransaction) SetSendRecordThreshold(recordThreshold uint64) AccountCreateTransaction {
builder.pb.SendRecordThreshold = recordThreshold
func (builder AccountCreateTransaction) SetSendRecordThreshold(recordThreshold Hbar) AccountCreateTransaction {
builder.pb.SendRecordThreshold = uint64(recordThreshold.AsTinybar())
return builder
}

func (builder AccountCreateTransaction) SetReceiveRecordThreshold(recordThreshold uint64) AccountCreateTransaction {
builder.pb.ReceiveRecordThreshold = recordThreshold
func (builder AccountCreateTransaction) SetReceiveRecordThreshold(recordThreshold Hbar) AccountCreateTransaction {
builder.pb.ReceiveRecordThreshold = uint64(recordThreshold.AsTinybar())
return builder
}

Expand All @@ -64,31 +64,12 @@ func (builder AccountCreateTransaction) SetReceiverSignatureRequired(required bo
return builder
}

func (builder AccountCreateTransaction) Build(client *Client) Transaction {
// If a shard/realm is not set, it is inferred from the Operator on the Client

if builder.pb.ShardID == nil && client != nil {
builder.pb.ShardID = &proto.ShardID{
ShardNum: int64(client.operator.accountID.Shard),
}
}

if builder.pb.RealmID == nil && client != nil {
builder.pb.RealmID = &proto.RealmID{
ShardNum: int64(client.operator.accountID.Shard),
RealmNum: int64(client.operator.accountID.Realm),
}
}

return builder.TransactionBuilder.Build(client)
}

//
// The following _5_ must be copy-pasted at the bottom of **every** _transaction.go file
// We override the embedded fluent setter methods to return the outer type
//

func (builder AccountCreateTransaction) SetMaxTransactionFee(maxTransactionFee uint64) AccountCreateTransaction {
func (builder AccountCreateTransaction) SetMaxTransactionFee(maxTransactionFee Hbar) AccountCreateTransaction {
return AccountCreateTransaction{builder.TransactionBuilder.SetMaxTransactionFee(maxTransactionFee), builder.pb}
}

Expand Down
9 changes: 1 addition & 8 deletions account_delete_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ func NewAccountDeleteTransaction() AccountDeleteTransaction {
return builder
}

// SetDeleteAccountID sets the account to delete. Note: To successfully delete
// an account one must also manually set the `TransactionID` to a
// `TransactionID` constructed from the same `AccountID`
func (builder AccountDeleteTransaction) SetDeleteAccountID(id AccountID) AccountDeleteTransaction {
builder.pb.DeleteAccountID = id.toProto()
return builder
Expand All @@ -34,16 +31,12 @@ func (builder AccountDeleteTransaction) SetTransferAccountID(id AccountID) Accou
return builder
}

func (builder AccountDeleteTransaction) Build(client *Client) Transaction {
return builder.TransactionBuilder.Build(client)
}

//
// The following _5_ must be copy-pasted at the bottom of **every** _transaction.go file
// We override the embedded fluent setter methods to return the outer type
//

func (builder AccountDeleteTransaction) SetMaxTransactionFee(maxTransactionFee uint64) AccountDeleteTransaction {
func (builder AccountDeleteTransaction) SetMaxTransactionFee(maxTransactionFee Hbar) AccountDeleteTransaction {
return AccountDeleteTransaction{builder.TransactionBuilder.SetMaxTransactionFee(maxTransactionFee), builder.pb}
}

Expand Down
29 changes: 16 additions & 13 deletions account_info_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ type AccountInfo struct {
ContractAccountID string
Deleted bool
ProxyAccountID AccountID
ProxyReceived int64
ProxyReceived Hbar
Key PublicKey
Balance uint64
GenerateSendRecordThreshold uint64
GenerateReceiveRecordThreshold uint64
Balance Hbar
GenerateSendRecordThreshold Hbar
GenerateReceiveRecordThreshold Hbar
ReceiverSigRequired bool
ExpirationTime time.Time
AutoRenewPeriod time.Duration
Expand All @@ -46,35 +46,38 @@ func (builder *AccountInfoQuery) Execute(client *Client) (AccountInfo, error) {
}

pubKey, err := publicKeyFromProto(resp.GetCryptoGetInfo().AccountInfo.Key)
if err != nil {
return AccountInfo{}, err
}

return AccountInfo{
AccountID: accountIDFromProto(resp.GetCryptoGetInfo().AccountInfo.AccountID),
ContractAccountID: resp.GetCryptoGetInfo().AccountInfo.ContractAccountID,
Deleted: resp.GetCryptoGetInfo().AccountInfo.Deleted,
ProxyAccountID: accountIDFromProto(resp.GetCryptoGetInfo().AccountInfo.ProxyAccountID),
ProxyReceived: resp.GetCryptoGetInfo().AccountInfo.ProxyReceived,
ProxyReceived: HbarFromTinybar(resp.GetCryptoGetInfo().AccountInfo.ProxyReceived),
Key: pubKey,
Balance: resp.GetCryptoGetInfo().AccountInfo.Balance,
GenerateSendRecordThreshold: resp.GetCryptoGetInfo().AccountInfo.GenerateSendRecordThreshold,
GenerateReceiveRecordThreshold: resp.GetCryptoGetInfo().AccountInfo.GenerateReceiveRecordThreshold,
Balance: HbarFromTinybar(int64(resp.GetCryptoGetInfo().AccountInfo.Balance)),
GenerateSendRecordThreshold: HbarFromTinybar(int64(resp.GetCryptoGetInfo().AccountInfo.GenerateSendRecordThreshold)),
GenerateReceiveRecordThreshold: HbarFromTinybar(int64(resp.GetCryptoGetInfo().AccountInfo.GenerateReceiveRecordThreshold)),
ReceiverSigRequired: resp.GetCryptoGetInfo().AccountInfo.ReceiverSigRequired,
ExpirationTime: timeFromProto(resp.GetCryptoGetInfo().AccountInfo.ExpirationTime),
}, nil
}

func (builder *AccountInfoQuery) Cost(client *Client) (uint64, error) {
func (builder *AccountInfoQuery) Cost(client *Client) (Hbar, error) {
// deleted files return a COST_ANSWER of zero which triggers `INSUFFICIENT_TX_FEE`
// if you set that as the query payment; 25 tinybar seems to be enough to get
// `ACCOUNT_DELETED` back instead.
cost, err := builder.QueryBuilder.Cost(client)
if err != nil {
return 0, err
return ZeroHbar, err
}

// math.Min requires float64 and returns float64
if cost > 25 {
// math.Max requires float64 and returns float64
if cost.AsTinybar() > 25 {
return cost, nil
}

return 25, nil
return HbarFromTinybar(25), nil
}
13 changes: 4 additions & 9 deletions account_stakers_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@ type AccountStakersQuery struct {
pb *proto.CryptoGetStakersQuery
}

type ProxyStaker struct {
AccountID AccountID
Amount int64
}

func NewAccountStakersQuery() *AccountStakersQuery {
pb := &proto.CryptoGetStakersQuery{Header: &proto.QueryHeader{}}

Expand All @@ -26,18 +21,18 @@ func (builder *AccountStakersQuery) SetAccountID(id AccountID) *AccountStakersQu
return builder
}

func (builder *AccountStakersQuery) Execute(client *Client) ([]ProxyStaker, error) {
var stakers = []ProxyStaker{}
func (builder *AccountStakersQuery) Execute(client *Client) ([]Transfer, error) {
var stakers = []Transfer{}

resp, err := builder.execute(client)
if err != nil {
return stakers, err
}

for _, element := range resp.GetCryptoGetProxyStakers().Stakers.ProxyStaker {
stakers = append(stakers, ProxyStaker{
stakers = append(stakers, Transfer{
AccountID: accountIDFromProto(element.AccountID),
Amount: element.Amount,
Amount: HbarFromTinybar(element.Amount),
})
}

Expand Down
16 changes: 6 additions & 10 deletions account_update_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func (builder AccountUpdateTransaction) SetAccountID(id AccountID) AccountUpdate
return builder
}

func (builder AccountUpdateTransaction) SetKey(publicKey Ed25519PublicKey) AccountUpdateTransaction {
func (builder AccountUpdateTransaction) SetKey(publicKey PublicKey) AccountUpdateTransaction {
builder.pb.Key = publicKey.toProto()
return builder
}
Expand All @@ -53,30 +53,26 @@ func (builder AccountUpdateTransaction) SetReceiverSignatureRequired(required bo
return builder
}

func (builder AccountUpdateTransaction) SetSendRecordThreshold(threshold uint64) AccountUpdateTransaction {
func (builder AccountUpdateTransaction) SetSendRecordThreshold(threshold Hbar) AccountUpdateTransaction {
builder.pb.SendRecordThresholdField = &proto.CryptoUpdateTransactionBody_SendRecordThreshold{
SendRecordThreshold: threshold,
SendRecordThreshold: uint64(threshold.AsTinybar()),
}
return builder
}

func (builder AccountUpdateTransaction) SetReceiveRecordThreshold(threshold uint64) AccountUpdateTransaction {
func (builder AccountUpdateTransaction) SetReceiveRecordThreshold(threshold Hbar) AccountUpdateTransaction {
builder.pb.ReceiveRecordThresholdField = &proto.CryptoUpdateTransactionBody_ReceiveRecordThreshold{
ReceiveRecordThreshold: threshold,
ReceiveRecordThreshold: uint64(threshold.AsTinybar()),
}
return builder
}

func (builder AccountUpdateTransaction) Build(client *Client) Transaction {
return builder.TransactionBuilder.Build(client)
}

//
// The following _5_ must be copy-pasted at the bottom of **every** _transaction.go file
// We override the embedded fluent setter methods to return the outer type
//

func (builder AccountUpdateTransaction) SetMaxTransactionFee(maxTransactionFee uint64) AccountUpdateTransaction {
func (builder AccountUpdateTransaction) SetMaxTransactionFee(maxTransactionFee Hbar) AccountUpdateTransaction {
return AccountUpdateTransaction{builder.TransactionBuilder.SetMaxTransactionFee(maxTransactionFee), builder.pb}
}

Expand Down
20 changes: 10 additions & 10 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ import (
)

// Default max fees and payments to 1 h-bar
const defaultMaxTransactionFee uint64 = 100_000_000
const defaultMaxQueryPayment uint64 = 100_000_000
var defaultMaxTransactionFee Hbar = NewHbar(1)
var defaultMaxQueryPayment Hbar = NewHbar(1)

type Client struct {
maxTransactionFee uint64
maxQueryPayment uint64
maxTransactionFee Hbar
maxQueryPayment Hbar

operator *operator

Expand Down Expand Up @@ -86,8 +86,8 @@ type configOperator struct {
}

type clientConfig struct {
Network map[string]AccountID `json:"network"`
Operator *configOperator `json:"operator"`
Network map[string]AccountID `json:"network"`
Operator *configOperator `json:"operator"`
}

func ClientFromJSON(jsonBytes []byte) (*Client, error) {
Expand Down Expand Up @@ -189,13 +189,13 @@ func (client *Client) SetOperatorWith(accountID AccountID, publicKey Ed25519Publ
return client
}

func (client *Client) SetMaxTransactionFee(tinyBars uint64) *Client {
client.maxTransactionFee = tinyBars
func (client *Client) SetMaxTransactionFee(fee Hbar) *Client {
client.maxTransactionFee = fee
return client
}

func (client *Client) SetMaxQueryPayment(tinyBars uint64) *Client {
client.maxQueryPayment = tinyBars
func (client *Client) SetMaxQueryPayment(payment Hbar) *Client {
client.maxQueryPayment = payment
return client
}

Expand Down
18 changes: 7 additions & 11 deletions contract_call_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,9 @@ func (builder *ContractCallQuery) SetMaxResultSize(size uint64) *ContractCallQue
return builder
}

func (builder *ContractCallQuery) SetFunctionParameters(params ContractFunctionParams) (*ContractCallQuery, error) {
function, err := params.build(nil)
if err != nil {
return builder, err
}

builder.pb.FunctionParameters = function
return builder, nil
func (builder *ContractCallQuery) SetFunction(name string, params ContractFunctionParams) *ContractCallQuery {
builder.pb.FunctionParameters = params.build(&name)
return builder
}

func (builder *ContractCallQuery) Execute(client *Client) (ContractFunctionResult, error) {
Expand All @@ -52,11 +47,12 @@ func (builder *ContractCallQuery) Execute(client *Client) (ContractFunctionResul
return contractFunctionResultFromProto(resp.GetContractCallLocal().FunctionResult), nil
}

func (builder *ContractCallQuery) Cost(client *Client) (uint64, error) {
func (builder *ContractCallQuery) Cost(client *Client) (Hbar, error) {
cost, err := builder.QueryBuilder.Cost(client)
if err != nil {
return 0, err
return ZeroHbar, err
}

return uint64(float64(cost) * float64(1.1)), nil
// TODO: Document why
return HbarFromTinybar(int64(float64(cost.AsTinybar()) * float64(1.1))), nil
}
31 changes: 6 additions & 25 deletions contract_create_transaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ func NewContractCreateTransaction() ContractCreateTransaction {
return builder
}

func (builder ContractCreateTransaction) SetBytecodeFile(id FileID) ContractCreateTransaction {
func (builder ContractCreateTransaction) SetBytecodeFileID(id FileID) ContractCreateTransaction {
builder.pb.FileID = id.toProto()
return builder
}
Expand All @@ -41,8 +41,8 @@ func (builder ContractCreateTransaction) SetGas(gas uint64) ContractCreateTransa
return builder
}

func (builder ContractCreateTransaction) SetInitialBalance(tinyBars uint64) ContractCreateTransaction {
builder.pb.InitialBalance = int64(tinyBars)
func (builder ContractCreateTransaction) SetInitialBalance(initialBalance Hbar) ContractCreateTransaction {
builder.pb.InitialBalance = initialBalance.AsTinybar()
return builder
}

Expand All @@ -56,36 +56,17 @@ func (builder ContractCreateTransaction) SetAutoRenewPeriod(autoRenewPeriod time
return builder
}

func (builder ContractCreateTransaction) SetConstructorParams(params []byte) ContractCreateTransaction {
builder.pb.ConstructorParameters = params
func (builder ContractCreateTransaction) SetConstructorParams(params ContractFunctionParams) ContractCreateTransaction {
builder.pb.ConstructorParameters = params.build(nil)
return builder
}

func (builder ContractCreateTransaction) Build(client *Client) Transaction {
// If a shard/realm is not set, it is inferred from the Operator on the Client

if builder.pb.ShardID == nil {
builder.pb.ShardID = &proto.ShardID{
ShardNum: int64(client.operator.accountID.Shard),
}
}

if builder.pb.RealmID == nil {
builder.pb.RealmID = &proto.RealmID{
ShardNum: int64(client.operator.accountID.Shard),
RealmNum: int64(client.operator.accountID.Realm),
}
}

return builder.TransactionBuilder.Build(client)
}

//
// The following _5_ must be copy-pasted at the bottom of **every** _transaction.go file
// We override the embedded fluent setter methods to return the outer type
//

func (builder ContractCreateTransaction) SetMaxTransactionFee(maxTransactionFee uint64) ContractCreateTransaction {
func (builder ContractCreateTransaction) SetMaxTransactionFee(maxTransactionFee Hbar) ContractCreateTransaction {
return ContractCreateTransaction{builder.TransactionBuilder.SetMaxTransactionFee(maxTransactionFee), builder.pb}
}

Expand Down
Loading

0 comments on commit 063feec

Please sign in to comment.