/
agent_pay.go
152 lines (124 loc) · 3.04 KB
/
agent_pay.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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
Copyright © 2023 Glif LTD
*/
package cmd
import (
"context"
"fmt"
"math/big"
"time"
"github.com/briandowns/spinner"
"github.com/glifio/cli/events"
"github.com/spf13/cobra"
)
type PaymentType int
const (
Principal PaymentType = iota
ToCurrent
Custom
)
var toString = map[PaymentType]string{
Principal: "principal",
ToCurrent: "to-current",
Custom: "custom",
}
var toPaymentType = map[string]PaymentType{
"principal": Principal,
"to-current": ToCurrent,
"custom": Custom,
}
func (p PaymentType) String() string {
return toString[p]
}
func ParsePaymentType(s string) (PaymentType, error) {
p, ok := toPaymentType[s]
if !ok {
return 0, fmt.Errorf("invalid payment type %s", s)
}
return p, nil
}
var payCmd = &cobra.Command{
Use: "pay",
}
func init() {
agentCmd.AddCommand(payCmd)
}
func pay(cmd *cobra.Command, args []string, paymentType PaymentType) (*big.Int, error) {
ctx := cmd.Context()
from := cmd.Flag("from").Value.String()
agentAddr, auth, _, requesterKey, err := commonOwnerOrOperatorSetup(ctx, from)
if err != nil {
return nil, err
}
payAmt, err := payAmount(ctx, cmd, args, paymentType)
if err != nil {
return nil, err
}
poolName := cmd.Flag("pool-name").Value.String()
poolID, err := parsePoolType(poolName)
if err != nil {
return nil, err
}
s := spinner.New(spinner.CharSets[9], 100*time.Millisecond)
s.Start()
defer s.Stop()
payevt := journal.RegisterEventType("agent", "pay")
evt := &events.AgentPay{
AgentID: agentAddr.String(),
PoolID: poolID.String(),
Amount: payAmt.String(),
PayType: paymentType.String(),
}
defer journal.Close()
defer journal.RecordEvent(payevt, func() interface{} { return evt })
tx, err := PoolsSDK.Act().AgentPay(ctx, auth, agentAddr, poolID, payAmt, requesterKey)
if err != nil {
evt.Error = err.Error()
return nil, err
}
evt.Tx = tx.Hash().String()
// transaction landed on chain or errored
_, err = PoolsSDK.Query().StateWaitReceipt(cmd.Context(), tx.Hash())
if err != nil {
evt.Error = err.Error()
return nil, err
}
s.Stop()
return payAmt, nil
}
// payAmount takes a string amount of FIL as the first value in args and
// returns a *big.Int in attoFIL based on the paymentType specified
func payAmount(ctx context.Context, cmd *cobra.Command, args []string, paymentType PaymentType) (*big.Int, error) {
agentAddr, err := getAgentAddressWithFlags(cmd)
if err != nil {
return nil, err
}
var payAmt *big.Int
switch paymentType {
case Principal:
amount, err := parseFILAmount(args[0])
if err != nil {
return nil, err
}
amountOwed, _, err := PoolsSDK.Query().AgentOwes(ctx, agentAddr)
if err != nil {
return nil, err
}
payAmt = new(big.Int).Add(amount, amountOwed)
case ToCurrent:
amountOwed, _, err := PoolsSDK.Query().AgentOwes(ctx, agentAddr)
if err != nil {
return nil, err
}
payAmt = amountOwed
case Custom:
amount, err := parseFILAmount(args[0])
if err != nil {
return nil, err
}
payAmt = amount
default:
return nil, fmt.Errorf("invalid payment type: %s", paymentType)
}
return payAmt, nil
}