This repository has been archived by the owner on Dec 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
contract.go
121 lines (99 loc) · 3.52 KB
/
contract.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
// SPDX-License-Identifier: GPL-3.0
package main
import (
"fmt"
"log"
"math/big"
"github.com/comrade-coop/trusted-pods/pkg/ethereum"
pb "github.com/comrade-coop/trusted-pods/pkg/proto"
"github.com/comrade-coop/trusted-pods/pkg/resource"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
"github.com/ethereum/go-ethereum/common"
"github.com/spf13/cobra"
)
var contractCmd = &cobra.Command{
Use: "contract",
Short: "Operations related to managing contracts",
}
var ethereumRpc string
var providerKey string
var paymentContractAddress string
var publisherEthAddress string
var podId string
var metricsTotal int64
func getPaymentChannelProto(providerAuth *bind.TransactOpts, chainID *big.Int) *pb.PaymentChannel {
return &pb.PaymentChannel{
ChainID: chainID.Bytes(),
ContractAddress: common.HexToAddress(paymentContractAddress).Bytes(),
PublisherAddress: common.HexToAddress(publisherEthAddress).Bytes(),
ProviderAddress: providerAuth.From.Bytes(),
PodID: common.HexToHash(podId).Bytes(),
}
}
var checkContractCmd = &cobra.Command{
Use: "check",
Short: "Check whether a payment contract is considered valid",
RunE: func(cmd *cobra.Command, args []string) error {
ethClient, err := ethereum.GetClient(ethereumRpc)
if err != nil {
return err
}
providerAuth, _, err := ethereum.GetAccountAndSigner(providerKey, ethClient)
if err != nil {
return err
}
pricingTables, err := openPricingTables()
if err != nil {
return err
}
validator, err := ethereum.NewPaymentChannelValidator(ethClient, pricingTables, providerAuth)
_, err = validator.Parse(getPaymentChannelProto(providerAuth, validator.ChainID))
if err != nil {
return err
}
fmt.Println("Correct Contract")
return nil
},
}
var withdrawContractCmd = &cobra.Command{
Use: "withdraw <amount>",
Short: "Withdraw funds from a payment contract",
Args: cobra.ExactArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
ethClient, err := ethereum.GetClient(ethereumRpc)
if err != nil {
return err
}
providerAuth, _, err := ethereum.GetAccountAndSigner(providerKey, ethClient)
if err != nil {
return err
}
paymentContract := common.HexToAddress(paymentContractAddress)
validator, err := ethereum.NewPaymentChannelValidator(
ethClient,
map[common.Address]resource.PricingTableMap{paymentContract: make(resource.PricingTableMap)},
providerAuth,
)
channel, err := validator.Parse(getPaymentChannelProto(providerAuth, validator.ChainID))
if err != nil {
return err
}
_, err = channel.WithdrawUpTo(common.Address{}, big.NewInt(metricsTotal))
if err != nil {
return err
}
log.Println("Amount transferred successfully")
return nil
},
}
func init() {
contractCmd.AddCommand(checkContractCmd)
contractCmd.AddCommand(withdrawContractCmd)
contractCmd.PersistentFlags()
contractCmd.PersistentFlags().StringVar(ðereumRpc, "ethereum-rpc", "http://127.0.0.1:8545", "client public address")
contractCmd.PersistentFlags().StringVar(&providerKey, "ethereum-key", "", "provider account string (private key | http[s]://clef#account | /keystore#account | account (in default keystore))")
contractCmd.PersistentFlags().StringVar(&paymentContractAddress, "payment-contract", "", "payment contract address")
contractCmd.PersistentFlags().StringVar(&publisherEthAddress, "publisher", "", "payment contract address")
contractCmd.PersistentFlags().StringVar(&podId, "pod-id", "00", "pod id")
withdrawContractCmd.Flags().Int64Var(&metricsTotal, "metric-price", 0, "amount to withdraw up to")
}