-
Notifications
You must be signed in to change notification settings - Fork 4
/
agent_create.go
136 lines (114 loc) · 3.46 KB
/
agent_create.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
/*
Copyright © 2023 Glif LTD
*/
package cmd
import (
"errors"
"fmt"
"math/big"
"os"
"time"
"github.com/AlecAivazis/survey/v2"
"github.com/briandowns/spinner"
"github.com/ethereum/go-ethereum/accounts"
"github.com/glifio/glif/v2/util"
walletutils "github.com/glifio/go-wallet-utils"
"github.com/spf13/cobra"
)
// createCmd represents the create command
var createCmd = &cobra.Command{
Use: "create",
Short: "Create a Glif agent",
Long: `Spins up a new Agent contract through the Agent Factory, passing the owner, operator, and requestor addresses.`,
Run: func(cmd *cobra.Command, args []string) {
as := util.AccountsStore()
agentStore := util.AgentStore()
ks := util.KeyStore()
backends := []accounts.Backend{}
backends = append(backends, ks)
manager := accounts.NewManager(&accounts.Config{InsecureUnlockAllowed: false}, backends...)
// Check if an agent already exists
addressStr, err := as.Get("address")
if err != nil {
var e *util.ErrKeyNotFound
if !errors.As(err, &e) {
logFatal(err)
}
}
if addressStr != "" {
logFatalf("Agent already exists: %s", addressStr)
}
ownerAddr, _, err := as.GetAddrs(string(util.OwnerKey))
checkExists(err)
operatorAddr, _, err := as.GetAddrs(string(util.OperatorKey))
checkExists(err)
requestAddr, _, err := as.GetAddrs(string(util.RequestKey))
checkExists(err)
account := accounts.Account{Address: ownerAddr}
passphrase, envSet := os.LookupEnv("GLIF_OWNER_PASSPHRASE")
if !envSet {
prompt := &survey.Password{
Message: "Owner key passphrase",
}
survey.AskOne(prompt, &passphrase)
}
wallet, err := manager.Find(account)
if err != nil {
logFatal(err)
}
if util.IsZeroAddress(ownerAddr) || util.IsZeroAddress(operatorAddr) || util.IsZeroAddress(requestAddr) {
logFatal("Keys not found. Please check your `keys.toml` file")
}
fmt.Printf("Creating agent, owner %s, operator %s, request %s\n", ownerAddr, operatorAddr, requestAddr)
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
defer s.Stop()
auth, err := walletutils.NewEthWalletTransactor(wallet, &account, passphrase, big.NewInt(chainID))
if err != nil {
logFatal(err)
}
// submit the agent create transaction
tx, err := PoolsSDK.Act().AgentCreate(
cmd.Context(),
auth,
ownerAddr,
operatorAddr,
requestAddr,
)
if err != nil {
logFatalf("pools sdk: agent create: %s", err)
}
s.Stop()
fmt.Printf("Agent create transaction submitted: %s\n", tx.Hash())
fmt.Println("Waiting for confirmation...")
s.Start()
// transaction landed on chain or errored
receipt, err := PoolsSDK.Query().StateWaitReceipt(cmd.Context(), tx.Hash())
if err != nil {
logFatalf("pools sdk: query: state wait receipt: %s", err)
}
// grab the ID and the address of the agent from the receipt's logs
addr, id, err := PoolsSDK.Query().AgentAddrIDFromRcpt(cmd.Context(), receipt)
if err != nil {
logFatalf("pools sdk: query: agent addr id from receipt: %s", err)
}
s.Stop()
fmt.Printf("Agent created: %s\n", addr.String())
fmt.Printf("Agent ID: %s\n", id.String())
agentStore.Set("id", id.String())
agentStore.Set("address", addr.String())
agentStore.Set("tx", tx.Hash().String())
},
}
func checkExists(err error) {
if err != nil {
var e *util.ErrKeyNotFound
if errors.As(err, &e) {
logFatal("Agent accounts not found in wallet. Setup with: glif wallet create-agent-accounts")
}
logFatal(err)
}
}
func init() {
agentCmd.AddCommand(createCmd)
}