forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_merge.go
62 lines (53 loc) · 1.83 KB
/
account_merge.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
package txnbuild
import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// AccountMerge represents the Stellar merge account operation. See
// https://developers.stellar.org/docs/start/list-of-operations/
type AccountMerge struct {
Destination string
SourceAccount string
}
// BuildXDR for AccountMerge returns a fully configured XDR Operation.
func (am *AccountMerge) BuildXDR() (xdr.Operation, error) {
var xdrOp xdr.MuxedAccount
err := xdrOp.SetAddress(am.Destination)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to set destination address")
}
opType := xdr.OperationTypeAccountMerge
body, err := xdr.NewOperationBody(opType, xdrOp)
if err != nil {
return xdr.Operation{}, errors.Wrap(err, "failed to build XDR OperationBody")
}
op := xdr.Operation{Body: body}
SetOpSourceAccount(&op, am.SourceAccount)
return op, nil
}
// FromXDR for AccountMerge initialises the txnbuild struct from the corresponding xdr Operation.
func (am *AccountMerge) FromXDR(xdrOp xdr.Operation) error {
if xdrOp.Body.Type != xdr.OperationTypeAccountMerge {
return errors.New("error parsing account_merge operation from xdr")
}
am.SourceAccount = accountFromXDR(xdrOp.SourceAccount)
if xdrOp.Body.Destination != nil {
am.Destination = xdrOp.Body.Destination.Address()
}
return nil
}
// Validate for AccountMerge validates the required struct fields. It returns an error if any of the fields are
// invalid. Otherwise, it returns nil.
func (am *AccountMerge) Validate() error {
var err error
_, err = xdr.AddressToMuxedAccount(am.Destination)
if err != nil {
return NewValidationError("Destination", err.Error())
}
return nil
}
// GetSourceAccount returns the source account of the operation, or the empty string if not
// set.
func (am *AccountMerge) GetSourceAccount() string {
return am.SourceAccount
}