Skip to content

Commit

Permalink
add evm-bully delete command
Browse files Browse the repository at this point in the history
  • Loading branch information
frankbraun committed Apr 30, 2021
1 parent 64cb942 commit f3c5e58
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 0 deletions.
57 changes: 57 additions & 0 deletions command/delete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package command

import (
"encoding/json"
"flag"
"fmt"
"os"

"github.com/aurora-is-near/evm-bully/nearapi"
"github.com/aurora-is-near/evm-bully/nearapi/utils"
)

// Delete implements the 'delete' command.
func Delete(argv0 string, args ...string) error {
var nodeURL nodeURLFlag
fs := flag.NewFlagSet(argv0, flag.ContinueOnError)
fs.Usage = func() {
fmt.Fprintf(os.Stderr, "Usage: %s <accountId> <beneficiaryId>\n", argv0)
fmt.Fprintf(os.Stderr, "Delete an account and transfer funds to beneficiary account.\n")
fs.PrintDefaults()
}
cfg := nearapi.GetConfig()
nodeURL.registerFlag(fs, cfg)
if err := fs.Parse(args); err != nil {
return err
}
if fs.NArg() != 2 {
fs.Usage()
return flag.ErrHelp
}
accountID := fs.Arg(0)
beneficiaryID := fs.Arg(1)
c := nearapi.NewConnection(string(nodeURL))
a, err := nearapi.LoadAccount(c, cfg, accountID)
if err != nil {
return err
}
fmt.Printf("Deleting account. Account ID: %s, node: %s, beneficiary: %s\n",
accountID, string(nodeURL), beneficiaryID)
txResult, err := a.DeleteAccount(beneficiaryID)
if err != nil {
return err
}
utils.PrettyPrintResponse(txResult)
res, err := nearapi.GetTransactionLastResult(txResult)
if err != nil {
return err
}
if res != nil {
jsn, err := json.MarshalIndent(res, "", " ")
if err != nil {
return err
}
fmt.Println(string(jsn))
}
return nil
}
3 changes: 3 additions & 0 deletions evm-bully.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ func usage() {
fmt.Fprintf(os.Stderr, " %s replay <evmContract>\n", cmd)
fmt.Fprintf(os.Stderr, " %s block\n", cmd)
fmt.Fprintf(os.Stderr, " %s state <accountId>\n", cmd)
fmt.Fprintf(os.Stderr, " %s delete <accountId> <beneficiaryId>\n", cmd)
fmt.Fprintf(os.Stderr, " %s call <contractName> <methodName>\n", cmd)
fmt.Fprintf(os.Stderr, " %s send <sender> <receiver> <amount>\n", cmd)
fmt.Fprintf(os.Stderr, " %s stats\n", cmd)
Expand Down Expand Up @@ -64,6 +65,8 @@ func main() {
err = command.Block(argv0, args...)
case "state":
err = command.State(argv0, args...)
case "delete":
err = command.Delete(argv0, args...)
case "call":
err = command.Call(argv0, args...)
case "send":
Expand Down
13 changes: 13 additions & 0 deletions nearapi/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ func (a *Account) SendMoney(
}})
}

// DeleteAccount deletes the account and sends the remaining Ⓝ balance to the
// account beneficiaryID.
func (a *Account) DeleteAccount(
beneficiaryID string,
) (map[string]interface{}, error) {
return a.signAndSendTransaction(a.AccountID, []Action{{
Enum: 7,
DeleteAccount: DeleteAccount{
BeneficiaryID: beneficiaryID,
},
}})
}

func (a *Account) signAndSendTransaction(
receiverID string,
actions []Action,
Expand Down
6 changes: 6 additions & 0 deletions nearapi/nearapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,12 @@ func GetTransactionLastResult(txResult map[string]interface{}) (interface{}, err
return string(buf), nil
}
return jsn, nil
} else if status["Failure"] != nil {
jsn, err := json.MarshalIndent(status["Failure"], "", " ")
if err != nil {
return nil, err
}
return nil, fmt.Errorf("failure:\n%s", string(jsn))
}
}
return nil, nil
Expand Down

0 comments on commit f3c5e58

Please sign in to comment.