forked from hiero-ledger/hiero-sdk-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contract_call_query.go
98 lines (80 loc) · 3.97 KB
/
contract_call_query.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package hedera
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 ContractExecuteTransaction, 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{}}
inner := newQueryBuilder(pb.Header)
inner.pb.Query = &proto.Query_ContractCallLocal{ContractCallLocal: pb}
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()
}
builder.pb.FunctionParameters = params.build(&name)
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 {
return ContractFunctionResult{}, err
}
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
}
return HbarFromTinybar(int64(float64(cost.AsTinybar()) * 1.1)), nil
}
//
// The following _3_ must be copy-pasted at the bottom of **every** _query.go file
// We override the embedded fluent setter methods to return the outer type
//
func (builder *ContractCallQuery) SetMaxQueryPayment(maxPayment Hbar) *ContractCallQuery {
return &ContractCallQuery{*builder.QueryBuilder.SetMaxQueryPayment(maxPayment), builder.pb}
}
func (builder *ContractCallQuery) SetQueryPayment(paymentAmount Hbar) *ContractCallQuery {
return &ContractCallQuery{*builder.QueryBuilder.SetQueryPayment(paymentAmount), builder.pb}
}
func (builder *ContractCallQuery) SetQueryPaymentTransaction(tx Transaction) *ContractCallQuery {
return &ContractCallQuery{*builder.QueryBuilder.SetQueryPaymentTransaction(tx), builder.pb}
}