-
Notifications
You must be signed in to change notification settings - Fork 175
/
contractFunctionInvocations.go
184 lines (168 loc) · 6.1 KB
/
contractFunctionInvocations.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
package fvm
import (
"github.com/opentracing/opentracing-go"
"github.com/onflow/cadence"
"github.com/onflow/cadence/runtime/common"
"github.com/onflow/cadence/runtime/interpreter"
"github.com/onflow/cadence/runtime/sema"
"github.com/onflow/flow-go/fvm/systemcontracts"
"github.com/onflow/flow-go/model/flow"
)
var deductTransactionFeesInvocationArgumentTypes = []sema.Type{
sema.AuthAccountType,
sema.UInt64Type,
sema.UInt64Type,
}
// DeductTransactionFeesInvocation prepares a function that calls fee deduction on the service account
func DeductTransactionFeesInvocation(
env Environment,
traceSpan opentracing.Span,
) func(payer flow.Address, inclusionEffort uint64, executionEffort uint64) (cadence.Value, error) {
feesAddress := FlowFeesAddress(env.Context().Chain)
return func(payer flow.Address, inclusionEffort uint64, executionEffort uint64) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(feesAddress),
Name: systemcontracts.ContractNameFlowFees,
},
systemcontracts.ContractServiceAccountFunction_deductTransactionFee,
[]interpreter.Value{
interpreter.NewUnmeteredAddressValueFromBytes(payer.Bytes()),
interpreter.UFix64Value(inclusionEffort),
interpreter.UFix64Value(executionEffort),
},
deductTransactionFeesInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
}
var setupNewAccountInvocationArgumentTypes = []sema.Type{
sema.AuthAccountType,
sema.AuthAccountType,
}
// SetupNewAccountInvocation prepares a function that calls new account setup on the service account
func SetupNewAccountInvocation(
env Environment,
traceSpan opentracing.Span,
) func(flowAddress flow.Address, payer common.Address) (cadence.Value, error) {
return func(flowAddress flow.Address, payer common.Address) (cadence.Value, error) {
// uses `FlowServiceAccount.setupNewAccount` from https://github.com/onflow/flow-core-contracts/blob/master/contracts/FlowServiceAccount.cdc
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractServiceAccount,
},
systemcontracts.ContractServiceAccountFunction_setupNewAccount,
[]interpreter.Value{
interpreter.NewAddressValue(env, common.Address(flowAddress)),
interpreter.NewAddressValue(env, payer),
},
setupNewAccountInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
}
var accountAvailableBalanceInvocationArgumentTypes = []sema.Type{
&sema.AddressType{},
}
// AccountAvailableBalanceInvocation prepares a function that calls get available balance on the storage fees contract
func AccountAvailableBalanceInvocation(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address) (cadence.Value, error) {
return func(address common.Address) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractStorageFees,
},
systemcontracts.ContractStorageFeesFunction_defaultTokenAvailableBalance,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountAvailableBalanceInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
}
var accountBalanceInvocationArgumentTypes = []sema.Type{
sema.PublicAccountType,
}
// AccountBalanceInvocation prepares a function that calls get available balance on the service account
func AccountBalanceInvocation(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address) (cadence.Value, error) {
return func(address common.Address) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractServiceAccount},
systemcontracts.ContractServiceAccountFunction_defaultTokenBalance,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountBalanceInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
}
var accountStorageCapacityInvocationArgumentTypes = []sema.Type{
&sema.AddressType{},
}
// AccountStorageCapacityInvocation prepares a function that calls get storage capacity on the storage fees contract
func AccountStorageCapacityInvocation(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address) (cadence.Value, error) {
return func(address common.Address) (cadence.Value, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractStorageFees,
},
systemcontracts.ContractStorageFeesFunction_calculateAccountCapacity,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
},
accountStorageCapacityInvocationArgumentTypes,
env.Context().Logger,
)
return invoker.Invoke(env, traceSpan)
}
}
var useContractAuditVoucherInvocationArgumentTypes = []sema.Type{
&sema.AddressType{},
sema.StringType,
}
// UseContractAuditVoucherInvocation prepares a function that can use a contract deployment audit voucher
func UseContractAuditVoucherInvocation(
env Environment,
traceSpan opentracing.Span,
) func(address common.Address, code string) (bool, error) {
return func(address common.Address, code string) (bool, error) {
invoker := NewContractFunctionInvoker(
common.AddressLocation{
Address: common.Address(env.Context().Chain.ServiceAddress()),
Name: systemcontracts.ContractDeploymentAudits,
},
systemcontracts.ContractDeploymentAuditsFunction_useVoucherForDeploy,
[]interpreter.Value{
interpreter.NewAddressValue(env, address),
interpreter.NewUnmeteredStringValue(code),
},
useContractAuditVoucherInvocationArgumentTypes,
env.Context().Logger,
)
resultCdc, err := invoker.Invoke(env, traceSpan)
if err != nil {
return false, err
}
result := resultCdc.(cadence.Bool).ToGoValue().(bool)
return result, nil
}
}