-
Notifications
You must be signed in to change notification settings - Fork 1
/
contract.go
119 lines (96 loc) · 3.27 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
package common
import (
"encoding/hex"
"fmt"
"os"
"strings"
"time"
"github.com/Conflux-Chain/conflux-toolkit/account"
"github.com/Conflux-Chain/conflux-toolkit/contract/abi"
"github.com/Conflux-Chain/conflux-toolkit/rpc"
sdk "github.com/Conflux-Chain/go-conflux-sdk"
"github.com/Conflux-Chain/go-conflux-sdk/types"
"github.com/Conflux-Chain/go-conflux-sdk/types/cfxaddress"
"github.com/spf13/cobra"
)
var contract string
// AddContractVar adds contract variable for specified command.
func AddContractVar(cmd *cobra.Command) {
cmd.PersistentFlags().StringVar(&contract, "contract", "", "Contract address in HEX format")
cmd.MarkPersistentFlagRequired("contract")
}
// MustCreateContract creates an instance to interact with contract.
func MustCreateContract(abiJSON string) *sdk.Contract {
return MustGetContract(abiJSON, contract)
}
// MustGetContract creates an instance to interact with contract at contractAddress.
func MustGetContract(abiJSON string, contractAddress string) *sdk.Contract {
client := rpc.MustCreateClient()
client.SetAccountManager(account.DefaultAccountManager)
addr := cfxaddress.MustNew(contractAddress)
contract, err := client.GetContract([]byte(abiJSON), &addr)
if err != nil {
fmt.Println("Failed to create contract instance:", err.Error())
os.Exit(1)
}
return contract
}
func MustGetCTokenContract(contractAddress string) *sdk.Contract {
return MustGetContract(abi.GetCTokenABI(), contractAddress)
}
// MustCall call contract for the specified method and arguments.
func MustCall(contract *sdk.Contract, resultPtr interface{}, method string, args ...interface{}) {
if err := contract.Call(nil, resultPtr, method, args...); err != nil {
fmt.Printf("Failed to call method %v: %v\n", method, err.Error())
os.Exit(1)
}
}
// MustCallAddress call contract and return address value for the specified method and arguments.
func MustCallAddress(contract *sdk.Contract, method string, args ...interface{}) string {
var result [20]byte
MustCall(contract, &result, method, args...)
return types.NewBytes(result[:]).String()
}
// MustExecuteTx call contract and wait for the succeeded receipt.
func MustExecuteTx(contract *sdk.Contract, option *types.ContractMethodSendOption, method string, args ...interface{}) string {
txHash, err := contract.SendTransaction(option, method, args...)
if err != nil {
fmt.Println("Failed to send transaction:", err.Error())
os.Exit(1)
}
for {
time.Sleep(time.Second)
receipt, err := contract.Client.GetTransactionReceipt(txHash)
if err != nil {
fmt.Println("Failed to get receipt:", err.Error())
os.Exit(1)
}
if receipt == nil {
continue
}
if receipt.OutcomeStatus == 0 {
break
}
fmt.Println("Receipt outcome status is:", receipt.OutcomeStatus)
os.Exit(1)
}
return txHash.String()
}
// MustAddress2Bytes20 converts address in HEX format to [20]byte.
func MustAddress2Bytes20(address string) [20]byte {
if strings.HasPrefix(address, "0x") {
address = address[2:]
}
decoded, err := hex.DecodeString(address)
if err != nil {
fmt.Println("Failed to decode address to [20]byte:", err.Error())
os.Exit(1)
}
if len(decoded) != 20 {
fmt.Println("Failed to decode address to [20]byte, invalid length", len(decoded))
os.Exit(1)
}
var result [20]byte
copy(result[:], decoded)
return result
}