-
Notifications
You must be signed in to change notification settings - Fork 0
/
reserve_fund.go
117 lines (104 loc) · 3.84 KB
/
reserve_fund.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
package tx
import (
"encoding/hex"
"fmt"
"math/big"
"github.com/spf13/cobra"
"github.com/spf13/viper"
"github.com/dnerochain/dnero/cmd/dnerocli/cmd/utils"
"github.com/dnerochain/dnero/ledger/types"
"github.com/dnerochain/dnero/rpc"
rpcc "github.com/ybbus/jsonrpc"
)
// reserveFundCmd represents the reserve fund command
// Example:
// dnerocli tx reserve --chain="privatenet" --from=2E833968E5bB786Ae419c4d13189fB081Cc43bab --fund=900 --collateral=1203 --seq=6 --duration=1002 --resource_ids=die_another_day,hello
var reserveFundCmd = &cobra.Command{
Use: "reserve",
Short: "Reserve fund for an off-chain micropayment",
Example: `dnerocli tx reserve --chain="privatenet" --from=2E833968E5bB786Ae419c4d13189fB081Cc43bab --fund=900 --collateral=1203 --seq=6 --duration=1002 --resource_ids=die_another_day,hello`,
Run: doReserveFundCmd,
}
func doReserveFundCmd(cmd *cobra.Command, args []string) {
wallet, fromAddress, err := walletUnlock(cmd, fromFlag)
if err != nil {
return
}
defer wallet.Lock(fromAddress)
fee, ok := types.ParseCoinAmount(feeFlag)
if !ok {
utils.Error("Failed to parse fee")
}
fund, ok := types.ParseCoinAmount(reserveFundInDTokenFlag)
if !ok {
utils.Error("Failed to parse fund")
}
col, ok := types.ParseCoinAmount(reserveCollateralInDTokenFlag)
if !ok {
utils.Error("Failed to parse collateral")
}
input := types.TxInput{
Address: fromAddress,
Coins: types.Coins{
DneroWei: new(big.Int).SetUint64(0),
DTokenWei: fund,
},
Sequence: uint64(seqFlag),
}
resourceIDs := []string{}
for _, id := range resourceIDsFlag {
resourceIDs = append(resourceIDs, id)
}
collateral := types.Coins{
DneroWei: new(big.Int).SetUint64(0),
DTokenWei: col,
}
if !collateral.IsPositive() {
utils.Error("Invalid input: collateral must be positive\n")
}
reserveFundTx := &types.ReserveFundTx{
Fee: types.Coins{
DneroWei: new(big.Int).SetUint64(0),
DTokenWei: fee,
},
Source: input,
ResourceIDs: resourceIDs,
Collateral: collateral,
Duration: durationFlag,
}
sig, err := wallet.Sign(fromAddress, reserveFundTx.SignBytes(chainIDFlag))
if err != nil {
utils.Error("Failed to sign transaction: %v\n", err)
}
reserveFundTx.SetSignature(fromAddress, sig)
raw, err := types.TxToBytes(reserveFundTx)
if err != nil {
utils.Error("Failed to encode transaction: %v\n", err)
}
signedTx := hex.EncodeToString(raw)
client := rpcc.NewRPCClient(viper.GetString(utils.CfgRemoteRPCEndpoint))
res, err := client.Call("dnero.BroadcastRawTransaction", rpc.BroadcastRawTransactionArgs{TxBytes: signedTx})
if err != nil {
utils.Error("Failed to broadcast transaction: %v\n", err)
}
if res.Error != nil {
utils.Error("Server returned error: %v\n", res.Error)
}
fmt.Printf("Successfully broadcasted transaction.\n")
}
func init() {
reserveFundCmd.Flags().StringVar(&chainIDFlag, "chain", "", "Chain ID")
reserveFundCmd.Flags().StringVar(&fromFlag, "from", "", "Address to send from")
reserveFundCmd.Flags().Uint64Var(&seqFlag, "seq", 0, "Sequence number of the transaction")
reserveFundCmd.Flags().StringVar(&reserveFundInDTokenFlag, "fund", "0", "DToken amount to reserve")
reserveFundCmd.Flags().StringVar(&reserveCollateralInDTokenFlag, "collateral", "0", "DToken amount as collateral")
reserveFundCmd.Flags().StringVar(&feeFlag, "fee", fmt.Sprintf("%dwei", types.MinimumTransactionFeeDTokenWeiJune2021), "Fee")
reserveFundCmd.Flags().Uint64Var(&durationFlag, "duration", 1000, "Reserve duration")
reserveFundCmd.Flags().StringSliceVar(&resourceIDsFlag, "resource_ids", []string{}, "Reserouce IDs")
reserveFundCmd.Flags().StringVar(&walletFlag, "wallet", "soft", "Wallet type (soft|nano)")
reserveFundCmd.MarkFlagRequired("chain")
reserveFundCmd.MarkFlagRequired("from")
reserveFundCmd.MarkFlagRequired("seq")
reserveFundCmd.MarkFlagRequired("duration")
reserveFundCmd.MarkFlagRequired("resource_id")
}