Skip to content

Commit

Permalink
#7: add "account-merge" command
Browse files Browse the repository at this point in the history
  • Loading branch information
tolitius committed Mar 4, 2018
1 parent d086c1f commit 724e6e0
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 4 deletions.
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ A command line interface to [Stellar](https://www.stellar.org/) networks.
- [Send Non Native Assets](#send-non-native-assets)
- [Send Native Assets](#send-native-assets)
- [Add Memo](#add-memo)
- [Account Merge](#account-merge)
- [Transaction Options](#transaction-options)
- [Add Discoverablity and Meta Information](#add-discoverablity-and-meta-information)
- [Inflation Destination](#inflation-destination)
Expand Down Expand Up @@ -255,6 +256,7 @@ Available Commands:
help Help about any command
load-account load and return account details
manage-data set, modify or delete a Data Entry (name/value pair)
account-merge merges two native (XLM) accounts
send-payment send payment from one account to another
set-options set options on the account
sign sign a base64 encoded transaction
Expand Down Expand Up @@ -664,6 +666,29 @@ $ bb send-payment -s '{"from": "'$(cat issuer)'",
"memo": "forty two"}'
```

## Account Merge

Stellar allows to merge two accounts by transferring the native balance (the amount of XLM an account holds) from the source account to another (destination) account and removing the source account from the ledger.

BB-8 has a `account-merge` command that takes a "destination account" to merge _to_, and optionally takes a "source account" that needs to be merged. If the source account is not provided, BB-8 will use an account that is set as [default](#set-default-address-and-seed):


```sh
$ bb gen-keys bar
2018/03/03 21:54:16 keys are created and stored in: bar.pub and bar

$ bb fund $(cat bar.pub)
```

merging `bar` XLMs to account `foo` requires bar's signature and foo's address:

```sh
$ bb account-merge -s '{"source_account": "'$(cat bar)'",
"destination":"'$(cat foo.pub)'"}'
```

once these two accounts are merged, `foo` would get all the `bar`'s XLM balance (`- 100` stroops fee for this transaction).
`bar` would no longer exist and would be removed from the ledger.

## Transaction Options

Expand Down
51 changes: 47 additions & 4 deletions cmd/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,41 @@ example: create-account '{"source_account":"seed", "new_account":"address", "amo
}

if standAloneFlag {
submitStandalone(conf, naccount.SourceAccountSeed, naccount.makeOp())
submitStandalone(conf, naccount.SourceAccountSeed, naccount.makeCreateAccountOp())
} else {
if len(args) == 1 {
encoded := makeEnvelope(conf, naccount.SourceAccountSeed, naccount.makeOp())
encoded := makeEnvelope(conf, naccount.SourceAccountSeed, naccount.makeCreateAccountOp())
fmt.Print(encoded)
} else {
encoded := composeWithOps(args[1], naccount.makeOp())
encoded := composeWithOps(args[1], naccount.makeCreateAccountOp())
fmt.Print(encoded)
}
}
},
}

var accountMergeCmd = &cobra.Command{
Use: "account-merge [ars]",
Short: "merges two native (XLM) accounts",
Long: `transfers the native balance (the amount of XLM an account holds)
to another account and removes the source account from the ledger.
example: account-merge '{"source_account":"seed", "destination":"address"}'`,
Args: cobra.MinimumNArgs(1),
Run: func(cmd *cobra.Command, args []string) {
maccount := &accountMerge{}
if err := json.Unmarshal([]byte(args[0]), maccount); err != nil {
log.Fatal(err)
}

if standAloneFlag {
submitStandalone(conf, maccount.SourceAccountSeed, maccount.makeAccountMergeOp())
} else {
if len(args) == 1 {
encoded := makeEnvelope(conf, maccount.SourceAccountSeed, maccount.makeAccountMergeOp())
fmt.Print(encoded)
} else {
encoded := composeWithOps(args[1], maccount.makeAccountMergeOp())
fmt.Print(encoded)
}
}
Expand All @@ -66,7 +94,7 @@ type newAccount struct {
Amount string
}

func (c *newAccount) makeOp() (muts []b.TransactionMutator) {
func (c *newAccount) makeCreateAccountOp() (muts []b.TransactionMutator) {

muts = []b.TransactionMutator{
b.CreateAccount(
Expand All @@ -76,3 +104,18 @@ func (c *newAccount) makeOp() (muts []b.TransactionMutator) {

return muts
}

type accountMerge struct {
SourceAccountSeed string `json:"source_account"`
Destination string
}

func (m *accountMerge) makeAccountMergeOp() (muts []b.TransactionMutator) {

muts = []b.TransactionMutator{
b.AccountMerge(
b.Destination{m.Destination},
)}

return muts
}
2 changes: 2 additions & 0 deletions cmd/bb8.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func AddCommands() {
bb8Cmd.AddCommand(decodeCmd)
bb8Cmd.AddCommand(signTransactionCmd)
bb8Cmd.AddCommand(submitTransactionCmd)
bb8Cmd.AddCommand(accountMergeCmd)
}

func init() {
Expand All @@ -63,6 +64,7 @@ func init() {
withStandAlone(setOptionsCmd)
withStandAlone(manageDataCmd)
withStandAlone(createAccountCmd)
withStandAlone(accountMergeCmd)
}

// Execute adds sub commands to bb8Cmd and sets all the command line flags
Expand Down

0 comments on commit 724e6e0

Please sign in to comment.