forked from stellar/go-stellar-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinvoke_host_function.go
More file actions
270 lines (243 loc) · 7.13 KB
/
invoke_host_function.go
File metadata and controls
270 lines (243 loc) · 7.13 KB
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
package txnbuild
import (
"github.com/stellar/go-stellar-sdk/amount"
"github.com/stellar/go-stellar-sdk/ingest/sac"
"github.com/stellar/go-stellar-sdk/strkey"
"github.com/stellar/go-stellar-sdk/support/errors"
"github.com/stellar/go-stellar-sdk/xdr"
)
type InvokeHostFunction struct {
HostFunction xdr.HostFunction
Auth []xdr.SorobanAuthorizationEntry
SourceAccount string
Ext xdr.TransactionExt
}
type SorobanFees struct {
Instructions uint32
DiskReadBytes uint32
WriteBytes uint32
ResourceFee int64
}
var defaultPaymentToContractFees = SorobanFees{
Instructions: 400_000,
DiskReadBytes: 1_000,
WriteBytes: 1_000,
ResourceFee: 5_000_000,
}
// PaymentToContractParams configures the payment returned by NewPaymentToContract
type PaymentToContractParams struct {
// NetworkPassphrase is the passphrase for the Stellar network
NetworkPassphrase string
// Destination is the contract recipient of the payment
Destination string
// Amount is the amount being transferred
Amount string
// Asset is the asset being transferred
Asset Asset
// SourceAccount is the source account of the payment, it must be a Stellar account in strkey encoded`VersionByteAccountID` format, i.e. a 'G' account.
SourceAccount string
// Fees configures the fee values for the
// soroban transaction. If this field is omitted
// default fee values will be used
Fees *SorobanFees
}
// NewPaymentToContract constructs an invoke host operation to send a payment from a
// an account to a destination smart contract. Note the account sending the payment
// must be the source account of the operation because the returned invoke host operation
// will use the source account as the auth credentials.
func NewPaymentToContract(params PaymentToContractParams) (InvokeHostFunction, error) {
asset, err := params.Asset.ToXDR()
if err != nil {
return InvokeHostFunction{}, err
}
var assetContractID xdr.ContractId
assetContractID, err = asset.ContractID(params.NetworkPassphrase)
if err != nil {
return InvokeHostFunction{}, err
}
sourceAccount, err := xdr.AddressToAccountId(params.SourceAccount)
if err != nil {
return InvokeHostFunction{}, err
}
decoded, err := strkey.Decode(strkey.VersionByteContract, params.Destination)
if err != nil {
return InvokeHostFunction{}, err
}
var destinationContractID xdr.ContractId
copy(destinationContractID[:], decoded)
parsedAmount, err := amount.Parse(params.Amount)
if err != nil {
return InvokeHostFunction{}, err
}
transferArgs := xdr.ScVec{
xdr.ScVal{
Type: xdr.ScValTypeScvAddress,
Address: &xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeAccount,
AccountId: &sourceAccount,
},
},
xdr.ScVal{
Type: xdr.ScValTypeScvAddress,
Address: &xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &destinationContractID,
},
},
xdr.ScVal{
Type: xdr.ScValTypeScvI128,
I128: &xdr.Int128Parts{
Hi: 0,
Lo: xdr.Uint64(parsedAmount),
},
},
}
resources := defaultPaymentToContractFees
if params.Fees != nil {
resources = *params.Fees
}
assetContractInstance := xdr.LedgerKey{
Type: xdr.LedgerEntryTypeContractData,
ContractData: &xdr.LedgerKeyContractData{
Contract: xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &assetContractID,
},
Key: xdr.ScVal{
Type: xdr.ScValTypeScvLedgerKeyContractInstance,
},
Durability: xdr.ContractDataDurabilityPersistent,
},
}
footprint := xdr.LedgerFootprint{
ReadOnly: []xdr.LedgerKey{
assetContractInstance,
},
ReadWrite: []xdr.LedgerKey{
sac.ContractBalanceLedgerKey(assetContractID, destinationContractID),
},
}
if asset.IsNative() {
footprint.ReadWrite = append(footprint.ReadWrite,
xdr.LedgerKey{
Type: xdr.LedgerEntryTypeAccount,
Account: &xdr.LedgerKeyAccount{
AccountId: sourceAccount,
},
},
)
} else {
issuer, err := asset.GetIssuerAccountId()
if err != nil {
return InvokeHostFunction{}, err
}
footprint.ReadOnly = append(footprint.ReadOnly,
xdr.LedgerKey{
Type: xdr.LedgerEntryTypeAccount,
Account: &xdr.LedgerKeyAccount{
AccountId: issuer,
},
},
)
if !sourceAccount.Equals(issuer) {
footprint.ReadWrite = append(footprint.ReadWrite,
xdr.LedgerKey{
Type: xdr.LedgerEntryTypeTrustline,
TrustLine: &xdr.LedgerKeyTrustLine{
AccountId: sourceAccount,
Asset: xdr.TrustLineAsset{
Type: asset.Type,
AlphaNum4: asset.AlphaNum4,
AlphaNum12: asset.AlphaNum12,
},
},
},
)
}
}
return InvokeHostFunction{
HostFunction: xdr.HostFunction{
Type: xdr.HostFunctionTypeHostFunctionTypeInvokeContract,
InvokeContract: &xdr.InvokeContractArgs{
ContractAddress: xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &assetContractID,
},
FunctionName: "transfer",
Args: transferArgs,
},
},
SourceAccount: params.SourceAccount,
Auth: []xdr.SorobanAuthorizationEntry{
{
Credentials: xdr.SorobanCredentials{
Type: xdr.SorobanCredentialsTypeSorobanCredentialsSourceAccount,
},
RootInvocation: xdr.SorobanAuthorizedInvocation{
Function: xdr.SorobanAuthorizedFunction{
Type: xdr.SorobanAuthorizedFunctionTypeSorobanAuthorizedFunctionTypeContractFn,
ContractFn: &xdr.InvokeContractArgs{
ContractAddress: xdr.ScAddress{
Type: xdr.ScAddressTypeScAddressTypeContract,
ContractId: &assetContractID,
},
FunctionName: "transfer",
Args: transferArgs,
},
},
},
},
},
Ext: xdr.TransactionExt{
V: 1,
SorobanData: &xdr.SorobanTransactionData{
Resources: xdr.SorobanResources{
Footprint: footprint,
Instructions: xdr.Uint32(resources.Instructions),
DiskReadBytes: xdr.Uint32(resources.DiskReadBytes),
WriteBytes: xdr.Uint32(resources.WriteBytes),
},
ResourceFee: xdr.Int64(resources.ResourceFee),
},
},
}, nil
}
func (f *InvokeHostFunction) BuildXDR() (xdr.Operation, error) {
opType := xdr.OperationTypeInvokeHostFunction
xdrOp := xdr.InvokeHostFunctionOp{
HostFunction: f.HostFunction,
Auth: f.Auth,
}
body, err := xdr.NewOperationBody(opType, xdrOp)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to build XDR Operation")
}
op := xdr.Operation{Body: body}
SetOpSourceAccount(&op, f.SourceAccount)
return op, nil
}
func (f *InvokeHostFunction) FromXDR(xdrOp xdr.Operation) error {
result, ok := xdrOp.Body.GetInvokeHostFunctionOp()
if !ok {
return errors.New("error parsing invoke host function operation from xdr")
}
f.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
f.HostFunction = result.HostFunction
f.Auth = result.Auth
return nil
}
func (f *InvokeHostFunction) Validate() error {
if f.SourceAccount != "" {
_, err := xdr.AddressToMuxedAccount(f.SourceAccount)
if err != nil {
return NewValidationError("SourceAccount", err.Error())
}
}
return nil
}
func (f *InvokeHostFunction) GetSourceAccount() string {
return f.SourceAccount
}
func (f *InvokeHostFunction) BuildTransactionExt() (xdr.TransactionExt, error) {
return f.Ext, nil
}