This repository has been archived by the owner on Dec 17, 2024. It is now read-only.
forked from stellar/go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
account_merge.go
63 lines (53 loc) · 1.84 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
63
package txnbuild
import (
"github.com/stellar/go/support/errors"
"github.com/stellar/go/xdr"
)
// AccountMerge represents the Stellar merge account operation. See
// https://www.stellar.org/developers/guides/concepts/list-of-operations.html
type AccountMerge struct {
Destination string
SourceAccount Account
}
// 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 {
aid := xdrOp.Body.Destination.ToAccountId()
am.Destination = aid.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 {
_, err := xdr.AddressToAccountId(am.Destination)
if err != nil {
return NewValidationError("Destination", err.Error())
}
return nil
}
// GetSourceAccount returns the source account of the operation, or nil if not
// set.
func (am *AccountMerge) GetSourceAccount() Account {
return am.SourceAccount
}