Loading...
Searching...
No Matches
contract_tools.cpp
Go to the documentation of this file.
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#include <glog/logging.h>
21
22#include <boost/algorithm/string.hpp>
23#include <vector>
24
27
31
32void ShowUsage() {
33 printf(
34 "<cmd> -c <config> -m <caller address> -n <contract name> -p <contact "
35 "path> -a <params> \n");
36 exit(0);
37}
38
40 auto account = client->CreateAccount();
41 if (!account.ok()) {
42 printf("create account fail\n");
43 return;
44 }
45 LOG(ERROR) << "create account:\n" << account->DebugString();
46}
47
48void DeployContract(ContractClient* client, const std::string& caller_address,
49 const std::string& contract_name,
50 const std::string& contract_path,
51 const std::vector<std::string>& init_params) {
52 auto contract = client->DeployContract(caller_address, contract_name,
53 contract_path, init_params);
54 if (!contract.ok()) {
55 printf("deploy contract fail\n");
56 return;
57 }
58 LOG(ERROR) << "deploy contract:\n" << contract->DebugString();
59}
60
61void ExecuteContract(ContractClient* client, const std::string& caller_address,
62 const std::string& contract_address,
63 const std::string& func_name,
64 const std::vector<std::string>& params) {
65 auto output = client->ExecuteContract(caller_address, contract_address,
66 func_name, params);
67 if (!output.ok()) {
68 printf("execute contract fail\n");
69 return;
70 }
71 LOG(ERROR) << "execute result:\n" << *output;
72}
73
74int main(int argc, char** argv) {
75 if (argc < 3) {
76 printf("<cmd> -c [config]\n");
77 return 0;
78 }
79
80 std::string cmd = argv[1];
81 std::string caller_address, contract_name, contract_path, params,
82 contract_address, func_name;
83 int c;
84 std::string client_config_file;
85 while ((c = getopt(argc, argv, "m:c:a:n:p:h:f:s:")) != -1) {
86 switch (c) {
87 case 'm':
88 caller_address = optarg;
89 break;
90 case 'c':
91 client_config_file = optarg;
92 break;
93 case 'n':
94 contract_name = optarg;
95 break;
96 case 'f':
97 func_name = optarg;
98 break;
99 case 'p':
100 contract_path = optarg;
101 break;
102 case 'a':
103 params = optarg;
104 break;
105 case 's':
106 contract_address = optarg;
107 break;
108 case 'h':
109 ShowUsage();
110 break;
111 }
112 }
113
114 printf("cmd = %s config path = %s\n", cmd.c_str(),
115 client_config_file.c_str());
116 ResDBConfig config = GenerateResDBConfig(client_config_file);
117 config.SetClientTimeoutMs(100000);
118
119 ContractClient client(config);
120
121 if (cmd == "create") {
122 CreateAccount(&client);
123 } else if (cmd == "deploy") {
124 std::vector<std::string> init_params;
125 boost::split(init_params, params, boost::is_any_of(","));
126
127 DeployContract(&client, caller_address, contract_name, contract_path,
128 init_params);
129 } else if (cmd == "execute") {
130 printf(
131 "execute\n caller address:%s\n contract address: %s\n func: %s\n "
132 "params:%s\n",
133 caller_address.c_str(), contract_address.c_str(), func_name.c_str(),
134 params.c_str());
135 std::vector<std::string> func_params;
136 boost::split(func_params, params, boost::is_any_of(","));
137
138 ExecuteContract(&client, caller_address, contract_address, func_name,
139 func_params);
140 }
141}
void SetClientTimeoutMs(int timeout_ms)
absl::StatusOr< Account > CreateAccount()
absl::StatusOr< std::string > ExecuteContract(const std::string &caller_address, const std::string &contract_address, const std::string &func_name, const std::vector< std::string > &func_params)
absl::StatusOr< Contract > DeployContract(const std::string &caller_address, const std::string &contract_name, const std::string &contract_path, const std::vector< std::string > &init_params)
void ExecuteContract(ContractClient *client, const std::string &caller_address, const std::string &contract_address, const std::string &func_name, const std::vector< std::string > &params)
int main(int argc, char **argv)
void ShowUsage()
void CreateAccount(ContractClient *client)
void DeployContract(ContractClient *client, const std::string &caller_address, const std::string &contract_name, const std::string &contract_path, const std::vector< std::string > &init_params)
std::unique_ptr< ResDBConfig > GenerateResDBConfig(const std::string &config_file, const std::string &private_key_file, const std::string &cert_file, std::optional< ReplicaInfo > self_info, std::optional< ConfigGenFunc > gen_func)