forked from stellar/go-stellar-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrestore_footprint.go
More file actions
129 lines (112 loc) · 3.39 KB
/
restore_footprint.go
File metadata and controls
129 lines (112 loc) · 3.39 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
package txnbuild
import (
"github.com/stellar/go/ingest/sac"
"github.com/stellar/go/strkey"
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
type RestoreFootprint struct {
SourceAccount string
Ext xdr.TransactionExt
}
var defaultAssetBalanceRestorationFees = SorobanFees{
Instructions: 0,
DiskReadBytes: 500,
WriteBytes: 500,
ResourceFee: 4_000_000,
}
// AssetBalanceRestorationParams configures the restore footprint operation returned by
// NewAssetBalanceRestoration
type AssetBalanceRestorationParams struct {
// NetworkPassphrase is the passphrase for the Stellar network
NetworkPassphrase string
// Contract is the contract which holds the asset balance
Contract string
// Asset is the asset which is held in the balance
Asset Asset
// SourceAccount is the source account for the restoration operation
SourceAccount string
// Fees configures the fee values for the
// soroban transaction. If this field is omitted
// default fee values will be used
Fees SorobanFees
}
// NewAssetBalanceRestoration constructs a restore footprint operation which restores an
// asset balance for a smart contract
func NewAssetBalanceRestoration(params AssetBalanceRestorationParams) (RestoreFootprint, error) {
asset, err := params.Asset.ToXDR()
if err != nil {
return RestoreFootprint{}, err
}
var assetContractID xdr.Hash
assetContractID, err = asset.ContractID(params.NetworkPassphrase)
if err != nil {
return RestoreFootprint{}, err
}
decoded, err := strkey.Decode(strkey.VersionByteContract, params.Contract)
if err != nil {
return RestoreFootprint{}, err
}
var contractID xdr.Hash
copy(contractID[:], decoded)
resources := params.Fees
if resources.ResourceFee == 0 {
resources = defaultAssetBalanceRestorationFees
}
return RestoreFootprint{
SourceAccount: params.SourceAccount,
Ext: xdr.TransactionExt{
V: 1,
SorobanData: &xdr.SorobanTransactionData{
Resources: xdr.SorobanResources{
Footprint: xdr.LedgerFootprint{
ReadWrite: []xdr.LedgerKey{
sac.ContractBalanceLedgerKey(assetContractID, contractID),
},
},
Instructions: xdr.Uint32(resources.Instructions),
DiskReadBytes: xdr.Uint32(resources.DiskReadBytes),
WriteBytes: xdr.Uint32(resources.WriteBytes),
},
ResourceFee: xdr.Int64(resources.ResourceFee),
},
},
}, nil
}
func (f *RestoreFootprint) BuildXDR() (xdr.Operation, error) {
xdrOp := xdr.RestoreFootprintOp{
Ext: xdr.ExtensionPoint{
V: 0,
},
}
body, err := xdr.NewOperationBody(xdr.OperationTypeRestoreFootprint, 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 *RestoreFootprint) FromXDR(xdrOp xdr.Operation) error {
_, ok := xdrOp.Body.GetRestoreFootprintOp()
if !ok {
return errors.New("error parsing invoke host function operation from xdr")
}
f.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
return nil
}
func (f *RestoreFootprint) Validate() error {
if f.SourceAccount != "" {
_, err := xdr.AddressToMuxedAccount(f.SourceAccount)
if err != nil {
return NewValidationError("SourceAccount", err.Error())
}
}
return nil
}
func (f *RestoreFootprint) GetSourceAccount() string {
return f.SourceAccount
}
func (f *RestoreFootprint) BuildTransactionExt() (xdr.TransactionExt, error) {
return f.Ext, nil
}