-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathdelegate.go
146 lines (114 loc) · 4.64 KB
/
delegate.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
package node_cmd
import (
"fmt"
"os"
"strconv"
"time"
"github.com/lunfardo314/proxima/ledger"
"github.com/lunfardo314/proxima/ledger/txbuilder"
"github.com/lunfardo314/proxima/proxi/glb"
"github.com/lunfardo314/proxima/util"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)
var targetChainIDStr string
func initDelegateCmd() *cobra.Command {
delegateCmd := &cobra.Command{
Use: "delegate <amount> -q <target sequencer ID hex encoded>",
Aliases: util.List("send"),
Short: `delegates amount to target sequencer by creating delegation chain output`,
Args: cobra.ExactArgs(1),
Run: runDelegateCmd,
}
glb.AddFlagTarget(delegateCmd)
delegateCmd.PersistentFlags().StringVarP(&targetChainIDStr, "seq", "q", "", "target sequencer ID")
err := viper.BindPFlag("seq", delegateCmd.PersistentFlags().Lookup("seq"))
glb.AssertNoError(err)
delegateCmd.InitDefaultHelpCmd()
return delegateCmd
}
func runDelegateCmd(_ *cobra.Command, args []string) {
glb.InitLedgerFromNode()
walletData := glb.GetWalletData()
glb.Infof("wallet account is: %s", walletData.Account.String())
glb.Assertf(targetChainIDStr != "", "target sequencer ID not specified")
targetSeqID, err := ledger.ChainIDFromHexString(targetChainIDStr)
glb.Assertf(err == nil, "failed parsing target chainID: %v", err)
seqOut, _, _, err := glb.GetClient().GetChainOutput(targetSeqID)
glb.Assertf(err == nil, "can't find sequencer ID %s: %v", targetSeqID.StringShort(), err)
glb.Assertf(seqOut.ID.IsSequencerTransaction(), "chainID %s does not represent a sequencer", targetSeqID.StringShort())
var tagAlongSeqID *ledger.ChainID
feeAmount := glb.GetTagAlongFee()
glb.Assertf(feeAmount > 0, "tag-along fee is configured 0. Fee-less option not supported yet")
tagAlongSeqID = glb.GetTagAlongSequencerID()
glb.Assertf(tagAlongSeqID != nil, "tag-along sequencer not specified")
amountInt, err := strconv.Atoi(args[0])
glb.AssertNoError(err)
amount := uint64(amountInt)
glb.Assertf(amount >= ledger.MinimumDelegationAmount(), "amount must be >= %d", ledger.MinimumDelegationAmount())
client := glb.GetClient()
walletOutputs, lrbid, _, err := client.GetOutputsForAmount(walletData.Account, amount+feeAmount)
glb.AssertNoError(err)
glb.PrintLRB(lrbid)
sumIn := uint64(0)
walletOutputs = util.PurgeSlice(walletOutputs, func(o *ledger.OutputWithID) bool {
if sumIn >= amount+feeAmount {
return false
}
sumIn += o.Output.Amount()
return true
})
glb.Assertf(sumIn >= amount+feeAmount, "not enough tokens. Needed %s, got %s", util.Th(amount+feeAmount), util.Th(sumIn))
txb := txbuilder.New()
_, inTs, err := txb.ConsumeOutputs(walletOutputs...)
glb.AssertNoError(err)
ts := ledger.MaximumTime(inTs, ledger.TimeNow())
for i := range walletOutputs {
if i == 0 {
txb.PutSignatureUnlock(0)
} else {
err = txb.PutUnlockReference(byte(i), ledger.ConstraintIndexLock, 0)
glb.AssertNoError(err)
}
}
outDelegation := ledger.NewOutput(func(o *ledger.Output) {
o.WithAmount(amount)
o.WithLock(ledger.NewDelegationLock(walletData.Account, targetSeqID.AsChainLock(), 2, ts, amount))
_, _ = o.PushConstraint(ledger.NewChainOrigin().Bytes())
})
delegationOutputIdx, _ := txb.ProduceOutput(outDelegation)
outTagAlong := ledger.NewOutput(func(o *ledger.Output) {
o.WithAmount(feeAmount)
o.WithLock(tagAlongSeqID.AsChainLock())
})
_, _ = txb.ProduceOutput(outTagAlong)
totalAmountConsumed := txb.ConsumedAmount()
totalAmountProduced, _ := txb.ProducedAmount()
if totalAmountConsumed > totalAmountProduced {
remainderOut := ledger.NewOutput(func(o *ledger.Output) {
o.WithAmount(totalAmountConsumed - totalAmountProduced)
o.WithLock(walletData.Account)
})
_, _ = txb.ProduceOutput(remainderOut)
}
totalAmountProduced, _ = txb.ProducedAmount()
glb.Assertf(totalAmountConsumed == totalAmountProduced, "totalAmountConsumed==totalAmountProduced")
txb.TransactionData.Timestamp = ts
txb.TransactionData.InputCommitment = txb.InputCommitment()
txb.SignED25519(walletData.PrivateKey)
txBytes, txid, failedTx, err := txb.BytesWithValidation()
glb.Assertf(err == nil, "transaction invalid: %v\n------------------\n%s", err, failedTx)
prompt := fmt.Sprintf("delegate amount %s to sequencer %s (plus tag-along fee %s)?",
util.Th(amount), targetSeqID.String(), util.Th(feeAmount))
if !glb.YesNoPrompt(prompt, true) {
glb.Infof("exit")
os.Exit(0)
}
delegationOid, err := ledger.NewOutputID(&txid, delegationOutputIdx)
glb.AssertNoError(err)
delegationID := ledger.MakeOriginChainID(&delegationOid)
glb.Infof("\ndelegation ID: %s\n", delegationID.String())
err = client.SubmitTransaction(txBytes)
glb.AssertNoError(err)
glb.TrackTxInclusion(txid, 2*time.Second)
}