-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39 from iotaledger/feat/delegation
Delegate
- Loading branch information
Showing
48 changed files
with
1,553 additions
and
601 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,144 @@ | ||
package accounts | ||
|
||
import ( | ||
"context" | ||
"os" | ||
|
||
"go.uber.org/dig" | ||
|
||
"github.com/iotaledger/evil-tools/pkg/accountwallet" | ||
"github.com/iotaledger/hive.go/app" | ||
"github.com/iotaledger/hive.go/ierrors" | ||
) | ||
|
||
const ( | ||
ScriptName = "accounts" | ||
) | ||
|
||
func init() { | ||
Component = &app.Component{ | ||
Name: "Accounts", | ||
Params: params, | ||
DepsFunc: func(cDeps dependencies) { deps = cDeps }, | ||
Run: run, | ||
Provide: func(c *dig.Container) error { | ||
return c.Provide(provideWallet) | ||
}, | ||
IsEnabled: func(_ *dig.Container) bool { return true }, | ||
} | ||
} | ||
|
||
var ( | ||
Component *app.Component | ||
deps dependencies | ||
) | ||
|
||
type dependencies struct { | ||
dig.In | ||
|
||
AccountWallets *accountwallet.AccountWallets | ||
} | ||
|
||
func run() error { | ||
Component.LogInfo("Starting evil-tools accounts ... done") | ||
// save wallet state on shutdown | ||
defer func() { | ||
err := accountwallet.SaveState(deps.AccountWallets) | ||
if err != nil { | ||
Component.LogErrorf("Error while saving wallet state: %v", err) | ||
} | ||
}() | ||
|
||
accountsSubcommandsFlags := parseAccountCommands(getCommands(os.Args[2:]), ParamsAccounts) | ||
accountsSubcommands( | ||
Component.Daemon().ContextStopped(), | ||
deps.AccountWallets, | ||
accountsSubcommandsFlags, | ||
) | ||
|
||
return nil | ||
} | ||
|
||
func provideWallet() *accountwallet.AccountWallets { | ||
// load wallet | ||
accWallet, err := accountwallet.Run(Component.Daemon().ContextStopped(), Component.Logger, | ||
accountwallet.WithClientURL(ParamsAccounts.NodeURLs[0]), | ||
accountwallet.WithFaucetURL(ParamsAccounts.FaucetURL), | ||
accountwallet.WithAccountStatesFile(ParamsAccounts.AccountStatesFile), | ||
accountwallet.WithFaucetAccountParams(&accountwallet.GenesisAccountParams{ | ||
FaucetPrivateKey: ParamsAccounts.BlockIssuerPrivateKey, | ||
FaucetAccountID: ParamsAccounts.AccountID, | ||
}), | ||
) | ||
if err != nil { | ||
Component.LogPanic(err.Error()) | ||
} | ||
|
||
return accWallet | ||
} | ||
|
||
func accountsSubcommands(ctx context.Context, wallets *accountwallet.AccountWallets, subcommands []accountwallet.AccountSubcommands) { | ||
for _, sub := range subcommands { | ||
err := accountsSubcommand(ctx, wallets, sub) | ||
if err != nil { | ||
Component.LogFatal(ierrors.Wrap(err, "failed to run subcommand").Error()) | ||
|
||
return | ||
} | ||
} | ||
} | ||
|
||
//nolint:all,forcetypassert | ||
func accountsSubcommand(ctx context.Context, wallets *accountwallet.AccountWallets, subCommand accountwallet.AccountSubcommands) error { | ||
Component.LogInfof("Run subcommand: %s, with parameter set: %v", subCommand.Type().String(), subCommand) | ||
|
||
switch subCommand.Type() { | ||
case accountwallet.OperationCreateAccount: | ||
//nolint:forcetypassert // we can safely assume that the type is correct | ||
accParams := subCommand.(*accountwallet.CreateAccountParams) | ||
|
||
accountID, err := wallets.CreateAccount(ctx, accParams) | ||
if err != nil { | ||
return ierrors.Wrap(err, "failed to create account") | ||
} | ||
|
||
Component.LogInfof("Created account %s", accountID) | ||
|
||
case accountwallet.OperationDestroyAccount: | ||
//nolint:forcetypassert // we can safely assume that the type is correct | ||
accParams := subCommand.(*accountwallet.DestroyAccountParams) | ||
|
||
if err := wallets.DestroyAccount(ctx, accParams); err != nil { | ||
return ierrors.Wrap(err, "failed to destroy account") | ||
} | ||
|
||
case accountwallet.OperationAllotAccount: | ||
//nolint:forcetypassert // we can safely assume that the type is correct | ||
accParams := subCommand.(*accountwallet.AllotAccountParams) | ||
|
||
if err := wallets.AllotToAccount(accParams); err != nil { | ||
return ierrors.Wrap(err, "failed to allot to account") | ||
} | ||
|
||
case accountwallet.OperationDelegateAccount: | ||
//nolint:forcetypassert // we can safely assume that the type is correct | ||
params := subCommand.(*accountwallet.DelegateAccountParams) | ||
|
||
if err := wallets.DelegateToAccount(ctx, params); err != nil { | ||
return ierrors.Wrap(err, "failed to delegate to account") | ||
} | ||
|
||
case accountwallet.OperationRewardsAccount: | ||
//nolint:forcetypassert // we can safely assume that the type is correct | ||
params := subCommand.(*accountwallet.RewardsAccountParams) | ||
|
||
if err := wallets.Rewards(ctx, params); err != nil { | ||
return ierrors.Wrap(err, "failed to get rewards") | ||
} | ||
|
||
default: | ||
return ierrors.New("unknown subcommand") | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.