Skip to content

Commit

Permalink
documentation for contract call query
Browse files Browse the repository at this point in the history
  • Loading branch information
QuestofIranon committed Feb 20, 2020
1 parent ca97c2f commit 8a94490
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions contract_call_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@ import (
"github.com/hashgraph/hedera-sdk-go/proto"
)

// ContractCallQuery calls a function of the given smart contract instance, giving it ContractFunctionParameters as its
// inputs. It will consume the entire given amount of gas.
//
// This is performed locally on the particular node that the client is communicating with. It cannot change the state of
// the contract instance (and so, cannot spend anything from the instance's Hedera account). It will not have a
// consensus timestamp. It cannot generate a record or a receipt. This is useful for calling getter functions, which
// purely read the state and don't change it. It is faster and cheaper than a normal call, because it is purely local to
// a single node.
type ContractCallQuery struct {
QueryBuilder
pb *proto.ContractCallLocalQuery
}

// NewContractCallQuery creates a ContractCallQuery builder which can be used to construct and execute a
// Contract Call Local Query.
func NewContractCallQuery() *ContractCallQuery {
pb := &proto.ContractCallLocalQuery{Header: &proto.QueryHeader{}}

Expand All @@ -18,21 +28,26 @@ func NewContractCallQuery() *ContractCallQuery {
return &ContractCallQuery{inner, pb}
}

// SetContractID sets the contract instance to call
func (builder *ContractCallQuery) SetContractID(id ContractID) *ContractCallQuery {
builder.pb.ContractID = id.toProto()
return builder
}

// SetGas sets the amount of gas to use for the call. All of the gas offered will be charged for.
func (builder *ContractCallQuery) SetGas(gas uint64) *ContractCallQuery {
builder.pb.Gas = int64(gas)
return builder
}

// SetMaxResultSize sets the max number of bytes that the result might include. The run will fail if it would have
// returned more than this number of bytes.
func (builder *ContractCallQuery) SetMaxResultSize(size uint64) *ContractCallQuery {
builder.pb.MaxResultSize = int64(size)
return builder
}

// SetFunction sets which function to call, and the ContractFunctionParams to pass to the function
func (builder *ContractCallQuery) SetFunction(name string, params *ContractFunctionParams) *ContractCallQuery {
if params == nil {
params = NewContractFunctionParams()
Expand All @@ -42,6 +57,8 @@ func (builder *ContractCallQuery) SetFunction(name string, params *ContractFunct
return builder
}

// Execute executes the ContractCallQuery using the provided client. The returned ContractFunctionResult will contain
// the output returned by the function call.
func (builder *ContractCallQuery) Execute(client *Client) (ContractFunctionResult, error) {
resp, err := builder.execute(client)
if err != nil {
Expand All @@ -51,14 +68,16 @@ func (builder *ContractCallQuery) Execute(client *Client) (ContractFunctionResul
return contractFunctionResultFromProto(resp.GetContractCallLocal().FunctionResult), nil
}

// Cost is a wrapper around the standard Cost function for a query. It must exist because the cost returned the standard
// QueryBuilder.Cost() function and therein the Hedera Network doesn't work for ContractCallQueries. However, if the
// value returned by the network is increased by 10% then most contractCallQueries will complete fine.
func (builder *ContractCallQuery) Cost(client *Client) (Hbar, error) {
cost, err := builder.QueryBuilder.GetCost(client)
if err != nil {
return ZeroHbar, err
}

// TODO: Document why
return HbarFromTinybar(int64(float64(cost.AsTinybar()) * float64(1.1))), nil
return HbarFromTinybar(int64(float64(cost.AsTinybar()) * 1.1)), nil
}

//
Expand Down

0 comments on commit 8a94490

Please sign in to comment.